Home > Software engineering >  Pygame error: TypeError: 'pygame.Surface' object is not subscriptable
Pygame error: TypeError: 'pygame.Surface' object is not subscriptable

Time:07-15

guys! I am trying to create a chess game. The algorithm is ready, but I am struggling with the GUI. I've decided that Pygame is a good library for the purpose of the project. However, I came across this problem, and I cannot find a solution. I am messing around with ways to visualize the pieces and this is what I have come up with so far:

import pygame as p

p.init()
board = [
    [['rook', 'white', False], ['knight', 'white'], ['bishop', 'white'], ['king', 'white', False], ['queen', 'white'],
     ['bishop', 'white'], ['knight', 'white'], ['rook', 'white', False]],
    [['pawn', 'white'], ['pawn', 'white'], ['pawn', 'white'], ['pawn', 'white'], ['pawn', 'white'],
     ['pawn', 'white'], ['pawn', 'white'], ['pawn', 'white']],
    ['_____', '_____', '_____', '_____', '_____', '_____', '_____', '_____'],
    ['_____', '_____', '_____', '_____', '_____', '_____', '_____', '_____'],
    ['_____', '_____', '_____', '_____', '_____', '_____', '_____', '_____'],
    ['_____', '_____', '_____', '_____', '_____', '_____', '_____', '_____'],
    [['pawn', 'black'], ['pawn', 'black'], ['pawn', 'black'], ['pawn', 'black'], ['pawn', 'black'],
     ['pawn', 'black'], ['pawn', 'black'], ['pawn', 'black']],
    [['rook', 'black', False], ['knight', 'black'], ['bishop', 'black'], ['king', 'black', False],
     ['queen', 'black'],
     ['bishop', 'black'], ['knight', 'black'], ['rook', 'black', False]]
]
dict_colors = {"white": "w", "black": "b"}
dict_pieces = {"rook": "R", "knight": "N", "bishop": "B", "king": "K", "queen": "Q", "pawn": "p"}
image_names = ['wp', 'wN', 'wB', 'wR', 'wQ', 'wK', 'bp', 'bN', 'bB', 'bR', 'bQ', 'bK']
images = {}
for piece in image_names:
    images = p.transform.scale(p.image.load(piece   ".png"), (64, 64))


def position_square(position):
    row_p = position[0]
    column_p = position[1]
    row = 0
    column = 0
    for i in range(8):
        if 100   i * 64 <= row_p < 100   (i   1) * 64:
            column = 7 - i
        if 100   i * 64 <= column_p < 100   (i   1) * 64:
            row = 7 - i
    square = [row, column]
    return square


screen = p.display.set_mode((712, 712))
square_side = 512 / 8
for i in range(8):
    for v in range(8):
        current_piece = board[i][v]
        if current_piece != '_____':
            color_for_dict = dict_colors.get(board[i][v][1])
            piece_for_dict = dict_pieces.get(board[i][v][0])
            current_string = str(color_for_dict   piece_for_dict   ".png")
            current_x = 100   ((i * square_side)   ((i   1) * 64)) / 2
            current_y = 100   ((v * square_side)   ((v   1) * 64)) / 2
            current_image = images[current_string] **#THIS IS LINE 53**
            screen.blit(current_image, (current_x, current_y))
running = True
while running:
    for event in p.event.get():
        if event.type == p.QUIT:
            running = False
    screen.fill((0, 0, 0))
    color_1 = p.Color(131, 145, 240)
    color_2 = p.Color(225, 229, 250)
    for i in range(len(board)):
        for v in range(len(board[i])):
            if (i   v) % 2 == 0:
                p.draw.rect(screen, color_2, p.Rect(100   (i * square_side), 100   (v * square_side), square_side,
                                                    square_side))
            else:
                p.draw.rect(screen, color_1, p.Rect(100   (i * square_side), 100   (v * square_side), square_side,
                                                    square_side))
    for event in p.event.get():
        if event.type == p.MOUSEBUTTONUP:
            position = p.mouse.get_pos()
            square = position_square(position)
            print(square)
    p.display.update()

When I try to run this code, I get the following error:

Traceback (most recent call last):
  File "C:\Users\denis\Desktop\Programming\chess\chess_pieces\images\test.py", line 53, in <module>
    current_image = images[current_string]
TypeError: 'pygame.Surface' object is not subscriptable

I would be thankful to anyone who can help me solve the problem!

CodePudding user response:

You need to add the images to the images dictionary instead of assigning each Surface object to the variable images:

for piece in image_names:

    # instead of
    #images = p.transform.scale(p.image.load(piece   ".png"), (64, 64))
    
    # do
    images[piece] = p.transform.scale(p.image.load(piece   ".png"), (64, 64))
  • Related