我在pygame中尝试增量时间,但是当我开始游戏时,增量时间在前两个游戏循环中总是非常高。这是我目前的代码:
app.use(session({
store: new RedisStore({
client: redis_client,
ttl: 28800 // 8 hours
}),
secret: config.redis_session_secret,
resave: false,
saveUninitialized: false,
cookie: {
path: '/',
httpOnly: true,
secure: false,
maxAge: 30 * 60 * 1000 //mins * seconds * milliseconds. session cookie will expire every 30 mins
},
rolling: true
}));
它给了我这个输出:
import sys
import pygame
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([32, 32])
self.image.fill((255, 128, 0))
self.rect = self.image.get_rect()
self.rect.x = 20
self.rect.y = 20
def update(self, dt):
self.rect.x += dt / 10
def draw(self, screen):
screen.blit(self.image, [self.rect.x, self.rect.y])
class GameState():
def __init__(self):
self.player = Player()
def update(self, dt):
self.player.update(dt)
def draw(self, screen):
self.player.draw(screen)
class Control():
def __init__(self):
self.state = GameState()
def main():
pygame.init()
fps = 60
fpsClock = pygame.time.Clock()
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
control = Control()
dt = fps
while True:
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
control.state.update(dt)
control.state.draw(screen)
pygame.display.flip()
dt = fpsClock.tick(fps)
if __name__ == '__main__':
main()
我尝试过更改dt的初始值,但这没有任何作用。我认为这与我的游戏初始化时间过长有关。如果是这样,我该如何解决这个问题?
答案 0 :(得分:2)
尝试在第一个循环开始之前将时钟的创建移动到右边。否则,您看到的第一个抖动是初始化代码的测量。
pygame.init()
fps = 60
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
control = Control()
dt = fps
# start recording clock from here
fpsClock = pygame.time.Clock()
while True:
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
control.state.update(dt)
control.state.draw(screen)
pygame.display.flip()
dt = fpsClock.tick(fps)