看看效果吧!
![弹跳的小球](https://img-blog.csdnimg.cn/8df5dafe8064452f95c8ab3bcbdbec40.gif#pic_center)
代码
import pgzrun
import keyboard
WIDTH = 640
HEIGHT = 480
TITLE = '弹跳的小球'
r = 20
c_x = 180
c_y = 100
r_x = 0
speed_x = 3
speed_y = 5
r_speed = 5
flag = 1
def draw():
rect = Rect((260 + r_x, 330), (120, 20))
if flag:
screen.fill('yellow')
screen.draw.filled_circle((c_x,c_y),r,'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:
r_x -= r_speed
if keyboard.is_pressed('d') and r_x <= 260:
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()