import random # 导入游戏库
import pgzrun
txtFile = open('maxscore.txt', 'r') # 打开最佳时间记录存档文件,该目录下必须存在该文件
line = txtFile.readline() # 读取一行字符串
maxScore = int(line) # 将记录的秒数转换为整型存储
txtFile.close() # 关闭文件
TITLE = "仿谷歌小恐龙" # 窗口标题
WIDTH = 640 # 窗口宽度
HEIGHT = 250 # 窗口高度
background1 = Actor('ggxkl05') # 导入背景图片
background1.x = WIDTH
background2 = Actor('ggxkl05')
background2.x = 0
# 导入角色图片
players = [Actor('ggxkl_player01'),Actor('ggxkl_player02'),Actor('ggxkl_player03'),Actor('ggxkl_player04'),Actor('ggxkl_player05'),Actor('ggxkl_player06')]
playersLength = len(players)
playersIndex = 0
player_x = 100
player_y = HEIGHT / 3 * 2 + 20
for i in range(playersLength):
players[i].x = player_x
players[i].y = player_y
playersSpeed = 0
# 导入障碍物
bars = [Actor('ggxkl01'),Actor('ggxkl03'),Actor('ggxkl06'),Actor('ggxkl07')]
bars[0].y = HEIGHT / 3 * 2 + 10
bars[1].y = HEIGHT / 3 * 2 + 20
bars[2].y = HEIGHT / 3 * 2 + 25
bars[3].y = HEIGHT / 3 * 2
barsIndex = random.randint(0,3)
barsLength = len(bars)
bars[barsIndex].x = 600
speed = 5 # 速度
jump = 0 # 跳跃
score = 0 # 分数
diff = 24 # 跳跃的高度
isLose = False # 游戏是否失败
def draw():
background1.draw() # 画背景
background2.draw()
screen.draw.text('得分:' + str(score), (480, 10), fontsize=25, fontname="simhei", color='white')
screen.draw.text('最高分:' + str(maxScore), (480, 40), fontsize=25, fontname="simhei", color='white')
bars[barsIndex].draw() # 绘制障碍物
players[playersIndex].draw() # 绘制角色
if isLose: # 失败时显示的文本
screen.draw.text('游戏结束', (WIDTH / 2 - 120, HEIGHT / 2 - 60), fontsize=60, fontname='fzshuangqtjw_cu', color=(237, 169, 82))
screen.draw.text('(按空格键重新开始游戏)', (WIDTH / 2 - 85, HEIGHT / 2 + 20), fontsize=15, fontname="fzshuangqtjw_cu", color='orange')
def update():
global playersIndex,playersSpeed,playersLength,barsIndex,score,isLose,maxScore,speed # 全局变量
if players[playersIndex].colliderect(bars[barsIndex]): # 判断障碍物是否与角色发生碰撞
if score > maxScore: # 如果失败时的分数高于最高分,则将它写入文档
txtFile = open('maxscore.txt','w')
txtFile.write(str(score))
txtFile.close()
isLose = True
if not isLose:
# 障碍物移动
bars[barsIndex].x -= speed
if bars[barsIndex].x < -40:
barsIndex = random.randint(0,3)
bars[barsIndex].x = WIDTH + 30
score += 1
if score % 5 == 0:
speed += 0.2
# 背景移动
background1.x -= speed
background2.x -= speed
if background1.x < - WIDTH / 2:
background1.x = WIDTH + 320
if background2.x < - WIDTH / 2:
background2.x = WIDTH + 320
# 减缓角色跑步动画
if not jump:
playersSpeed += 1
if playersSpeed % 3 == 0:
playersIndex += 1
if playersIndex >= playersLength:
playersIndex = 0
def jumping(): # 跳跃函数,跳跃时相当于一个等差数列,26->-26
global jump,diff,player_y,playersIndex
if jump:
player_y -= diff
diff -= 2
if player_y == HEIGHT / 3 * 2 + 20:
diff = 26
jump = 0
for i in range(playersLength):
players[i].y = player_y
playersIndex = 5
clock.schedule_unique(jumping,0.035) # 延迟执行
def on_key_down(): # 按键按下函数
global jump,isLose,speed,score,maxScore
if not isLose: # 当游戏没有失败时,按下空格键角色跳起
if keyboard.space:
jump = 1
else: # 如果游戏失败时按下空格键,则重置游戏
if keyboard.space:
speed = 5
score = 0
bars[barsIndex].x = WIDTH + 30
txtFile = open('maxscore.txt', 'r') # 打开最佳时间记录存档文件,该目录下必须存在该文件
line = txtFile.readline() # 读取一行字符串
maxScore = int(line) # 将记录的秒数转换为整型存储
txtFile.close() # 关闭文件
isLose = False
jumping() # 调用跳跃函数
pgzrun.go() # 游戏开始执行
问题还是挺多的,首先是跳跃的问题,不是很流畅,而且还可以做二段跳的;第二是角色和障碍物的碰撞问题,没有找到合适的障碍物素材,这个影响游戏体验,有时候角色还没碰到障碍物就判定失败了。。。