小游戏
import pygame
import sys
import random
初始化Pygame
pygame.init()
确保中文正常显示
pygame.font.init()
font_path = pygame.font.match_font('simsun') or pygame.font.match_font('microsoftyahei')
if not font_path:
# 如果没有中文字体,使用默认字体
font_path = pygame.font.get_default_font()
游戏窗口设置
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("平台跳跃游戏")
颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
玩家类
class Player(pygame.sprite.Sprite):
def init(self, x, y):
super().init()
self.image = pygame.Surface((30, 50))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.vel_y = 0
self.on_ground = False
self.jump_power = -15
self.speed = 5
self.gravity = 0.7
def update(self, platforms):
# 应用重力
self.vel_y += self.gravity
self.rect.y += self.vel_y
# 检测平台碰撞
self.on_ground = False
for platform in platforms:
if (self.rect.bottom >= platform.rect.top and
self.rect.bottom <= platform.rect.top + 20 and
self.rect.right >= platform.rect.left and
self.rect.left <= platform.rect.right and
self.vel_y > 0):
self.rect.bottom = platform.rect.top
self.vel_y = 0
self.on_ground = True
# 处理键盘输入
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.rect.x -= self.speed
if keys[pygame.K_RIGHT]:
self.rect.x += self.speed
if keys[pygame.K_SPACE] and self.on_ground:
self.vel_y = self.jump_power
# 边界检查
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > SCREEN_HEIGHT:
self.rect.bottom = SCREEN_HEIGHT
平台类
class Platform(pygame.sprite.Sprite):
def init(self, x, y, width, height):
super().init()
self.image = pygame.Surface((width, height))
self.image.fill(GREEN)
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((20, 20))
self.image.fill(YELLOW)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
创建精灵组
all_sprites = pygame.sprite.Group()
platforms = pygame.sprite.Group()
coins = pygame.sprite.Group()
创建玩家
player = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
all_sprites.add(player)
创建平台
platform = Platform(0, SCREEN_HEIGHT - 50, SCREEN_WIDTH, 50)
all_sprites.add(platform)
platforms.add(platform)
创建一些随机平台和金币
for i in range(5):
x = random.randint(0, SCREEN_WIDTH - 100)
y = random.randint(100, SCREEN_HEIGHT - 150)
width = random.randint(100, 200)
plat = Platform(x, y, width, 20)
all_sprites.add(plat)
platforms.add(plat)
# 在平台上放置金币
coin_x = random.randint(x + 10, x + width - 30)
coin = Coin(coin_x, y - 30)
all_sprites.add(coin)
coins.add(coin)
游戏主循环
clock = pygame.time.Clock()
score = 0
running = True
while running:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 更新
player.update(platforms)
# 检测金币碰撞
coin_hits = pygame.sprite.spritecollide(player, coins, True)
for coin in coin_hits:
score += 1
# 绘制
screen.fill(BLACK)
all_sprites.draw(screen)
# 显示分数
font = pygame.font.Font(font_path, 36)
text = font.render(f"分数: {score}", True, WHITE)
screen.blit(text, (10, 10))
# 显示控制说明
control_text = font.render("左右箭头移动,空格跳跃", True, WHITE)
screen.blit(control_text, (SCREEN_WIDTH - 350, 10))
# 更新显示
pygame.display.flip()
# 控制帧率
clock.tick(60)
退出游戏
pygame.quit()
sys.exit() 

浙公网安备 33010602011771号