Python 恐龍跑跑小遊戲實現流程

導語:

谷歌瀏覽器中有個很有名的彩蛋:當你網絡出現問題時,就會出現一個“小恐龍遊戲”。🦖

圖片

相信很多人都玩過 chrome 上提供的恐龍跑跑遊戲,在我們斷網或者直接在瀏覽器輸入地址“chrome://dino/”都可以進入遊戲。

今天我們就來給大傢演示下,用Python來自己做一個仿制的“小恐龍遊戲”!🦖

廢話不多說,讓我們愉快地開始吧~先給你們看一下運你效果🦖🦖🦖

正文:

開發工具:

Python版本:3.6.4

相關模塊:

pygame模塊;以及一些python自帶的模塊。

環境搭建

安裝Python並添加到環境變量,pip安裝需要的相關模塊即可。

在終端運行如下命令即可:

python Game7.py

素材準備

首先我們準備下遊戲所需的素材,比如恐龍圖片,仙人掌圖片,天空,地面等等,我們統一放到 dino 文件夾下

遊戲邏輯

我們使用 Pygame 來制作遊戲,先進行遊戲頁面的初始化

import pygame
 
# 初始化
pygame.init()
pygame.mixer.init()
# 設置窗口大小
screen = pygame.display.set_mode((900, 200))
# 設置標題
pygame.display.set_caption("恐龍跳跳")
# 使用系統自帶的字體
my_font = pygame.font.SysFont("arial", 20)
score = 0
# 背景色
bg_color = (218,220,225)

接下來,我們來考慮一下,遊戲中有哪些遊戲元素:

小恐龍:由玩傢控制以躲避路上的障礙物;

路面:遊戲的背景;

雲:遊戲的背景;

仙人掌:路上的障礙物之一,小恐龍碰上就會死掉;

記分板:記錄當前的分數和歷史最高分。

接下來我們將各種素材加載進內存

# 加載正常恐龍
dino_list = []
temp = ""
for i in range(1, 7):
    temp = pygame.image.load(f"dino/dino_run{i}.png")
    dino_list.append(temp)
dino_rect = temp.get_rect()
index = 0
 
# x 初始值
dino_rect.x = 100
# y 初始值
dino_rect.y = 150
# print(dino_rect)
 
# 設置y軸上的初速度為0
y_speed = 0
# 起跳初速度
jumpSpeed = -20
# 模擬重力
gravity = 2
 
 加載地面
ground = pygame.image.load("dino/ground.png")
 
# 加載仙人掌
cactus = pygame.image.load("dino/cactus1.png")
cactus_rect = cactus.get_rect()
cactus_rect.x,cactus_rect.y = 900,140
 
# 加載重新再來
restart = pygame.image.load("dino/restart.png")
restart_rect = restart.get_rect()
restart_rect.x,restart_rect.y = (900-restart.get_rect().width)/2,(200-restart.get_rect().height)/2+50
# 加載 gameover
gameover = pygame.image.load("dino/gameover.png")
gameover_rect = gameover.get_rect()
gameover_rect.x, gameover_rect.y = (
    900-gameover.get_rect().width)/2, (200-gameover.get_rect().height)/2
# 地面移動速度與距離
ground_speed = 10
ground_move_distance = 0
 
# 時鐘
clock = pygame.time.Clock()
 
# 重新再來一次
is_restart = False
text_color = (0,0,0)

再接下來,我們通過一個 while 死循環來保持遊戲進程

while True:
    # 每秒30次
    clock.tick(30)
    ...

在上面的循環當中,我們需要兩個檢測機制,事件檢測和碰撞檢測

事件檢測

# 事件偵測
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            if result_flag:
                with open("result.ini", "w+") as f:
                    f.write(str(best))
            sys.exit()
        # 空格鍵偵測
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and dino_rect.y==150:
                y_speed = jumpSpeed

主要檢測退出事件和空格鍵事件

碰撞檢測

# 碰撞檢測
    if dino_rect.colliderect(cactus_rect):
        while not is_restart:
            # 事件偵測
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    if result_flag:
                        with open("result.ini", "w+") as f:
                            f.write(str(best))
                    sys.exit()
                # 空格鍵偵測
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        is_restart = True
                        bg_color = (218,220,225)
                        ground_speed = 10
            # 設置重新再來圖片
            screen.blit(restart, restart_rect)
            screen.blit(gameover, gameover_rect)
            pygame.display.update()

對於碰撞,隻要恐龍碰撞到瞭仙人掌,那麼遊戲結束,展示重新再來圖片

由於我們希望遊戲可以記錄我們的最好成績,所以這裡使用瞭本地文件存儲遊戲記錄的方式,當遊戲結束的時候,根據當前遊戲成績來判斷是否將新的成績寫入文件當中

下面是計算跑動距離和最好成績的代碼

# 統計距離
    score += ground_speed
    score_surface = my_font.render("Distance: "+str(score), True, text_color)
 
    # 計算最好成績
    result_flag = False
    if score >= best:
        best = score
        result_flag = True
    best_result = my_font.render("Best Result: " + str(best), True, text_color)

我們還需要給不同距離增加不同的遊戲難度,畢竟跑起來,肯定距離越遠,難度越大嘛

# 更換背景色,成績大於4000
    if score > 4000:
        bg_color = (55,55,55)
        ground_speed = 15
        text_color = (255,255, 255)
# 更換背景色,成績大於8000
    if score > 8000:
        bg_color = (220,20,60)
        ground_speed = 20
        text_color = (255, 255, 255)
 
    # 更換背景色,成績大於12000
    if score > 12000:
        bg_color = (25,25,112)
        ground_speed = 25
        text_color = (255, 255, 255)
 
    # 設置背景色
    screen.fill(bg_color)

最後我們將所有加載到內存當中的元素都呈現在 screen 上

# 設置地面圖片1
    screen.blit(ground, (0-ground_move_distance, 180))
    # 設置地面圖片2,在右邊邊界外
    screen.blit(ground, (900-ground_move_distance, 180))
    # 設置恐龍圖片
    screen.blit(dino_list[index % 6], dino_rect)
    # 設置仙人掌圖片
    screen.blit(cactus, cactus_rect)
    # 設置分數
    screen.blit(score_surface,(780,20))
    # 設置最好成績
    screen.blit(best_result, (20, 20))
 
    pygame.display.update()

為瞭增加遊戲性,我們再增加背景音樂和跳躍音效

pygame.mixer.music.load("background.mp3")
pygame.mixer.music.play(-1, 0)
sound = pygame.mixer.Sound('preview.mp3')

結尾:

這樣,一個簡單易用的恐龍跑跑遊戲就完成瞭,今天的分享就到這裡,喜歡就點個贊吧!

到此這篇關於Python 恐龍跑跑小遊戲實現流程的文章就介紹到這瞭,更多相關Python 恐龍跑跑內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: