TypeError:无法散列的类型“ Vector”

问题描述

我正在做吃豆子游戏,遇到一个叫做Vector的对象的问题。我有矢量类型的常量(上,下,左和右),希望它们成为字典中的键。

vector.py:

from math import sqrt

class Vector(object):
    def __init__(self,x=0,y=0):
        self.x = x
        self.y = y
        self.error = 0.00001

    def __str__(self):
        return '<'+str(self.x)+','+str(self.y)+'>'

    def __add__(self,other):
        return Vector(self.x+other.x,self.y+other.y)

    def __sub__(self,other):
        return Vector(self.x-other.x,self.y-other.y)

    def __neg__(self):
        return Vector(-self.x,-self.y)

    def __mul__(self,scalar):
        return Vector(self.x*scalar,self.y*scalar)

    def __div__(self,scalar):
        if scalar!=0:
            return Vector(self.x/scalar,self.y/scalar)
        return None

    def __truediv__(self,scalar):
        return  self.__div__(scalar)

    def asInt(self):
        return int(self.x),int(self.y)

    def asTuple(self):
        return (self.x,self.y)

    def __eq__(self,other):
        if (self.x-other.x)<self.error and (self.y-other.y)<self.error:
            return True
        return False

    def magnitudeSquared(self):
        return self.x**2+self.y**2

    def magnitude(self):
        return sqrt(self.magnitude())

    def hash(self):
        return id(self)

    def dot(self,other):
        return self.x*other.x,self.y*other.y

    def normalize(self):
        if self.magnitude()!=0:
            return self.__div__(self.magnitude())
        return None

    def copy(self):
        return Vector(self.x,self.y)

nodes.py:

from vector import Vector
from constants import *
import pygame

class Node(object):
    def __init__(self,row,col):
        self.row,self.col = row,col
        self.position = Vector(col*BOTWIDTH,row*BOTWIDTH)
        self.neighbour = {UP: None,DOWN: None,LEFT: None,RIGHT: None}

    def render(self,screen):
        for n in self.neighbour.keys():
            if self.neighbour[n]:
                start = self.position
                end = self.neighbour[n].position
                pygame.draw.line(screen,WHITE,start,end,5)
                pygame.draw.circle(screen,RED,self.position.asInt(),12)

class NodeGroup(object):
    def __init__(self):
        self.nodes = []

    def setupNodes(self):
        nodeA = Node(5,5)
        nodeB = Node(5,10)
        nodeC = Node(10,5)
        nodeD = Node(10,10)
        nodeE = Node(10,13)
        nodeF = Node(20,5)
        nodeG = Node(20,13)
        nodeA.neighbors[RIGHT] = nodeB
        nodeA.neighbors[DOWN] = nodeC
        nodeB.neighbors[LEFT] = nodeA
        nodeB.neighbors[DOWN] = nodeD
        nodeC.neighbors[UP] = nodeA
        nodeC.neighbors[RIGHT] = nodeD
        nodeC.neighbors[DOWN] = nodeF
        nodeD.neighbors[UP] = nodeB
        nodeD.neighbors[LEFT] = nodeC
        nodeD.neighbors[RIGHT] = nodeE
        nodeE.neighbors[LEFT] = nodeD
        nodeE.neighbors[DOWN] = nodeG
        nodeF.neighbors[UP] = nodeC
        nodeF.neighbors[RIGHT] = nodeG
        nodeG.neighbors[UP] = nodeE
        nodeG.neighbors[LEFT] = nodeF
        self.nodes = [nodeA,nodeB,nodeC,nodeD,nodeE,nodeF,nodeG]

    def render(self,screen):
        for node in self.nodes:
            node.render(self.screen)

它返回错误

“ strong> init

中的文件“ /Users/user/Documents/TheProject/node.py”,第9行
self.neighbour = {UP: None,RIGHT: None}

TypeError:不可哈希类型:'Vector'

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...