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

Python小游戏29乒乓球

import pygame

import sys

# 初始化pygame

pygame.init()

# 屏幕大小

screen_width = 800

screen_height = 600

screen = pygame.display.set_mode((screen_width, screen_height))

pygame.display.set_caption("打乒乓球")

 

# 颜色定义

WHITE = (255, 255, 255)

BLACK = (0, 0, 0)

 

# 球类

class Ball:

    def __init__(self, x, y, radius, color, x_vel, y_vel):

        self.x = x

        self.y = y

        self.radius = radius

        self.color = color

        self.x_vel = x_vel

        self.y_vel = y_vel

 

    def draw(self, screen):

        pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)

 

    def move(self):

        self.x += self.x_vel

        self.y += self.y_vel

 

        # 碰到左右边界反弹

        if self.x - self.radius < 0 or self.x + self.radius > screen_width:

            self.x_vel = -self.x_vel

 

        # 碰到上边界增加速度,碰到下边界游戏结束(这里简单处理为碰到下边界也反弹,用于演示)

        if self.y - self.radius < 0:

            self.y_vel = -self.y_vel

            self.x_vel *= 1.1 # 增加水平速度,使游戏更有挑战性

        elif self.y + self.radius > screen_height:

            # 实际应用中可以在这里结束游戏

            self.y = screen_height - self.radius # 为了演示,让球从底部反弹

            self.y_vel = -self.y_vel

            self.x_vel *= 0.9 # 减速,增加游戏难度

 

# 玩家类(这里只实现一个玩家,即左侧玩家,使用键盘W和S键控制)

class Player:

    def __init__(self, x, y, width, height, color):

        self.x = x

        self.y = y

        self.width = width

        self.height = height

        self.color = color

        self.vel = 0

 

    def draw(self, screen):

        pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))

 

    def move(self):

        keys = pygame.key.get_pressed()

        if keys[pygame.K_w] and self.y > 0:

            self.y -= 10

        if keys[pygame.K_s] and self.y < screen_height - self.height:

            self.y += 10

 

# 创建球和玩家对象

ball = Ball(screen_width // 2, screen_height // 2, 15, WHITE, 5, 5)

player = Player(10, screen_height // 2 - 20, 10, 40, WHITE)

 

# 游戏主循环

clock = pygame.time.Clock()

running = True

while running:

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            running = False

 

    # 移动球和玩家

    ball.move()

    player.move()

 

    # 检测球是否碰到玩家(这里只检测左侧玩家)

    if (ball.x - ball.radius < player.x + player.width and

        ball.x + ball.radius > player.x and

        ball.y - ball.radius < player.y + player.height and

        ball.y + ball.radius > player.y):

        ball.y_vel = -ball.y_vel

 

    # 填充背景色

    screen.fill(BLACK)

 

    # 绘制球和玩家

    ball.draw(screen)

    player.draw(screen)

 

    # 更新屏幕显示

    pygame.display.flip()

 

    # 控制帧率

    clock.tick(60)

 

pygame.quit()

sys.exit()

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

相关文章:

  • 220.存在重复元素③
  • 使用 Go 语言调用 DeepSeek API:完整指南
  • AJAX笔记原理篇
  • ubuntu直接运行arm环境qemu-arm-static
  • 尝试把clang-tidy集成到AWTK项目
  • 一文了解性能优化的方法
  • 【怎么用系列】短视频戒断——对推荐算法进行干扰
  • C#中的委托(Delegate)
  • PostCss
  • Linux 系统上安装 Docker 并进行配置
  • DeepSeek 等 AI 技术能否推动股市的繁荣?
  • 【网络】应用层协议http
  • 大数据数仓实战项目(离线数仓+实时数仓)2
  • 测试csdn图片发布
  • 站在JavaScript的视角去看,HTML的DOM和GLTF的Json数据。
  • 传输层协议 UDP 与 TCP
  • VSCode源码分析参考资料
  • 使用VCS对Verilog/System Verilog进行单步调试的步骤
  • ROS-激光雷达-消息包格式-获取激光雷达数据-激光雷达避障
  • c++之模板进阶
  • 关于Internet Download Manager(IDM)强制下载合并相关二次开发
  • 鸿蒙HarmonyOS Next 视频边播放边缓存- OhosVideoCache
  • (10) 如何获取 linux 系统上的 TCP 、 UDP 套接字的收发缓存的默认大小,以及代码范例
  • 程序代码篇---项目目录结构HSV掩膜Opencv图像处理
  • 注解与反射基础
  • Vue指令v-html
  • 院校联合以项目驱动联合培养医工计算机AI人才路径探析
  • CDDIS从2025年2月开始数据迁移
  • 前端 | JavaScript中的reduce方法
  • 【C++】B2124 判断字符串是否为回文