Python Help about fixing code

I’m coding The ‘Snake Game’ as an assignment from the teacher. This is something I copy paste from the intenet and self-coding, the game had run but it didn’t count scores. I hope somebody will fix it because I’m just a beginner and have no idea where’s wrong`

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

gameSurface = pygame.display.set_mode((735,475))
m = 20
Imgbody = pygame.transform.scale(pygame.image.load('Body.jpg'),(m,m))
Imghead = pygame.transform.scale(pygame.image.load('Head.png'),(m,m))
Imgfood = pygame.transform.scale(pygame.image.load('Covide.jpg'),(m,m))
red = pygame.Color(255,0,0)
Blue = pygame.Color(65,105,255)
Black = pygame.Color(0,0,0)
White = pygame.Color(255,255,255)
Grey = pygame.Color(128,128,128)
snakepos=[100,60]
snakebody =[[100,60],[80,60],[60,60]]
foodx = random.randrange(1,71)
foody = random.randrange(1,45)
if foodx % 2 !=0: foodx +=1
if foody % 2 !=0: foody +=1
foodpos = [foodx * 10, foody * 10]
foodflat = True
direction ='RIGHT'
changeto = direction
score = 0
def show_score(choice = 1):
  sfont = pygame.font.SysFont('conlaso',40)
  ssurf = sfont.render('Score: {0}'.format(score),True,Black)
  srect = ssurf.get_rect()
  if choice == 1:
    srect.midtop = (70,20)
  else:
    srect.midtop = (360,230)
  gameSurface.blit(ssurf,srect)
def game_over():
  gfont = pygame.font.SysFont('conlaso',40)
  gsurf = gfont.render('Game Over!',True,red)
  grect = gsurf.get_rect()
  grect.midtop = (360, 150 )
  gameSurface.blit(gsurf,grect)
  show_score(0)
  pygame.display.flip()
  time.sleep(5)
  pygame.quit()
  sys.exit() 
def game_loop():
  m = 20
  snakepos = [100,60]
  snakebody =[[100,60], [80,60],[60,60]]
  foodx = random.randrange(1,71)
  foody = random.randrange(1,45)
  if foodx % 2 !=0: foodx +=1
  if foody % 2 !=0: foody +=1
  foodpos = [foodx * 10, foody * 10]
  score = 0
  direction = "RIGHT"
  changeto =direction
  running = True
  gameOver= False
  foodflat = True
  Imgbody = pygame.transform.scale(pygame.image.load('Body.jpg'),(m,m))
  Imghead = pygame.transform.scale(pygame.image.load('Head.png'),(m,m))
  Imgfood = pygame.transform.scale(pygame.image.load('Covide.jpg'),(m,m))
  while running:
        if gameOver==True:
            message_to_screen("Game over",red,-50,size="large")
            message_to_screen("Press C to play again or Q to quit",Black,50,size="medium")
            pygame.display.update()
        while gameOver == True:
            for event in pygame.event.get():
                if event.type==pygame.QUIT:
                    gameOver=False
                    running=False
                if event.type==pygame.KEYDOWN:
                    if event.key==pygame.K_q:
                        running=False
                        gameOver=False
                    if event.key==pygame.K_c:
                        game_loop()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            elif event.type == pygame.KEYDOWN:   
              if event.key == pygame.K_RIGHT or event.key == ord('d') : 
                changeto = "RIGHT" 
                if changeto == 'RIGHT' and not direction == 'LEFT':
                  direction = 'RIGHT'
              elif event.key == pygame.K_LEFT or event.key == ord('a'):
                  changeto = "LEFT"
                  if changeto == 'LEFT' and not direction == 'RIGHT':
                    direction = 'LEFT'
              elif event.key == pygame.K_DOWN or event.key == ord('s'):
                  changeto = "DOWN"
                  if changeto == 'DOWN' and not direction == 'UP':
                    direction = 'DOWN'
              elif event.key == pygame.K_UP or event.key == ord('w'):
                  changeto = "UP"
                  if changeto == 'UP' and not direction == 'DOWN':
                    direction = 'UP'
              elif event.key == pygame.K_ESCAPE:
                  pygame.event.post(pygame.event.Event(pygame.QUIT))
        if direction == "RIGHT":
            snakepos[0] += m
        elif direction == "UP":
            snakepos[1] -= m
        elif direction == "DOWN":
            snakepos[1] += m
        elif direction == "LEFT":
            snakepos[0] -= m
        
        snakebody.insert(0, list(snakepos))       
        if snakepos[0] == foodpos[0] and foodpos[1] ==  snakepos[1]:
              score += 1
              foodflat = False
        else:
            snakebody.pop()
        pygame.display.update()
        if foodflat == False:
          foodx = random.randrange(1,71)
          foody = random.randrange(1,45)
          if foodx % 2 !=0: foodx +=1
          if foody % 2 !=0: foody +=1
          foodpos = [foodx * 10, foody * 10]
        foodflat = True
        
        for pos in snakebody:
          gameSurface.blit(Imgbody,pygame.Rect(pos[0],pos[1],m,m))
        gameSurface.blit(Imghead, pygame.Rect(snakebody[0][0],snakebody[0][1],m,m))
        gameSurface.blit(Imgfood, pygame.Rect(foodpos[0],foodpos[1],m,m))
        pygame.draw.rect(gameSurface,Grey,(10,10,715,455),2)
        if snakepos[0] > 710 or snakepos[0] < 0:
          game_over()
        if snakepos[1] > 450 or snakepos[1] < 0:
          game_over()
        for block in snakebody[1:]:
          if snakepos[0] == block[0] and snakepos[1] == block[1]:
            game_over()
        pygame.display.flip()
        gameSurface.fill(White)
        pygame.time.delay(200)
        show_score()
game_loop()

Since your game depends on external media files that are not provided along your question, I can’t run and test it.

However,

what this actually means? Do you mean both the head and the neck of the snake should be eating the food at the same time? Or do you mean either one of them? How about changing it to or instead of and.

I can’t guarantee that would fix the problem since I can’t run the code myself.

I think problem might be that you are changing local variable score inside of the game_loop(), but when show_score() is called, it prints the score that’s in the global frame and which wasn’t changed.