无法解压缩Python中的非iterable方法对象错误

无法解压缩Python中的非iterable方法对象错误,python,pygame,typeerror,Python,Pygame,Typeerror,我正在为Python中的*搜索构建一个可视化工具,在尝试计算节点的g分数时遇到了以下错误 line 115, in g x1, y1 = s TypeError: cannot unpack non-iterable method object 见下面的代码: from graphics import * import pygame, sys import math import numpy as np from queue import PriorityQueue pyga

我正在为Python中的*搜索构建一个可视化工具,在尝试计算节点的g分数时遇到了以下错误

    line 115, in g
    x1, y1 = s
TypeError: cannot unpack non-iterable method object
见下面的代码:

from graphics import *
import pygame, sys
import math
import numpy as np
from queue import PriorityQueue

pygame.init()

# #set the screen size and captions
size = (500, 500)
rows = 50
margin = 1
screen = pygame.display.set_mode(size)
pygame.display.set_caption("A* Pathfinding")
screen.fill((173, 172, 166))

#define colors
aqua = (0, 255, 255)
black = (0, 0, 0)
blue = (0, 0, 255)
green = (0, 128, 0)
red = (255, 0, 0)
white = (255, 255, 255)
purple = (128, 0, 128)

#initialize PriorityQueue
q = PriorityQueue()



#define all possible states of each node
class Node:
    def __init__(self, row, col, width):
        self.row = row
        self.col = col
        self.x = row*width
        self.y = col*width  
        self.color = white
        self.width = width
        self.neighbors = [(col+1, row), (col-1, row), (col, row+1), (col, row-1)]
        #self.neighbors = []
    
    def get_pos(self):
        return self.row, self.col
    
    def is_closed(self):
        return self.color == red
    
    def is_open(self):
        return self.color == aqua
    
    def is_barrier(self):
        return self.color == black
    
    def is_start(self):
        return self.color == green
    
    def is_end(self):
        return self.color == blue
    
    def reset(self):
        return self.color == white
    
    def close(self):
        self.color = red
    
    def open_node(self):
        self.color = blue
    
    def barrier(self):
        self.color = black
    
    def start(self):
        self.color = green
    
    def end(self):
        self.color = aqua
        
    def path(self):
        self.color = purple

    


def init_grid():
    board = []
    for i in range(rows):
        board.append([])
        for j in range(rows):
            node = Node(i, j, size[0]/rows)
            board[i].append(node)
    return board


#heuristic
def h(c, end):
    x1, y1 = c
    x2, y2 = end
    return abs(x1 - x2) + abs(y1 - y2)


#distance between current node and start node
def g(c, s):
    x1, y1 = s
    x2, y2 = c
    return abs(x1 - x2) + abs(y1 - y2)



    

board = init_grid()


barrier = []
frontier = {}
path = {} 


#starting conditions using Node class and associated methods
start = board[5][5]
goal = board[30][35]
current = start
count = 0

q.put(0, (start))


