【发布时间】:2014-05-16 05:10:08
【问题描述】:
我似乎真的卡在我的程序中的某个点上,希望你能提供帮助。
我基本上是在尝试创建一个简单的热门运动图像游戏。图像被随机放置在一系列“向上点”中的一个,然后玩家需要在图像被放置到新位置之前击中图像。
一切似乎都很好,但我似乎遇到了 pygame.tick 或 python time.sleep() 的奇怪延迟。
我只想说我是一个 python 和 pygame 新手,但我一生都无法理解出了什么问题。到目前为止,根据我的理解,图像应该以 1 秒的间隔移动到新位置。
但有时程序似乎“挂起”或“延迟”,图像似乎卡在原地几秒钟,然后来回奔波,好像试图赶上,然后程序似乎可以工作几秒钟内似乎没问题(通常延迟 1 秒),然后回到努力移动或跟上。
希望这是有道理的 :) 她的代码是带有循环的,所以你可以看到我认为我有问题的地方:
# The values are tuples which hold the top-left x, y co-ordinates at which the
# rabbit image is to be blitted...
uppoints = {1: (70, 70), 2: (250, 70), 3: (430, 70), 4: (600, 70),
5: (70, 250), 6: (250, 250), 7: (430, 250), 8: (600, 250),
9: (70, 430), 10: (250, 430), 11: (430, 430), 12: (600, 430)}
# Get a random location to pop up...
def getpos(ups):
x = random.randint(1, 2)
if x == 1:
up = ups[1]
return up
elif x == 2:
up = ups[2]
return up
uppos = ()
while gamerunning:
uppos = getpos(uppoints)
for event in pygame.event.get():
if event.type == QUIT:
gamerunning = False
if event.type == pygame.MOUSEMOTION:
mpos = pygame.mouse.get_pos()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if rabbits.rect.collidepoint(event.pos):
score += 10
mainwin.fill((0, 0, 0))
scoretxt2 = basefont.render(str(score), True, (0, 0, 0))
mainwin.blit(mainback, (0, 0))
mainwin.blit(rabbits.image, uppos)
mainwin.blit(scoretxt, (70, 30))
mainwin.blit(scoretxt2, (160, 32))
mainwin.blit(livestxt, (600, 30))
mainwin.blit(livestxt2, (690, 32))
pygame.display.flip()
time.sleep(1)
# Check which level the user is by score, and increase the speed of the rabbits as needed...
fpsclock.tick(60)
如果我使用 time.sleep,游戏会以 1 秒间隔的正确速度运行。如果我省略 time.sleep() 并使用 .tick(60),图像会疯狂闪烁,但我确信我仍然可以看到某种延迟。
我试着用谷歌搜索了一下,发现有几页说 pygame 的 .tick() 方法和 python sleep() 方法都有问题,但无法确定这是不是真的。
我真的不知道我在这里做错了什么,所以希望你能帮忙:)
非常感谢!
【问题讨论】: