找回密码
 中文实名注册
查看: 101|回复: 1

跳跳小游戏--王明杰

[复制链接]

5

主题

5

帖子

405

积分

中级会员

Rank: 3Rank: 3

积分
405
发表于 2025-1-18 17:39:46 | 显示全部楼层 |阅读模式
[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()
回复

使用道具 举报

5

主题

7

帖子

703

积分

高级会员

Rank: 4

积分
703
发表于 2025-1-19 16:12:23 | 显示全部楼层
我气死了,老师要我们每个人打赏10金币。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 中文实名注册

本版积分规则

小黑屋|东台市机器人学会 ( 苏ICP备2021035350号-1;苏ICP备2021035350号-2;苏ICP备2021035350号-3 )

GMT+8, 2025-3-15 00:21 , Processed in 0.039877 second(s), 28 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表