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