12ball


1. `import pygame`: 导入 Pygame 库。 2. `import time`: 导入 time 模块。 3. `import random`: 导入 random 模块。 4. `pygame.init()`: 初始化 Pygame 库。 5. `screen = pygame.display.set_mode((600, 500))`: 创建一个窗口并设置其大小。 6. `pygame.display.set_caption("接小球游戏")`: 设置窗口标题。 7. `ball_x = 300`: 初始化小球的 x 坐标。 8. `ball_y = 250`: 初始化小球的 y 坐标。 9. `rect_x, rect_y, rect_w, rect_h = 300, 460, 120, 40`: 初始化挡板的位置和大小。 10. `font1 = pygame.font.Font(None, 24)`: 创建一个字体对象。 11. `score = 0`: 初始化分数为 0。 12. `lives = 3`: 初始化生命值为 3。 13. `def ball(ball_x, ball_y):`: 定义一个函数 `ball`,用于绘制小球。 14. `pygame.draw.circle(screen, (255., 25, 52), (ball_x, ball_y), 20, 0)`: 绘制一个圆形表示小球。 15. `while True:`: 开始一个无限循环。 16. `for event in pygame.event.get():`: 处理 Pygame 产生的事件。 17. `print(event)`: 打印事件。 18. `if event.type == pygame.QUIT: pygame.quit()`: 如果是退出事件,关闭 Pygame 库。 19. `elif event.type == pygame.MOUSEMOTION: rect_x, _ = event.pos`: 如果是鼠标移动事件,获取鼠标的坐标并更新挡板的位置。 20. `screen.fill((34, 177, 135))`: 填充屏幕背景颜色。 21. `ball_y = ball_y + 1`: 更新小球的 y 坐标。 22. `if ball_y > 500: ball_y = 0 ball_x = random.randint(0, 600) lives = lives -1`: 如果小球落出屏幕,重新生成小球并减少生命值。 23. `ball(ball_x, ball_y)`: 调用 `ball` 函数绘制小球。 24. `if rect_x < ball_x < rect_x + rect_w and rect_y < ball_y < rect_y + rect_h: score = score + 1 ball_y = 0 ball_x = random.randint(0, 600)`: 如果小球与挡板碰撞,增加分数并重新生成小球。 25. `Text_score = font1.render('score:%d' % score, True, (0, 0, 0))`: 创建一个表示分数的文本对象。 26. `screen.blit(Text_score, (0, 0))`: 在屏幕上绘制分数文本。 27. `Text_lives = font1.render('lives:%d' % lives, True, (0, 0, 0))`: 创建一个表示生命值的文本对象。 28. `screen.blit(Text_lives, (530, 0))`: 在屏幕上绘制生命值文本。 29. `pygame.draw.rect(screen, (100, 200, 30), (rect_x, rect_y, rect_w, rect_h), 0)`: 绘制一个矩形表示挡板。 30. `pygame.display.update()`: 更新屏幕显示。 31. `pygame.quit()`: 关闭 Pygame 库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值