童年記憶第三彈!!!用python寫一個天天酷跑
阿新 • • 發佈:2021-01-11
感覺上次寫的植物大戰殭屍與俄羅斯方塊還不錯,然後就有了這次的文章
這次就寫一個天天酷跑吧
寫出來的效果圖就是這樣了
下面就更新一下全部的程式碼吧
還是老樣子先定義
import pygame,sys
import random
寫一下游戲配置
width = 1200 #視窗寬度 height = 508 #視窗高度 size = width, height score=None #分數 myFont=myFont1=None #字型 surObject=None #障礙物圖片 surGameOver=None #遊戲結束圖片 bg=None #背景物件 role=None #人物物件 object=None #障礙物物件 objectList=[] #障礙物物件陣列 clock=None #時鐘 gameState=None #遊戲狀態(0,1)表示(遊戲中,遊戲結束)
寫人物
class Role: #人物 def __init__(self,surface=None,y=None): self.surface=surface self.y=y self.w=(surface.get_width())/12 self.h=surface.get_height()/2 self.currentFrame=-1 self.state=0 #0代表跑步狀態,1代表跳躍狀態,2代表連續跳躍 self.g=1 #重力加速度 self.vy=0 #y軸速度 self.vy_start=-20 #起跳開始速度 def getRect(self): return (0,self.y+12,self.w,self.h)
寫障礙物
class Object: #障礙物 def __init__(self,surface,x=0,y=0): self.surface=surface self.x=x self.y=y self.w=surface.get_width() self.h=surface.get_height() self.currentFrame=random.randint(0,6) self.w = 100 self.h = 100 def getRect(self): return (self.x,self.y,self.w,self.h) def collision(self,rect1,rect2): #碰撞檢測 if (rect2[0]>=rect1[2]-20) or (rect1[0]+40>=rect2[2])or (rect1[1]+rect1[3]<rect2[1]+20) or (rect2[1]+rect2[3]<rect1[1]+20): return False return True
寫背景
class Bg: #背景
def __init__(self,surface):
self.surface=surface
self.dx=-10
self.w=surface.get_width()
self.rect=surface.get_rect()
def initGame():
global bg,role,clock,gameState,surObject,surGameOver,score,myFont,myFont1,objectList
#分數初始化
score=0
#初始化
objectList=[]
#載入字型
myFont=pygame.font.Font("./freesansbold.ttf",32)
myFont1=pygame.font.Font("./freesansbold.ttf",64)
# 建立時鐘物件 (可以控制遊戲迴圈頻率)
clock = pygame.time.Clock()
#初始化遊戲狀態
gameState=0
#遊戲背景
surBg=pygame.image.load("image/bg.bmp").convert_alpha()
bg=Bg(surBg)
#結束畫面
surGameOver=pygame.image.load("image/gameover.bmp").convert_alpha()
#人物圖片
surRole=pygame.image.load("image/role.png").convert_alpha()
role=Role(surRole,508-85)
#障礙物圖片
surObject=pygame.image.load("image/object.png").convert_alpha()
def addObject():
global surObject,object,objectList,object
rate=4
#是否生成障礙物
if not random.randint(0,300)<rate:
return
y=random.choice([height-100,height-200,height-300,height-400])
object=Object(surObject,width+40,y)
objectList.append(object)
def updateLogic():
global gameState,score
#鍵盤事件處理
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type==pygame.KEYDOWN:
#空格鍵跳躍
if gameState==0:
if event.key==pygame.K_SPACE:
if role.state==0:
role.state=1
role.vy=role.vy_start
elif role.state==1:
role.state=2
role.vy=role.vy_start
elif gameState==1:
if event.key==pygame.K_SPACE:
#重新開始遊戲
initGame()
if gameState==0:
#背景的移動
bg.dx+=10
if bg.dx==1200:
bg.dx=0
#人物的移動
if role.state==0:
role.currentFrame+=1
if role.currentFrame==12:
role.currentFrame=0
else:
role.y+=role.vy
role.vy+=role.g
if role.y>=508-85:
role.y=508-85
role.state=0
#障礙物的移動
addObject()
for object in objectList:
object.x-=10 #障礙物移動
# 障礙物超出螢幕,移除障礙物
if object.x+object.w<=0:
objectList.remove(object)
score+=10 #避開障礙物,加10分
print("移除了一個目標")
#碰撞檢測
if object.collision(role.getRect(),object.getRect()):
if(object.currentFrame==6):
objectList.remove(object)
score+=100 #吃金幣加100分
print(score)
print("吃了一個金幣")
else:
gameState=1 #遊戲失敗
print("發生了碰撞!")
推薦 :
- 精品小圈子每日都有新內容,乾貨濃度極高。
- 結實人脈、討論技術 你想要的這裡都有!
- 搶先入群,跑贏同齡人!(入群無需任何費用)
- 點選此處,與Python開發大牛一起交流學習
- 群號:974724894
申請即送:
Python軟體安裝包,Python實戰教程資料免費領取,包括 Python基礎學習、進階學習、爬蟲、自動化運維、自動化測試等,還有爬蟲練習題和程式碼和小遊戲的原始碼