Pygame游戏实战四:打砖块

 介绍模块

本游戏使用的是由Pycharm中的pygame模块来实现的,也可以在python中运行。通过Pygame制作一个打砖块,通过击打砖块来得到更多的分数,看看这个是你小时候玩的游戏吗?

最小开发框架

详情请看此文章:
Pygame游戏模块介绍二:最小开发框架代码_pygame最小开发框架_牧子川的博客-CSDN博客

Pygame和sys模块

import pygame # 制作游戏时要使用的模块
import sys # python的标准库,对内部各功能模块进行初始化创建,系统模块

random模块

需要在屏幕上随机生成食物的位置,通过随机一个位置来表示食物的位置

from random import randint

详情请看此文章:Pygame游戏模块介绍一:random模块简介_random 范围_牧子川的博客-CSDN博客

相关功能  

【童年游戏】打砖块

砖块设定

每次游戏出现的砖块数量、位置、大小均不同

    for row in range(brick_rows):
        for col in range(brick_cols):
            brick_x = col * (brick_width + 5) + 5  # +5 是为了增加砖块之间的间隔
            brick_y = row * (brick_height + 5) + 50
            brick_width = random.randint(40, 80)  # 随机砖块的宽度
            bricks.append(pygame.Rect(brick_x, brick_y, brick_width, brick_height))

