基于Python實(shí)現(xiàn)天天酷跑功能
感覺(jué)上次寫(xiě)的植物大戰(zhàn)僵尸與俄羅斯方塊的反應(yīng)還不錯(cuò),這次這個(gè)文章就更有動(dòng)力了
這次就寫(xiě)一個(gè)天天酷跑吧

寫(xiě)出來(lái)的效果圖就是這樣了
下面就更新一下全部的代碼吧
還是老樣子先定義
import pygame,sys import random
寫(xiě)一下游戲配置
width = 1200 #窗口寬度 height = 508 #窗口高度 size = width, height score=None #分?jǐn)?shù) myFont=myFont1=None #字體 surObject=None #障礙物圖片 surGameOver=None #游戲結(jié)束圖片 bg=None #背景對(duì)象 role=None #人物對(duì)象 object=None #障礙物對(duì)象 objectList=[] #障礙物對(duì)象數(shù)組 clock=None #時(shí)鐘 gameState=None #游戲狀態(tài)(0,1)表示(游戲中,游戲結(jié)束)
寫(xiě)人物
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 #起跳開(kāi)始速度 def getRect(self): return (0,self.y+12,self.w,self.h)
寫(xiě)障礙物
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): #碰撞檢測(cè) 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
寫(xiě)背景
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
#分?jǐn)?shù)初始化
score=0
#初始化
objectList=[]
#加載字體
myFont=pygame.font.Font("./freesansbold.ttf",32)
myFont1=pygame.font.Font("./freesansbold.ttf",64)
# 創(chuàng)建時(shí)鐘對(duì)象 (可以控制游戲循環(huán)頻率)
clock = pygame.time.Clock()
#初始化游戲狀態(tài)
gameState=0
#游戲背景
surBg=pygame.image.load("image/bg.bmp").convert_alpha()
bg=Bg(surBg)
#結(jié)束畫(huà)面
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
#鍵盤(pán)事件處理
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:
#重新開(kāi)始游戲
initGame()
if gameState==0:
#背景的移動(dòng)
bg.dx+=10
if bg.dx==1200:
bg.dx=0
#人物的移動(dòng)
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
#障礙物的移動(dòng)
addObject()
for object in objectList:
object.x-=10 #障礙物移動(dòng)
# 障礙物超出屏幕,移除障礙物
if object.x+object.w<=0:
objectList.remove(object)
score+=10 #避開(kāi)障礙物,加10分
print("移除了一個(gè)目標(biāo)")
#碰撞檢測(cè)
if object.collision(role.getRect(),object.getRect()):
if(object.currentFrame==6):
objectList.remove(object)
score+=100 #吃金幣加100分
print(score)
print("吃了一個(gè)金幣")
else:
gameState=1 #游戲失敗
print("發(fā)生了碰撞!")
ok啦,這就是這個(gè)天天酷跑的全部代碼啦,有問(wèn)題可以留言,我看到都會(huì)回的。
到此這篇關(guān)于基于Python實(shí)現(xiàn)天天酷跑功能的文章就介紹到這了,更多相關(guān)Python寫(xiě)天天酷跑內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Python的地理可視化庫(kù)進(jìn)行地圖動(dòng)畫(huà)的制作方法
隨著數(shù)據(jù)科學(xué)和可視化的迅速發(fā)展,地圖動(dòng)畫(huà)成為了展示地理數(shù)據(jù)變化的有力工具,Python作為一種強(qiáng)大的編程語(yǔ)言,有著豐富的地理可視化庫(kù),本文將介紹如何使用Python的地理可視化庫(kù)來(lái)制作地圖動(dòng)畫(huà),并通過(guò)代碼實(shí)例來(lái)演示,需要的朋友可以參考下2024-05-05
Python+PyQt手搓一個(gè)簡(jiǎn)單的記事本
這篇文章主要為大家詳細(xì)介紹了Python如何結(jié)合PyQt手搓一個(gè)簡(jiǎn)單的記事本,文中的示例代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-02-02
Python Selenium截圖功能實(shí)現(xiàn)代碼
這篇文章主要介紹了Python Selenium截圖功能實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Python標(biāo)準(zhǔn)庫(kù)之sqlite3使用實(shí)例
這篇文章主要介紹了Python標(biāo)準(zhǔn)庫(kù)之sqlite3使用實(shí)例,本文講解了創(chuàng)建數(shù)據(jù)庫(kù)、插入數(shù)據(jù)、查詢數(shù)據(jù)、更新與刪除數(shù)據(jù)操作實(shí)例,需要的朋友可以參考下2014-11-11
Python合并多個(gè)Excel數(shù)據(jù)的方法
這篇文章主要介紹了Python合并多個(gè)Excel數(shù)據(jù)的方法也就是說(shuō)將多個(gè)excel中的數(shù)據(jù)合并到另一個(gè)表中,本文通過(guò)實(shí)例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-07-07
python爬蟲(chóng)scrapy框架的梨視頻案例解析
這篇文章主要介紹了python爬蟲(chóng)scrapy框架的梨視頻案例解析,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02

