1

A graphic should fall at a random position from top to bottom. When the graphic has fallen down, and has "disappeared" from the screen, it should fall again at a random position from top to bottom. I always get the following error message: "empty range for randrange() (0,-799, -799)". Before the graphic appears in the game window it must have a negative y-coordinate? So, how can i make falling objects?

from random import randint
import pygame

WIDTH   = 800
HEIGHT  = 800

apple = Actor("apple")
apple.pos = randint(0, 800), randint(0, -800)

score = 0

def draw():
    screen.clear()
    apple.draw()
    screen.draw.text("Punkte: " + str(score), (700, 5), color = "white")


def update():
    if apple.y < 800:
        apple.y = apple.y + 4   
    else:
        apple.x = randint(0, 800)
        apple.y = randint(0, -800)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
siclaro
  • 137
  • 13

1 Answers1

4

When you use random.randint(a, b), then a has to be less or equal than b:

apple.y = randint(0, -800)

apple.y = randint(-800, 0)

Note, randint returns a random integer N such that a <= N <= b.


Note, if you want to start the apple at the top of the screen, them the y coordinate has to be set 0 (or -radius of the apple):

apple.x = randint(0, 800)
apple.y = 0

What you actually do is to set the apple at a random position above off screen.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174