你给我用python写一个打砖块
时间: 2023-12-16 19:03:57 浏览: 116
很高兴为您提供帮助,以下是一个简单的打砖块游戏的Python代码:
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 游戏屏幕大小
screen_width = 640
screen_height = 480
# 创建游戏窗口
game_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)
blue = (0, 0, 255)
green = (0, 255, 0)
# 球的半径
ball_radius = 10
# 球的位置和速度
ball_x = 0
ball_y = 0
ball_speed_x = 5
ball_speed_y = 5
# 砖块的宽度和高度
brick_width = 60
brick_height = 15
# 砖块的位置和颜色
brick_x = 0
brick_y = 0
brick_color = red
# 砖块的数量和间距
brick_count = 10
brick_spacing = 10
# 挡板的宽度和高度
paddle_width = 60
paddle_height = 10
# 挡板的位置和速度
paddle_x = (screen_width - paddle_width) // 2
paddle_y = screen_height - paddle_height - 10
paddle_speed = 5
# 分数
score = 0
# 游戏结束标志
game_over = False
# 设置游戏时钟
clock = pygame.time.Clock()
# 加载字体
font = pygame.font.Font(None, 36)
# 创建球体
def create_ball():
global ball_x, ball_y, ball_speed_x, ball_speed_y
ball_x = screen_width // 2
ball_y = screen_height // 2
ball_speed_x = random.randint(-5, 5)
ball_speed_y = -5
# 创建砖块
def create_bricks():
global bricks
bricks = []
for row in range(5):
for col in range(brick_count):
brick_x = col * (brick_width + brick_spacing) + brick_spacing
brick_y = row * (brick_height + brick_spacing) + brick_spacing
bricks.append(pygame.Rect(brick_x, brick_y, brick_width, brick_height))
# 碰撞检测
def check_collisions():
global ball_x, ball_y, ball_speed_x, ball_speed_y, score, game_over
# 球和挡板的碰撞检测
if ball_y + ball_radius >= paddle_y and ball_x >= paddle_x and ball_x <= paddle_x + paddle_width:
ball_speed_y = -ball_speed_y
# 球和砖块的碰撞检测
for brick in bricks:
if ball_y - ball_radius <= brick.bottom and ball_x >= brick.left and ball_x <= brick.right:
ball_speed_y = -ball_speed_y
bricks.remove(brick)
score += 100
if len(bricks) == 0:
game_over = True
create_bricks()
create_ball()
# 绘制游戏界面
def draw_game():
# 清空屏幕
game_screen.fill(black)
# 绘制球体
pygame.draw.circle(game_screen, white, [ball_x, ball_y], ball_radius)
# 绘制挡板
pygame.draw.rect(game_screen, white, [paddle_x, paddle_y, paddle_width, paddle_height])
# 绘制砖块
for brick in bricks:
pygame.draw.rect(game_screen, brick_color, brick)
# 绘制分数
score_text = font.render("Score: " + str(score), True, white)
game_screen.blit(score_text, [10, 10])
# 更新屏幕
pygame.display.update()
# 创建球体和砖块
create_ball()
create_bricks()
# 游戏循环
while not game_over:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
paddle_x -= paddle_speed
elif event.key == pygame.K_RIGHT:
paddle_x += paddle_speed
# 移动球体
ball_x += ball_speed_x
ball_y += ball_speed_y
# 球体碰到屏幕边缘反弹
if ball_x - ball_radius <= 0 or ball_x + ball_radius >= screen_width:
ball_speed_x = -ball_speed_x
if ball_y - ball_radius <= 0:
ball_speed_y = -ball_speed_y
if ball_y + ball_radius >= screen_height:
game_over = True
create_ball()
# 移动挡板
if paddle_x < 0:
paddle_x = 0
elif paddle_x + paddle_width > screen_width:
paddle_x = screen_width - paddle_width
# 碰撞检测
check_collisions()
# 绘制游戏界面
draw_game()
# 控制游戏帧率
clock.tick(60)
# 退出pygame
pygame.quit()
```
希望对您有所帮助!
阅读全文