1

I have started to make a platforming game in pygame. In the game, I want to use a 'timewarp' option, which allows the player to go back to a previous point int he game with all the variables being the same as at that point, e.g. if they had not defeated an enemy at that point but defeated it later on, then the enemy should reappear after timewarp.

To do this, I have used a list to store key details about the game every second. All of it seems to work apart from one thing: the enemies do not return to their state at this point.

I have found that this is because when adding a sprite group to a list (in this case enemyList), it replaces all of the enemyLists stored in the previous list with the most current one, so this code:

    self.timewarpList.append([self.currentTime, self.presentLevel.enemyList])
    print(self.timewarpList)

returns this at each second:

[[14, <Group(1 sprites)>]]

[[14, <Group(1 sprites)>], [13, <Group(1 sprites)>]]

[[14, <Group(1 sprites)>], [13, <Group(1 sprites)>], [12, <Group(1 sprites)>]]

[[14, <Group(1 sprites)>], [13, <Group(1 sprites)>], [12, <Group(1 sprites)>], 
[11, <Group(1 sprites)>]]

[[14, <Group(0 sprites)>], [13, <Group(0 sprites)>], [12, <Group(0 
sprites)>],[11, <Group(0 sprites)>], [10, <Group(0 sprites)>]]

[[14, <Group(0 sprites)>], [13, <Group(0 sprites)>], [12, <Group(0 sprites)>],
 [11, <Group(0 sprites)>], [10, <Group(0 sprites)>], [9, <Group(0 sprites)>]]

Does anyone know why this happens? And also how I can fix this so my values don't change? Thanks :)

Rupinder
  • 11
  • 2
  • You need to copy the list to make a proper snapshot. See [this answer for more information](https://stackoverflow.com/a/2612815/7675174). You'll probably need `copy.deepcopy()` in your case. – import random Mar 25 '18 at 21:38
  • You're welcome @Rupinder, you can always answer your own question if you've worked out the answer in case someone stumbles across this question in the future. If you run into memory issues with so many copies, you might need to look at just recording the changes to the environment and then rolling some of them back. – import random Mar 26 '18 at 04:20

0 Answers0