python實(shí)現(xiàn)flappy bird游戲
flappy bird最近火遍大江南北,教你用python寫游戲的第一課就向它開刀了。
這個(gè)課程的基礎(chǔ)是假定你有比較不錯(cuò)的編程功底,對(duì)python有一點(diǎn)點(diǎn)的基礎(chǔ)。
一、準(zhǔn)備工作
1、用python寫游戲需要什么呢?
1)當(dāng)然是python本身了,我用的是python2.7,不同版本大同小異。
2)pygame,這個(gè)非常重要,所有的核心都是基于這個(gè)lib的。
2、分析游戲
flappy bird這個(gè)游戲很簡單,大致可以分為4個(gè)部分:
1)背景。背景分為兩個(gè),一個(gè)是bg,一個(gè)是land。bg就是那張有著天空白云的圖,land就是最下面有斜杠的圖。
2)bird。這個(gè)不用我說,主角是也。
3)pipe。就是那個(gè)水管。
4)其他。包括開始菜單和分?jǐn)?shù)板。
著重分析的就是bird和pipe。
bird會(huì)一直往右飛,不點(diǎn)屏幕就會(huì)往下掉。
pipe會(huì)不斷地出現(xiàn),每通過一個(gè)pipe就會(huì)加一分。
bird撞到pipe或者掉到地上游戲就會(huì)結(jié)束。
3、準(zhǔn)備資源
找一個(gè)flappy bird的apk,提取一下內(nèi)部文件,你就可以獲得:
1)一張叫做atlas.png的圖片。里面有所有我們要用得到的圖。
2)5個(gè)ogg文件,包含了所有音效。
3)一個(gè)叫做atlas.txt的文本文件,包含了圖片在大圖中的位置。
二、開始
上一中,我們已經(jīng)分析過了2個(gè)核心,bird和pipe。這一單元,我要講訴的就是bird。
首先呢,我們需要?jiǎng)?chuàng)建一個(gè)對(duì)象,這個(gè)對(duì)象取名為Bird。
Bird具有以下屬性:
1)圖片。具體來說就是他長什么樣。
2)大小。長多大。
3)是否撞到了。還記得游戲規(guī)則么,撞到就gameover了。
4)速度。每一幀移動(dòng)多遠(yuǎn)。
這只bird沒事還會(huì)往下掉,點(diǎn)一下就會(huì)往上飛,這就是兩個(gè)動(dòng)作。
于是,編寫了如下代碼:
class Bird(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 def move(self): self.rect.left += self.speed self.rect.top += self.speed def click(self): self.rect.top -= 1.5*self.speed
還記得最開始我說過,flappy bird所有的圖片資源都在一張圖片altas.png上。
pygame提供了一個(gè)函數(shù),可以讓我們方便的取出資源。
我們先載入圖片
#load img 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() 然后分別獲取需要的圖片。 #config bird 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)的圖片就落實(shí)了。
最后,因?yàn)槭窃陔娔X上運(yùn)行,點(diǎn)屏幕就需要改成相應(yīng)的按下空格鍵。
key_pressed = pygame.key.get_pressed() if not bird.is_hit: if key_pressed[K_SPACE]: bird.click()
終于,任務(wù)完成了,雖然,雖然程序有點(diǎn)小bug,但這是下面要說的問題了。
完整代碼如下:
# -*- coding: utf-8 -*- """ @author: Kevio """ import pygame from pygame.locals import * from sys import exit import random # configure screen_w = 288 screen_h = 512 # class class Bird(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 def move(self): self.rect.left += self.speed self.rect.top += self.speed def click(self): self.rect.top -= 1.5*self.speed # init the game pygame.init() screen = pygame.display.set_mode((screen_w,screen_h)) pygame.display.set_caption('flappy bird @Kevio') #load img 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() #config bird 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) #config the game score = 0 clock = pygame.time.Clock() running = True while running: clock.tick(60) screen.fill(0) screen.blit(bg_img,(0,0)) if not bird.is_hit: screen.blit(bird.image,bird.rect) bird.move() else: running = False pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() key_pressed = pygame.key.get_pressed() if not bird.is_hit: if key_pressed[K_SPACE]: bird.click()
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談Django自定義模板標(biāo)簽template_tags的用處
這篇文章主要介紹了淺談Django自定義模板標(biāo)簽template_tags的用處,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12TensorFlow實(shí)現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)
這篇文章主要為大家詳細(xì)介紹了TensorFlow實(shí)現(xiàn)卷積神經(jīng)網(wǎng)絡(luò),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Python使用Matplotlib實(shí)現(xiàn)Logos設(shè)計(jì)代碼
這篇文章主要介紹了Python使用Matplotlib實(shí)現(xiàn)Logos設(shè)計(jì)代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12