通过tick()方法对类实例的更改不具有永久效果

时间:2014-05-28 21:40:39

标签: python class methods namespaces self

我正在开发一个涉及Vehicle类的游戏。它具有记录位置的mapx和mapy对象,以及还有x和y对象的航点列表。在游戏的每个刻度中,我迭代存储在列表中的所有Vehicle实例并调用方法tick(),这应该将mapx和mapy值1移动到更接近列表中第一个航点的x和y值。该方法的代码如下:

def Tick(self,universe):
        print("tick vehicle")
        print(self.waypoint[0].__dict__)
        if self.mapx > self.waypoint[0].x:
            self.mapx -= 1
            print("L")
        if self.mapx < self.waypoint[0].x:
            self.mapx += 1
            print("R")

        if self.mapy > self.waypoint[0].y:
            self.mapy -= 1
            print("U")
        if self.mapy < self.waypoint[0].y:
            self.mapy += 1
            print("D")

        print("Vehicle:"+str(self.mapx)+","+str(self.mapy))

我发现游戏运行时,Vehicle实例的mapx和mapy值根本没有变化,尽管所有的print()调用都表明正在遵循正确的if / then语句。有没有理由tick()方法不能改变self的属性?

编辑:这是完整的车辆类:

import math

class Vehicle:
    def __init__(self,x,y):
        self.waypoint = []
        self.systems = []

        self.mapx = x
        self.mapy = y

        self.hp = 200
        self.regen = 1

        self.holding = {
            "Iron" : 0,
            "Coal" : 0,
            "Nickel" : 0,
            "RareEarthMetals" : 0,
            "Copper" : 0,
            "Oil" : 0,
            "Soy" : 0,
            "Algae" : 0,
            "Steel" : 0,
            "Stainless" : 0,
            "Machinery" : 0,
            "Robots" : 0,
            "Biotech" : 0,
            "Electronics" : 0,
            "Microchips" : 0,
            "Plastic" : 0,
            "Wire" : 0,
            "ConsumerGoods" : 0,
            "Food" : 0,
            "FastFood" : 0}

    def Tick(self,universe):
        print("tick vehicle")
        print(self.waypoint[0].__dict__)
        print("Vehicle Before:"+str(self.mapx)+","+str(self.mapy))
        if self.mapx > self.waypoint[0].x:
            self.mapx -= 1
            print("L")
        if self.mapx < self.waypoint[0].x:
            self.mapx += 1
            print("R")

        if self.mapy > self.waypoint[0].y:
            self.mapy -= 1
            print("U")
        if self.mapy < self.waypoint[0].y:
            self.mapy += 1
            print("D")

        print("Vehicle After:"+str(self.mapx)+","+str(self.mapy))

        if (self.mapx == self.waypoint[0].x) and (self.mapy == self.waypoint[0].y):
            if self.waypoint[0].type == "temp":
                current = self.waypoint[0]
                self.waypoint.remove(current)
            elif self.waypoint[0].type == "looping":
                current = self.waypoint[0]
                self.waypoint.remove(current)
                self.waypoint.append(current)

        for system in self.systems:
            if (self.mapx == system[0].mapx) and (self.mapy == system[0].mapy):
                if system[1].action == "RX":
                    for planet in system[0].planetList:
                        if planet.storedCommodities[system[1].subject] > 0:
                            quantity = planet.storedCommodities[system[1].subject]
                            planet.storedCommodities[system[1].subject] = 0
                            self.holding[system[1].subject] = quantity

                if system[1].action == "TX":
                    quantity = self.holding[system[1].subject]
                    system[0].planetList[system[1].destination].storedCommodities[system[1].subject] = quantity
                    self.holding[system[1].subject] = 0

        for otherVehicle in universe.vehicleList:
            if math.sqrt( (otherVehicle.mapx - self.mapx)**2 + (otherVehicle.mapy - self.mapy)**2 ) <= 5: self.attack(otherVehicle)

        if self.hp < 200:
            self.hp += self.regen

    def attack(self,otherVehicle):
        pass#pew pew pew

这是调用vehicle.Tick(self)的主循环:

def Tick(self):
        for system in self.systemList:
            system.Tick()

        for vehicle in self.vehicleList:
            vehicle.Tick(self)

1 个答案:

答案 0 :(得分:0)

所以,我正在回答这个问题,因为经过相当多的调查后,我发现这些症状的原因是一个完全不同的问题,我认为不适合这里。当我在每个循环中调用print(vehicle)时,它是一个不同的内存地址,尽管只是一个车辆。我不确定这是否应该是Python的行为,返回对象的副本而不是对象本身,但这就是造成这个问题的原因。

编辑:我是一个巨大的白痴,他没有意识到它造成了一个的区别,我的车辆列表不是一个真正的列表,而是一个来自多处理的列表管理器。很抱歉浪费你的时间。