sprite_period = 0.2
sprite_steps = sprite_period / period
step = 0
sprites = cycle([LEDSprite("ball%d.txt" % i) for i in range(4)])
sprite = sprites.next()
while True:
    # Draw the ball
    led.erase()
    led.sprite(sprite, (int(x), int(y)))
    led.show()

    # Change to next sprite, once every sprite_steps
    step += 1
    if step % sprite_steps == 0:
        sprite = sprites.next()

    # Gravity
    xangle, yangle, zangle = accel.get_angle()
    xdist += xangle / 90
    ydist += yangle / 90

    # Move the point to a new position.  If it hits a wall, reverse the
    # direction of the ball.
    x, y = (x + xdist, y + ydist)
    if x >= (led.width() - (sprite.width() - 1)) or x < 0:
        xdist = -xdist * 0.99
    if y >= (led.height() - (sprite.height() - 1)) or y < 0:
        ydist = -ydist * 0.99

    time.sleep(period)
sprite_steps = sprite_period/period
step = 0
sprites = cycle([LEDSprite("ball%d.txt" % i) for i in range(4)])
sprite = sprites.next()
while True:
    # Draw the ball
    led.erase()
    led.sprite(sprite, (int(x), int(y)))
    led.show()

    # Change to next sprite, once every sprite_steps
    step += 1
    if step % sprite_steps == 0:
        sprite = sprites.next()

    # Gravity
    xangle, yangle, zangle = accel.get_angle()
    xdist += xangle/90
    ydist += yangle/90

    # Move the point to a new position.  If it hits a wall, reverse the
    # direction of the ball.
    x, y = (x+xdist, y+ydist)
    if x >= (led.width() - (sprite.width() - 1)) or x < 0:
        xdist = - xdist * 0.99
    if y >= (led.height() - (sprite.height() - 1)) or y < 0:
        ydist = - ydist * 0.99

    time.sleep(period)

        led.point(int(self.x), int(self.y))

    def move(self, angle=None):
        # Gravity
        if angle:
            xangle, yangle, zangle = angle
            self.xdist += self.xangle/90
            self.ydist += self.yangle/90

        # Move the point to a new position.  If it hits a wall, reverse the
        # direction of the ball.
        self.x, self.y = (self.x+self.xdist, self.y+self.ydist)
        if self.x >= led.width() or self.x < 0:
            self.xdist = - self.xdist
        if self.y >= led.height() or self.y < 0:
            self.ydist = - self.ydist


ball = Ball()
while True:
    # Draw the ball
    led.erase()
    ball.draw()
    led.show()

    # Move it (in the presense of gravity)
    ball.move(accel.get_angle())

    time.sleep(period)

    def draw(self):
        led.point(int(self.x), int(self.y))

    def move(self, angle=None):
        # Gravity
        if angle:
            xangle, yangle, zangle = angle
            self.xdist += self.xangle / 90
            self.ydist += self.yangle / 90

        # Move the point to a new position.  If it hits a wall, reverse the
        # direction of the ball.
        self.x, self.y = (self.x + self.xdist, self.y + self.ydist)
        if self.x >= led.width() or self.x < 0:
            self.xdist = -self.xdist
        if self.y >= led.height() or self.y < 0:
            self.ydist = -self.ydist


ball = Ball()
while True:
    # Draw the ball
    led.erase()
    ball.draw()
    led.show()

    # Move it (in the presense of gravity)
    ball.move(accel.get_angle())

    time.sleep(period)