0

Currently I am doing an assignment for school to create a game in Pygame. I am stuck on the process of creating a clock which determines the; days, hour, and minute.

So far I have created some code but my Pygame crashes when ever I run it. I know this is probably a bit nooby but I would really appreciate some help.

This is my code:

import sys, pygame, random, time
pygame.init()

size = width, height = 1280, 720
screen = pygame.display.set_mode(size)

done = False

Black=0,0,0
White=255,255,255

Time = 0
Minute = 0
Hour = 0
Day = 0

#Fonts
Font = pygame.font.SysFont("Trebuchet MS", 25)

#Day
DayFont = Font.render("Day:"+str(Day),1, Black)
DayFontR=DayFont.get_rect()
DayFontR.center=(985,20)
#Hour
HourFont = Font.render("Hour:"+str(Hour),1, Black)
HourFontR=HourFont.get_rect()
HourFontR.center=(1085,20)
#Minute
MinuteFont = Font.render("Minute:"+str(Minute),1, Black)
MinuteFontR=MinuteFont.get_rect()
MinuteFontR.center=(1200,20)

Clock = pygame.time.Clock()

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

    screen.fill(White)

    #Timer
    while Time==0:
        time.sleep(1)
        Minute=Minute+1
        screen.blit(MinuteFont, MinuteFontR)
        if Minute == 60:
            Hour=Hour+1        
            screen.blit(HourFont, HourFontR)
        if Hour==12:
            Hour=0
            Day=Day+1
            screen.blit(DayFont, DayFontR)

    pygame.display.flip()

    Clock.tick(60)

pygame.quit()
Joshua
  • 35
  • 1
  • 9
  • what line causes the crash? what is the exception? – kmaork Jun 26 '16 at 13:59
  • It doesn't say sorry. Whenever I press run in idle, the program starts normally, then it stops responding and crashes. The screen usually stays black but sometimes displays the proper color white. I hope that helps :/ @Ni. – Joshua Jun 26 '16 at 14:33

2 Answers2

0

Your problem is:

while Time==0: # <--- here
    time.sleep(1)
    # ...    

You initialize Time with 0 but you never change it's value. That causes an endless loop.

  • Thank you so much. I managed to stop the crashing by changing the 'while' to an 'if' statement and for the loop only to happen: if Time<60. I also made it so when the Minute==60, Minute=0. Now I just need the display to show the code. Next part in this mystery really haha – Joshua Jun 26 '16 at 22:55
0

Here's a working solution of the posted initial code. Notice that the clock counter elements formatting code was moved inside the main Pygame loop. Second modification: the blit() instruction is called at each loop iteration, not only within the respective if's. Finally, displaying seconds was added to make the sample more vivid.

I am not an expert in Pygame, so do not consider this solution is an optimal one !

import sys, pygame, random, time
pygame.init()

size = width, height = 1380, 720
screen = pygame.display.set_mode(size)

done = False

Black=0,0,0
White=255,255,255

Time = 0
Second = 0
Minute = 0
Hour = 0
Day = 0


Clock = pygame.time.Clock()


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

    # Fonts
    Font = pygame.font.SysFont("Trebuchet MS", 25)

    # Day
    DayFont = Font.render("Days:" + str(Day).zfill(2), 1, Black)
    DayFontR = DayFont.get_rect()
    DayFontR.center = (975, 20)
    # Hour
    HourFont = Font.render("Hours:" + str(Hour).zfill(2), 1, Black)
    HourFontR = HourFont.get_rect()
    HourFontR.center = (1075, 20)
    # Minute
    MinuteFont = Font.render("Minutes:" + str(Minute).zfill(2), 1, Black)
    MinuteFontR = MinuteFont.get_rect()
    MinuteFontR.center = (1190, 20)
    # Second
    SecondFont = Font.render("Seconds:" + (str(Second).zfill(2)), 1, Black)
    SecondFontR = SecondFont.get_rect()
    SecondFontR.center = (1317, 20)

    screen.fill(White)

    #Timer
#    while Time==0: this caused the crash !
    time.sleep(1)
    Second += 1
    screen.blit(SecondFont, SecondFontR)
    screen.blit(MinuteFont, MinuteFontR)
    screen.blit(HourFont, HourFontR)
    screen.blit(DayFont, DayFontR)

    if Second == 60:
        Second = 0
        Minute=Minute+1
    if Minute == 60:
        Minute = 0
        Second = 0
        Hour=Hour+1
    if Hour==24:
        Hour=0
        Second = 0
        Minutes=0
        Day=Day+1

    pygame.display.flip()

    Clock.tick(60)

pygame.quit()
Jean-Pierre Schnyder
  • 1,572
  • 1
  • 15
  • 24