212 lines
6.4 KiB
Python
212 lines
6.4 KiB
Python
import pygame
|
|
import random
|
|
import sys
|
|
|
|
# 初始化pygame
|
|
pygame.init()
|
|
|
|
# 游戏常量
|
|
SCREEN_WIDTH = 800
|
|
SCREEN_HEIGHT = 600
|
|
FPS = 60
|
|
PLAYER_SPEED = 5
|
|
OBSTACLE_SPEED = 3
|
|
GRAVITY = 0.5
|
|
|
|
# 颜色定义
|
|
WHITE = (255, 255, 255)
|
|
BLUE = (135, 206, 235)
|
|
BROWN = (139, 69, 19)
|
|
GREEN = (34, 139, 34)
|
|
RED = (255, 0, 0)
|
|
BLACK = (0, 0, 0)
|
|
|
|
class Player:
|
|
def __init__(self, x, y):
|
|
self.x = x
|
|
self.y = y
|
|
self.width = 30
|
|
self.height = 40
|
|
self.velocity_y = 0
|
|
self.on_ground = False
|
|
|
|
def update(self):
|
|
# 应用重力
|
|
if not self.on_ground:
|
|
self.velocity_y += GRAVITY
|
|
|
|
self.y += self.velocity_y
|
|
|
|
# 限制在屏幕内
|
|
if self.y > SCREEN_HEIGHT - self.height - 50:
|
|
self.y = SCREEN_HEIGHT - self.height - 50
|
|
self.velocity_y = 0
|
|
self.on_ground = True
|
|
elif self.y < 0:
|
|
self.y = 0
|
|
self.velocity_y = 0
|
|
|
|
def jump(self):
|
|
if self.on_ground:
|
|
self.velocity_y = -12
|
|
self.on_ground = False
|
|
|
|
def move_left(self):
|
|
self.x -= PLAYER_SPEED
|
|
if self.x < 0:
|
|
self.x = 0
|
|
|
|
def move_right(self):
|
|
self.x += PLAYER_SPEED
|
|
if self.x > SCREEN_WIDTH - self.width:
|
|
self.x = SCREEN_WIDTH - self.width
|
|
|
|
def draw(self, screen):
|
|
# 绘制滑雪者(简单矩形表示)
|
|
pygame.draw.rect(screen, RED, (self.x, self.y, self.width, self.height))
|
|
# 绘制滑雪板
|
|
pygame.draw.rect(screen, BLACK, (self.x - 5, self.y + self.height, self.width + 10, 5))
|
|
|
|
class Obstacle:
|
|
def __init__(self, x, y, width, height, type):
|
|
self.x = x
|
|
self.y = y
|
|
self.width = width
|
|
self.height = height
|
|
self.type = type # 'tree' or 'rock'
|
|
|
|
def update(self):
|
|
self.y -= OBSTACLE_SPEED # 障碍物向上移动(相对运动)
|
|
|
|
def is_off_screen(self):
|
|
return self.y + self.height < 0
|
|
|
|
def draw(self, screen):
|
|
if self.type == 'tree':
|
|
# 绘制树
|
|
pygame.draw.rect(screen, GREEN, (self.x, self.y, self.width, self.height))
|
|
elif self.type == 'rock':
|
|
# 绘制石头
|
|
pygame.draw.rect(screen, BROWN, (self.x, self.y, self.width, self.height))
|
|
|
|
class Game:
|
|
def __init__(self):
|
|
self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
|
pygame.display.set_caption("滑雪大冒险")
|
|
self.clock = pygame.time.Clock()
|
|
self.font = pygame.font.Font(None, 36)
|
|
|
|
self.player = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT - 90)
|
|
self.obstacles = []
|
|
self.score = 0
|
|
self.game_over = False
|
|
self.obstacle_timer = 0
|
|
self.obstacle_interval = 60 # 每60帧生成一个障碍物
|
|
|
|
def spawn_obstacle(self):
|
|
# 随机生成障碍物
|
|
obstacle_type = random.choice(['tree', 'rock'])
|
|
width = random.randint(20, 40)
|
|
height = random.randint(30, 60)
|
|
x = random.randint(0, SCREEN_WIDTH - width)
|
|
y = SCREEN_HEIGHT
|
|
|
|
obstacle = Obstacle(x, y, width, height, obstacle_type)
|
|
self.obstacles.append(obstacle)
|
|
|
|
def check_collisions(self):
|
|
player_rect = pygame.Rect(self.player.x, self.player.y,
|
|
self.player.width, self.player.height)
|
|
|
|
for obstacle in self.obstacles:
|
|
obstacle_rect = pygame.Rect(obstacle.x, obstacle.y,
|
|
obstacle.width, obstacle.height)
|
|
if player_rect.colliderect(obstacle_rect):
|
|
self.game_over = True
|
|
|
|
def update(self):
|
|
if not self.game_over:
|
|
# 更新玩家
|
|
self.player.update()
|
|
|
|
# 生成障碍物
|
|
self.obstacle_timer += 1
|
|
if self.obstacle_timer >= self.obstacle_interval:
|
|
self.spawn_obstacle()
|
|
self.obstacle_timer = 0
|
|
|
|
# 更新障碍物
|
|
for obstacle in self.obstacles[:]:
|
|
obstacle.update()
|
|
if obstacle.is_off_screen():
|
|
self.obstacles.remove(obstacle)
|
|
self.score += 10 # 成功避开障碍物得分
|
|
|
|
# 检查碰撞
|
|
self.check_collisions()
|
|
|
|
# 增加游戏难度
|
|
if self.score > 0 and self.score % 100 == 0:
|
|
global OBSTACLE_SPEED
|
|
OBSTACLE_SPEED += 0.5
|
|
|
|
def draw(self):
|
|
# 绘制天空背景
|
|
self.screen.fill(BLUE)
|
|
|
|
# 绘制雪地
|
|
pygame.draw.rect(self.screen, WHITE, (0, SCREEN_HEIGHT - 50, SCREEN_WIDTH, 50))
|
|
|
|
# 绘制玩家
|
|
self.player.draw(self.screen)
|
|
|
|
# 绘制障碍物
|
|
for obstacle in self.obstacles:
|
|
obstacle.draw(self.screen)
|
|
|
|
# 绘制分数
|
|
score_text = self.font.render(f"分数: {self.score}", True, BLACK)
|
|
self.screen.blit(score_text, (10, 10))
|
|
|
|
# 绘制游戏结束信息
|
|
if self.game_over:
|
|
game_over_text = self.font.render("游戏结束! 按R重新开始", True, RED)
|
|
text_rect = game_over_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))
|
|
self.screen.blit(game_over_text, text_rect)
|
|
|
|
def handle_events(self):
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
return False
|
|
elif event.type == pygame.KEYDOWN:
|
|
if event.key == pygame.K_r and self.game_over:
|
|
# 重新开始游戏
|
|
self.__init__()
|
|
|
|
# 处理按键
|
|
keys = pygame.key.get_pressed()
|
|
if keys[pygame.K_LEFT]:
|
|
self.player.move_left()
|
|
if keys[pygame.K_RIGHT]:
|
|
self.player.move_right()
|
|
if keys[pygame.K_SPACE]:
|
|
self.player.jump()
|
|
|
|
return True
|
|
|
|
def run(self):
|
|
running = True
|
|
while running:
|
|
running = self.handle_events()
|
|
self.update()
|
|
self.draw()
|
|
pygame.display.flip()
|
|
self.clock.tick(FPS)
|
|
|
|
pygame.quit()
|
|
sys.exit()
|
|
|
|
if __name__ == "__main__":
|
|
game = Game()
|
|
game.run()
|