基于Python怎么制作flappybird游戏(python,开发技术)

时间:2024-05-03 03:19:57 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

开发工具

**Python****版本:**3.6.4

相关模块:

pygame 模块;

以及一些 python 自带的模块

环境搭建

安装 Python 并添加到环境变量,pip 安装需要的相关模块即可。

先睹为快

在 cmd 窗口运行如下命令即可:

pythonGame6.py

基于Python怎么制作flappybird游戏

原理简介

因为是重写的,所以就重新介绍一下实现原理呗。

首先,我们来写个开始界面,让他看起来更像个游戏一些。效果大概是这样的:

基于Python怎么制作flappybird游戏

原理也简单,关键点有三个:(1)下方深绿浅绿交替的地板不断往左移动来制造小鸟向前飞行的假象;(2)每过几帧切换一下小鸟的图片来实现小鸟翅膀扇动的效果:

基于Python怎么制作flappybird游戏

(3)有规律地改变小鸟竖直方向上的位置来实现上下移动的效果。

具体而言,代码实现如下:

'''显示开始界面'''defstartGame(screen,sounds,bird_images,other_images,backgroud_image,cfg):base_pos=[0,cfg.SCREENHEIGHT*0.79]base_diff_bg=other_images['base'].get_width()-backgroud_image.get_width()msg_pos=[(cfg.SCREENWIDTH-other_images['message'].get_width())/2,cfg.SCREENHEIGHT*0.12]bird_idx=0bird_idx_change_count=0bird_idx_cycle=itertools.cycle([0,1,2,1])bird_pos=[cfg.SCREENWIDTH*0.2,(cfg.SCREENHEIGHT-list(bird_images.values())[0].get_height())/2]bird_y_shift_count=0bird_y_shift_max=9shift=1clock=pygame.time.Clock()whileTrue:foreventinpygame.event.get():ifevent.type==pygame.QUITor(event.type==pygame.KEYDOWNandevent.key==pygame.K_ESCAPE):pygame.quit()sys.exit()elifevent.type==pygame.KEYDOWN:ifevent.key==pygame.K_SPACEorevent.key==pygame.K_UP:return{'bird_pos':bird_pos,'base_pos':base_pos,'bird_idx':bird_idx}sounds['wing'].play()bird_idx_change_count+=1ifbird_idx_change_count%5==0:bird_idx=next(bird_idx_cycle)bird_idx_change_count=0base_pos[0]=-((-base_pos[0]+4)%base_diff_bg)bird_y_shift_count+=1ifbird_y_shift_count==bird_y_shift_max:bird_y_shift_max=16shift=-1*shiftbird_y_shift_count=0bird_pos[-1]=bird_pos[-1]+shiftscreen.blit(backgroud_image,(0,0))screen.blit(list(bird_images.values())[bird_idx],bird_pos)screen.blit(other_images['message'],msg_pos)screen.blit(other_images['base'],base_pos)pygame.display.update()clock.tick(cfg.FPS)

点击空格键或者 ↑ 键进入主程序。对于主程序,在进行了必要的初始化工作之后,在游戏开始界面中实现的内容的基础上,主要还需要实现的内容有以下几个部分:

(1) 管道和深绿浅绿交替的地板不断往左移来实现小鸟向前飞行的效果;

(2) 按键检测,当玩家点击空格键或者 ↑ 键时,小鸟向上做加速度向下的均减速直线运动直至向上的速度衰减为 0,否则小鸟做自由落体运动(实现时为了方便,可以认为在极短的时间段内小鸟的运动方式为匀速直线运动);

(3) 碰撞检测,当小鸟与管道/游戏边界碰撞到时,游戏失败并进入游戏结束界面。注意,为了碰撞检测更精确,我们使用:

pygame.sprite.collide_mask

来代替之前的:

pygame.sprite.collide_rect

(4) 进入游戏后,随机产生两对管道,并不断左移,当最左边的管道快要因为到达游戏界面的左边界而消失时,重新生成一对管道(注意不要重复生成);

(5) 当小鸟穿越一个上下管道之间的缺口时,游戏得分加一(注意不要重复记分)。

这里简单贴下主程序的源代码吧:

