【问题标题】:Countdown timer for pygamepygame 的倒数计时器
【发布时间】:2013-08-03 11:03:26
【问题描述】:

我之前问过一个关于多重碰撞检测的问题,但我没有创建代码来解决该问题的技能,而是我只是添加第二面墙并尝试放入计时器。

我的问题是如何将计时器放入我的代码中?

它需要倒计时,当它点击00:00时显示文字:GAME OVER并结束游戏。如果有帮助,我将放入我的代码的主线。如果您需要更多代码,例如类等。我很乐意发布它。

while done == False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

            #This makes the player move
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player.changespeed(-6,0)
            if event.key == pygame.K_RIGHT:
                player.changespeed(6,0)
            if event.key == pygame.K_UP:
                player.changespeed(0,-6)
            if event.key == pygame.K_DOWN:
                player.changespeed(0,6)

        #This stops the player from moving
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                player.changespeed(6,0)
            if event.key == pygame.K_RIGHT:
                player.changespeed(-6,0)
            if event.key == pygame.K_UP:
                player.changespeed(0,6)
            if event.key == pygame.K_DOWN:
                player.changespeed(0,-6)

    player.updateWall(wall_list)

    window.fill(black)

    movingsprt.draw(window)
    wall_list.draw(window)

    pygame.display.flip()

    clock.tick(40)

pygame.quit()            

【问题讨论】:

    标签: python pygame pacman


    【解决方案1】:

    就像 Hans Then 所说的,你可以使用pygame.time.set_timer() 方法在经过特定时间后调用函数。但就显示已经过去了多少时间而言,您可能最好使用另一种方法。

    使用 pygame.time.Clock.tick_busy_loop 方法,您可以控制游戏运行的 FPS 并跟踪经过的时间,因为它返回自上次调用以来经过了多少毫秒:

    clock = pygame.time.Clock()
    minutes = 0
    seconds = 0
    milliseconds = 0
    
    while True: #game loop
        #do stuff here
        if milliseconds > 1000:
            seconds += 1
            milliseconds -= 1000
        if seconds > 60:
            minutes += 1
            seconds -= 60
    
        print ("{}:{}".format(minutes, seconds))
    
        milliseconds += clock.tick_busy_loop(60) #returns the time since the last time we called the function, and limits the frame rate to 60FPS
    

    【讨论】:

      【解决方案2】:

      您可以在一段时间后生成事件。见:http://www.pygame.org/docs/ref/time.html#pygame.time.set_timer

      使用与您现在的代码类似的代码,您可以查看该事件是否为计时器事件并采取相应措施。

      【讨论】:

        【解决方案3】:

        你可以这样做

        time = 100 #time in seconds
        pygame.time.set_timer(USEREVENT+1, 1000)#1 second is 1000 milliseconds 
        
        #everything below this line should be in your main loop
        for event in pygame.event.get():
            if event.type = USEREVENT+1:
                time -= 1
        
        if time == 0:
            print "GAME OVER"
        

        【讨论】:

          【解决方案4】:

          我不确定您是否找到了答案,或者就此而言,这是否会有所帮助...我也在努力为游戏添加计时器,但我也有一些问题...对于初学者,我在下面的代码中取得了成功。 (我从另一个留言板上得到了这个,但我似乎无法再次找到它,所以我很抱歉此时没有给予信任 - 我会在找到该网站后编辑这篇文章。

          我将这段代码用于在控制台窗口中显示计数并将其修改为在屏幕上显示。

          我在计时器和滴答声方面收效甚微(出于某种原因,我的程序似乎在运行无限循环......无论如何:

          这是我正在使用的:

          import time
          import threading
          import pygame, sys
          #among others....
          
          #timer clock
          global timerrun #I added this to show / hide the clock from my main screen
          timerrun = 0 #just a default 
          
          class Timer (threading.Thread):
              def __init__(self,seconds):
                  self.runTime = seconds
                  threading.Thread.__init__(self)
              def run(self):
                  time.sleep(self.runTime)
          
          class CountDownTimer(Timer):
              def run(self):
                      counter = self.runTime
                      for sec in range(self.runTime):
                          global timerrun
                          timerrun = 1 #set timer is running global variable - tells main loop to display clock
                          global timedisp #set timedisplay as global var so it can be shared between threads
                          timedisp = clockfont.render(str(counter),1,(255,255,255))
                          #print str(counter)
                          time.sleep(1.0)
                          counter -= 1
          
          class CountDownExec(CountDownTimer):
                  def __init__(self, seconds, action):
                      self.action = action
                      CountDownTimer.__init__(self, seconds)
                  def run (self):
                      CountDownTimer.run(self)
                      self.action()
          
          def myAction():
              #print "Perform an action here... say GAME OVER!"
              global timedisp
              timedisp = clockfont.render("GAME OVER",1,(255,255,255))
          
          c = CountDownExec(timeonclock,myAction)
          

          一些注意事项: 首先,我不能 100% 确定这一切的作用……本质上,第一部分设置了一个计时器功能。第二部分设置计时器的变量(计数和延迟......现在,为了简单起见,我使用 1 秒增量。)

          下一部分(我认为)告诉这个函数在一个新线程内运行 - 所以它以自己的速度独立于我的主循环运行(即,它不会一次暂停我的游戏 1 秒)

          最后一部分是我想要在时钟达到零时发生的事情......我重新设置全局 timedisp 变量说“GAME OVER”

          另外,我不知道这种方法对系统的影响有多大。我不知道这是否“有效” - 我希望有人对此提出意见。

          在这些函数之前,我将字体定义为:

          clockfont = pygame.font.SysFont("#FONT_NAME",SIZE) #font to use for timer clock
          

          在我的主循环中,我正在显示结果:

          if timerrun == 1:
                  clockpos = (screenwidth - timedisp.get_width()) /2 #center my clock
                  screen.blit(timedisp,(clockpos,100)) #put it on screen
          

          再次,我使用 timerrun 变量是因为我希望时钟(在我的游戏中)是可选的......为了测试,时钟不会显示,直到你按下键盘上的“t”键...... 在新线程中执行计时器:

              if event.key==K_t:
                  c.start();
          

          然后它会显示时钟,倒计时,完成后显示“GAME OVER”。

          如果有人可以提供帮助,这是我的问题...

          我不确定通过全局变量传递更改的文本是否是执行此操作的“正确”方式.. 谁能告诉我是否/如何使用这两个线程更新屏幕显示的更好方法。

          我希望时间到了时发生的动作(显示“游戏结束”的部分 - 在主线程中执行一些函数(我说对了吗?)

          例如:当时间用完时,它会运行一个函数来比较两个玩家的分数并宣布获胜者。 - 如何让“线程 2”(计时器)在“线程 1”(游戏的其余部分)中调用函数??

          最后,我正在寻找一种方法来“重置”时钟而不重置整个程序。当我再次按“t”时,我得到一个错误,你不能重新开始一个线程。任何人都可以提供有关如何重新启动我的时钟的建议吗?

          非常感谢!

          【讨论】:

            猜你喜欢
            • 2015-08-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-03-06
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多