import pgzrun

WIDTH=800
HEIGHT=600
TITLE='Basketball'

class Basketball(Actor):
    def __init__(self, image, pos):
        super().__init__(image, pos, anchor=('left','bottom'))
        self.falling_speed = 0
        self.basketball_frame = 0
        self.spinning = True
        self.counter = 0

    def update(self):
        self.counter += 1
        if self.spinning==True:
            if self.counter==4:
                self.basketball_frame += 1
                self.counter = 0
            if self.basketball_frame == 12:
                self.basketball_frame = 0
            if self.basketball_frame<10:
                self.image = '0' + str(self.basketball_frame) + '-basketball'
            else:
                self.image = str(self.basketball_frame) + '-basketball'
        if self.y>=HEIGHT:
            # test if the ball is at the bottom of the window
            if self.falling_speed<1:
                self.spinning = False
                return # stop the ball from bouncing any more
            else:
                sounds.bounce.play()
                self.falling_speed *= -1 # make ball "fall upwards"
                self.falling_speed += 1.5 # lose some speed to friction
                self.y = self.y + self.falling_speed # move the ball
        else:
            self.falling_speed += 0.5 # speed up
            self.y = self.y + self.falling_speed # move the ball

basketball1 = Basketball('00-basketball', (0,130))
basketball2 = Basketball('00-basketball', (200,90))
basketball3 = Basketball('00-basketball', (400,110))
basketball4 = Basketball('00-basketball', (600,150))

def update():
    basketball1.update()
    basketball2.update()
    basketball3.update()
    basketball4.update()

def draw():
    screen.fill((255,255,255))
    basketball1.draw()
    basketball2.draw()
    basketball3.draw()
    basketball4.draw()

def on_key_down(key):
    if key==keys.SPACE:
        basketball1.falling_speed = 12

pgzrun.go()