激情六月丁香婷婷|亚洲色图AV二区|丝袜AV日韩AV|久草视频在线分类|伊人九九精品视频|国产精品一级电影|久草视频在线99|在线看的av网址|伊人99精品无码|午夜无码视频在线

高校合作1:010-59833514 ?咨詢電話:400-810-1418 服務(wù)與監(jiān)督電話:400-810-1418轉(zhuǎn)接2

python游戲開發(fā)視頻(使用Python開發(fā)一個恐龍跑跑小游戲,玩起來)

發(fā)布時間:2023-11-29 16:04:59 瀏覽量:109次

?使用Python開發(fā)一個恐龍跑跑小游戲,玩起來

python游戲開發(fā)視頻(使用Python開發(fā)一個恐龍跑跑小游戲,玩起來)

相信很多人都玩過 chrome 瀏覽器上提供的恐龍跑跑游戲,在我們斷網(wǎng)或者直接在瀏覽器輸入地址“chrome://dino/”都可以進(jìn)入游戲

今天我們就是用 Python 來制作一個類似的小游戲

首先我們準(zhǔn)備下游戲所需的素材,比如恐龍圖片,仙人掌圖片,天空,地面等等,我們統(tǒng)一放到 dino 文件夾下

我們使用 Pygame 來制作游戲,先進(jìn)行游戲頁面的初始化

import pygame

# 初始化
pygame.init()
pygame.mixer.init()
# 設(shè)置窗口大小
screen = pygame.display.set_mode((900, 200))
# 設(shè)置標(biāo)題
pygame.display.set_caption("恐龍?zhí)?)
# 使用系統(tǒng)自帶的字體
my_font = pygame.font.SysFont("arial", 20)
score = 0
# 背景色
bg_color = (218,220,225)

接下來我們將各種素材加載進(jìn)內(nèi)存

# 加載正??铸?/span>
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)

# 設(shè)置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 死循環(huán)來保持游戲進(jìn)程

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

在上面的循環(huán)當(dāng)中,我們需要兩個檢測機制,事件檢測和碰撞檢測

事件檢測

# 事件偵測
    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
            # 設(shè)置重新再來圖片
            screen.blit(restart, restart_rect)
            screen.blit(gameover, gameover_rect)
            pygame.display.update()

對于碰撞,只要恐龍碰撞到了仙人掌,那么游戲結(jié)束,展示重新再來圖片

由于我們希望游戲可以記錄我們的最好成績,所以這里使用了本地文件存儲游戲記錄的方式,當(dāng)游戲結(jié)束的時候,根據(jù)當(dāng)前游戲成績來判斷是否將新的成績寫入文件當(dāng)中

python游戲開發(fā)視頻(使用Python開發(fā)一個恐龍跑跑小游戲,玩起來)

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

# 統(tǒng)計距離
    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)

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

# 更換背景色,成績大于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)

    # 設(shè)置背景色
    screen.fill(bg_color)

最后我們將所有加載到內(nèi)存當(dāng)中的元素都呈現(xiàn)在 screen 上

# 設(shè)置地面圖片1
    screen.blit(ground, (0-ground_move_distance, 180))
    # 設(shè)置地面圖片2,在右邊邊界外
    screen.blit(ground, (900-ground_move_distance, 180))
    # 設(shè)置恐龍圖片
    screen.blit(dino_list[index % 6], dino_rect)
    # 設(shè)置仙人掌圖片
    screen.blit(cactus, cactus_rect)
    # 設(shè)置分?jǐn)?shù)
    screen.blit(score_surface,(780,20))
    # 設(shè)置最好成績
    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')

這樣,一個簡單易用的恐龍跑跑游戲就完成了,我們來看下效果吧

好了,今天的分享就到這里,喜歡就點個

END

最近有一些小伙伴,讓我?guī)兔φ乙恍?學(xué)習(xí)資料,于是我翻遍了收藏的 5T 資料后,匯總整理出來,可以說是Python學(xué)習(xí)必備!

所有資料都整理到網(wǎng)盤了,需要的私信777獲取!

python游戲開發(fā)視頻(使用Python開發(fā)一個恐龍跑跑小游戲,玩起來)

熱門課程推薦

熱門資訊

請綁定手機號

x

同學(xué)您好!

您已成功報名0元試學(xué)活動,老師會在第一時間與您取得聯(lián)系,請保持電話暢通!
確定