pygame 零 - 向下移动图形

pygame zero - moving graphics downwards

图形应落在从上到下的随机位置。当图形掉落并离开屏幕 "disappeared" 时,它应该再次掉落到从上到下的随机位置。我总是收到以下错误消息:"empty range for randrange() (0,-799, -799)"。 在图形出现在游戏中之前 window 它必须具有负的 y 坐标?那么,我怎样才能制作坠落的物体呢?

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)

当您使用 random.randint(a, b) 时,a 必须小于或等于 b

apple.y = randint(0, -800)

apple.y = randint(-800, 0)

注意,randint returns 一个随机整数 N 使得 a <= N <= b.


注意,如果要在屏幕顶部开始apple,则y坐标必须设置为0(或-radius苹果):

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

您实际要做的是将 apple 设置在屏幕上方的随机位置。