四个循环内的变量分配,更改类中的其他变量

时间:2019-01-09 22:47:22

标签: python python-3.x

我在班级的 init 函数中分配了两个变量。然后,在类内部函数的开头,将第二个变量self.ents_room设置为等于第一个变量self.room。 然后,我对变量self.ents_room进行操作,该变量是字符数组的数组。我在for循环内执行此操作,该循环实际上获取了另一个对象的位置,并将该数组中的一个字符设置为该对象。在for循环完成之后,奇怪的是,当我只打算更改self.ents_room时,变量self.room已更改。

我已经重写了创建问题的函数以及重命名变量和类。

room1 = [['x','x','x','x','x'],
        ['x','.','.','.','x'],
        ['x','.','.','.','x'],
        ['x','.','.','.','x'],
        ['x','x','x','x','x']]

#whenever you initialize an entity be sure to add it to the list
entities = []

class Entity:
    def __init__(self, x_pos, y_pos, char):
        #used to store the position of the entity
        self.x_pos = x_pos
        self.y_pos = y_pos

        #used to temporarily store the new position of the entity
        #while it is checked for collisions
        self.new_x_pos = x_pos
        self.new_y_pos = y_pos

        #character that represents the entity
        self.char = char

    #moves the entity based on ints passed to x_move and y_move
    def move(self, x_move, y_move):
        #add the movement amount to the position
        self.new_y_pos = self.y_pos + y_move
        self.new_x_pos = self.x_pos + x_move
        #passes an entity too checkCollision as ToCheck and
        #stops the movement if there is a collision
        if self.checkCollision(self) is True:
            print("collision")
            return
        #if no collision is found then the movement is finalized
        #by setting x_pos and y_pos as equal to new_x_pos and new_y_pos
        self.x_pos = self.new_x_pos
        self.y_pos = self.new_y_pos

    #checks if the entity passed to ToCheck collides
    #with any entities in the array entities
    def checkCollision(self, ToCheck):
        for entity in entities:
            if ToCheck.new_y_pos == entity.y_pos:
                if ToCheck.new_x_pos == entity.x_pos:
                    #if ToCheck's position matches the position of
                    #any entity in entities it returns true because
                    #there was a collision
                    return True
        #called when x or y positions don't match for any entity
        return False

class Level:
    def __init__(self, room):
        #saves room as an internal variable
        self.room = room

        #saves a new version of the room array for use with entities
        self.ents_room = room

        #saves a new version of the room array for concatination
        self.conc_room = room  

    def addEntities(self):
        self.ents_room = self.room
        for entity in entities:
            self.ents_room[entity.y_pos][entity.x_pos] = entity.char
        print(self.room)


house = Level(room1)
calipso = Entity(1, 1, "@")
entities.append(calipso)
joseph = Entity(3,2, "*")
entities.append(joseph)

house.addEntities()
#house.concatinateRoom()
#house.printRoom()

calipso.move(2,1)

我希望在调用addEntities()之后self.room保持不变,但是当前它与self.ents_room一起发生了变化。调用函数addEntities()时如何防止self.room更改?

0 个答案:

没有答案