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()

相关推荐
2501_9159214312 小时前
Fastlane 结合 开心上架(Appuploader)命令行版本实现跨平台上传发布 iOS App 免 Mac 自动化上架实战全解析
android·macos·ios·小程序·uni-app·自动化·iphone
lskisme13 小时前
springboot maven导入本地jar包
开发语言·python·pycharm
开心-开心急了13 小时前
pyside6实现win10自动切换主题
开发语言·python·pyqt·pyside
mortimer14 小时前
一键实现人声伴奏分离:基于 `uv`, `FFmpeg` 和 `audio-separator` 的高效解决方案
python·ffmpeg·音视频开发
游戏开发爱好者814 小时前
iOS 上架要求全解析,App Store 审核标准、开发者准备事项与开心上架(Appuploader)跨平台免 Mac 实战指南
android·macos·ios·小程序·uni-app·iphone·webview
Sunhen_Qiletian14 小时前
Python 类继承详解:深度学习神经网络架构的构建艺术
python·深度学习·神经网络
点金石游戏出海14 小时前
每周资讯 | 印度数字媒体与娱乐市场在2025财年达93亿美;《崩坏:星穹铁道》新版本登顶iOS畅销榜首
游戏·娱乐·媒体·游戏资讯·崩坏星穹铁道
00后程序员张14 小时前
混淆 iOS 类名与变量名的实战指南,多工具组合把混淆做成工程能力(混淆 iOS 类名变量名/IPA 成品混淆Ipa/Guard CLI 实操)
android·ios·小程序·https·uni-app·iphone·webview
程序员大雄学编程14 小时前
用Python来学微积分34-定积分的基本性质及其应用
开发语言·python·数学·微积分
Q_Q51100828515 小时前
python+django/flask的莱元元电商数据分析系统_电商销量预测
spring boot·python·django·flask·node.js·php