小球设置

    ball_x = random.randint(ball_radius, WINDOW_WIDTH - ball_radius)
    ball_y = random.randint(ball_radius, WINDOW_HEIGHT // 2)

    ball_speed_x = 5
    ball_speed_y = 5

碰撞检测

        # 碰撞检测和游戏逻辑
        if ball_x < ball_radius or ball_x > WINDOW_WIDTH - ball_radius:
            ball_speed_x *= -1
        if ball_y < ball_radius:
            ball_speed_y *= -1
        elif ball_y > WINDOW_HEIGHT - ball_radius:
            initialize_game()  # 重新初始化游戏
            game_over = True  # 游戏结束

键盘移动

        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 < WINDOW_WIDTH - paddle_width:
            paddle_x += paddle_speed

游戏结束

        game_over_text = font.render("Game Over!", True, BRICK_COLOR)
        window.blit(game_over_text, (WINDOW_WIDTH // 2 - 100, WINDOW_HEIGHT // 2 - 20))

挡板与小球

        # 绘制板
        pygame.draw.rect(window, BRICK_COLOR, (paddle_x, paddle_y, paddle_width, paddle_height))

        # 绘制弹球
        pygame.draw.circle(window, BRICK_COLOR, (ball_x, ball_y), ball_radius)

图片界面  

扩展可添加功能

1.砖块改变,打击砖块出现不同的分数

2.增加鼠标控制

3.小球移动速度改变

4.添加背景音乐

5.砖块需要击打多次才能得分

源码获取

GitHub 打砖块

 欢迎关注我的公众号:@AI算法与电子竞赛    

硬性的标准其实限制不了无限可能的我们,所以啊!少年们加油吧!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
抱歉,我是一个语言模型,不能实际运行程序。但是,我可以提供下面的代码和说明供您参考。 首先,需要导入pygame和其他必要的库: ```python import pygame import random ``` 接下来,需要定义一些常量和变量: ```python # 窗口大小 WINDOW_WIDTH = 800 WINDOW_HEIGHT = 600 # 砖块大小 BRICK_WIDTH = 60 BRICK_HEIGHT = 20 # 砖块和列 BRICK_ROWS = 5 BRICK_COLS = 10 # 球的大小和速度 BALL_RADIUS = 10 BALL_SPEED = 5 # 板子的大小和速度 PADDLE_WIDTH = 100 PADDLE_HEIGHT = 10 PADDLE_SPEED = 5 # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # 初始化pygame pygame.init() # 创建窗口 window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) # 设置窗口标题 pygame.display.set_caption('打砖块') # 加载字体 font = pygame.font.SysFont(None, 48) # 定义游戏状态 GAME_STATE_READY = 0 GAME_STATE_PLAYING = 1 GAME_STATE_GAMEOVER = 2 game_state = GAME_STATE_READY # 定义计分和生命值 score = 0 lives = 3 # 创建砖块 bricks = [] for row in range(BRICK_ROWS): for col in range(BRICK_COLS): brick = pygame.Rect(col * BRICK_WIDTH, row * BRICK_HEIGHT + 50, BRICK_WIDTH, BRICK_HEIGHT) bricks.append(brick) # 创建球和板子 ball = pygame.Rect(WINDOW_WIDTH // 2 - BALL_RADIUS, WINDOW_HEIGHT // 2 - BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2) paddle = pygame.Rect(WINDOW_WIDTH // 2 - PADDLE_WIDTH // 2, WINDOW_HEIGHT - PADDLE_HEIGHT - 10, PADDLE_WIDTH, PADDLE_HEIGHT) # 设置球的速度和方向 ball_speed_x = BALL_SPEED ball_speed_y = -BALL_SPEED ``` 然后,需要定义一些函来处理游戏逻辑: ```python def draw_ball(): pygame.draw.circle(window, WHITE, (ball.x + BALL_RADIUS, ball.y + BALL_RADIUS), BALL_RADIUS) def draw_paddle(): pygame.draw.rect(window, WHITE, paddle) def draw_bricks(): for brick in bricks: pygame.draw.rect(window, GREEN, brick) def draw_score(): score_text = font.render('Score: ' + str(score), True, WHITE) window.blit(score_text, (10, 10)) def draw_lives(): lives_text = font.render('Lives: ' + str(lives), True, WHITE) window.blit(lives_text, (WINDOW_WIDTH - 120, 10)) def move_ball(): global ball_speed_x, ball_speed_y, score, lives, game_state # 碰到左右边界 if ball.left < 0 or ball.right > WINDOW_WIDTH: ball_speed_x = -ball_speed_x # 碰到上边界 if ball.top < 0: ball_speed_y = -ball_speed_y # 碰到板子 if ball.colliderect(paddle): ball_speed_y = -BALL_SPEED ball_speed_x = random.randint(-BALL_SPEED, BALL_SPEED) # 碰到砖块 for brick in bricks: if ball.colliderect(brick): bricks.remove(brick) ball_speed_y = -ball_speed_y score += 10 # 球掉落到底部 if ball.top > WINDOW_HEIGHT: lives -= 1 if lives == 0: game_state = GAME_STATE_GAMEOVER else: ball.center = (WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2) ball_speed_x = BALL_SPEED ball_speed_y = -BALL_SPEED ball.move_ip(ball_speed_x, ball_speed_y) def move_paddle(): keys = pygame.key.get_pressed() # 左右移动板子 if keys[pygame.K_LEFT] and paddle.left > 0: paddle.move_ip(-PADDLE_SPEED, 0) if keys[pygame.K_RIGHT] and paddle.right < WINDOW_WIDTH: paddle.move_ip(PADDLE_SPEED, 0) def show_message(message): message_text = font.render(message, True, WHITE) message_rect = message_text.get_rect(center=(WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2)) window.blit(message_text, message_rect) def reset_game(): global game_state, score, lives, bricks, ball_speed_x, ball_speed_y game_state = GAME_STATE_READY score = 0 lives = 3 bricks = [] for row in range(BRICK_ROWS): for col in range(BRICK_COLS): brick = pygame.Rect(col * BRICK_WIDTH, row * BRICK_HEIGHT + 50, BRICK_WIDTH, BRICK_HEIGHT) bricks.append(brick) ball.center = (WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2) ball_speed_x = BALL_SPEED ball_speed_y = -BALL_SPEED ``` 最后,需要在主循环中调用这些函: ```python 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_SPACE: if game_state == GAME_STATE_READY: game_state = GAME_STATE_PLAYING elif game_state == GAME_STATE_GAMEOVER: reset_game() # 清空屏幕 window.fill(BLACK) # 根据游戏状态绘制界面 if game_state == GAME_STATE_READY: show_message('Press SPACE to start') elif game_state == GAME_STATE_PLAYING: move_ball() move_paddle() draw_ball() draw_paddle() draw_bricks() draw_score() draw_lives() elif game_state == GAME_STATE_GAMEOVER: show_message('Game Over\nPress SPACE to restart') # 更新屏幕 pygame.display.update() ``` 这就是一个简单的打砖块游戏。您可以根据需要修改其中的细节和功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牧锦程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值