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

时间:2020-10-29 07:52:43

标签: python pacman

我正在做吃豆子游戏,遇到一个叫做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, DOWN: None, LEFT: None, RIGHT: None}

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

0 个答案:

没有答案