Alex Firefly
Alex Firefly

Reputation: 45

Font not printing out in pygame program

I'm writing a program in Python that will act as a scratch ticket type of game. I have everything done except printing out the numbers when the user 'scratches off' the square on the ticket. This is my code:

    import pygame
    pygame.init()
    pygame.font.init()

    screen = pygame.display.set_mode((300,450))
    pygame.display.set_caption("test grid")

    background = pygame.Surface(screen.get_size())
    background.fill((209, 95, 238))

    clock = pygame.time.Clock()
    keepGoing = True

    pos = [0, 0]

    x = 40
    for line in range(5):
        y = 200
        for row in range(4):
            pygame.draw.rect(background, (238, 201, 0), (x, y, 40, 40), 0)
            y += 45
        x += 45

    board = [[1, 1, 3, 15, 11],
             [1, 14, 13, 15, 9],
             [2, 6, 7, 15, 5],
             [8, 10, 4, 12, 7]]

    myfont = pygame.font.SysFont("arial", 50)

    while keepGoing:
        clock.tick(30)

        y = 200
        x = 40
        x1 = 40
        x2 = 80
        y1 = 200
        y2 = 240
        num = 0
        label = myfont.render(str(num),1,(255,255,255))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()
                print pygame.mouse.get_pos()
                for row in range(4):
                    for line in range(5):
                        if pos[0] >= x1 and pos[0] <= x2 and pos[1] >= y1 and pos[1] <= y2:
                            pygame.draw.rect(background, (0,0,0), (x,y,40,40), 0)
                            num = board[row][line]
                            label = myfont.render(str(num), 1, (255,255,255))
                            x1 += 45
                        else:
                            x1 += 45
                            x2 += 45
                            x += 45
                    x = 40
                    y += 45
                    y1 += 45
                    y2 += 45
                    x1 = 40
                    x2 = 80

        screen.blit(label,(200,200))         
        screen.blit(background, (0,0))
        pygame.display.flip()

    pygame.quit()

For some reason that I can't figure out, not matter what I do, none of my font stuff will print out. I tried declaring it outside the loop only, I tried declaring it inside the loop only, I tried both together, and for some reason nothing will work, and I can't figure out why my text refuses to print on the screen. Help, please?

My teacher said it was something to do with my x1 and x2 variables, but I checked and they're doing exactly what they should be doing.

(For anyone wondering, the reason I added 45 to x1 even if the statement in the if is true is because that way it won't continue to repeat and print out the number again and again until the for loop is over. Not the greatest solution but the only one I could think of.)

Upvotes: 2

Views: 67

Answers (1)

pppery
pppery

Reputation: 3804

Just before updating the screen with pygame.display.flip, your code runs screen.blit(background,(0,0)), which completely overrides everything you have drawn to the screen. Moving the blitting of the background to the beginning of your while keepGoing loop would solve this.

Upvotes: 2

Related Questions