开发者

How do I reset pygame.time.Clock()?

开发者 https://www.devze.com 2022-12-07 20:15 出处:网络
I made a sudoku game for my introduction to computer programming class and I want to expand it over Winter break by adding some new features. The first of which I want to be a clock. I found some code

I made a sudoku game for my introduction to computer programming class and I want to expand it over Winter break by adding some new features. The first of which I want to be a clock. I found some code on here that helped me implement a clock system on my screen, but now I want the clock to reset when the restart button is pressed and the user goes back to the home screen.

Any help would be greatly appreciated! I am not married to my current code I am more than willing to change it, I am just very new to programming and don't even know where to start with something like this.

Attached is the current code I have to make my clock run and display it to my screen:

How do I reset pygame.time.Clock()?

I tried to "time = 0", and "time = pygame.clock.Time()" hoping that it would initialize the time again and the count would start from 0 but it did not work. I looked for other answers, but I am new to programming and they didn't make much sense to me.

I was expecting the time to be reset to 0.000 and to start counting up again, but it just continued counting from where it left off

Below is the code I currently have that makes my clock and displays it to my game screen:

clock = pygame.time.Clock()                                 # initialize clock
font = pygame.freetype.SysFont(None, 34)                    # font for clock
font.origin = True                                          # makes the font not shake around for whatever 

while True:

    if not game_over:
        screen.fill(pygame.Color(button_words_color), (0, 0, screen_dimension, 50))      
        ticks = pygame.time.get_ticks()
        millis = ticks % 1000
        seconds = int(ticks / 1000 % 60)
        minutes = int(ticks / 60000 % 24)
        out = '{minutes:02d}:{seconds:02d}:{millis}'.format(minutes=minutes, millis=millis, seconds=seconds)
        font.render_to开发者_如何学运维(screen, (5, 35), out, pygame.Color(button_color))  # (5, 35) is where it's displayed)
        pygame.display.flip()
        clock.tick(60)


pygame.time.get_ticks() returns the milliseconds since pygame.init() and cannot be reseted. However you can remember and change the start time:

start_ticks = pygame.time.get_ticks()
while True:
    current_ticks = pygame.time.get_ticks()

    # [...]

    if not game_over:
        ticks = current_ticks - start_ticks
        # [...]

    else:
        start_ticks = current_ticks
        # [...]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号