天天酷跑游戏
游戏效果:
游戏主要是躲避障碍物,这里也添加了金币,增加一点积分的娱乐性,人物设置是三条命,障碍物有6种,包括金币,障碍物随机生成,碰到障碍物掉一滴血,没血了结束游戏。
初始化任务,障碍物和背景:
class Person(): # 人物
def __init__(self, surf=None, y=None):
self.surface = surf
self.y = y # y坐标
self.w = (surf.get_width()) / 12 # 宽度
self.h = surf.get_height() / 2 # 高度
self.cur_frame = -1 # 当前的运动状态帧
self.state = 0 # 0代表跑步状态,1代表跳跃状态,2代表连续跳跃
self.gravity = 1 # 重力加速度
self.velocity_y = 0 # y方向的速度
self.vy_start = -20 # 起跳开始速度
def getPos(self): # 获取当前的位置信息,用于碰撞检测
return (0, self.y + 12, self.w, self.h)
class Obstacle(object): # 障碍物
def __init__(self, surf, x=0, y=0):
self.surface = surf
self.x = x
self.y = y
self.w = surf.get_width()
self.h = surf.get_height()
self.cur_frame = random.randint(0, 6) # 随机获取一种障碍物的类型
self.w = 100
self.h = 100
def getPos(self): # 当前的坐标信息
return (self.x, self.y, self.w, self.h)
def check_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 BackGround(object): # 背景
def __init__(self, surf):
self.surface = surf # 初始化一个Surface 对象
self.dx = -10
self.w = surf.get_width() # 返回 Surface 对象的宽度,以像素为单位。
self.rect = surf.get_rect() # 获取 Surface 对象的矩形区域
设计障碍物:
def add_obstacle(self): # 添加障碍物
rate = 4
# 是否生成障碍物
if not random.randint(0, 300) < rate:
return
y = random.choice([self.height - 100, self.height - 200, self.height - 300, self.height - 400])
obstacle = Obstacle(self.obstacle_pic, self.width + 40, y)
self.obstacle_list.append(obstacle)
处理键盘跳跃,吃金币,碰撞障碍物的操作:
# 处理事件监听
def manage_game(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
# 空格键跳跃
if self.game_state == 0:
if event.key == pygame.K_SPACE:
pygame.mixer.Sound(r"resource/jump.wav").play()
if self.person.state == 0:
self.person.state = 1
self.person.velocity_y = self.person.vy_start
elif self.person.state == 1:
self.person.state = 2
self.person.velocity_y = self.person.vy_start
elif self.game_state == 1:
if event.key == pygame.K_RETURN: # 重新开始游戏
self.bg_music.stop()
self.__init__()
if self.game_state == 0:
self.bg.dx += 10
if self.bg.dx == 1200:
self.bg.dx = 0
#人物运动
if self.person.state == 0:
self.person.cur_frame += 1
if self.person.cur_frame == 12:
self.person.cur_frame = 0
else:
self.person.y += self.person.velocity_y
self.person.velocity_y += self.person.gravity
if self.person.y >= 500 - 85:
self.person.y = 500 - 85
self.person.state = 0
# 生成障碍物
self.add_obstacle()
for obstacle in self.obstacle_list:
obstacle.x -= 10 # obstacle向左移动十个像素
if obstacle.x + obstacle.w <= 0: # 当obstacle离开界面时
self.obstacle_list.remove(obstacle)
self.score += 10 # 避开obstacle,加10分
if obstacle.check_collision(self.person.getPos(), obstacle.getPos()): # 碰撞检测
if obstacle.cur_frame == 6:
self.obstacle_list.remove(obstacle)
self.score += 100 # 吃金币加100分
coin_sound = pygame.mixer.Sound(
r"resource/coin.mp3")
coin_sound.play()
else:
self.life -= 1
self.obstacle_list.remove(obstacle)
if self.life <= 0:
self.game_state = 1 # 游戏失败,播放结束音乐
die_sound = pygame.mixer.Sound(r"resource/dead.wav")
die_sound.play()
else:
die_sound = pygame.mixer.Sound(r"resource/collision.mp3") # 播放碰撞之后产生的音效
刷新页面,处理状态:
# 更新显示界面
def refresh_screen(self, screen):
screen.blit(self.bg.surface, [-self.bg.dx, 0]) # 背景的贴图
screen.blit(self.bg.surface, [1200 - self.bg.dx, 0])
text = self.font1.render("score:%d" % self.score, True, (128, 128, 128)) # 分数的贴图
screen.blit(text, (500, 20))
del text
rest_life = self.font1.render("life:%d" % self.life, True, (128, 128, 128)) # 剩余生命
screen.blit(rest_life, (400, 20))
del rest_life
screen.blit(self.person.surface, [0, self.person.y],
[int(self.person.cur_frame) * self.person.w, 0, self.person.w, self.person.h]) # 人物的贴图
for obstacle in self.obstacle_list: # 障碍物的贴图
screen.blit(obstacle.surface, [obstacle.x, obstacle.y],
[int(obstacle.cur_frame) * obstacle.w, 0, obstacle.w, obstacle.h])
# 判断游戏的状态
def check_status(self, screen):
if self.game_state == 0:
self.refresh_screen(screen)
return
elif self.game_state == 1:
screen.blit(self.game_over, [0, 0])
text = self.font1.render("GameOver Score:%d Press Enter to restart" % self.score, True, (255, 0, 0))
screen.blit(text, (self.width / 2 - 350, self.height / 2 + 150))
游戏主入口:
# 游戏主入口函数
def main(self):
self.startGame(self.screen)
while True:
self.clock.tick(40)
self.check_status(self.screen)
self.manage_game()
pygame.display.flip()
if __name__ == '__main__':
parkour = Parkour()
parkour.main()
运行后效果:
需要游戏素材,和完整代码可在文末Python学习资料那获取
今天的分享就到这里