游戏计时器使用Pygame

时间:2015-02-03 23:24:58

标签: python timer pygame

我正在使用Pygame进行反应游戏,并且在计时器方面遇到了一些困难。目前这个代码的功能是,当你启动程序时,你必须点击" a"为倒数计时器启动。当屏幕出现在屏幕上时,您会点击相应的键盘键,从而更新乐谱。我想改变的两件事与计时器有关。我想让计时器自动启动而不必点击" a"按钮,我希望一旦时间到期,分数不会继续计算。

你会看到我以前的一些尝试被注释掉了。在我试图实现自动定时器的所有版本中,定时器会快速关闭,而不是等到秒数之间等待。

import pygame
from pygame.locals import *
import os, sys
import random
import time

time_a = 20
a_on = False
n1 = time.time()
letter = ["c", "b"]

pygame.init()
pygame.display.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Reaction Game")

background = pygame.Surface(screen.get_size())
rect = background.fill((0, 0, 0))

clock = pygame.time.Clock()
score = 0
font = pygame.font.SysFont("monospace", 36)
scoreupdate = font.render(format(score), 1, (255, 255, 255))
x = (random.choice(letter))
xupdate = font.render(format(x), 1, (255, 255, 255))
print x

#mainloop
while True:
  clock.tick(30)
  for event in pygame.event.get():
    if event.type == QUIT:
      sys.exit()


    if event.type == USEREVENT:
      if time_a > 0:
        time_a -= 1
      else:
        break
      #else:
        #pygame.time.set_timer(USEREVENT, 0)

    elif event.type == KEYDOWN:

      if event.key == K_a:
        if not a_on:
          # Set for 1 second (1000 milliseconds)
          pygame.time.set_timer(USEREVENT, 1000)
          a_on = True
        else:
          # The other one should turn on immediately
          pygame.time.set_timer(USEREVENT, 0)
          pygame.time.set_timer(USEREVENT+1, 1000)


      if x == "b" and event.key == K_b:
        n3 = time.time()-n1
        print n3
        score += 1
        n1 = time.time()
        scoreupdate = font.render(format(score), 1, (255, 255, 255))
        print "B button Hit" 
        print score
        x = (random.choice(letter))
        xupdate = font.render(format(x), 1, (255, 255, 255))
        print x

      if x == "c" and event.key == K_c:
        n3 = time.time()-n1
        print n3
        score += 1
        scoreupdate = font.render(format(score), 1, (255, 255, 255))
        n1 = time.time()
        print "C button Hit" 
        print score
        x = (random.choice(letter))
        xupdate = font.render(format(x), 1, (255, 255, 255))
        print x                      


      if event.key == K_PAUSE or event.key == K_p:
        #pause both timers
        pygame.time.set_timer(USEREVENT+1, 0)
        pygame.time.set_timer(USEREVENT, 0)

      #pygame.time.set_timer(USEREVENT, 1000)
      #a_on = True

  # Format time into minutes:seconds
  #pygame.time.set_timer(USEREVENT, 1000)

  time_a_str = "%d:%02d" % (int(time_a/60),int(time_a%60))

  time_a_txt = font.render(time_a_str, 1, (255, 255, 255))
  score_txt = scoreupdate
  letter_txt = xupdate

  letter_rect = letter_txt.get_rect()
  letter_rect.center = (160, 30)
  score_rect = score_txt.get_rect()
  score_rect.center = (30, 30)
  time_a_rect = time_a_txt.get_rect()
  time_a_rect.center = (310, 310)

  screen.blit(background, rect)
  screen.blit(xupdate, letter_rect)
  screen.blit(scoreupdate, score_rect)
  screen.blit(time_a_txt, time_a_rect)
  pygame.display.update()
#end mainloop  

1 个答案:

答案 0 :(得分:2)

我会看this example of a timer。它应该有你需要的东西。