1

I assigned two variables in the init function of my class. I then set the second variable, self.ents_room, equal to the first variable, self.room, at the beginning of a function inside the class. I then operate on the variable self.ents_room, which is an array of arrays of characters. I do this operation inside a for loop, which essentially takes the position of another object and sets one of the characters in the array of arrays to that object. Weirdly after the for loop is completed the variable self.room has been changed, when I only intend to change self.ents_room.

I have already rewritten the function that is creating the issue as well as renaming variables and the class.

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)

I expect self.room to stay the same after calling addEntities(), but it is currently changing along with self.ents_room. How can I prevent self.room from changing when I call the function addEntities()?

  • `self.ents_room` and `room1` are two names for the same list-of-lists - assignment in Python *never* copies anything. – jasonharper Jan 09 '19 at 22:50
  • I tried removing room1 and directly passing the list-of-lists to the class, but that did not change the behavior of the function. – John C. Jameson Jan 09 '19 at 22:57
  • all your lists are referring to the same list. You have comments which claim "saves a new version of the room array for ..." but the comments are false. Reread jasonharper's comment. A quick fix might be "whatever = list(room)" but I have not tested it. – Kenny Ostrom Jan 09 '19 at 23:13
  • ah, list of list. self.ents_room = [list(x) for x in room] – Kenny Ostrom Jan 09 '19 at 23:35
  • I fixed it by slicing the list to create a new list with a new id – John C. Jameson Jan 10 '19 at 00:10

0 Answers0