找回密码
 会员注册
查看: 23|回复: 0

python小游戏——躲避球(可当课设)

[复制链接]

3

主题

0

回帖

10

积分

新手上路

积分
10
发表于 2024-9-7 17:06:37 | 显示全部楼层 |阅读模式
游戏简介:没有美术,画面简洁(懒得做)。玩家控制小球躲避敌人(上下左右,闪避),敌人体积越大速度越慢,随机生成道具球(目前只有生命球),靠近道具球可拾取。未来展望:1.添加其他道具球2.添加攻击手段,目前只能闪避。3.添加耐力条4.添加更多属性核心代码玩家移动defplayer_move(space_down,player):"""控制玩家移动处理:paramspace_down:是否按下空格:paramplayer:玩家:return:"""globalmonitorifspace_down:speed=player.dodge_speedelse:speed=player.speedkeys=pygame.key.get_pressed()ifkeys[pygame.K_LEFT]:player.x-=speedifkeys[pygame.K_RIGHT]:player.x+=speedifkeys[pygame.K_UP]:player.y-=speedifkeys[pygame.K_DOWN]:player.y+=speedifplayer.xmonitor.width:player.x=player.x-monitor.widthifplayer.ymonitor.height:player.y=player.y-monitor.height生成小怪defmake_enemy():"""生成小怪:return:"""globalmonitor,scoreboss=Falseifscore%20000==0andscore!=0:boss=TruesizeList=[10,30,50,70,100,130,150,180,200,250]random_int=random.randint(1,10)ifbossisFalseelse20enemy=Enemy(atc=random_int,max_health=random_int,defense=0,speed=(11-random_int)*1.5ifbossisFalseelse1.5,attribute=0,x=random.uniform(0.1,1)*monitor.width,y=random.uniform(0.1,1)*monitor.height,size=sizeList[random_int-1]ifbossisFalseelse500)returnenemy道具球处理defpropBall_handle(propBall_list,window,player):"""道具球处理:parampropBall_list::paramwindow::paramplayer::return:"""count=0forpropBallinpropBall_list:pygame.draw.circle(window,propBall.color,(propBall.x,propBall.y),propBall.size)propBall.moveToPlayer(player.x,player.y)ifdetectIntersect(player,propBall):propBall_function(player,propBall)delpropBall_list[count]count+=1ifscore%200==0:propBall=generate_propBall()ifpropBallisnotNone:propBall_list.append(propBall)returnpropBall_list游戏主要逻辑defmain():globalis_running,is_playing,font,pass_time,pass_time_made,score,start_time,monitorpropBall_list=[]window=pygame.display.set_mode((monitor.width,monitor.height))pygame.display.set_caption("demo")player=init_player()health_bal=pygame.Rect(20,20,player.health*20,20)enemyList=[make_enemy()]button_playAgain=pygame.Rect(monitor.width//2-button_width//2,monitor.height*0.6,button_width,button_height)button_quit=pygame.Rect(monitor.width//2-button_width//2,monitor.height*0.6+30+button_height,button_width,button_height)buttonList=[button_playAgain,button_quit]whileis_running:score+=1window.fill(color_dict['black'])#填充黑色health_bal.width=player.health*20space_down=Falseforeventinpygame.event.get():ifevent.type==pygame.QUIT:is_running=Falseelifevent.type==KEYDOWN:ifevent.key==K_SPACE:space_down=Trueifevent.type==pygame.MOUSEBUTTONDOWN:mouse_pos=pygame.mouse.get_pos()button_serialNum=button_clicked(buttonList,mouse_pos)ifbutton_serialNum==0:is_playing=Trueplayer=init_player()enemyList=[make_enemy()]elifbutton_serialNum==1:is_running=Falseifis_playing:ifpass_time_madeisFalse:pass_time=0start_time=time.perf_counter()pass_time_made=Trueifplayer.health==0:is_playing=Falseifscore%400==0:enemyList.append(make_enemy())propBall_list=propBall_handle(propBall_list,window,player)player_move(space_down,player)#玩家移动player_twinkle(player,window)#玩家绘制draw_healthBar(window,player,health_bal)#血条更新make_enemyThreading(enemyList,window,player)#小怪更新draw_score(window)pass_time=int(time.perf_counter()-start_time)else:draw_scoreTitle(window)draw_button(buttonList,window)pass_time_made=FalsepropBall_list=[]enemyList=[]pygame.display.flip()#刷新屏幕time.sleep(0.01)pygame.quit()游戏画面完整代码有需要者自取,盘内还有打包好的exe文件链接:https://pan.baidu.com/s/1rZ1xNZJYtvyXPIG9Rgh5Hw提取码:iq6l
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2024-12-26 12:16 , Processed in 1.563148 second(s), 25 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表