Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Copying classes in a list
#1
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/
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copying the order of another list with identical values gohanhango 7 1,165 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Copy List Not Copying BAbdulBaki 3 626 Aug-19-2023, 02:03 AM
Last Post: perfringo
  Theory behind referencing a dictionary rather than copying it to a list sShadowSerpent 2 2,082 Mar-24-2020, 07:18 PM
Last Post: sShadowSerpent
  Getting attributes from a list containing classes midarq 2 2,162 Dec-03-2019, 12:42 AM
Last Post: midarq
  Copying a file Kundan 1 2,097 Aug-21-2019, 09:55 AM
Last Post: snippsat
  Using classes? Can I just use classes to structure code? muteboy 5 5,061 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020