我正在研究如何利用pygame的图形资料,并在制作雪花发生器时遇到问题。它的作用是随机选择屏幕的一部分并在其中放置一个白色圆圈,并将该圆圈的坐标添加到列表中以重绘下一帧。雪花都以恒定的速度向下移动,一旦它们到达底部,我想在屏幕上方略微重绘它们。当我尝试这样做时,我收到此错误:
File "C:\Users\User\Pokemon game\Pokemon-game\snowflakes stuff.py", line 33, in <module>
y_coord=randint(-10, -50)
File "C:\Python36-32\lib\random.py", line 221, in randint
return self.randrange(a, b+1)
File "C:\Python36-32\lib\random.py", line 199, in randrange
raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
ValueError: empty range for randrange() (-10,-49, -39)
[Finished in 1.7s with exit code 1]
这是我的代码:
import pygame
from random import *
pygame.init()
Black=(0, 0, 0)
White=(255, 255, 255)
size=(800, 600)
game_display=pygame.display.set_mode(size)
pygame.display.set_caption('Snowflakes')
closed=False
clock=pygame.time.Clock()
pygame.mouse.set_visible(False)
snow=[]
while not closed:
for event in pygame.event.get():
if event.type==pygame.QUIT:
close=True
game_display.fill(Black)
x_coord=randint(0, 800)
y_coord=randint(0, 600)
snow.append([x_coord, y_coord])
for i in range(len(snow)):
pygame.draw.circle(game_display, White, snow[i], 2)
snow[i][1]+=1
if snow[i][1]>600:
x_coord=randint(0, 800)
y_coord=randint(-10, -50)
snow[i][0]=x_coord
snow[i][1]=y_coord
pygame.display.flip()
clock.tick(60)
pygame.quit()
quit()