这是我与主循环的游戏功能。问题是,我的计时器包含了clock.tick(60)
,从100(指定值)开始倒计时,但速度慢于秒(约2秒)。如果没有clock.tick(60)
,倒计时从100开始,然后立即跳到97,然后以略长于秒的速度继续。我所知道的是,除了clock.tick(60)之外,milliseconds += clock.tick(60)
也有效果。有没有办法确保倒计时从100秒开始,并且有第二个间隔,而不会对急需的fps产生负面影响?如果需要更多这样的代码,我很乐意提交
注意:我已经删除了这篇文章中一些不必要的细节,因此似乎没有使用全局变量
def Gameplay():
global P1_sped
global P1_speed
global P2_sped
global P2_speed
global Touch
global player1_Lives
global player2_Lives
global P1_score
global P2_score
global done
Player_1_turn = True
Player_2_turn = False
P1_Turns = 3
P2_Turns = 3
resetTimer = 100
clock = pygame.time.Clock()
seconds = 100
milliseconds = 0
P1_sped = P1_sped + 1
P1_speed = P1_speed - 1
print(P1_sped, "sped")
print(P1_speed, "speed")
Countdown = True
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# Keydown movement bindings
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
P2.changespeed(P2_speed, 0)
if event.key == pygame.K_RIGHT:
P2.changespeed(P2_sped, 0)
if event.key == pygame.K_UP:
P2.changespeed(0, P2_speed)
if event.key == pygame.K_DOWN:
P2.changespeed(0, P2_sped)
if event.key == pygame.K_a:
P1.changespeed(P1_speed, 0)
if event.key == pygame.K_d:
P1.changespeed(P1_sped, 0)
if event.key == pygame.K_w:
P1.changespeed(0, P1_speed)
if event.key == pygame.K_s:
P1.changespeed(0, P1_sped)
# Keyup movement bindings
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
P2.changespeed(P2_sped, 0)
if event.key == pygame.K_RIGHT:
P2.changespeed(P2_speed, 0)
if event.key == pygame.K_UP:
P2.changespeed(0, P2_sped)
if event.key == pygame.K_DOWN:
P2.changespeed(0, P2_speed)
if event.key == pygame.K_a:
P1.changespeed(P1_sped, 0)
if event.key == pygame.K_d:
P1.changespeed(P1_speed, 0)
if event.key == pygame.K_w:
P1.changespeed(0, P1_sped)
if event.key == pygame.K_s:
P1.changespeed(0, P1_speed)
#milliseconds += clock.tick_busy_loop(60) #returns the time since the last time we called the function, and limits the frame rate to 60FPS
milliseconds += clock.tick(60)
# Applying an update to the sprites
all_sprite_list.update()
# Backgorund colour
screen.fill(D_BLUE)
# Applying the sprites
all_sprite_list.draw(screen)
# The countdown used before the game starts
if Countdown == True:
screen.blit(THREE, (SCREEN_WIDTH/2-53.5,SCREEN_HEIGHT/2-91.5))
pygame.display.update()
time.sleep(1)
screen.fill(D_BLUE)
all_sprite_list.draw(screen)
#pygame.draw.rect(screen, BLACK,(SCREEN_WIDTH/2-250,SCREEN_HEIGHT/2-250,500,500))
screen.blit(TWO, (SCREEN_WIDTH/2-52,SCREEN_HEIGHT/2-88.5))
pygame.display.update()
time.sleep(1)
screen.fill(D_BLUE)
all_sprite_list.draw(screen)
#pygame.draw.rect(screen, BLACK,(SCREEN_WIDTH/2-250,SCREEN_HEIGHT/2-250,500,500))
screen.blit(ONE, (SCREEN_WIDTH/2-39,SCREEN_HEIGHT/2-87))
pygame.display.update()
time.sleep(1)
Countdown = False
if milliseconds > 1000:
seconds -= 1
milliseconds -= 1000
if seconds == 0:
#Countdown = True
P1.reset(500, 500)
P2.reset(800, 500)
if Player_1_turn:
P1_score += 1000
P1_Turns -= 1
Countdown = True
seconds = resetTimer
if P1_Turns == 0:
Text("GameOver Player1", 100, WHITE, SCREEN_WIDTH/2, SCREEN_HEIGHT/2-350)
pygame.display.update()
time.sleep(1)
Player_2_turn = True
Player_1_turn = False
else:
#if Player_2_turn == True or Player_1_turn == False:
P2_score += 1000
P2_Turns -= 1
Countdown = True
seconds = resetTimer
if P2_Turns == 0:
Text("GameOver Player2", 100, WHITE, SCREEN_WIDTH/2, SCREEN_HEIGHT/2-350)
pygame.display.update()
time.sleep(1)
#GameEnd()
CharSel()
#print(P1_score, "P1 score")
#print(P2_score, "P2 score")
Text(seconds, 100, WHITE, SCREEN_WIDTH/2, SCREEN_HEIGHT/2-350)
pygame.display.flip()
# Used for fps
clock.tick(60)
StartScreen()
pygame.quit()
在主循环中调用
clock.tick(60)
两次。一次在执行milliseconds += clock.tick(60)
操作的地方,一次在循环结束时。如果您阅读docs,您将看到这个“这个方法应该每帧调用一次。它将计算自上次调用以来经过的毫秒数。如果您传递可选的framerate参数,函数将延迟以使游戏运行速度低于给定的每秒滴答声因此,如果每个循环调用它两次,效果将是使循环以预期帧速率的一半执行。因为你称它为60,这意味着你的帧速率实际上是每秒30帧
此外,返回值是自上次调用以来的时间量。因为您调用了它两次,所以循环顶部的调用将返回自循环底部调用以来的时间,而不是自循环顶部的最后一次调用以来的时间。因此,在你的游戏循环中,只需要计算大约一半的时间,这就解释了为什么你试图计算1秒的价值,但2秒已经过去了
您需要删除对
clock.tick(60)
的两个调用中的一个编辑:
您有
Countdown == True
部分,它在初始启动时使用了3秒。倒计时发生在第一个milliseconds += clock.tick(60)
和下一个clock.tick(60)
之间。这3秒被返回,然后被clock.tick(60)
隐藏,如果没有返回,它们将以3000毫秒的速度返回给milliseconds += clock.tick(60)
,因此导致显示的时间快速下降3秒您可以通过在
if Countdown == True
块内的Countdown = False
之后添加clock.tick()
来隐藏时间损失相关问题 更多 >
编程相关推荐