找回密码
 中文实名注册
搜索
查看: 21|回复: 0

弹球游戏 在在问上找的 樊泽宇

[复制链接]

31

主题

98

回帖

1162

积分

金牌会员

积分
1162
发表于 4 天前 | 显示全部楼层 |阅读模式
[Python] 纯文本查看 复制代码
import pygame
import random

# 初始化Pygame
pygame.init()

# 游戏窗口设置
WIDTH, HEIGHT = 800, 600
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("弹球游戏")

# 颜色定义
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)

# 游戏元素尺寸
PADDLE_WIDTH = 100
PADDLE_HEIGHT = 20
BALL_RADIUS = 10
BRICK_WIDTH = 75
BRICK_HEIGHT = 30

# 计时系统参数
TIME_LIMIT = 60000  # 60秒(单位:毫秒)
start_time = pygame.time.get_ticks()

# 挡板类
class Paddle:
    def __init__(self):
        self.x = WIDTH // 2 - PADDLE_WIDTH // 2
        self.y = HEIGHT - 50
        self.speed = 8

    def draw(self):
        pygame.draw.rect(win, BLUE, (self.x, self.y, PADDLE_WIDTH, PADDLE_HEIGHT))

# 球类
class Ball:
    def __init__(self):
        self.x = WIDTH // 2
        self.y = HEIGHT // 2
        self.x_speed = 5 * random.choice([-1, 1])
        self.y_speed = -5
        self.lost = False

    def draw(self):
        pygame.draw.circle(win, RED, (int(self.x), int(self.y)), BALL_RADIUS)

# 砖块类
class Brick:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.active = True

    def draw(self):
        if self.active:
            pygame.draw.rect(win, GREEN, (self.x, self.y, BRICK_WIDTH, BRICK_HEIGHT))

# 初始化游戏元素
paddle = Paddle()
ball = Ball()

# 创建砖块阵列
bricks = []
for row in range(4):
    for col in range(WIDTH // BRICK_WIDTH):
        bricks.append(Brick(col * BRICK_WIDTH, row * BRICK_HEIGHT + 50))

# 游戏状态(使用中文字体)
font = pygame.font.SysFont('simhei', 36)
score = 0
lives = 3

def draw_game():
    win.fill(BLACK)
    # 绘制所有元素
    paddle.draw()
    ball.draw()
    for brick in bricks:
        brick.draw()
    
    # 显示游戏信息(中文版)
    elapsed_time = pygame.time.get_ticks() - start_time
    remaining_time = max(TIME_LIMIT - elapsed_time, 0)
    mins, secs = divmod(remaining_time // 1000, 60)
    
    score_text = font.render(f"得分: {score}", True, WHITE)
    lives_text = font.render(f"生命: {lives}", True, WHITE)
    time_text = font.render(f"剩余时间: {mins:02}:{secs:02}", True, WHITE)
    
    win.blit(score_text, (20, 10))
    win.blit(lives_text, (WIDTH - 140, 10))
    win.blit(time_text, (WIDTH//2 - 100, 10))
    
    pygame.display.update()

def game_over(message):
    win.fill(BLACK)
    text = font.render(f"{message} 最终得分: {score}", True, WHITE)
    win.blit(text, (WIDTH//2 - 180, HEIGHT//2))
    pygame.display.update()
    pygame.time.wait(3000)

# 游戏主循环
clock = pygame.time.Clock()
running = True

while running:
    clock.tick(60)
    
    # 时间检查
    elapsed_time = pygame.time.get_ticks() - start_time
    if elapsed_time >= TIME_LIMIT:
        game_over("时间到!")
        running = False
        continue
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 挡板移动控制
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and paddle.x > 0:
        paddle.x -= paddle.speed
    if keys[pygame.K_RIGHT] and paddle.x < WIDTH - PADDLE_WIDTH:
        paddle.x += paddle.speed

    # 球移动
    ball.x += ball.x_speed
    ball.y += ball.y_speed

    # 碰撞检测
    if ball.x <= BALL_RADIUS or ball.x >= WIDTH - BALL_RADIUS:
        ball.x_speed *= -1
    
    if ball.y <= BALL_RADIUS:
        ball.y_speed *= -1
    
    if ball.y >= HEIGHT - BALL_RADIUS:
        lives -= 1
        if lives == 0:
            game_over("游戏失败!")
            running = False
        else:
            ball = Ball()

    # 挡板碰撞
    if (paddle.x < ball.x < paddle.x + PADDLE_WIDTH and
        paddle.y < ball.y + BALL_RADIUS < paddle.y + PADDLE_HEIGHT):
        ball.y_speed *= -1
        hit_pos = (ball.x - paddle.x) / PADDLE_WIDTH
        ball.x_speed = 5 * (2 * hit_pos - 1)

    # 砖块碰撞
    for brick in bricks:
        if brick.active and (brick.x < ball.x < brick.x + BRICK_WIDTH and
                            brick.y < ball.y < brick.y + BRICK_HEIGHT):
            brick.active = False
            ball.y_speed *= -1
            score += 10
            break

    # 胜利条件
    if all(not brick.active for brick in bricks):
        game_over("恭喜通关!")
        running = False

    draw_game()

pygame.quit()

回复

使用道具 举报

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

本版积分规则

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