Search code examples
pythonpygameintegerhelper

'int' object is not subscriptable pygame


i wrote my code and it was working yesterday but now it's not, i don't know where it's come from please help :/ i restart my computer, desinstall python and install it again, i don't uderstand

i think my list pos and snake_position are not working

then this is my code :

```import pygame
import time
import random
snake_speed = 60
#fenêtre
window_x = 700
window_y = 500
#Couleurs
black = pygame.Color(0,0,0)
white = pygame.Color(255,255,255)
red = pygame.Color(255,0,0)
green = pygame.Color(0,255,0)
blue = pygame.Color(0,0,255)
pygame.init()
pygame.display.set_caption('Snake Game')
game_window = pygame.display.set_mode((window_x, window_y))
fps = pygame.time.Clock()
snake_position = [100, 50]
#snake body
snake_body = [100, 50]
#food
food_position = [random.randrange(1, (window_x//10)) * 10,
                 random.randrange(1, (window_y//10)) * 10]
food_spawn = True
#direction
direction = ''
change_to = direction
#score
score = 0
#def score
def show_score(choice, color, font, size):
    score_font = pygame.font.SysFont('Verdana', 14)
    score_surface = score_font.render('Score : ' + str(score), True, green)
    score_rect = score_surface.get_rect()
    game_window.blit(score_surface, score_rect)
def game_over():
    
    my_font = pygame.font.SysFont('Verdana',40)
    game_over_surface = my_font.render('Your score is : ' + str(score), True, red)
    
    game_over_rect = game_over_surface.get_rect()
    game_over_rect.midtop = (300,300)
    
    game_window.blit(game_over_surface, game_over_rect)
    pygame.display.flip()
    time.sleep(5)
    pygame.quit()
    
    quit()
while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_z:
                change_to = 'z'
            if event.key == pygame.K_d:
                change_to = 'd'
            if event.key == pygame.K_q:
                change_to = 'q'
            if event.key == pygame.K_s:
                change_to = 's'
    
    if change_to == 'z' and direction != 's':
        direction = 'z'
    if change_to == 's' and direction != 'z':
        direction = 's'    
    if change_to == 'd' and direction != 'q':
        direction = 'd'
    if change_to == 'q' and direction != 'd':
        direction = 'q'
    if direction == 'z':
        snake_position[1] -= 10
    if direction == 's':
        snake_position[1] += 10
    if direction == 'q':
        snake_position[0] -= 10
    if direction == 'd':
        snake_position[0] += 10
   
    snake_body.insert(0, list(snake_position))
    if snake_position[0] == food_position[0] and snake_position[1] == food_position[1]:
        score +=10
        food_spawn = False
    else:
        snake_body.pop()
    if not food_spawn:
        food_position = [random.randrange(1, (window_x//10)) * 10,
                         random.randrange(1, (window_y//10)) * 10]
    
    food_spawn = True
    game_window.fill(black)
    for pos in snake_body:
        pygame.draw.rect(game_window, green, pygame.Rect(pos[0], pos[1], 10, 10))
    pygame.draw.rect(game_window, white, pygame.Rect(food_position[0], food_position[1], 10, 10))
    
    
    if snake_position[0] < 0 or snake_position[0] > window_x-10:
        game_over()
    if snake_position[1] < 0 or snake_position[1] > window_y-10:
        game_over()
    for block in snake_body[1:]:
        if snake_position[0] == block[0] and snake_position[0] == block[1]:
            game_over()
    show_score(1, white, 'Verdana', 20)
    
    
    if event.type == pygame.QUIT:
        pygame.quit()
    pygame.display.update()
    fps.tick(snake_speed)```

and this is my error :

 pygame.draw.rect(game_window, green, pygame.Rect(pos[0], pos[1], 10, 10))
TypeError: 'int' object is not subscriptable

if u can help me it will be nice thx :)


Solution

  • This error is raised because pos is just an integer. So pos[0] does not make sense: an integer is not subscriptable.

    The error comes from the initialisation of snake_body that is supposed to be a list of positions in your code whereas you initialised it as a single position. So you should just initialise it like this:

    snake_body = [[100, 50]]  #  instead of [100, 50]