python打砖块游戏代码
时间: 2023-07-09 10:47:05 浏览: 125
以下是一个简单的 Python 打砖块游戏代码,使用 Pygame 模块实现:
```python
import pygame
import random
# 定义窗口大小和颜色
WINDOW_WIDTH = 480
WINDOW_HEIGHT = 600
BG_COLOR = pygame.Color(0, 0, 0)
# 定义球的速度
BALL_SPEED = 5
# 定义挡板的宽度和高度
PADDLE_WIDTH = 80
PADDLE_HEIGHT = 20
# 定义砖块的宽度和高度
BRICK_WIDTH = 60
BRICK_HEIGHT = 20
# 初始化 Pygame
pygame.init()
# 创建窗口
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('打砖块游戏')
# 创建字体对象
font = pygame.font.SysFont('Microsoft YaHei', 24)
# 创建球对象
class Ball:
def __init__(self):
self.image = pygame.image.load('ball.png')
self.rect = self.image.get_rect()
self.rect.centerx = WINDOW_WIDTH // 2
self.rect.centery = WINDOW_HEIGHT // 2
self.direction = random.randint(45, 135)
def update(self):
radian = self.direction * math.pi / 180
self.rect.x += int(BALL_SPEED * math.cos(radian))
self.rect.y -= int(BALL_SPEED * math.sin(radian))
def change_direction(self, direction):
self.direction = direction
# 创建挡板对象
class Paddle:
def __init__(self):
self.image = pygame.image.load('paddle.png')
self.rect = self.image.get_rect()
self.rect.centerx = WINDOW_WIDTH // 2
self.rect.bottom = WINDOW_HEIGHT - 20
def update(self, direction):
self.rect.x += direction
if self.rect.left < 0:
self.rect.left = 0
elif self.rect.right > WINDOW_WIDTH:
self.rect.right = WINDOW_WIDTH
# 创建砖块对象
class Brick:
def __init__(self, color, x, y):
self.image = pygame.Surface((BRICK_WIDTH, BRICK_HEIGHT))
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# 初始化游戏对象
ball = Ball()
paddle = Paddle()
bricks = []
for i in range(0, 5):
for j in range(0, 8):
color = pygame.Color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
brick = Brick(color, j * BRICK_WIDTH + 40, i * BRICK_HEIGHT + 100)
bricks.append(brick)
# 游戏主循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
paddle.update(-5)
elif event.key == pygame.K_RIGHT:
paddle.update(5)
# 更新球的位置
ball.update()
# 碰撞检测
if ball.rect.bottom >= paddle.rect.top and ball.rect.centerx >= paddle.rect.left and ball.rect.centerx <= paddle.rect.right:
ball.change_direction(random.randint(135, 225))
elif ball.rect.top <= 0:
ball.change_direction(random.randint(45, 135))
elif ball.rect.left <= 0 or ball.rect.right >= WINDOW_WIDTH:
ball.change_direction(random.randint(-45, 45))
for brick in bricks:
if ball.rect.colliderect(brick.rect):
bricks.remove(brick)
ball.change_direction(random.randint(-45, 45))
# 绘制游戏界面
window.fill(BG_COLOR)
window.blit(ball.image, ball.rect)
window.blit(paddle.image, paddle.rect)
for brick in bricks:
window.blit(brick.image, brick.rect)
# 显示得分
score = len(bricks) * 10
score_text = font.render('得分:' + str(score), True, pygame.Color(255, 255, 255))
window.blit(score_text, (10, 10))
# 更新窗口
pygame.display.update()
```
你可以将本代码保存为 `brickbreaker.py` 文件,并在同一目录下放置球、挡板和砖块的图片文件,然后运行该文件即可开始游戏。
阅读全文