python实现flappy bird游戏
flappybird最近火遍大江南北,教你用python写游戏的第一课就向它开刀了。
这个课程的基础是假定你有比较不错的编程功底,对python有一点点的基础。
一、准备工作
1、用python写游戏需要什么呢?
1)当然是python本身了,我用的是python2.7,不同版本大同小异。
2)pygame,这个非常重要,所有的核心都是基于这个lib的。
2、分析游戏
flappybird这个游戏很简单,大致可以分为4个部分:
1)背景。背景分为两个,一个是bg,一个是land。bg就是那张有着天空白云的图,land就是最下面有斜杠的图。
2)bird。这个不用我说,主角是也。
3)pipe。就是那个水管。
4)其他。包括开始菜单和分数板。
着重分析的就是bird和pipe。
bird会一直往右飞,不点屏幕就会往下掉。
pipe会不断地出现,每通过一个pipe就会加一分。
bird撞到pipe或者掉到地上游戏就会结束。
3、准备资源
找一个flappybird的apk,提取一下内部文件,你就可以获得:
1)一张叫做atlas.png的图片。里面有所有我们要用得到的图。
2)5个ogg文件,包含了所有音效。
3)一个叫做atlas.txt的文本文件,包含了图片在大图中的位置。
二、开始
上一中,我们已经分析过了2个核心,bird和pipe。这一单元,我要讲诉的就是bird。
首先呢,我们需要创建一个对象,这个对象取名为Bird。
Bird具有以下属性:
1)图片。具体来说就是他长什么样。
2)大小。长多大。
3)是否撞到了。还记得游戏规则么,撞到就gameover了。
4)速度。每一帧移动多远。
这只bird没事还会往下掉,点一下就会往上飞,这就是两个动作。
于是,编写了如下代码:
classBird(pygame.sprite.Sprite): def__init__(self,bird_img,pos): pygame.sprite.Sprite.__init__(self) self.image=bird_img self.rect=self.image.get_rect() self.rect.midbottom=pos self.speed=1 self.is_hit=False defmove(self): self.rect.left+=self.speed self.rect.top+=self.speed defclick(self): self.rect.top-=1.5*self.speed
还记得最开始我说过,flappybird所有的图片资源都在一张图片altas.png上。
pygame提供了一个函数,可以让我们方便的取出资源。
我们先载入图片
#loadimg game_img=pygame.image.load('res/img/atlas.png') bg_rect=pygame.Rect(0,0,288,512) bg_img=game_img.subsurface(bg_rect).convert() 然后分别获取需要的图片。 #configbird bird_rect=pygame.Rect(0,970,48,48) bird_pos=[100,230] bird_img=game_img.subsurface(bird_rect).convert_alpha() bird=Bird(bird_img,bird_pos)
这样bird和bg(background)的图片就落实了。
最后,因为是在电脑上运行,点屏幕就需要改成相应的按下空格键。
key_pressed=pygame.key.get_pressed() ifnotbird.is_hit: ifkey_pressed[K_SPACE]: bird.click()
终于,任务完成了,虽然,虽然程序有点小bug,但这是下面要说的问题了。
完整代码如下:
#-*-coding:utf-8-*- """ @author:Kevio """ importpygame frompygame.localsimport* fromsysimportexit importrandom #configure screen_w=288 screen_h=512 #class classBird(pygame.sprite.Sprite): def__init__(self,bird_img,pos): pygame.sprite.Sprite.__init__(self) self.image=bird_img self.rect=self.image.get_rect() self.rect.midbottom=pos self.speed=1 self.is_hit=False defmove(self): self.rect.left+=self.speed self.rect.top+=self.speed defclick(self): self.rect.top-=1.5*self.speed #initthegame pygame.init() screen=pygame.display.set_mode((screen_w,screen_h)) pygame.display.set_caption('flappybird@Kevio') #loadimg game_img=pygame.image.load('res/img/atlas.png') bg_rect=pygame.Rect(0,0,288,512) bg_img=game_img.subsurface(bg_rect).convert() #configbird bird_rect=pygame.Rect(0,970,48,48) bird_pos=[100,230] bird_img=game_img.subsurface(bird_rect).convert_alpha() bird=Bird(bird_img,bird_pos) #configthegame score=0 clock=pygame.time.Clock() running=True whilerunning: clock.tick(60) screen.fill(0) screen.blit(bg_img,(0,0)) ifnotbird.is_hit: screen.blit(bird.image,bird.rect) bird.move() else: running=False pygame.display.update() foreventinpygame.event.get(): ifevent.type==pygame.QUIT: pygame.quit() exit() key_pressed=pygame.key.get_pressed() ifnotbird.is_hit: ifkey_pressed[K_SPACE]: bird.click()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。