本文主要是介绍140行Python代码实现Flippy Bird!应该很多人都玩过!,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
话说这游戏中文名叫什么来着,死活想不起来了,算了话不多说,140行实现小游戏系列第二章,依然是简单小游戏,与数独游戏相比,在游戏界面显示上更难一些,但是在逻辑方面更简单一些,需要处理的无非是速度、加速度、时间、位置、碰撞检测,界面方面则要实现整个动态的显示;
依旧在最后会给出全部代码,不过依然可以从我的 Github仓库 Fork下来直接运行,图片资源也在那里,have fun.
运行以及玩法:
- python main.py运行游戏;
- 鼠标点击是暂停,再点击则是继续;
- 空格键进行跳跃;
后续扩展:
- 管道的出现可以更加随机,包括位置和长度等,目前是很简单的方式出现;
- 游戏速度可以越来越快,目前是固定的;
- 小鸟的自由落体速度、跳跃速度等需要优化,目前操作感觉没有那么流畅;
- 增加计分系统、开始、重来等按钮;
小鸟图,需要的自取
游戏截图
进行中
暂停时
死亡时
关键代码分析
随时间移动的管道
可以看到对于这个游戏,实际上移动的是管道而不是小鸟,因此这里主要是处理管道绘制的位置变化,以及整个一个循环的过程,如果屏幕上显示的管道是N个,那么可以想象是N+2个管道在不停地转圈圈出现在我们的界面上就行了;
tunnel_list = [x-speed if x-speed>-200 else 2100 for x in tunnel_list ]def draw_tunnel():for x in tunnel_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 # g JUMP_V = -300 bird_x,bird_y = 700,450 bird_v = 0if not jump:bird_v += G*frame else:bird_v = JUMP_Vjump = False bird_y += frame*bird_vdef draw_bird():screen.blit(birdImg,[bird_x,bird_y])
碰撞检测
检测小鸟是否碰到管道或者是掉到地上,这么说是只无脚鸟咯,实际上就是检测两个矩形是否有重叠的部分;
def rect_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即可if up and (left_up2[1]<=right_up1[1]<=left_down2[1]):return Trueelif (not up) and (left_up2[1]<=right_down1[1]<=left_down2[1]):return Truereturn False
pygame绘制rect不支持透明度下实现的透明图层
看到,实际上是借助了Surface,将其设置为想要的透明度后blit到我们的screen上即可,直接draw rect是不支持RGBA的A设置alpha的,不知道为啥这么坑爹的设计;
def draw_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))
全部代码
import sysimport pygame from pygame.color import THECOLORS as COLORSdef draw_background():# white backgroundscreen.fill(COLORS['lightblue'])pygame.draw.rect(screen,COLORS['black'],(-100,902,3000,200),5)def draw_tunnel():for x in tunnel_list:pygame.draw.rect(screen,COLORS['darkgreen'],(x,0,100,350),0)pygame.draw.rect(screen,COLORS['darkgreen'],(x+100,550,100,350),0)def draw_bird():screen.blit(birdImg,[bird_x,bird_y])def draw_context():txt = font50.render('Count time: '+str(int(count_time))+' S',True,COLORS['black'])x,y = 10,920screen.blit(txt,(x,y))def draw_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))def draw_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))def rect_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即可if up and (left_up2[1]<=right_up1[1]<=left_down2[1]):return Trueelif (not up) and (left_up2[1]<=right_down1[1]<=left_down2[1]):return Truereturn Falsedef check_dead():bird_rect = (bird_x,bird_y,70,70)if bird_rect[1]+bird_rect[3]>900:return Truefor x in tunnel_list:up_rect = (x,0,100,350)down_rect = (x+100,550,100,350)if rect_cover(bird_rect,up_rect) or rect_cover(bird_rect,down_rect,up=False):return Truereturn Falseif __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 = Falsewhile running:for event in pygame.event.get():if event.type == pygame.QUIT:running = Falsebreakelif event.type == pygame.MOUSEBUTTONDOWN:pause = not pauseelif event.type == pygame.KEYUP:if chr(event.key) == ' ':jump = True# update dataif not pause and not dead:count_time += frametunnel_list = [x-speed if x-speed>-200 else 2100 for x in tunnel_list ]if not jump:bird_v += G*frameelse:bird_v = JUMP_Vjump = Falsebird_y += frame*bird_v# backgrounddraw_background()# tunneldraw_tunnel()# choose itemdraw_bird()# pointdraw_context()# pauseif not dead and pause:draw_pause()# deadif dead:draw_dead()# flippygame.display.flip()# pause 20mspygame.time.delay(int(frame*1000))# check win or notif check_dead():#print('You dead, dumb ass!!!')#breakdead = Truepygame.quit()
最后
数独和FlippyBird都在 这里 ,欢迎大家Fork下来直接运行,后续会不定期更新其他小游戏,目前以简单的动作小游戏、棋牌类为主,想到啥做啥,或者大家有什么想做想玩的可以评论区告诉我哈,搞得定的我会尽快完成;
详细完整代码加群:850591259
这篇关于140行Python代码实现Flippy Bird!应该很多人都玩过!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!