当前位置: 首页 > news >正文

Python 用pygame简简单单实现一个打砖块

#  -*- coding: utf-8 -*-
#  #
#  Copyright (C) 2024 , Inc. All Rights Reserved
#  #
#  @Time    : 2024/3/30 14:34
#  @Author  : 赫凯
#  @Email   : hekaiiii@163.com
#  @File    : ballgame.py
#  @Software: PyCharm
import math
import randomimport pygame
import sys# 球体类
class Ball:def __init__(self, radius):self.radius = radiusself.pos = [screen_width // 2, screen_height - 20 - radius]self.velocity = [2, -2]def draw(self, surface):pygame.draw.circle(surface, WHITE, self.pos, self.radius)def update(self):self.pos[0] += self.velocity[0]self.pos[1] += self.velocity[1]# print(self.pos)# 碰到墙壁反弹# print(screen_width - self.radius)if self.pos[0] < self.radius or self.pos[0] > (screen_width - self.radius):# self.pos[0] = -self.pos[0]self.velocity[0] *= -1# if self.pos[0] < 0:#     self.pos[0] = -self.pos[0]# print(screen_height - self.radius)if self.pos[1] <= self.radius or self.pos[1] > (screen_height - self.radius):self.velocity[1] *= -1# if self.pos[1] < 0:#     self.pos[1] = -self.pos[1]# 挡板类
class Paddle:def __init__(self, width, height):self.width = widthself.height = heightself.pos = [screen_width // 2 - width // 2, screen_height - 20]self.velocity = [-5, 0]def draw(self, surface):pygame.draw.rect(surface, WHITE, (self.pos[0], self.pos[1], self.width, self.height), 0, 0)def update(self):keys = pygame.key.get_pressed()if keys[pygame.K_LEFT] and self.pos[0] > 0:self.pos[0] += self.velocity[0]if keys[pygame.K_RIGHT] and self.pos[0] < screen_width - self.width:self.pos[0] -= self.velocity[0]class Brick:def __init__(self, x, y, width, height, color):self.rect = pygame.Rect(x, y, width, height)self.color = colordef draw(self, surface):pygame.draw.rect(surface, self.color, self.rect)def check_collision(ball, brick):# 检查球与砖块的左右边界碰撞if (brick.rect.x - ball.radius <= ball.pos[0] <= brick.rect.x + brick.rect.width + ball.radius) and (brick.rect.y <= ball.pos[1] <= brick.rect.y + brick.rect.height):return 1  # 返回1表示碰撞发生在砖块的左右边界# 检查球与砖块的上下边界碰撞if (brick.rect.y - ball.radius <= ball.pos[1] <= brick.rect.y + brick.rect.height + ball.radius) and (brick.rect.x <= ball.pos[0] <= brick.rect.x + brick.rect.width):return 2  # 返回2表示碰撞发生在砖块的上下边界return 0def update_bricks(ball, bricks):score = 0for brick in bricks[:]:if check_collision(ball, brick) == 1:# 处理碰撞效果,比如删除砖块或改变球的方向bricks.remove(brick)ball.velocity[0] *= -1score += 10breakelif check_collision(ball, brick) == 2:bricks.remove(brick)ball.velocity[1] *= -1score += 10break# 可能还需要更新分数或其他游戏状态return scoredef create_explosion(brick):# 创建一个表示爆炸效果的对象或动画passdef update_explosions(explosions, bricks):for explosion in explosions[:]:# 更新爆炸效果if explosion.is_finished():explosions.remove(explosion)# 如果爆炸与砖块碰撞,移除砖块if explosion.intersects(brick):bricks.remove(brick)if __name__ == '__main__':# 初始化Pygamepygame.init()# 设置屏幕大小screen_width, screen_height = 640, 480screen = pygame.display.set_mode((screen_width, screen_height))# 设置标题和时钟pygame.display.set_caption('Bounce Game')clock = pygame.time.Clock()# 定义颜色WHITE = (255, 255, 255)BLACK = (0, 0, 0)RED = (255, 0, 0)# 创建球体和挡板ball = Ball(10)paddle = Paddle(80, 10)# 创建砖块bricks = []for x in range(0, screen_width, 80):  # 假设每个砖块宽度为80像素for y in range(0, screen_height // 4, 20):  # 假设每个砖块高度为40像素brick = Brick(x + 2, y + 2, 80 - 2, 20 - 2, (255, 255, 255))  # 白色砖块bricks.append(brick)# 得分变量score = 0# 游戏主循环running = Truewhile running:for event in pygame.event.get():if event.type == pygame.QUIT:running = False# 更新球体和挡板的位置ball.update()paddle.update()# print(ball.pos, paddle.pos)# 检测球体是否碰到挡板if ball.pos[1] + ball.radius > paddle.pos[1]:if ball.pos[0] < paddle.pos[0] or ball.pos[0] > paddle.pos[0] + paddle.width:# running = Falsescore -= 1else:ss = abs(ball.pos[0] - (paddle.pos[0]+paddle.width//2)) / 20ball.velocity[0] = 2+ss*2ball.velocity[1] *= -1# screen.fill(BLACK)# 渲染背景screen.fill(BLACK)# 绘制球体和挡板ball.draw(screen)paddle.draw(screen)xx = random.randint(0, 255)# 绘制砖块for brick in bricks:# 渐变ß# r = math.sqrt((ball.pos[0] - brick.rect.x) ** 2 + (ball.pos[1] - brick.rect.y) ** 2)# brick.color = ((r)/720 *255 % 255,  255, (r)/720*255 % 255)brick.draw(screen)if ball.pos[1] <= screen_height//2:score += update_bricks(ball, bricks)# 显示得分font = pygame.font.Font(None, 36)score_text = font.render('Score: ' + str(score), True, RED)screen.blit(score_text, (10, 10))# 更新屏幕显示pygame.display.flip()# 设置帧率clock.tick(60)# 退出游戏pygame.quit()sys.exit()

在这里插入图片描述

http://www.lryc.cn/news/333164.html

相关文章:

  • 软考113-上午题-【计算机网络】-IPv6、无线网络、Windows命令
  • 深入浅出 -- 系统架构之负载均衡Nginx资源压缩
  • 基于jsp+Spring boot+mybatis的图书管理系统设计和实现
  • Pytorch转onnx
  • 苍穹外卖——项目搭建
  • 云原生架构(微服务、容器云、DevOps、不可变基础设施、声明式API、Serverless、Service Mesh)
  • 基于重写ribbon负载实现灰度发布
  • 无端科技一面(生死狙击项目组 战斗客户端 40min)
  • idea开发 java web 高校学籍管理系统bootstrap框架web结构java编程计算机网页
  • linux之文件系统、inode和动静态库制作和发布
  • C++IO类,输入输出缓冲区,流状态
  • 机器学习笔记 - 文字转语音技术路线简述以及相关工具不完全清单
  • 阿里云4核8G服务器ECS通用算力型u1实例优惠价格
  • Jetson nano部署Yolov8 安装Archiconda3+创建pytorch环境(详细教程+错误解决)
  • Node.JS多线程PromisePool之promise-pool库实现
  • 【C++】红黑树讲解及实现
  • security如何不拦截websocket
  • Unity类银河恶魔城学习记录12-3 p125 Limit Inventory Slots源代码
  • 【智能排班系统】雪花算法生成分布式ID
  • sass中的导入与部分导入
  • 工业组态 物联网组态 组态编辑器 web组态 组态插件 编辑器
  • git可视化工具
  • 基于单片机电子密码锁系统设计
  • 点云从入门到精通技术详解100篇-基于点云与图像纹理的 道路识别(续)
  • 《机器学习在量化投资中的应用研究》目录
  • Spring拓展点之SmartLifecycle如何感知容器启动和关闭
  • 深入理解Java匿名内部类(day21)
  • 《状态模式(极简c++)》
  • Day4-Hive直播行业基础笔试题
  • mybatis批量新增数据