1

I've been working on a Snake game in python 3.8 with PyGame as a side project. I've got all of the basics down, the only thing I am having trouble with is adding a score tracker.

def main():
    global width, rows, s, snack
    title()
    width = 500
    rows = 20
    win = pygame.display.set_mode((width, width)) # Creates the screen, sets resolution
    s = snake((0,255,0), (10,10))    # Sets snake colour to green and starting position
    snack = cube(randomSnack(rows, s), colour=(255,0,0))
    score = 0
    flag = True

    white = (255, 255, 255)

    message = score
    font = pygame.font.Font('Fonts/PressStart2P-vaV7.ttf', 30)
    text = font.render(str(message), True, (255, 255, 255))
    win.blit(text, (120,30))
    pygame.display.flip()

    clock = pygame.time.Clock()    # Creates clock object that can change refresh rate

    while flag:
        pygame.time.delay(50)    # Delays by 50 ms, makes it so snake cannot move 10 blocks a second
        clock.tick(10)    # Sets max refresh rate
        s.move()
        if s.body[0].position == snack.position:
            s.addCube()
            score += 1
            print(score)
            snack = cube(randomSnack(rows, s), colour=(255,0,0))    # Randomizes snack position and sets colour as red

        for x in range(len(s.body)):    
            if s.body[x].position in list(map(lambda z:z.position,s.body[x+1:])):   # This checks if you die
                print('Score: ', len(s.body))
                s.reset((10,10))
                break

The score is in the while flag part, It pops up for a brief moment at the start then disappears. Spacing is a bit messed up on the question but the actual code works fine. The score just flashes and goes away

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
TooDqrk46
  • 11
  • 2
  • 1
    Your code to display the score is outside the while loop - `text = font.render(str(message), True, (255, 255, 255)); win.blit(text, (120,30))`. It needs to be called both before and inside the while loop, perhaps my putting it into a separate function and calling it from both places. Alternatively adjust the other parts of your code that show the snake so that they also show the updated score. – Stuart Jan 17 '20 at 22:57
  • inside `while` loop you have to draw/blit all elements - snake, snack, score - and later send buffer on screen using `pygame.display.flip()` – furas Jan 17 '20 at 23:18

1 Answers1

0

You have to do the drawing in the main application loop. The main application loop continuously redraws the entire scene. The main application loop has to do the following:

  • handle the events and change game sates dependent on the events and time
  • clear the display
  • draw the scene
  • update the display

Create the initial score Surface before the main application loop. Create a new score Surface when the score has changed. Draw the sore in the main application loop:

def main():
    global width, rows, s, snack
    title()
    width = 500
    rows = 20
    win = pygame.display.set_mode((width, width)) # Creates the screen, sets resolution
    s = snake((0,255,0), (10,10))    # Sets snake colour to green and starting position
    snack = cube(randomSnack(rows, s), colour=(255,0,0))
    score = 0
    flag = True
    white = (255, 255, 255)

    # create score surface
    message = score
    font = pygame.font.Font('Fonts/PressStart2P-vaV7.ttf', 30)
    text = font.render(str(message), True, (255, 255, 255))

    clock = pygame.time.Clock()    # Creates clock object that can change refresh rate

    while flag:
        pygame.time.delay(50)    # Delays by 50 ms, makes it so snake cannot move 10 blocks a second
        clock.tick(10)    # Sets max refresh rate

        # handle the events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                flag = False

        s.move()
        if s.body[0].position == snack.position:
            s.addCube()
            score += 1

            # update score surface
            message = score
            text = font.render(str(message), True, (255, 255, 255))

            print(score)
            snack = cube(randomSnack(rows, s), colour=(255,0,0))    # Randomizes snack position and sets colour as red

        for x in range(len(s.body)):    
            if s.body[x].position in list(map(lambda z:z.position,s.body[x+1:])):   # This checks if you die
                print('Score: ', len(s.body))
                s.reset((10,10))
                break

        # clear the display
        # [...]

        # draw the score
        win.blit(text, (120,30))

        # draw the snake etc.
        # [...]

        pygame.display.flip()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174