基于Python编写简易版的天天跑酷游戏的⽰例代码写出来的效果图就是这样了:
下⾯就更新⼀下全部的代码吧
还是⽼样⼦先定义
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              #时钟
python新手代码示例gameState=None          #游戏状态(0,1)表⽰(游戏中,游戏结束)
写⼈物
class Role: #⼈物
def __init__(self,surface=None,y=None):
self.surface=surface
self.y=y
self.w=(_width())/12
self._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._width()
self._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._width()
<=_rect()
def initGame():
global bg,role,clock,gameState,surObject,surGameOver,score,myFont,myFont1,objectList    #分数初始化
score=0
#初始化
objectList=[]
#加载字体
myFont=pygame.font.Font("./f",32)
myFont1=pygame.font.Font("./f",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 ():
pe == pygame.QUIT:
pe==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:
score+=10    #避开障碍物,加10分
print("移除了⼀个⽬标")
#碰撞检测
Rect(),Rect()):
if(object.currentFrame==6):
score+=100  #吃⾦币加100分
print(score)
print("吃了⼀个⾦币")
else:
gameState=1  #游戏失败
print("发⽣了碰撞!")
ok啦,这就是这个天天酷跑的全部代码啦。
到此这篇关于基于Python编写简易版的天天跑酷游戏的⽰例代码的⽂章就介绍到这了,更多相关Python跑酷游戏内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!