python實(shí)現(xiàn)Flappy Bird源碼
Flappy Bird是前段時(shí)間(好像一年or兩年前....)特別火的有一個(gè)小游戲,相信大家都玩過。
Flappy Bird操作簡單,通過點(diǎn)擊手機(jī)屏幕使Bird上升,穿過柱狀障礙物之后得分,碰到則游戲結(jié)束。由于障礙物高低不等,控制Bird上升和下降需要反應(yīng)快并且靈活,要得到較高的分?jǐn)?shù)并不容易。作為一個(gè)游戲渣,我最高紀(jì)錄是8分......
我記得當(dāng)時(shí)還想,是誰發(fā)明了這個(gè)小游戲,逼死強(qiáng)迫癥,記得當(dāng)時(shí)本科時(shí)好多人在玩....
無意間在GitHub上看到了python實(shí)現(xiàn)的代碼,所以拿來學(xué)習(xí)了一番。代碼思路比較簡潔。
因?yàn)榈谝淮谓佑|pygame,所以代碼注釋寫的比較詳細(xì),也算是一次新體驗(yàn)。
玩法:空格鍵進(jìn)入游戲,↑控制小鳥飛行
注意:需要安裝pygame模塊
代碼:
# -*- coding: utf8 -*-
from itertools import cycle
import random
import sys
import pygame #將pygame庫導(dǎo)入到python程序中
from pygame.locals import * #需要引入pygame中的所有常量。
FPS = 30
SCREENWIDTH = 288 #屏幕寬度
SCREENHEIGHT = 512 #屏幕高度
# amount by which base can maximum shift to left
PIPEGAPSIZE = 100 # gap between upper and lower part of pipe 管道上下之間的間隙
BASEY = SCREENHEIGHT * 0.79 #base那個(gè)條條所在的高度 注意以左上角為坐標(biāo)起始點(diǎn) 所以這個(gè)高度是往下為正
# image, sound and hitmask dicts
IMAGES, SOUNDS, HITMASKS = {}, {}, {} #圖像,聲音,撞擊的文件
# list of all possible players (tuple of 3 positions of flap) #三種小鳥造型
PLAYERS_LIST = (
# red bird
(
'assets/sprites/redbird-upflap.png',
'assets/sprites/redbird-midflap.png',
'assets/sprites/redbird-downflap.png',
),
# blue bird
(
# amount by which base can maximum shift to left
'assets/sprites/bluebird-upflap.png',
'assets/sprites/bluebird-midflap.png',
'assets/sprites/bluebird-downflap.png',
),
# yellow bird
(
'assets/sprites/yellowbird-upflap.png',
'assets/sprites/yellowbird-midflap.png',
'assets/sprites/yellowbird-downflap.png',
),
)
# list of backgrounds 兩種背景,一種白天,一種黑夜
BACKGROUNDS_LIST = (
'assets/sprites/background-day.png',
'assets/sprites/background-night.png',
)
# list of pipes 管道的兩種顏色,一種綠色,一種紅色
PIPES_LIST = (
'assets/sprites/pipe-green.png',
'assets/sprites/pipe-red.png',
)
try:
xrange
except NameError:
xrange = range
def main():
global SCREEN, FPSCLOCK
pygame.init() #經(jīng)過初始化以后我們就可以盡情地使用pygame了。
#使用Pygame時(shí)鐘之前,必須先創(chuàng)建Clock對象的一個(gè)實(shí)例,
FPSCLOCK = pygame.time.Clock()#控制每個(gè)循環(huán)多長時(shí)間運(yùn)行一次。這就像一個(gè)定時(shí)器在控制時(shí)間進(jìn)程,指出“現(xiàn)在開始下一個(gè)循環(huán)”!現(xiàn)在開始下一個(gè)循環(huán)!……
SCREEN = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))#通常來說我們需要先創(chuàng)建一個(gè)窗口,方便我們與程序的交互。
pygame.display.set_caption('Flappy Bird')#設(shè)置窗口標(biāo)題
# numbers sprites for score display #加載并轉(zhuǎn)換圖像
#在pygame中可以使用pygame.image.load()函數(shù)來加載位圖 (支持jpg,png,gif,bmp,pcx,tif,tga等多種圖片格式)。
#convert_alpha()方法會(huì)使用透明的方法繪制前景對象。
# 因此在加載一個(gè)有alpha通道的素材時(shí)(比如PNG TGA),需要使用convert_alpha()方法,當(dāng)然普通的圖片也是可以使用這個(gè)方法的,用了也不會(huì)有什么副作用。
IMAGES['numbers'] = (
pygame.image.load('assets/sprites/0.png').convert_alpha(),
pygame.image.load('assets/sprites/1.png').convert_alpha(),
pygame.image.load('assets/sprites/2.png').convert_alpha(),
pygame.image.load('assets/sprites/3.png').convert_alpha(),
pygame.image.load('assets/sprites/4.png').convert_alpha(),
pygame.image.load('assets/sprites/5.png').convert_alpha(),
pygame.image.load('assets/sprites/6.png').convert_alpha(),
pygame.image.load('assets/sprites/7.png').convert_alpha(),
pygame.image.load('assets/sprites/8.png').convert_alpha(),
pygame.image.load('assets/sprites/9.png').convert_alpha()
)
# game over sprite 游戲結(jié)束顯示的圖像
IMAGES['gameover'] = pygame.image.load('assets/sprites/gameover.png').convert_alpha()
# message sprite for welcome screen 歡迎界面顯示的圖像
IMAGES['message'] = pygame.image.load('assets/sprites/message.png').convert_alpha()
# base (ground) sprite 始終顯示的base圖像
IMAGES['base'] = pygame.image.load('assets/sprites/base.png').convert_alpha()
# sounds
# WAV版 OGG版是指游戲的音頻格式
# WAV版是屬于游戲原版
# OGG是大大們通過轉(zhuǎn)換器把音頻格式的WAV改成OGG,這樣游戲的配置提高要求使游戲本身的體積而縮小節(jié)省了空間。
#可以看一下同一個(gè)音頻 ogg版的是比wav版的文件小很多
if 'win' in sys.platform: #判斷當(dāng)前系統(tǒng)平臺(tái) 來設(shè)置聲音文件后綴
soundExt = '.wav'
else:
soundExt = '.ogg'
# 音效:pygame.mixer
# sound = pygame.mixer.Sound('/home/liumin/love.wav')使用指定文件名載入一個(gè)音頻文件,并創(chuàng)建一個(gè)Sound對象。 音頻文件可以是wav,ogg等格式。
# 音頻文件的內(nèi)容會(huì)被全部載入到內(nèi)存中。
SOUNDS['die'] = pygame.mixer.Sound('assets/audio/die' + soundExt)
SOUNDS['hit'] = pygame.mixer.Sound('assets/audio/hit' + soundExt)
SOUNDS['point'] = pygame.mixer.Sound('assets/audio/point' + soundExt)
SOUNDS['swoosh'] = pygame.mixer.Sound('assets/audio/swoosh' + soundExt)
SOUNDS['wing'] = pygame.mixer.Sound('assets/audio/wing' + soundExt)
while True:
# select random background sprites 加載隨機(jī)背景 (白天或者黑夜)
randBg = random.randint(0, len(BACKGROUNDS_LIST) - 1)#隨機(jī)選擇0或者1
IMAGES['background'] = pygame.image.load(BACKGROUNDS_LIST[randBg]).convert()#加載隨機(jī)背景
# select random player sprites 加載隨機(jī)角色 (紅色、藍(lán)色、黃色小鳥)
randPlayer = random.randint(0, len(PLAYERS_LIST) - 1)
IMAGES['player'] = (
pygame.image.load(PLAYERS_LIST[randPlayer][0]).convert_alpha(),
pygame.image.load(PLAYERS_LIST[randPlayer][1]).convert_alpha(),
pygame.image.load(PLAYERS_LIST[randPlayer][2]).convert_alpha(),
)
# select random pipe sprites 加載隨機(jī)管道樣式
pipeindex = random.randint(0, len(PIPES_LIST) - 1)
IMAGES['pipe'] = (
pygame.transform.rotate(
pygame.image.load(PIPES_LIST[pipeindex]).convert_alpha(), 180),#旋轉(zhuǎn)180度
pygame.image.load(PIPES_LIST[pipeindex]).convert_alpha(),
)#一個(gè)上面的管道 一個(gè)下面的管道
# hismask for pipes #得到管道的邊界mask
HITMASKS['pipe'] = (
getHitmask(IMAGES['pipe'][0]),
getHitmask(IMAGES['pipe'][1]),
)
# hitmask for player #得到player的邊界mask
HITMASKS['player'] = (
getHitmask(IMAGES['player'][0]),
getHitmask(IMAGES['player'][1]),
getHitmask(IMAGES['player'][2]),
)
movementInfo = showWelcomeAnimation()#返回'playery'(player所在位置),'basex'(base圖像所在位置) 'playerIndexGen'(飛行姿勢index)
crashInfo = mainGame(movementInfo)
showGameOverScreen(crashInfo)
def showWelcomeAnimation():
"""Shows welcome screen animation of flappy bird"""
# index of player to blit on screen
playerIndex = 0
playerIndexGen = cycle([0, 1, 2, 1])
# iterator used to change playerIndex after every 5th iteration
loopIter = 0
#player所在位置
playerx = int(SCREENWIDTH * 0.2)
playery = int((SCREENHEIGHT - IMAGES['player'][0].get_height()) / 2)
#歡迎圖像所在位置
messagex = int((SCREENWIDTH - IMAGES['message'].get_width()) / 2)
messagey = int(SCREENHEIGHT * 0.12)
basex = 0
# amount by which base can maximum shift to left 可以最大限度地向左移動(dòng)的距離
baseShift = IMAGES['base'].get_width() - IMAGES['background'].get_width()
# player shm for up-down motion on welcome screen 角色在歡迎屏幕上進(jìn)行上下移動(dòng)
playerShmVals = {'val': 0, 'dir': 1}
while True:
for event in pygame.event.get():#使用pygame.event.get()來處理所有的事件,
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):#如果 quit 或者 按鍵之后又按下esc,就結(jié)束游戲
pygame.quit()
sys.exit()
if event.type == KEYDOWN and (event.key == K_SPACE or event.key == K_UP):#如果按鍵之后點(diǎn)擊或者按下↑
# make first flap sound and return values for mainGame
SOUNDS['wing'].play()#播放飛的特效聲音
return {#返回初始位置 進(jìn)入maingame
'playery': playery + playerShmVals['val'],
'basex': basex,
'playerIndexGen': playerIndexGen,
}
# adjust playery, playerIndex, basex
if (loopIter + 1) % 5 == 0:
playerIndex = next(playerIndexGen)#獲得匹配元素集合中每個(gè)元素緊鄰的同胞元素 調(diào)整飛行姿勢圖片
loopIter = (loopIter + 1) % 30
basex = -((-basex + 4) % baseShift)
playerShm(playerShmVals)
# draw sprites
#screen.blit(space, (0,0))可以繪制位圖 第一個(gè)參數(shù)是加載完成的位圖,第二個(gè)參數(shù)是繪制的起始坐標(biāo)。
SCREEN.blit(IMAGES['background'], (0,0))
SCREEN.blit(IMAGES['player'][playerIndex],
(playerx, playery + playerShmVals['val']))
SCREEN.blit(IMAGES['message'], (messagex, messagey))
SCREEN.blit(IMAGES['base'], (basex, BASEY))
pygame.display.update()#更新整個(gè)窗口
FPSCLOCK.tick(FPS)#循環(huán)應(yīng)該多長時(shí)間運(yùn)行一次
def mainGame(movementInfo):
score = playerIndex = loopIter = 0#初始得分以及初始player的姿態(tài)以及迭代次數(shù)都為0
playerIndexGen = movementInfo['playerIndexGen']#得到飛行姿勢
playerx, playery = int(SCREENWIDTH * 0.2), movementInfo['playery']#player所在位置
basex = movementInfo['basex']#base圖像所在位置
baseShift = IMAGES['base'].get_width() - IMAGES['background'].get_width()
# get 2 new pipes to add to upperPipes lowerPipes list
newPipe1 = getRandomPipe()
newPipe2 = getRandomPipe()
# list of upper pipes
upperPipes = [
{'x': SCREENWIDTH + 200, 'y': newPipe1[0]['y']},
{'x': SCREENWIDTH + 200 + (SCREENWIDTH / 2), 'y': newPipe2[0]['y']},
]
# list of lowerpipe
lowerPipes = [
{'x': SCREENWIDTH + 200, 'y': newPipe1[1]['y']},
{'x': SCREENWIDTH + 200 + (SCREENWIDTH / 2), 'y': newPipe2[1]['y']},
]
pipeVelX = -4
# player velocity, max velocity, downward accleration, accleration on flap 角色速度,最大速度,向下加速度,襟翼加速度
playerVelY = -9 # player's velocity along Y, default same as playerFlapped
playerMaxVelY = 10 # max vel along Y, max descend speed
playerMinVelY = -8 # min vel along Y, max ascend speed
playerAccY = 1 # players downward accleration
playerRot = 45 # player's rotation
playerVelRot = 3 # angular speed
playerRotThr = 20 # rotation threshold
playerFlapAcc = -9 # players speed on flapping
playerFlapped = False # True when player flaps
while True:
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
if event.type == KEYDOWN and (event.key == K_SPACE or event.key == K_UP):
if playery > -2 * IMAGES['player'][0].get_height():#如果點(diǎn)擊
playerVelY = playerFlapAcc#上升
playerFlapped = True
SOUNDS['wing'].play()#并播放飛行音效
# check for crash here
crashTest = checkCrash({'x': playerx, 'y': playery, 'index': playerIndex},
upperPipes, lowerPipes)
if crashTest[0]:#如果掉在地上或者撞擊到了管道,就返回結(jié)束游戲
return {
'y': playery,
'groundCrash': crashTest[1],
'basex': basex,
'upperPipes': upperPipes,
'lowerPipes': lowerPipes,
'score': score,
'playerVelY': playerVelY,
'playerRot': playerRot
}
# check for score
playerMidPos = playerx + IMAGES['player'][0].get_width() / 2
for pipe in upperPipes:
pipeMidPos = pipe['x'] + IMAGES['pipe'][0].get_width() / 2
if pipeMidPos <= playerMidPos < pipeMidPos + 4:#當(dāng)角色達(dá)到管道縫隙的中間+4時(shí),score+1,并且在此時(shí)播放得分音效
score += 1
SOUNDS['point'].play()
# playerIndex basex change
if (loopIter + 1) % 3 == 0:
playerIndex = next(playerIndexGen)
loopIter = (loopIter + 1) % 30
basex = -((-basex + 100) % baseShift)
# rotate the player
if playerRot > -90:
playerRot -= playerVelRot
# player's movement
if playerVelY < playerMaxVelY and not playerFlapped:
playerVelY += playerAccY
if playerFlapped:
playerFlapped = False
# more rotation to cover the threshold (calculated in visible rotation)
playerRot = 45
playerHeight = IMAGES['player'][playerIndex].get_height()
playery += min(playerVelY, BASEY - playery - playerHeight)
# move pipes to left
for uPipe, lPipe in zip(upperPipes, lowerPipes):
uPipe['x'] += pipeVelX #管道移動(dòng)
lPipe['x'] += pipeVelX
# add new pipe when first pipe is about to touch left of screen
if 0 < upperPipes[0]['x'] < 5:#當(dāng)?shù)谝粋€(gè)管道移動(dòng)到屏幕左側(cè)邊緣時(shí),生成下一個(gè)管道
newPipe = getRandomPipe()
upperPipes.append(newPipe[0])
lowerPipes.append(newPipe[1])
# remove first pipe if its out of the screen
if upperPipes[0]['x'] < -IMAGES['pipe'][0].get_width(): #當(dāng)管道移動(dòng)到屏幕外側(cè)后,刪除它
upperPipes.pop(0)
lowerPipes.pop(0)
# draw sprites
SCREEN.blit(IMAGES['background'], (0,0))
for uPipe, lPipe in zip(upperPipes, lowerPipes):
SCREEN.blit(IMAGES['pipe'][0], (uPipe['x'], uPipe['y']))
SCREEN.blit(IMAGES['pipe'][1], (lPipe['x'], lPipe['y']))
SCREEN.blit(IMAGES['base'], (basex, BASEY))
# print score so player overlaps the score
showScore(score) #顯示得分
# Player rotation has a threshold
visibleRot = playerRotThr
if playerRot <= playerRotThr:
visibleRot = playerRot
playerSurface = pygame.transform.rotate(IMAGES['player'][playerIndex], visibleRot)#旋轉(zhuǎn)角色
SCREEN.blit(playerSurface, (playerx, playery))#顯示旋轉(zhuǎn)后的角色
pygame.display.update()#更新窗口
FPSCLOCK.tick(FPS)#循環(huán)應(yīng)該多長時(shí)間運(yùn)行一次
def showGameOverScreen(crashInfo):
"""crashes the player down ans shows gameover image"""
score = crashInfo['score']#獲取得分
playerx = SCREENWIDTH * 0.2
playery = crashInfo['y']
playerHeight = IMAGES['player'][0].get_height()
playerVelY = crashInfo['playerVelY']
playerAccY = 2
playerRot = crashInfo['playerRot']
playerVelRot = 7
basex = crashInfo['basex']
upperPipes, lowerPipes = crashInfo['upperPipes'], crashInfo['lowerPipes']
# play hit and die sounds
SOUNDS['hit'].play()
if not crashInfo['groundCrash']:#如果沒有撞擊到地面,就播放die音效就可以了
SOUNDS['die'].play()
while True:
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
if event.type == KEYDOWN and (event.key == K_SPACE or event.key == K_UP):
if playery + playerHeight >= BASEY - 1:
return
# player y shift
if playery + playerHeight < BASEY - 1:
playery += min(playerVelY, BASEY - playery - playerHeight)
# player velocity change
if playerVelY < 15:
playerVelY += playerAccY
# rotate only when it's a pipe crash
if not crashInfo['groundCrash']:
if playerRot > -90:
playerRot -= playerVelRot
# draw sprites
SCREEN.blit(IMAGES['background'], (0,0))
for uPipe, lPipe in zip(upperPipes, lowerPipes):
SCREEN.blit(IMAGES['pipe'][0], (uPipe['x'], uPipe['y']))
SCREEN.blit(IMAGES['pipe'][1], (lPipe['x'], lPipe['y']))
SCREEN.blit(IMAGES['base'], (basex, BASEY))
showScore(score)
playerSurface = pygame.transform.rotate(IMAGES['player'][1], playerRot)
SCREEN.blit(playerSurface, (playerx,playery))
FPSCLOCK.tick(FPS)
pygame.display.update()
def playerShm(playerShm):
"""oscillates the value of playerShm['val'] between 8 and -8"""
if abs(playerShm['val']) == 8:
playerShm['dir'] *= -1
if playerShm['dir'] == 1:
playerShm['val'] += 1
else:
playerShm['val'] -= 1
def getRandomPipe():#隨機(jī)生成隨機(jī)高度的管道 ????????還需要看細(xì)節(jié)
"""returns a randomly generated pipe"""
# y of gap between upper and lower pipe
gapY = random.randrange(0, int(BASEY * 0.6 - PIPEGAPSIZE))
gapY += int(BASEY * 0.2)
pipeHeight = IMAGES['pipe'][0].get_height()
pipeX = SCREENWIDTH + 10
return [
{'x': pipeX, 'y': gapY - pipeHeight}, # upper pipe
{'x': pipeX, 'y': gapY + PIPEGAPSIZE}, # lower pipe
]
def showScore(score):
"""displays score in center of screen"""
scoreDigits = [int(x) for x in list(str(score))]
totalWidth = 0 # total width of all numbers to be printed
for digit in scoreDigits:
totalWidth += IMAGES['numbers'][digit].get_width()
Xoffset = (SCREENWIDTH - totalWidth) / 2
for digit in scoreDigits:
SCREEN.blit(IMAGES['numbers'][digit], (Xoffset, SCREENHEIGHT * 0.1))#顯示得分
Xoffset += IMAGES['numbers'][digit].get_width()
def checkCrash(player, upperPipes, lowerPipes):
"""returns True if player collders with base or pipes."""
pi = player['index']#飛行姿勢
player['w'] = IMAGES['player'][0].get_width()
player['h'] = IMAGES['player'][0].get_height()
# if player crashes into ground 掉在地上
if player['y'] + player['h'] >= BASEY - 1:
return [True, True] #返回
else:
playerRect = pygame.Rect(player['x'], player['y'],
player['w'], player['h'])
pipeW = IMAGES['pipe'][0].get_width()
pipeH = IMAGES['pipe'][0].get_height()
for uPipe, lPipe in zip(upperPipes, lowerPipes):
# upper and lower pipe rects
uPipeRect = pygame.Rect(uPipe['x'], uPipe['y'], pipeW, pipeH)
lPipeRect = pygame.Rect(lPipe['x'], lPipe['y'], pipeW, pipeH)
# player and upper/lower pipe hitmasks
pHitMask = HITMASKS['player'][pi]
uHitmask = HITMASKS['pipe'][0]
lHitmask = HITMASKS['pipe'][1]
# if bird collided with upipe or lpipe
uCollide = pixelCollision(playerRect, uPipeRect, pHitMask, uHitmask)
lCollide = pixelCollision(playerRect, lPipeRect, pHitMask, lHitmask)
if uCollide or lCollide:#如果撞擊到了上管道或者下管道 返回
return [True, False]
return [False, False]
def pixelCollision(rect1, rect2, hitmask1, hitmask2):
"""Checks if two objects collide and not just their rects"""
rect = rect1.clip(rect2)#角色和管道之間重合的情況
if rect.width == 0 or rect.height == 0:#沒重合就是沒撞擊到
return False
x1, y1 = rect.x - rect1.x, rect.y - rect1.y
x2, y2 = rect.x - rect2.x, rect.y - rect2.y
for x in xrange(rect.width):
for y in xrange(rect.height):
if hitmask1[x1+x][y1+y] and hitmask2[x2+x][y2+y]:#撞擊到了
return True
return False
def getHitmask(image):
"""returns a hitmask using an image's alpha."""
#得到撞擊mask
mask = []
for x in xrange(image.get_width()):
mask.append([])
for y in xrange(image.get_height()):
mask[x].append(bool(image.get_at((x,y))[3]))
return mask
if __name__ == '__main__':
main()
游戲截圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)一個(gè)帶權(quán)無回置隨機(jī)抽選函數(shù)的方法
這篇文章主要介紹了Python實(shí)現(xiàn)一個(gè)帶權(quán)無回置隨機(jī)抽選函數(shù)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Pandas實(shí)現(xiàn)數(shù)據(jù)拼接的操作方法詳解
Python處理大規(guī)模數(shù)據(jù)集的時(shí)候經(jīng)常需要使用到合并、鏈接的方式進(jìn)行數(shù)據(jù)集的整合,本文為大家主要介紹了.merge()、?.join()?和?.concat()?三種方法,感興趣的可以了解一下2022-04-04
Python多線程threading和multiprocessing模塊實(shí)例解析
這篇文章主要介紹了Python多線程threading和multiprocessing模塊等相關(guān)內(nèi)容,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,這里分享給大家,需要的朋友可以參考下2018-01-01
python3.6.3轉(zhuǎn)化為win-exe文件發(fā)布的方法
今天小編就為大家分享一篇python3.6.3轉(zhuǎn)化為win-exe文件發(fā)布的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10

