Python Forum

Full Version: Copying classes in a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey everyone

So I am getting back into doing some python and I wrote a simple maze game with rooms in pygame. It is very simple with everything being drawn in 20x20 square blocks, so the player is a white block, the bad guys are different colour blocks, and the gold is a G and the pistol a p. The game works, I can move around and collect stuff, shoot bad guys, do the collision detection. I even changed it to use the lovely state engine by metalburr - after reading the code and his explanation it was clear that my titles class would kinda equate to his menu class and my game to his game, so I hacked it out and got it working.

The issue I have is that the room, bad_guy, weapon and gold items are classes in the room class, and I have a list of rooms I created with a simple room/map editor and saved off. So it loads, and the game works, but then, when it restarts, it is using the map which has had items collected and bad guys killed. I tried using:

self.the_map_copy = self.the_map[:]
and

self.the_map_copy = self.the_map.deepcopy()
and even tried iterating through the map and creating new rooms and appending them to the copy. The problem is that room.clone() doe stuff like:

r = room(self.x, self.y, self.exits, self.color)
		r.number = self.number
		r.bad_guy = self.bad_guy
		r.weapons = self.weapons
		r.weapon = self.weapon
		r.health = self.health
		r.is_start_room = self.is_start_room
		r.visited = self.visited
		r.north = self.north
		r.south = self.south
		r.east = self.east
		r.west = self.west
		r.gold = self.gold
		r.icon = self.icon
Which doesn't work (I think it is the pass by reference issue.)

So how do I make a copy of the data other than reloading it from disk and doing it that way? Do I need a clone function for everything in the room that is a class and do:

r = room(self.x, self.y, self.exits, self.color)
		r.number = self.number
		r.bad_guy = self.bad_guy.clone()
		r.weapon = self.weapon.clone()
what is the best way forwards?

I have put the code in a bitbucket repo. Requires pygame and python 2.7. run maze_game.py to play. crs keys to move, space to fire, control and down to select weapon (when collected)

Not really finished, just looking for comments, advice, and improvements

https://bitbucket.org/marienbad/maze_game/src/master/
There are two ways to do this that I can think of. One is to have clone methods for everything (although note that copy(obj) calls obj.__copy__, so you usually override the latter method). Another is to have reset methods for everything. So you have a general reset method that puts the gold back, and the pistol back, and so on. It also calls the specific reset methods. So the bad guy might have a reset method that gives him full hit points, and the gun might have one that reloads it. I generally use the reset methods for starting over, and the __copy__ method for AI search trees.