pygame零向下移动图形

2024-10-03 09:07:35 发布

您现在位置:Python中文网/ 问答频道 /正文

图形应该从上到下随机放置。当图形从屏幕上落下并“消失”时,它应该在从上到下的任意位置再次落下。我总是收到以下错误消息:“randrange()的空范围(0,-799,-799)”。 在图形出现在游戏窗口之前,它必须有一个负的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)

Tags: import图形游戏消息apple屏幕def错误
1条回答
网友
1楼 · 发布于 2024-10-03 09:07:35

使用^{}时,a必须小于或等于b

apple.y = randint(0, -800)

apple.y = randint(-800, 0)

注意,randint返回一个随机整数N,这样a <= N <= b


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

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

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

相关问题 更多 >