前言
嗨嘍,大家好呀~這里是愛看美女的茜茜吶
天天酷跑是一款輕松好玩、簡單時尚的跑酷類手機游戲。
那我們能不能用python模擬出這個一個游戲呢?
答案當然是可以的,今天我就給大家?guī)砗喴装娴奶焯炜崤苄∮螒?/p>
開發(fā)環(huán)境:
-
版 本: python 3.8
-
編輯器:pycharm 2021.2
準備事宜
音樂準備:
字體準備:
圖片準備:
代碼展示
import pygame,sys
import random
游戲配置
width = 1200 #窗口寬度
height = 508 #窗口高度
size = width, height
score=None #分數(shù)
myFont=myFont1=None #字體
surObject=None #障礙物圖片
surGameOver=None #游戲結(jié)束圖片
bg=None #背景對象
role=None #人物對象
object=None #障礙物對象
objectList=[] #障礙物對象數(shù)組
clock=None #時鐘
gameState=None #游戲狀態(tài)(0,1)表示(游戲中,游戲結(jié)束)
人物
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代表跑步狀態(tài),1代表跳躍狀態(tài),2代表連續(xù)跳躍
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
#分數(shù)初始化
score=0
#初始化
objectList=[]
#加載字體
myFont=pygame.font.Font("./freesansbold.ttf",32)
myFont1=pygame.font.Font("./freesansbold.ttf",64)
# 創(chuàng)建時鐘對象 (可以控制游戲循環(huán)頻率)
clock = pygame.time.Clock()
#初始化游戲狀態(tài)
gameState=0
#游戲背景
surBg=pygame.image.load("image/bg.bmp").convert_alpha()
bg=Bg(surBg)
#結(jié)束畫面
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("發(fā)生了碰撞!")
def updateView(screen):
#背景的貼圖
screen.blit(bg.surface,[-bg.dx,0])
screen.blit(bg.surface,[1200-bg.dx,0])
#分數(shù)的貼圖
textSur=myFont.render("score:%d"%score, True, (128, 128, 128))
screen.blit(textSur, (500,20))
del textSur
#人物的貼圖
screen.blit(role.surface, [0, role.y], [int(role.currentFrame) * role.w, 0, role.w, role.h])
#障礙物的貼圖
for object in objectList:
screen.blit(object.surface, [object.x, object.y], [int(object.currentFrame) * object.w, 0, object.w, object.h])
def judgeState(screen):
global gameState
if gameState==0:
updateView(screen)
return
elif gameState==1:
screen.blit(surGameOver,[0,0])
textSur = myFont1.render("GameOver Score:%d"%score, True, (255, 0, 0))
screen.blit(textSur, (width/2-350, height/2+150))
def main():
# 音樂的路徑
file = r'music.mp3'
# 初始化
pygame.mixer.init()
# 加載音樂文件
track = pygame.mixer.music.load(file)
# 開始播放音樂流
pygame.mixer.music.play()
pygame.init()
screen = pygame.display.set_mode(size)
pygame.display.set_caption('天天酷跑')
initGame()
screen.blit(bg.surface,[0,0])
while True:
#設(shè)置時鐘頻率
clock.tick(60)
judgeState(screen)
updateLogic()
pygame.display.flip()
main()
尾語
感謝你觀看我的文章吶~本次航班到這里就結(jié)束啦 ??
希望本篇文章有對你帶來幫助 ??,有學(xué)習到一點知識~
躲起來的星星??也在努力發(fā)光,你也要努力加油(讓我們一起努力叭)。
文章來源:http://www.zghlxwxcb.cn/news/detail-762726.html
最后,宣傳一下呀~??????更多源碼、資料、素材、解答、交流皆點擊下方名片獲取呀????文章來源地址http://www.zghlxwxcb.cn/news/detail-762726.html
到了這里,關(guān)于python制作簡單版天天酷跑,是不是你平日里摸魚小游戲呀的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!