话不多说,直接上代码:
import pygame
import random
# 游戏参数
WIDTH, HEIGHT = 600, 800
PLAYER_SIZE = 50
OBSTACLE_WIDTH = 100
OBSTACLE_HEIGHT = 20
OBSTACLE_GAP = 200
GRAVITY = 0.5
# 颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
# 初始化 Pygame
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("跳一跳游戏")
clock = pygame.time.Clock()
# 创建玩家对象
player = pygame.Rect(WIDTH / 2 - PLAYER_SIZE / 2, HEIGHT - PLAYER_SIZE * 2, PLAYER_SIZE, PLAYER_SIZE)
player_jump = False
jump_velocity = 0
jump_start_time = 0
# 创建障碍物列表
obstacles = []
# 添加初始障碍物
obstacle = pygame.Rect(random.randint(0, WIDTH - OBSTACLE_WIDTH), HEIGHT - OBSTACLE_HEIGHT, OBSTACLE_WIDTH, OBSTACLE_HEIGHT)
obstacles.append(obstacle