Oct-14-2018, 02:45 PM
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:
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:
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/
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.iconWhich 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/