abc1234
abc1234

Reputation: 260

How to stop timer tick

In my code, I have a timer called timer. I started it somewhere in the game by doing timer.tick(). I was wondering if there is any kind of command that can stop the timer from recording time anymore and just stop. Because if I just set it to zero it keeps going, which I have tried. Is there a command like timer.stop(). I have tried many things such as looking online for this, and settings it to zero, etc..

Thank you EDIT_______________________

This is the variables before main loop

clock = pygame.time.Clock()
FPS = 60
timer = pygame.time.Clock()
storydelay = 5000
time_count = 0

After main loop

if story:

    timer.tick()
    time_count += timer.get_time()

    if time_count < storydelay:
        mainmenu = False
        screen.blit(black_image,zero_position)
    if time_count > storydelay:
        mainmenu = True

Basically what I want to do is when the time_count > storydelay, the timer should stop, so I can recall it back some other time

Upvotes: 1

Views: 1711

Answers (1)

DCA-
DCA-

Reputation: 1292

Because timer.get_time() returns the time from the last call to timer.tick(), all you have to do is set the timer_count back to 0 and call timer.tick() to reset everything.

Here is an example:

import pygame

clock = pygame.time.Clock()
FPS = 60
timer = pygame.time.Clock()
storydelay = 5000
time_count = 0

story = True
timer.tick()

while story:
    time_count += timer.tick()
    if time_count < storydelay:
        pass
    if time_count > storydelay:
        time_count = 0
        print "story done"
        story = False
    clock.tick(FPS)

main_menu = True
timer.tick()

while main_menu:
    time_count += timer.tick()
    if time_count > 3000:
        time_count = 0
        print "menu done"
        main_menu = False
    clock.tick(FPS)

print "done"

As you can see, before each loop if started; timer.tick() is called and when each loop is closed time_count is reset

Hope this helps.

Upvotes: 2

Related Questions