Why won't my player reset to the same position more than once in Pygame?

Viewed 31

I'm making a complicated (at least for what I've ever done) platformer in pygame. It involves having the player reset to a position when the timer runs out or they get hurt by a spike, but I am having an issue. The player's reset method doesn't work more than once.

In the player's class, I use vector2's for the position and movement:

#Set kinematic constants
        self.HORIZONTAL_ACCELERATION = 3
        self.HORIZONTAL_FRICTION = .5
        self.VERTICAL_ACCELERATION = .5 #Gravity
        self.ORIGINAL_POS = vector(x, y)

        #Set movement vectors
        self.position = vector(x, y)
        self.acceleration = vector(0, 0)
        self.velocity = vector(0, 0)

And in the reset method, I use this code:

self.velocity = vector(0, 0)
self.position = self.ORIGINAL_POS
self.rect.bottomleft = self.position

But for some reason, the ORIGINAL_POS variable doesn't seem to function properly. In my main script, I have a game class that calls this once a spike collides with the player or the timer runs out:

elif collided_tiles[0].index == 7:
               #Spikes, other one is for timer
               self.pause_game("Watch out for spikes!", "Press 'ENTER' to continue")
               self.player.reset()
               self.player.lives -= 1
               self.timer = 100

        if self.timer < 0:
            self.pause_game("Be quicker next time...", "Press 'ENTER' to continue")
            self.player.reset()
            self.player.lives -= 1
            self.timer = 100

I don't know why the code is resetting the ORIGINAL_POS variable, and I can't wrap my head around it. Any help would be very appreciated!

Here is the link to the project.

1 Answers

With self.position = self.ORIGINAL_POS the vector object is not copied. If you assign self.position = self.ORIGINAL_POS then self.position and self.ORIGINAL_POS refer to the same object. From now on, it looks like if one of the vectors changes, the other one changes in the same way, since there is only one vector object named by 2 variables You have to copy the components from self.ORIGINAL_POS to self.position.

e.g.:

self.position.x = self.ORIGINAL_POS.x
self.position.y = self.ORIGINAL_POS.y

or

self.position = vector(self.ORIGINAL_POS.x, self.ORIGINAL_POS.y)
Related