python?flappy?bird小游戲分步實現(xiàn)流程
導語:
哈嘍,哈嘍~今天小編又來分享小游戲了——flappy bird(飛揚的小鳥),這個游戲非常的經(jīng)典,游戲中玩家必須控制一只小鳥,跨越由各種不同長度水管所組成的障礙。這個游戲能對于小編來說還是有點難度的。


開發(fā)工具:
Python版本:3.6.4
相關(guān)模塊:
pygame模塊;
以及一些python自帶的模塊。
環(huán)境搭建
安裝Python并添加到環(huán)境變量,pip安裝需要的相關(guān)模塊即可。
運行視頻:

播放鏈接:https://live.csdn.net/v/embed/184490
正文:
首先,我們來寫個開始界面,讓他看起來更像個游戲一些。效果大概是這樣的:

原理也簡單,關(guān)鍵點有三個:
(1)下方深綠淺綠交替的地板不斷往左移動來制造小鳥向前飛行的假象;
(2)每過幾幀切換一下小鳥的圖片來實現(xiàn)小鳥翅膀扇動的效果:

(3)有規(guī)律地改變小鳥豎直方向上的位置來實現(xiàn)上下移動的效果。
具體而言,代碼實現(xiàn)如下:
'''顯示開始界面'''
def startGame(screen, sounds, bird_images, other_images, backgroud_image, cfg):
base_pos = [0, cfg.SCREENHEIGHT*0.79]
base_diff_bg = other_images['base'].get_width() - backgroud_image.get_width()
msg_pos = [(cfg.SCREENWIDTH-other_images['message'].get_width())/2, cfg.SCREENHEIGHT*0.12]
bird_idx = 0
bird_idx_change_count = 0
bird_idx_cycle = itertools.cycle([0, 1, 2, 1])
bird_pos = [cfg.SCREENWIDTH*0.2, (cfg.SCREENHEIGHT-list(bird_images.values())[0].get_height())/2]
bird_y_shift_count = 0
bird_y_shift_max = 9
shift = 1
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE or event.key == pygame.K_UP:
return {'bird_pos': bird_pos, 'base_pos': base_pos, 'bird_idx': bird_idx}
sounds['wing'].play()
bird_idx_change_count += 1
if bird_idx_change_count % 5 == 0:
bird_idx = next(bird_idx_cycle)
bird_idx_change_count = 0
base_pos[0] = -((-base_pos[0] + 4) % base_diff_bg)
bird_y_shift_count += 1
if bird_y_shift_count == bird_y_shift_max:
bird_y_shift_max = 16
shift = -1 * shift
bird_y_shift_count = 0
bird_pos[-1] = bird_pos[-1] + shift
screen.blit(backgroud_image, (0, 0))
screen.blit(list(bird_images.values())[bird_idx], bird_pos)
screen.blit(other_images['message'], msg_pos)
screen.blit(other_images['base'], base_pos)
pygame.display.update()
clock.tick(cfg.FPS)點擊空格鍵或者↑鍵進入主程序。對于主程序,在進行了必要的初始化工作之后,在游戲開始界面中實現(xiàn)的內(nèi)容的基礎(chǔ)上,主要還需要實現(xiàn)的內(nèi)容有以下幾個部分:
(1) 管道和深綠淺綠交替的地板不斷往左移來實現(xiàn)小鳥向前飛行的效果;
(2) 按鍵檢測,當玩家點擊空格鍵或者↑鍵時,小鳥向上做加速度向下的均減速直線運動直至向上的速度衰減為0,否則小鳥做自由落體運動(實現(xiàn)時為了方便,可以認為在極短的時間段內(nèi)小鳥的運動方式為勻速直線運動);
(3) 碰撞檢測,當小鳥與管道/游戲邊界碰撞到時,游戲失敗并進入游戲結(jié)束界面。注意,為了碰撞檢測更精確,我們使用:
pygame.sprite.collide_mask
管道:



(4) 進入游戲后,隨機產(chǎn)生兩對管道,并不斷左移,當最左邊的管道快要因為到達游戲界面的左邊界而消失時,重新生成一對管道(注意不要重復生成);
(5) 當小鳥穿越一個上下管道之間的缺口時,游戲得分加一(注意不要重復記分)。
計分表


