【Python】使用Pygame做一個Flappy bird小遊戲(三)

原文鏈接:
【Python】使用Pygame做一個Flappy bird小遊戲(三)

需要遊戲素材和程序的朋友可以到我的公衆號【拇指筆記】後臺回覆"FPB"自取~

0. 最終效果

在這裏插入圖片描述

1. 添加隨機管道

本節文章介紹如何在遊戲中添加隨機生成的管道。下面我們來理一理思路。

玩過Flappy bird的朋友都知道,這個遊戲隨機生成長短不一的上下管道,上下管道之間存在着一定距離,並且每隔一定距離就會有新的管道生成。管道素材的長度當然是一樣長短的,所以我們隨機生成管道的座標y來實現隨機生成一定長度的管道。下面我們來算一算如何計算兩個管道之間的距離。

1.1 上下管座標關係

整個窗口的高度是512像素。我們設兩個管道之間的距離爲50像素。管道圖像的高度爲320像素。

因爲y值座標對應的是圖像的上端,所以上下管子之間的關係是:下管y座標=上管y座標+320+50

pipe_y的範圍爲-320到0,負的越多,上管長度越短。爲了保證適當長度,我把pipe_y的範圍設爲-270到-20

在這裏插入圖片描述

1.2 隨機生成管子

在這裏我們使用random模塊裏的randint(-270,-10)生成一定範圍內的隨機整數並將整個整數賦給pipe_y。這樣就實現了隨機產生不同高度但間距相同的隨機管道。

import random
pipe_y = random.randint(-270,-20)

爲了讓效果更明顯,我添加了空格控制隨機管子生成。

效果如下:
在這裏插入圖片描述

1.3 讓管道動起來

在遊戲中,管道的運動和綠磚的運動速度是相同的,所以我們使用相同的速度,每幀移動距離仍然取決於每幀時間。管道的左邊界對應着x值,管子本身的寬度爲52像素。所以x值的範圍應該是-52~288。

pipe_x = 288

#進入循環:
    time_passed = clock.tick()
    time_passed_seconds = time_passed / 1000 
    #計算每一幀的時間 
    distance_moved = time_passed_seconds * speed
    pipe_x -=distance_moved
    if(pipe_x<0):
        pipe_x+=288
        pipe_y = random.randint(-270,-20)

實現效果如下,爲了效果更明顯,我加快了管子的移動速度。
在這裏插入圖片描述

下面我們根據剛纔的思路,把這些代碼整合到之前的程序中。從而實現小鳥、管道和綠磚一起運動的場景。

首先是初始化,需要初始化的變量有pipe_y和pipe_x

pipe_y = random.randint(-270,-20)
pipe_x = 288

其次是由pipe_y(上管道座標)計算除pipe_dy(下管道座標)的值。

def calculation(pipe_y):
    pipe_dy = pipe_y+370
    return pipe_dy

然後修改遊戲界面的start_updatexy()函數,將管道更新的部分加入。

def start_updatexy(time_passed_seconds,base_x,base_y,dirx):
    distance_moved = time_passed_seconds * speed
    bird_distance = time_passed_seconds * bird_speed
    base_y = base_y + dirx*bird_distance
    base_x -= distance_moved
    pipe_x -=distance_moved
    
    if(pipe_x<0):
        pipe_x+=288
        pipe_y = random.randint(-270,-20)
        
    if base_x<-40:
        base_x += 40
    if 490>base_y >20:
        dirx = 2
    elif base_y >490:
        dirx = 0         
    elif base_y <0:
        base_y = 0
    return base_x,base_y,dirx

最後修改一下顯示界面,將管道繪製到窗口。

def start():
    screen.blit(background,(0,0))
    
    screen.blit(top,(pipe_x,pipe_y))
    pipe_dy = calculation(pipe_y)
    screen.blit(bottom,(pipe_x,pipe_dy))

    screen.blit(green_base,(base_x,400))

1.4 添加管道

添加管道最重要的是把握兩組管道之間的距離。首先我們假設管道移動的總長度爲600,窗口(288像素)位於中間,得到第一個管道起始位置座標爲444,結束位置座標爲-156;但是第二個管道不能在一開始就出現,因此講第二個管道第一週期起始位置設置爲744,但結束位置爲-156。

程序實現:

#首先初始化起始位置
pipe_x = 444
pipe_x1 = 744

#更改start()函數

def start():
    screen.blit(background,(0,0))
    
    screen.blit(top,(pipe_x,pipe_y))
    pipe_dy = calculation(pipe_y)
    screen.blit(bottom,(pipe_x,pipe_dy))
    
    screen.blit(top,(pipe_x1,pipe_y1))
    pipe_dy1 = calculation(pipe_y1)
    screen.blit(bottom,(pipe_x1,pipe_dy1))
    
    screen.blit(green_base,(base_x,400))

#更改start_updatexy()函數
    pipe_x -=distance_moved
    pipe_x1 -=distance_moved
    
    if(pipe_x1<-156):
        pipe_x1+=600
        pipe_y1 = random.randint(-270,-20)    
    if(pipe_x<-156):
        pipe_x+=600
        pipe_y = random.randint(-270,-20)

最終效果:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章