cannot unpack non-iterable int object в коде, как решить?
Программирую одну игру, но почему-то в коде возникает ошибка:
line 173, in draw_drag
color, type = selected_piece[0]
^^^^^^^^^^^
TypeError: cannot unpack non-iterable int object
Подскажите, как это можно исправить? Вот кусок кода, который может помочь.
class Sprite(pygame.sprite.Sprite):
def __init__(self, x, y, filename):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(filename).convert_alpha()
self.size = self.image.get_size()
self.image = pygame.transform.scale(self.image, (int(self.size[0] // 10), int(self.size[1] // 10)))
self.x = x
self.y = y
self.rect = self.image.get_rect(center=(x, y))
self.image.set_colorkey((255, 255, 255))
balls = pygame.sprite.Group()
balls.add((Sprite(55, 55, 'c.jpg')), (Sprite(145, 55, 'c3.jpg')), (Sprite(235, 55, 'c.jpg')))
def create_board():
board = []
for y in range(8):
board.append([])
for x in range(8):
board[y].append(None)
for x in range(0, 8):
board[0][x] = ('black', 'circle')
for x in range(0, 8):
board[7][x] = ('white', 'circle')
return board
def draw_pieces(screen, board, selected_piece):
sx, sy = None, None
if selected_piece:
piece, sx, sy = selected_piece
for y in range(8):
for x in range(8):
piece = board[y][x]
if piece:
selected = x == sx and y == sy
color, type = piece
for ball in balls:
screen.blit(ball.image, ball.rect)
balls.update()
pos = pygame.Rect(BOARD_POS[0] + x * TILESIZE + 1, BOARD_POS[1] + y * TILESIZE + 1, TILESIZE, TILESIZE)
def draw_selector(screen, piece, x, y):
if piece != None:
rect = (BOARD_POS[0] + x * TILESIZE, BOARD_POS[1] + y * TILESIZE, TILESIZE, TILESIZE)
pygame.draw.rect(screen, (255, 0, 0, 50), rect, 2)
def get_square_under_mouse(board):
mouse_pos = pygame.Vector2(pygame.mouse.get_pos()) - BOARD_POS
x, y = [int(v // TILESIZE) for v in mouse_pos]
try:
if x >= 0 and y >= 0: return board[y][x], x, y
except IndexError:
pass
return None, None, None
def draw_drag(screen, board, selected_piece):
if selected_piece:
piece, x, y = get_square_under_mouse(board)
if x != None:
rect = (BOARD_POS[0] + x * TILESIZE, BOARD_POS[1] + y * TILESIZE, TILESIZE, TILESIZE)
pygame.draw.rect(screen, (0, 255, 0, 50), rect, 2)
color, type = selected_piece[0]
for ball in balls:
screen.blit(ball.image, ball.rect)
balls.update()
pos = pygame.Vector2(pygame.mouse.get_pos())
balls.update()
selected_rect = pygame.Rect(BOARD_POS[0] + selected_piece[1] * TILESIZE,
BOARD_POS[1] + selected_piece[2] * TILESIZE, TILESIZE, TILESIZE)
pygame.draw.line(screen, pygame.Color('red'), selected_rect.center, pos)
return x, y
def main():
pygame.init()
screen = pygame.display.set_mode((800, 800))
board = create_board()
board_surf = create_board_surf()
clock = pygame.time.Clock()
selected_piece = None
drop_pos = None
while True:
piece, x, y = get_square_under_mouse(board)
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
if e.type == pygame.MOUSEBUTTONDOWN:
if piece != None:
selected_piece = piece, x, y
if e.type == pygame.MOUSEBUTTONUP:
if drop_pos:
piece, old_x, old_y = selected_piece
board[old_y][old_x] = 0
new_x, new_y = drop_pos
board[new_y][new_x] = piece
selected_piece = None
drop_pos = None
screen.fill(pygame.Color('black'))
screen.blit(board_surf, BOARD_POS)
draw_pieces(screen, board, selected_piece)
draw_selector(screen, piece, x, y)
drop_pos = draw_drag(screen, board, selected_piece)
pygame.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
Источник: Stack Overflow на русском