[Python] 纯文本查看 复制代码 import pygame
import sys
import random
# 初始化 Pygame
pygame.init()
# --- 游戏设置 ---
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
PLAYER_WIDTH = 40
PLAYER_HEIGHT = 60
PLATFORM_WIDTH = 100
PLATFORM_HEIGHT = 20
COIN_SIZE = 20
GRAVITY = 1
JUMP_STRENGTH = -20 # 负值表示向上跳跃
PLAYER_SPEED = 5
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
GREEN = (0, 255, 0)
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Jump Game")
clock = pygame.time.Clock()
# --- 玩家角色类 ---
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface([PLAYER_WIDTH, PLAYER_HEIGHT])
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.x = SCREEN_WIDTH // 4
self.rect.y = SCREEN_HEIGHT - PLAYER_HEIGHT
self.y_velocity = 0
self.on_ground = True # 新增: 用于跟踪是否在地面上
def update(self):
# 应用重力
self.y_velocity += GRAVITY
self.rect.y += self.y_velocity
# 保持在屏幕内,防止跌出屏幕底部
if self.rect.bottom > SCREEN_HEIGHT:
self.rect.bottom = SCREEN_HEIGHT
self.y_velocity = 0
self.on_ground = True
def jump(self):
if self.on_ground: # 如果在地面上才能跳跃
self.y_velocity = JUMP_STRENGTH
self.on_ground = False # 设置为不在地面上
# 控制左右移动
def move(self, direction):
self.rect.x += direction * PLAYER_SPEED
# 防止越出屏幕
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH
# --- 平台类 ---
class Platform(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface([PLATFORM_WIDTH, PLATFORM_HEIGHT])
self.image.fill(BLUE)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# --- 金币类 ---
class Coin(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface([COIN_SIZE, COIN_SIZE])
self.image.fill(YELLOW)
self.rect = self.image.get_rect()
self.rect.center = (x, y)
# --- 创建精灵组 ---
all_sprites = pygame.sprite.Group()
platforms = pygame.sprite.Group()
coins = pygame.sprite.Group()
# --- 创建玩家实例 ---
player = Player()
all_sprites.add(player)
# --- 创建平台 ---
# 创建初始平台 (在玩家下方)
platform = Platform(0, SCREEN_HEIGHT - PLATFORM_HEIGHT)
platforms.add(platform)
all_sprites.add(platform)
# 创建其他平台 (随机分布)
for i in range(5):
x = random.randint(0, SCREEN_WIDTH - PLATFORM_WIDTH)
y = random.randint(SCREEN_HEIGHT // 4, SCREEN_HEIGHT - 100)
platform = Platform(x, y)
platforms.add(platform)
all_sprites.add(platform)
# --- 创建金币 ---
for _ in range(5):
x = random.randint(50, SCREEN_WIDTH - 50)
y = random.randint(50, SCREEN_HEIGHT - 50)
coin = Coin(x, y)
coins.add(coin)
all_sprites.add(coin)
# --- 游戏逻辑变量 ---
score = 0
running = True
# --- 游戏主循环 ---
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
player.jump() # 调用玩家的跳跃函数
# 检测按键左右移动
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player.move(-1)
if keys[pygame.K_RIGHT]:
player.move(1)
# 检测与平台的碰撞
player.on_ground = False
for platform in platforms:
if pygame.sprite.collide_rect(player, platform):
if player.rect.bottom <= platform.rect.bottom + 10 and player.y_velocity >= 0:
player.rect.bottom = platform.rect.top
player.y_velocity = 0
player.on_ground = True
# 检测与金币的碰撞
coin_collisions = pygame.sprite.spritecollide(player, coins, True)
if coin_collisions:
score += len(coin_collisions)
# 更新精灵位置
all_sprites.update()
# 绘制背景
screen.fill(BLACK)
# 绘制所有精灵
all_sprites.draw(screen)
# 绘制得分
font = pygame.font.Font(None, 36)
score_text = font.render("Score: " + str(score), True, WHITE)
screen.blit(score_text, (10, 10))
# 更新屏幕
pygame.display.flip()
# 控制游戏速度
clock.tick(60)
pygame.quit()
sys.exit()
|