弹跳的小球(python)

弹跳的小球(Python)

看看效果吧!

弹跳的小球

代码

# 安装以下几个库
# pip install pygame
# pip install pgzero
# pip install keyboard
import pgzrun # 导入游戏库
import keyboard

WIDTH = 640 # 设置窗口宽度
HEIGHT = 480 # 设置窗口高度
TITLE = '弹跳的小球' # 窗口标题
r = 20 # 设置小球的半径
c_x = 180 # 设置小球的起始坐标x
c_y = 100 # 设置小球的起始坐标y
r_x = 0 #滑块的移动
speed_x = 3 # 设置小球x轴方向
speed_y = 5 #设置小球y轴方向
r_speed = 5 #设置滑块的移动速度
flag = 1 # 设置一个标志,来判断游戏失败

# 绘制函数
def draw():
    rect = Rect((260 + r_x, 330), (120, 20)) #矩形的坐标,大小
    if flag:
        screen.fill('yellow') # 修改背景颜色
        # screen.draw.circle((400,300),100,'white')# 画一个空心圆
        screen.draw.filled_circle((c_x,c_y),r,'red') # 画一个实心圆
        # screen.draw.rect(rect,'red') # 画一个空心矩形
        screen.draw.filled_rect(rect,"red") # 画一个实心矩形
    else:
        screen.clear() # 清屏
        screen.draw.text('Game Over!',(130,150),fontsize=100,color='red') #画文本
        screen.draw.text('游戏失败',(130,250),fontsize=100,fontname="simhei",color='yellow')

# 更新函数,当程序运行后,每帧都会执行一次该函数
def update():
    global c_x,c_y,speed_x,speed_y,r,r_x,flag # 表示全局变量
    c_x += speed_x # 小球移动
    c_y += speed_y
    if c_x >= WIDTH -r or c_x <= r: # 如果小球撞到窗口就改变方向
        speed_x = -speed_x
    if c_y > HEIGHT - r or c_y <= r:
        speed_y = -speed_y
    if keyboard.is_pressed('a') and r_x >= -260: # 当按下a键,滑块左移,并当碰到窗口左壁时停止左移
        r_x -= r_speed
    if keyboard.is_pressed('d') and r_x <= 260: # 当按下d键,滑块右移,并当碰到窗口右壁时停止右移
        r_x += r_speed
    if c_x >= 260 + r_x and c_x <= 380 + r_x and c_y >= 325 and c_y <= 330: #如果小球碰到滑块就反弹
        speed_x = -speed_x
        speed_y = -speed_y
    if c_y == HEIGHT - r: # 如果小球碰到窗口底端,标志位置零,游戏结束
        flag = 0
pgzrun.go() # 开始执行游戏
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python弹跳是一个简单的游戏,通过使用Python编程语言来模拟小在不同条件下的弹跳行为。在这个游戏中,你可以控制小的起始位置、速度和重力等参数,然后观察小在屏幕上的弹跳运动。 要实现Python弹跳,你可以使用Pygame库来创建游戏窗口和处理游戏逻辑。以下是一个简单的实现步骤: 1. 导入Pygame库并初始化游戏: ```python import pygame pygame.init() ``` 2. 设置游戏窗口的大小和标题: ```python width = 800 height = 600 window = pygame.display.set_mode((width, height)) pygame.display.set_caption("Python弹跳") ``` 3. 定义小的属性和初始状态: ```python ball_radius = 20 ball_color = (255, 0, 0) ball_x = width // 2 ball_y = height // 2 ball_speed_x = 5 ball_speed_y = 0 gravity = 0.5 ``` 4. 创建游戏主循环,处理事件和更新小位置: ```python running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False ball_speed_y += gravity ball_x += ball_speed_x ball_y += ball_speed_y if ball_y + ball_radius > height: ball_speed_y = -ball_speed_y * 0.8 ball_y = height - ball_radius window.fill((255, 255, 255)) pygame.draw.circle(window, ball_color, (ball_x, ball_y), ball_radius) pygame.display.flip() ``` 5. 游戏结束后,退出Pygame: ```python pygame.quit() ``` 这只是一个简单的示例,你可以根据自己的需求和想法来扩展和改进这个游戏。希望对你有帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值