yerim2
yerim2

Reputation: 83

How to stop timer in pygame when it reaches a certain situation

Hi I'm writing a code for memory game (a.k.a. card matching game) right now and I'm using a timer in pygame to tell how long the player took to make every card uncovered (matched every pair in card deck completely).

self.score = pygame.time.get_ticks() // 1000

this is what I wrote to check the time and I want to make this stop when all the cards are uncovered is there any way that I can make is stop? I've searched a bit but nothing looked suitable for what I need now.

https://www.youtube.com/watch?v=9-6iDRyPoVg&feature=youtu.be this is how it's suppose to look like.

Thank you!

Upvotes: 1

Views: 433

Answers (2)

Kingsley
Kingsley

Reputation: 14924

The function pygame.time.get_ticks() returns the number of milliseconds since pygame.init() was called.

I think a better approach is to remember the time of the first card-click, and then compare this with the time of the last card-click.

For example:

### Card was clicked
if ( game_start_time == 0 ):
    game_start_time = pygame.time.get_ticks()   # game start time

    ...


    if ( cards_left_covered == 0 ):
        # calculate the elapsed time
        game_end_time = pygame.time.get_ticks()
        self.score = ( game_end_time - game_start_time ) // 1000  # seconds

Upvotes: 2

mcropper14
mcropper14

Reputation: 17

If you want the timer to stop, set the miliseconds argument equal to 0. When the cards are all covered you might want to say something like this: cards_flipped_time = (pygame.time.get_ticks() - start_time).

Hopefully that helped

Upvotes: 2

Related Questions