#main game loop
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
            
        #allows user to draw barriers
        elif pygame.mouse.get_pressed()[0]:
            pos = pygame.mouse.get_pos()
            y_pos = int(pos[0] // (board[0][0].width + margin))
            x_pos = int(pos[1] // (board[0][0].width + margin))
            board[x_pos][y_pos].barrier()
            barrier.append(board[x_pos][y_pos].get_pos())
    
    #draw the grid
    for i in range(rows):
        for j in range(rows):
            color = board[i][j].color
            #print(board[i][j].color)
            if board[i][j] == start:
                board[i][j].start()
            elif board[i][j] == goal:
                board[i][j].end()
             
            

            pygame.draw.rect(screen, color, [j*(board[0][0].width + margin), i*(board[0][0].width + margin), board[0][0].width, board[0][0].width])
   
    
    #game loop
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_SPACE:
            while not q.empty():
                for neighbor in current.neighbors:
                    g_temp = g(current.get_pos, start.get_pos) + 1
                    if g_temp < g(neighbor, start.get_pos()): 
                        f = g_temp + h(current.get_pos(), goal.get_pos())
                        if neighbor not in frontier:
                            q.put((f, neighbor))
                            frontier.add(neighbor)
                            board[neighbor[0]][neighbor[1]].open_node()
                
                
                if current != goal:
                    new = q.get()[1]
                    path.add(current)
                    current = board[new[0]][new[1]]
                
                if current == goal:
                    break


    pygame.display.flip()
        
pygame.quit()
从图形导入*
导入pygame,sys
输入数学
将numpy作为np导入
从队列导入优先级队列
pygame.init()
##设置屏幕大小和标题
大小=(500500)
行数=50
保证金=1
screen=pygame.display.set_模式(大小)
pygame.display.set_标题(“A*寻路”)
屏幕填充((17317216))
#定义颜色
水=(0,255,255)
黑色=(0,0,0)
蓝色=(0,0255)
绿色=(0,128,0)
红色=(255,0,0)
白色=(255,255,255)
紫色=(128,0,128)
#初始化优先级队列
q=优先级队列()
#定义每个节点的所有可能状态
类节点:
定义初始值(自身、行、列、宽度):
self.row=行
self.col=col
self.x=行*宽度
self.y=列*宽度
self.color=白色
self.width=宽度
self.neighbories=[(列+1,行),(列-1,行),(列,行+1),(列,行-1)]
#self.neights=[]
def获取位置(自身):
返回self.row,self.col
def已关闭(自身):
返回self.color==红色
def打开(自身):
返回self.color==aqua
def是_屏障(自身):
返回self.color==黑色
def为自动启动(自):
返回self.color==绿色
def为_端(自身):
返回self.color==蓝色
def重置(自):
返回self.color==白色
def关闭(自我):
self.color=红色
def open_节点(自身):
self.color=蓝色
def屏障(自):
self.color=黑色
def启动(自):
self.color=绿色
def端(自身):
self.color=aqua
def路径(自身):
self.color=紫色
def init_grid():
董事会=[]
对于范围内的i(行):
董事会。追加([]))
对于范围内的j(行):
节点=节点(i,j,大小[0]/行)
板[i]。追加(节点)
返回板
#启发性
def h(c,结束):
x1,y1=c
x2,y2=结束
返回abs(x1-x2)+abs(y1-y2)
#当前节点和开始节点之间的距离
def g(c,s):
x1,y1=s
x2,y2=c
返回abs(x1-x2)+abs(y1-y2)
board=init_grid()
屏障=[]
前沿={}
路径={}
#使用节点类和关联方法的启动条件
开始=电路板[5][5]
目标=董事会[30][35]
电流=启动
计数=0
q、 put(0,(开始))
#主游戏循环
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
sys.exit()
#允许用户绘制障碍
elif pygame.mouse.get_pressed()[0]:
pos=pygame.mouse.get_pos()
y_pos=int(pos[0]//(板[0][0]。宽度+边距))
x_pos=int(pos[1]/(板[0][0]。宽度+边距))
板[x_位置][y_位置].barrier()
barrier.append(board[x_pos][y_pos].get_pos())
#画网格
对于范围内的i(行):
对于范围内的j(行):
颜色=电路板[i][j]。颜色
#打印(板[i][j]。颜色)
如果板[i][j]==开始:
板[i][j].start()
elif董事会[i][j]==目标:
董事会[i][j].完()
pygame.draw.rect(屏幕,颜色,[j*(棋盘[0][0]。宽度+边距),i*(棋盘[0][0]。宽度+边距),棋盘[0][0]。宽度,棋盘[0][0]。宽度])
#游戏循环
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_空间:
而不是q.empty():
对于current.Neights中的邻居:
g_temp=g(current.get_pos,start.get_pos)+1
如果g_temp
我已经试过调试这个问题,但我不明白为什么它会给我这个错误。该函数应该接受两个元组参数,并返回一个整数作为g-score,我相信这就是我在游戏循环中传递的结果


请让我知道,如果有一些明显的错误,我错过了这里,我仍然是新的编程。非常感谢您的时间和帮助

您有一段很长的代码

似乎没有将元组传递给g()

我发现了至少一个错误:必须在方法调用后添加“()”,否则将方法名而不是方法值传递给g()

代码中的示例:

g\u temp=g(current.get\u pos,start.get\u pos)+1

应该是:

g_temp=g(current.get_pos(),start.get_pos())+1

这可能就是问题所在。 我没有检查所有其他行,因为这段代码很长;)
测试并返回告诉我们。

您正在发送
g_temp=g(current.get_pos,start.get_pos)+1
此消息,并希望值在
x1,y1=s
中解包。这是要解包的列表还是元组?我希望你理解x1,y1=s并不意味着x1=s和y1=s。