mozcelikors
mozcelikors

Reputation: 2744

Resetting pygame's timer

I'm working on a laser tag game project that uses pygame and Raspberry Pi. In the game, I need a background timer in order to keep track of game time. Currently I'm using the following to do this but doesnt seem to work correctly:

pygame.timer.get_ticks()

My second problem is resetting this timer when the game is restarted. The game should restart without having to restart the program and that is only likely to be done with resetting the timer, I guess.

What I need, in brief, is to have a background timer variable and be able to reset it any time in a while loop.

I'm a real beginner to python and pygame, but the solution of this problem will give a great boost to my knowledge and the progress of the project. Any help will be greately appreciated.

Upvotes: 0

Views: 8322

Answers (2)

xor
xor

Reputation: 2698

After pygame.init() is called the pygame timer starts. So suppose you run your program once and then start making different game sessions in the same run then you can use a reference variable to keep track of timer and resetting it.
Example:

#your game
import pygame
from pygame import time as T
#do your stuffs
pygame.init()
while True: #this loop never ends and you want to reset timer in this loop if I am not wrong.
    if(new_session):
        tim_var=T.get_ticks()
        new_session=0

    #do your stuffs
    print 'time elaspsed in this session upto now '+str(T.get_ticks()-tim_var)+' millisecs'

    if(game_ended):
        new_session=1
        #restart_your_new_game by resetting your variables or whatever

Upvotes: 2

svk
svk

Reputation: 5919

You don't necessarily need Pygame for this -- time.time() should work just as well as pygame.time.get_ticks(), though it reports seconds since the Unix Epoch instead of milliseconds since the Pygame was initialized.

You want to measure the time has elapsed since the last reset (or since the game started, which you can think of as the first reset). The two functions you've got both return the time elapsed since some arbitrary reference point. The simplest way to achieve this to keep a variable t0. This variable will hold the time value at the last reset (or game start).

Here's an example that loops infinitely and constantly prints the value of such a timer, resetting it whenever it reaches 3 seconds:

# import the builtin time module, this is always available
import time

# initialize the t0 variable, "starting the stopwatch"
t0 = time.time()

while True:
  # calculate the time since some reference point (here the Unix Epoch)
  t1 = time.time()

  # calculate the difference, i.e. the time elapsed
  dt = t1 - t0

  if dt >= 3:
    print "Three seconds reached, resetting timer"
    t0 = t1
  else:
    print "Time elapsed is", dt, "seconds"

If you want to get to know the object-oriented features of Python, this is a good opportunity for an exercise. The above behaviour can be neatly encapsulated in a Stopwatch class, e.g. with get_seconds() and reset() methods.

If you write such a class, your high-level timing logic code can be very simple and readable:

my_timer = Stopwatch()
print my_timer.get_seconds(), "seconds have elapsed since start/reset"
my_timer.reset()

The details of the underlying implementation are hidden away in the code for the Stopwatch class.

Upvotes: 2

Related Questions