willlplaya
willlplaya

Reputation: 49

Finding the x and y values of a sprite in pygame

I have a sprite, named Player and when it collides with another sprite, named Powerup, it will launch the sprite Player in the negative y direction up 700 pixels.

After the sprite Player has launched itself up 700 pixels I want a sound to play, however to do this I need to find a way to check whether the sprite Player has finished being launched up 700 pixels.

Basically something like this:

if sprite_player collides with sprite_powerup:
     sprite_playerYcoordbeforepowerup = previous_Y
     launch_player_up_700pixels
     then check
if sprite_playercurrentYcoord - previous_Y == 700:
     playSound()

Is there something similar to the function pygame.mouse.get_pos() which returns the position of the mouse cursor, that instead returns the x and y values of the sprite? If not, how would I go about doing this?

Sorry if this was worded badly, hope it's readable :)

Upvotes: 2

Views: 1371

Answers (1)

Omega0x013
Omega0x013

Reputation: 161

your should use some rendition of an "islaunched" variable, to tell the system whether the player has just been launched, but would reset at the next iteration of the loop.

Alternatively, you could simply play the sound within the action loop, after the change in y (although not if motion is based off velocity). By this i mean that you would most likely simply teleport the player 700px upwards, in which case a delay between action and sound would be inconsequential, and would quite possible detract from the gameplay.

Upvotes: 1

Related Questions