我正在创建一个基本上显示单词的打字游戏,然后检查您是否输入了单词,然后移动到下一个单词。我希望这一切都在计时器下完成。事情是,计时器和游戏不能同时运行,任何想法如何做到这一点,这里是代码(如果需要将提供更多):
def countdown():
clock = pygame.time.Clock()
done = False
font = pygame.font.Font(None, 25)
frame_count = 0
frame_rate = 60
start_time = 15
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
pygame.quit()
quit()# Flag that we are done so we exit this loop
button("",600,0,200,50,white,white)
# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
# --- Timer going up ---
# Calculate total seconds
total_seconds = frame_count // frame_rate
# Divide by 60 to get total minutes
minutes = total_seconds // 60
# Use modulus (remainder) to get seconds
seconds = total_seconds % 60
# --- Timer going down ---
# --- Timer going up ---
# Calculate total seconds
total_seconds = start_time - (frame_count // frame_rate)
if total_seconds < 0:
total_seconds = 0
# Divide by 60 to get total minutes
minutes = total_seconds // 60
# Use modulus (remainder) to get seconds
seconds = total_seconds % 60
# Use python string formatting to format in leading zeros
output_string = "Time left: {0:02}:{1:02}".format(minutes, seconds)
# Blit to the screen
text = font.render(output_string, True, black)
gameDisplay.blit(text, [600, 0])
pygame.display.update()
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
frame_count += 1
# Limit to 20 frames per second
clock.tick(frame_rate)
# Go ahead and update the screen with what we've drawn.
pygame.display.update()
def gameLoop(level):
pygame.display.update()
if level == '1':
gameDisplay.fill(white)
keyboard()
for turn in range(3):
randWord()
count = 0
while count < len(chars):
if chars[count] == 'a':
key("A",132.5,430,45,40,white)
letter = pygame.K_a
elif chars[count] == 'b':
key("B",342.5,470,45,40,white)
letter = pygame.K_b
elif chars[count] == 'c':
key("C",252.5,470,45,40,white)
letter = pygame.K_c
elif chars[count] == 'd':
key("D",222.5,430,45,40,white)
letter = pygame.K_d
elif chars[count] == 'e':
key("E",207.5,390,45,40,white)
letter = pygame.K_e
elif chars[count] == 'f':
key("F",267.5,430,45,40,white)
letter = pygame.K_f
elif chars[count] == 'g':
key("G",312.5,430,45,40,white)
letter = pygame.K_g
elif chars[count] == 'h':
key("H",357.5,430,45,40,white)
letter = pygame.K_h
elif chars[count] == 'i':
key("I",432.5,390,45,40,white)
letter = pygame.K_i
elif chars[count] == 'j':
key("J",402.5,430,45,40,white)
letter = pygame.K_j
elif chars[count] == 'k':
key("K",447.5,430,45,40,white)
letter = pygame.K_k
elif chars[count] == 'l':
key("L",492.5,430,45,40,white)
letter = pygame.K_l
elif chars[count] == 'm':
key("M",432.5,470,45,40,white)
letter = pygame.K_m
elif chars[count] == 'n':
key("N",387.5,470,45,40,white)
letter = pygame.K_n
elif chars[count] == 'o':
key("O",477.5,390,45,40,white)
letter = pygame.K_o
elif chars[count] == 'p':
key("P",522.5,390,45,40,white)
letter = pygame.K_p
elif chars[count] == 'q':
key("Q",117.5,390,45,40,white)
letter = pygame.K_q
elif chars[count] == 'r':
key("R",252.5,390,45,40,white)
letter = pygame.K_r
elif chars[count] == 's':
key("S",177.5,430,45,40,white)
letter = pygame.K_s
elif chars[count] == 't':
key("T",297.5,390,45,40,white)
letter = pygame.K_t
elif chars[count] == 'u':
key("U",387.5,390,45,40,white)
letter = pygame.K_u
elif chars[count] == 'v':
key("V",297.5,470,45,40,white)
letter = pygame.K_v
elif chars[count] == 'w':
key("W",162.5,390,45,40,white)
letter = pygame.K_w
elif chars[count] == 'x':
key("X",207.5,470,45,40,white)
letter = pygame.K_x
elif chars[count] == 'y':
key("Y",342.5,390,45,40,white)
letter = pygame.K_y
elif chars[count] == 'z':
key("Z",162.5,470,45,40,white)
letter = pygame.K_z
pygame.display.update()
true = True
while true:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == letter:
keyboard()
count += 1
pygame.display.update()
true = False
countdown()
finishLevel()
答案 0 :(得分:2)
使用pygame.time.set_timer(eventid, milliseconds)
。当前版本的pygame中eventid
为24。要同时使用更多计时器,请使用eventid
25,26,依此类推。当计时器关闭时,它会在事件队列中生成pygame.USEREVENT
事件,您可以使用pygame.event.get()
获取该事件,我假设您知道该如何使用。
答案 1 :(得分:1)
你可以更新任意数量的东西并同时绘制尽可能多的东西,但是你需要在同一个循环中。我建议你重新安排一下程序。
class Program:
def __init__(self):
timer = Timer()
game = Game()
def run():
while not done:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
pygame.quit()
timer.handle_event(event)
game.handle_event(event)
timer.update()
game.update()
timer.draw(screen)
game.draw(screen)
class Game:
#will include all game logic
class Timer:
#will include all timer logic
您还可以提取计时器并将更新的ms传递给更新方法,以便稍后Game对象也能够计算基于时间的移动。