程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Use Python to develop a dinosaur jumping game and play it

編輯:Python

攜手創作,共同成長!這是我參與「掘金日新計劃 · 8 月更文挑戰」的第6天,點擊查看活動詳情

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

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

素材准備

首先我們准備下游戲所需的素材,比如恐龍圖片,仙人掌圖片,天空,地面等等,我們統一放到 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')
復制代碼

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

好了,今天的分享就到這裡,喜歡就點個贊吧

Friends who need complete code,下方點擊“在看”,Wechat private chat acquisition!

本文由mdnice多平台發布


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved