Python代码实现Flippy Bird

140行代码实现Flippy Bird

话说这游戏中文名叫什么来着,死活想不起来了,算了话不多说,140行实现小游戏系列第二章,依然是简单小游戏,与数独游戏相比,在游戏界面显示上更难一些,但是在逻辑方面更简单一些,需要处理的无非是速度、加速度、时间、位置、碰撞检测,界面方面则要实现整个动态的显示;

依旧在最后会给出全部代码,不过依然可以从我的 Github仓库 Fork下来直接运行,图片资源也在那里,have fun.

运行以及玩法:

python main.py运行游戏;

鼠标点击是暂停,再点击则是继续;

空格键进行跳跃;

后续扩展:

管道的出现可以更加随机,包括位置和长度等,目前是很简单的方式出现;

游戏速度可以越来越快,目前是固定的;

小鸟的自由落体速度、跳跃速度等需要优化,目前操作感觉没有那么流畅;

增加计分系统、开始、重来等按钮;

小鸟图,需要的自取

游戏截图

进行中

暂停时

死亡时

关键代码分析

随时间移动的管道

可以看到对于这个游戏,实际上移动的是管道而不是小鸟,因此这里主要是处理管道绘制的位置变化,以及整个一个循环的过程,如果屏幕上显示的管道是N个,那么可以想象是N+2个管道在不停地转圈圈出现在我们的界面上就行了;

tunnel_list = [x-speedifx-speed>-200else2100forxintunnel_list ]defdraw_tunnel():forxintunnel_list:        pygame.draw.rect(screen,COLORS['darkgreen'],(x,0,100,350),0)        pygame.draw.rect(screen,COLORS['darkgreen'],(x+100,550,100,350),0)

自由落体的小鸟和点击空格后跳起

不操作的情况下,小鸟的上下移动是做自由落体,也就是越来越快的下降的过程,而当我们点击 空格 进行跳跃后,实际上改变的就是小鸟的当前速度,因此小鸟会向上越来越慢的跳跃,直到速度为0后,继续下降,符合基本的物理规则;

G =9.8*30# gJUMP_V =-300bird_x,bird_y =700,450bird_v =0ifnotjump:    bird_v += G*frameelse:    bird_v = JUMP_V    jump =Falsebird_y += frame*bird_vdefdraw_bird():screen.blit(birdImg,[bird_x,bird_y])

碰撞检测

检测小鸟是否碰到管道或者是掉到地上,这么说是只无脚鸟咯,实际上就是检测两个矩形是否有重叠的部分;

defrect_cover(rect1,rect2,up=True):# birdleft_up1 = (rect1[0],rect1[1])    left_down1 = (rect1[0],left_up1[1]+rect1[3])    right_up1 = (left_up1[0]+rect1[2],rect1[1])    right_down1 = (left_up1[0]+rect1[2],left_up1[1]+rect1[3])# tunnelleft_up2 = (rect2[0],rect2[1])    left_down2 = (rect2[0],left_up2[1]+rect2[3])    right_up2 = (left_up2[0]+rect2[2],rect2[1])    right_down2 = (left_up2[0]+rect2[2],left_up2[1]+rect2[3])# checkif(left_up2[0]<=right_up1[0]<=right_up2[0]):# x,肯定是右侧线接触,因此判断bird的right即可ifupand(left_up2[1]<=right_up1[1]<=left_down2[1]):returnTrueelif(notup)and(left_up2[1]<=right_down1[1]<=left_down2[1]):returnTruereturnFalse

pygame绘制rect不支持透明度下实现的透明图层

看到,实际上是借助了Surface,将其设置为想要的透明度后blit到我们的screen上即可,直接draw rect是不支持RGBA的A设置alpha的,不知道为啥这么坑爹的设计;

defdraw_dead():s = pygame.Surface(SIZE, pygame.SRCALPHA)    s.fill((255,255,255,240))    screen.blit(s, (0,0))    txt = font120.render('YOU DEAD',True,COLORS['black'])    x,y =450,400screen.blit(txt,(x,y))

全部代码