#进入主游戏score=0bird_pos,base_pos,bird_idx=list(game_start_info.values())base_diff_bg=other_images['base'].get_width()-backgroud_image.get_width()clock=pygame.time.Clock()#--管道类pipe_sprites=pygame.sprite.Group()foriinrange(2):pipe_pos=Pipe.randomPipe(cfg,pipe_images.get('top'))pipe_sprites.add(Pipe(image=pipe_images.get('top'),position=(cfg.SCREENWIDTH+200+i*cfg.SCREENWIDTH/2,pipe_pos.get('top')[-1])))pipe_sprites.add(Pipe(image=pipe_images.get('bottom'),position=(cfg.SCREENWIDTH+200+i*cfg.SCREENWIDTH/2,pipe_pos.get('bottom')[-1])))#--bird类bird=Bird(images=bird_images,idx=bird_idx,position=bird_pos)#--是否增加pipeis_add_pipe=True#--游戏是否进行中is_game_running=Truewhileis_game_running:foreventinpygame.event.get():ifevent.type==pygame.QUITor(event.type==pygame.KEYDOWNandevent.key==pygame.K_ESCAPE):pygame.quit()sys.exit()elifevent.type==pygame.KEYDOWN:ifevent.key==pygame.K_SPACEorevent.key==pygame.K_UP:bird.setFlapped()sounds['wing'].play()#--碰撞检测forpipeinpipe_sprites:ifpygame.sprite.collide_mask(bird,pipe):sounds['hit'].play()is_game_running=False#--更新小鸟boundary_values=[0,base_pos[-1]]is_dead=bird.update(boundary_values,float(clock.tick(cfg.FPS))/1000.)ifis_dead:sounds['hit'].play()is_game_running=False#--移动base实现小鸟往前飞的效果base_pos[0]=-((-base_pos[0]+4)%base_diff_bg)#--移动pipe实现小鸟往前飞的效果flag=Falseforpipeinpipe_sprites:pipe.rect.left-=4ifpipe.rect.centerx<bird.rect.centerxandnotpipe.used_for_score:pipe.used_for_score=Truescore+=0.5if'.5'instr(score):sounds['point'].play()ifpipe.rect.left<5andpipe.rect.left>0andis_add_pipe:pipe_pos=Pipe.randomPipe(cfg,pipe_images.get('top'))pipe_sprites.add(Pipe(image=pipe_images.get('top'),position=pipe_pos.get('top')))pipe_sprites.add(Pipe(image=pipe_images.get('bottom'),position=pipe_pos.get('bottom')))is_add_pipe=Falseelifpipe.rect.right<0:pipe_sprites.remove(pipe)flag=Trueifflag:is_add_pipe=True#--绑定必要的元素在屏幕上screen.blit(backgroud_image,(0,0))pipe_sprites.draw(screen)screen.blit(other_images['base'],base_pos)showScore(screen,score,number_images)bird.draw(screen)pygame.display.update()clock.tick(cfg.FPS)

游戏结束后,进入游戏界面。没找到对应的游戏素材,所以只是让游戏界面静止了,然后小鸟做自由落体运行直到掉到地面上。代码实现如下:

'''游戏结束界面'''defendGame(screen,sounds,showScore,score,number_images,bird,pipe_sprites,backgroud_image,other_images,base_pos,cfg):sounds['die'].play()clock=pygame.time.Clock()whileTrue:foreventinpygame.event.get():ifevent.type==pygame.QUITor(event.type==pygame.KEYDOWNandevent.key==pygame.K_ESCAPE):pygame.quit()sys.exit()elifevent.type==pygame.KEYDOWN:ifevent.key==pygame.K_SPACEorevent.key==pygame.K_UP:returnboundary_values=[0,base_pos[-1]]bird.update(boundary_values,float(clock.tick(cfg.FPS))/1000.)screen.blit(backgroud_image,(0,0))pipe_sprites.draw(screen)screen.blit(other_images['base'],base_pos)showScore(screen,score,number_images)bird.draw(screen)pygame.display.update()clock.tick(cfg.FPS)

再点击一下空格键或者 &uarr; 键即可重新开始游戏。

All done 完整源代码详见相关文件

se_pos)showScore(screen,score,number_images)bird.draw(screen)pygame.display.update()clock.tick(cfg.FPS)

再点击一下空格键或者 &uarr; 键即可重新开始游戏。

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:基于Python怎么制作flappybird游戏的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:怎么用一行Python代码实现GUI图形界面下一篇:

6 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18