這里簡單貼下主程序的源代碼吧:
# 進入主游戲
score = 0
bird_pos, base_pos, bird_idx = list(game_start_info.values())
base_diff_bg = other_images['base'].get_width() - backgroud_image.get_width()
clock = pygame.time.Clock()
# --管道類
pipe_sprites = pygame.sprite.Group()
for i in range(2):
pipe_pos = Pipe.randomPipe(cfg, pipe_images.get('top'))
pipe_sprites.add(Pipe(image=pipe_images.get('top'), position=(cfg.SCREENWIDTH+200+i*cfg.SCREENWIDTH/2, pipe_pos.get('top')[-1])))
pipe_sprites.add(Pipe(image=pipe_images.get('bottom'), position=(cfg.SCREENWIDTH+200+i*cfg.SCREENWIDTH/2, pipe_pos.get('bottom')[-1])))
# --bird類
bird = Bird(images=bird_images, idx=bird_idx, position=bird_pos)
# --是否增加pipe
is_add_pipe = True
# --游戲是否進行中
is_game_running = True
while is_game_running:
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE or event.key == pygame.K_UP:
bird.setFlapped()
sounds['wing'].play()
# --碰撞檢測
for pipe in pipe_sprites:
if pygame.sprite.collide_mask(bird, pipe):
sounds['hit'].play()
is_game_running = False
# --更新小鳥
boundary_values = [0, base_pos[-1]]
is_dead = bird.update(boundary_values, float(clock.tick(cfg.FPS))/1000.)
if is_dead:
sounds['hit'].play()
is_game_running = False
# --移動base實現(xiàn)小鳥往前飛的效果
base_pos[0] = -((-base_pos[0] + 4) % base_diff_bg)
# --移動pipe實現(xiàn)小鳥往前飛的效果
flag = False
for pipe in pipe_sprites:
pipe.rect.left -= 4
if pipe.rect.centerx < bird.rect.centerx and not pipe.used_for_score:
pipe.used_for_score = True
score += 0.5
if '.5' in str(score):
sounds['point'].play()
if pipe.rect.left < 5 and pipe.rect.left > 0 and is_add_pipe:
pipe_pos = Pipe.randomPipe(cfg, pipe_images.get('top'))
pipe_sprites.add(Pipe(image=pipe_images.get('top'), position=pipe_pos.get('top')))
pipe_sprites.add(Pipe(image=pipe_images.get('bottom'), position=pipe_pos.get('bottom')))
is_add_pipe = False
elif pipe.rect.right < 0:
pipe_sprites.remove(pipe)
flag = True
if flag: is_add_pipe = True
# --綁定必要的元素在屏幕上
screen.blit(backgroud_image, (0, 0))
pipe_sprites.draw(screen)
screen.blit(other_images['base'], base_pos)
showScore(screen, score, number_images)
bird.draw(screen)
pygame.display.update()
clock.tick(cfg.FPS)游戲結(jié)束
假如我們的主角真的一個不小心如我們所料的撞死在了鋼管上(往上翻,就在游戲開始那里),那就表示gameOver();
'''游戲結(jié)束界面'''
def endGame(screen, sounds, showScore, score, number_images, bird, pipe_sprites, backgroud_image, other_images, base_pos, cfg):
sounds['die'].play()
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE or event.key == pygame.K_UP:
return
boundary_values = [0, base_pos[-1]]
bird.update(boundary_values, float(clock.tick(cfg.FPS))/1000.)
screen.blit(backgroud_image, (0, 0))
pipe_sprites.draw(screen)
screen.blit(other_images['base'], base_pos)
showScore(screen, score, number_images)
bird.draw(screen)
pygame.display.update()
clock.tick(cfg.FPS)結(jié)尾:
這期游戲分享就到這結(jié)束啦喜歡的友友們動手試試看哦!家人們的支持是小編更新最大的動力
到此這篇關(guān)于python flappy bird小游戲分布實現(xiàn)流程的文章就介紹到這了,更多相關(guān)python flappy bird內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Python將Mysql的查詢數(shù)據(jù)導出到文件的方法
今天小編就為大家分享一篇關(guān)于使用Python將Mysql的查詢數(shù)據(jù)導出到文件的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02
Python實現(xiàn)刪除排序數(shù)組中重復項的兩種方法示例
這篇文章主要介紹了Python實現(xiàn)刪除排序數(shù)組中重復項的兩種方法,涉及Python數(shù)組元素的遍歷、判斷、刪除等相關(guān)操作技巧,需要的朋友可以參考下2019-01-01
詳解使用Python寫一個向數(shù)據(jù)庫填充數(shù)據(jù)的小工具(推薦)
這篇文章主要介紹了用Python寫一個向數(shù)據(jù)庫填充數(shù)據(jù)的小工具,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Python數(shù)據(jù)結(jié)構(gòu)與算法中的隊列詳解(2)
這篇文章主要為大家詳細介紹了Python中的隊列,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
一個Python優(yōu)雅的數(shù)據(jù)分塊方法詳解
在做需求過程中有一個對大量數(shù)據(jù)分塊處理的場景,具體來說就是幾十萬量級的數(shù)據(jù),分批處理,每次處理100個。這時就需要一個分塊功能的代碼。本文為大家分享了一個Python中優(yōu)雅的數(shù)據(jù)分塊方法,需要的可以參考一下2022-05-05
Python try-except-else-finally的具體使用
本文主要介紹了Python try-except-else-finally的具體使用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08