importsysimportpygamefrompygame.colorimportTHECOLORSasCOLORSdefdraw_background():# white backgroundscreen.fill(COLORS['lightblue'])    pygame.draw.rect(screen,COLORS['black'],(-100,902,3000,200),5)defdraw_tunnel():forxintunnel_list:        pygame.draw.rect(screen,COLORS['darkgreen'],(x,0,100,350),0)        pygame.draw.rect(screen,COLORS['darkgreen'],(x+100,550,100,350),0)defdraw_bird():screen.blit(birdImg,[bird_x,bird_y])defdraw_context():txt = font50.render('Count time: '+str(int(count_time))+' S',True,COLORS['black'])    x,y =10,920screen.blit(txt,(x,y))defdraw_pause():s = pygame.Surface(SIZE, pygame.SRCALPHA)    s.fill((255,255,255,220))    screen.blit(s, (0,0))    txt = font120.render('PAUSE',True,COLORS['darkgray'])    x,y =550,400screen.blit(txt,(x,y))defdraw_dead():s = pygame.Surface(SIZE, pygame.SRCALPHA)    s.fill((255,255,255,240))    screen.blit(s, (0,0))    txt = font120.render('YOU DEAD',True,COLORS['black'])    x,y =450,400screen.blit(txt,(x,y))defrect_cover(rect1,rect2,up=True):# birdleft_up1 = (rect1[0],rect1[1])    left_down1 = (rect1[0],left_up1[1]+rect1[3])    right_up1 = (left_up1[0]+rect1[2],rect1[1])    right_down1 = (left_up1[0]+rect1[2],left_up1[1]+rect1[3])# tunnelleft_up2 = (rect2[0],rect2[1])    left_down2 = (rect2[0],left_up2[1]+rect2[3])    right_up2 = (left_up2[0]+rect2[2],rect2[1])    right_down2 = (left_up2[0]+rect2[2],left_up2[1]+rect2[3])# checkif(left_up2[0]<=right_up1[0]<=right_up2[0]):# x,肯定是右侧线接触,因此判断bird的right即可ifupand(left_up2[1]<=right_up1[1]<=left_down2[1]):returnTrueelif(notup)and(left_up2[1]<=right_down1[1]<=left_down2[1]):returnTruereturnFalsedefcheck_dead():bird_rect = (bird_x,bird_y,70,70)ifbird_rect[1]+bird_rect[3]>900:returnTrueforxintunnel_list:        up_rect = (x,0,100,350)        down_rect = (x+100,550,100,350)ifrect_cover(bird_rect,up_rect)orrect_cover(bird_rect,down_rect,up=False):returnTruereturnFalseif__name__ =="__main__":# init pygamepygame.init()# contantSIZE = [1500,1000]    font50 = pygame.font.SysFont('Times',50)    font120 = pygame.font.SysFont('Times',120)    G =9.8*30# gJUMP_V =-300# bridbirdPath ='bird.png'birdImg = pygame.image.load(birdPath)# tunneltunnel_list = [100,600,1100,1600,2100]# create screen 500*500screen = pygame.display.set_mode(SIZE)# variable parameterbird_x,bird_y =700,450bird_v =0count_time =0# levelspeed =5frame =0.02# main looprunning =Truepause =Falsejump =Falsedead =Falsewhilerunning:foreventinpygame.event.get():ifevent.type == pygame.QUIT:                running =Falsebreakelifevent.type == pygame.MOUSEBUTTONDOWN:                pause =notpauseelifevent.type == pygame.KEYUP:ifchr(event.key) ==' ':                    jump =True# update dataifnotpauseandnotdead:            count_time += frame            tunnel_list = [x-speedifx-speed>-200else2100forxintunnel_list ]ifnotjump:                bird_v += G*frameelse:                bird_v = JUMP_V                jump =Falsebird_y += frame*bird_v# backgrounddraw_background()# tunneldraw_tunnel()# choose itemdraw_bird()# pointdraw_context()# pauseifnotdeadandpause:            draw_pause()# deadifdead:            draw_dead()# flippygame.display.flip()# pause 20mspygame.time.delay(int(frame*1000))# check win or notifcheck_dead():#print('You dead, dumb ass!!!')#breakdead =Truepygame.quit()

最后

数独和FlippyBird都在 这里 ,欢迎大家Fork下来直接运行,后续会不定期更新其他小游戏,目前以简单的动作小游戏、棋牌类为主,想到啥做啥,或者大家有什么想做想玩的可以评论区告诉我哈,搞得定的我会尽快完成;

最后的最后

大家可以到我的Github上看看有没有其他需要的东西,目前主要是自己做的机器学习项目、Python各种脚本工具、有意思的小项目以及Follow的大佬、Fork的项目等:

你可能感兴趣的:(Python代码实现Flippy Bird)