小白随笔5946

导航

Pygame _来回弹跳的小球

 

----------------------------------------------------------------------

import pgzrun

WIDTH = 600
HEIGHT = 400

speed_x = 2
speed_y = 2

ball_r =30

x=ball_r
y=HEIGHT/2


bounce_height= HEIGHT-ball_r
bounce_width = WIDTH - ball_r


def draw():
screen.fill((0,125,125))
screen.draw.filled_circle((x,y),ball_r,(220,100,100))

def update():
global x,y,speed_x,speed_y, ball_r,bounce_height,bounce_width

x = x + speed_x
y = y + speed_y

if x>= bounce_width or x<ball_r:
speed_x = -speed_x
sounds.crash.play()
if y >= bounce_height or y<= ball_r:
speed_y = -speed_y
sounds.crash.play()
pgzrun.go()

--------------------------------------------------------------------------

 

posted on 2021-10-02 14:22  小白随笔5946  阅读(110)  评论(0编辑  收藏  举报