Bootstrap

Python 方框消除小游戏

在这里插入图片描述

import pygame
import random

# 初始化pygame
pygame.init()

# 设置屏幕大小
screen = pygame.display.set_mode((800, 600))

# 设置标题
pygame.display.set_caption("打砖块")

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

# 定义球类
class Ball:
    def __init__(self):
        self.reset()

    def reset(self):
        self.x = 400
        self.y = 300
        self.radius = 5  # 调整球的半径
        self.dx = 3 * random.choice((1, -1))
        self.dy = 3 * random.choice((1, -1))

    def move(self):
        self.x += self.dx
        self.y += self.dy

        if self.x <= 0 or self.x >= 800:
            self.dx = -self.dx
        if self.y <= 0:
            self.dy = -self.dy

    def draw(self):
        pygame.draw.circle(screen, RED, (self.x, self.y), self.radius)

# 定义板类
class Paddle:
    def __init__(self):
        self.x = 350
        self.y = 550
        self.width = 100
        self.height = 10
        self.dx = 0

    def move(self):
        self.x += self.dx
        if self.x < 0:
            self.x = 0
        if self.x > 700:
            self.x = 700

    def draw(self):
        pygame.draw.rect(screen, BLUE, (self.x, self.y, self.width, self.height))

# 定义砖块类
class Brick:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.width = 50  # 调整砖块的宽度
        self.height = 20
        self.color = GREEN

    def draw(self):
        pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))

# 创建游戏对象
ball = Ball()
paddle = Paddle()
bricks = [Brick(x, y) for x in range(0, 800, 55) for y in range(0, 300, 25)]  # 增加砖块的数量

# 定义重新开始按钮
font = pygame.font.Font(None, 74)
restart_text = font.render('重新开始', True, WHITE)
restart_rect = restart_text.get_rect(center=(400, 300))

# 游戏主循环
running = True
game_over = False
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                paddle.dx = -5
            elif event.key == pygame.K_RIGHT:
                paddle.dx = 5
        elif event.type == pygame.KEYUP:
            if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
                paddle.dx = 0
        elif event.type == pygame.MOUSEBUTTONDOWN and game_over:
            if restart_rect.collidepoint(event.pos):
                game_over = False
                ball.reset()
                bricks = [Brick(x, y) for x in range(0, 800, 55) for y in range(0, 300, 25)]  # 重新生成砖块

    if not game_over:
        ball.move()
        paddle.move()

        # 检查球与板的碰撞
        if paddle.y < ball.y + ball.radius < paddle.y + paddle.height and paddle.x < ball.x < paddle.x + paddle.width:
            ball.dy = -ball.dy

        # 检查球与砖块的碰撞
        for brick in bricks:
            if brick.y < ball.y < brick.y + brick.height and brick.x < ball.x < brick.x + brick.width:
                ball.dy = -ball.dy
                bricks.remove(brick)
                break

        # 检查球是否落到屏幕底部
        if ball.y > 600:
            game_over = True

    # 绘制屏幕
    screen.fill(BLACK)
    ball.draw()
    paddle.draw()
    for brick in bricks:
        brick.draw()
    if game_over:
        screen.blit(restart_text, restart_rect)
    pygame.display.flip()

    pygame.time.delay(10)

pygame.quit()
;