我想让玩家在迷宫中一次移动一个街区。你知道吗
我试着用一个时钟和time.time()
,但都没用。你知道吗
这是我的游戏循环:
while self.running:
self.counter += 1
self.clock.tick(self.fps)
if self.counter == self.fps:
self.counter = 0
self.canUpdate = True
这是移动代码:
if self.game.canUpdate:
if pressed_keys[K_DOWN]:
self.moveDown()
self.game.canUpdate = False
def moveDown(self):
if self.canMoveTo(self.gridx, self.gridy+1):
for sprite in self.game.sprites:
if sprite != self:
sprite.y -= self.game.gridSize
self.gridy += 1
print(self.gridy, self.game.canUpdate)
按一次向下箭头gridy
增加到500以上,self.game.canUpdate
保持为真
您可以使用
time.sleep()
用按键事件调用此块,以便下一次按键时,代码执行停止500秒,然后等待下一次按键事件。而且
Counter()
需要算上500,如果你打算做更大的事情,这需要比sleep()
更多的CPU。你知道吗如果要在每次按键时移动一次,应该使用event loop或^{} 。当按下向下键时,带有
key
属性pygame.K_DOWN
的单个pygame.KEYDOWN
事件将被添加到事件队列中。只需检查这个键是否在事件循环中按下,然后移动精灵。你知道吗相关问题 更多 >
编程相关推荐