0

I am currently working on creating a function for my side scroller game in pyhton 3.2.3 and pygame that allows the main character to collect coins. What I am trying to do is when my character's postion is equal to the money's postition, the money disappears. Here is my code:

def drawMoney(x,y):
offset = 0 - guy[X]
guyPos = guy[X]+640
moneyPos = x
print(guyPos,moneyPos)
if lvlNum == level1:
    money1  = screen.blit(money,(offset+x, y))
if guyPos == moneyPos or guy[Y] == y:
    y = 10000

I am trying to make the money disappear by re-blitting the level picture but for some reason, this does not happen. I am not sure why but for some reason it is not working. Everything seems to be correct but am assuming that the guyPos never equals the moneyPos. Any help is appreciated.

user2950875
  • 89
  • 3
  • 13

2 Answers2

1

By doing:

offset = 0 - guy[X] 
guyPos = guy[X] + offset

you're practically assigning zero to guyPos

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
0

For something like this I'd use the colliderect() function already built-in in pygame. Just check, whether the player collides with any of the coins and if so, add the coin to the player's collection by adding the respective amount to that variable and remove the coin from the list of sprites to blit.

In Pseudo-Code:

For coin in coin_list:
    Does the coin collide with the player (check by using pygame.Rect.colliderect())
        If so: add coin amount to player's coin variable and remove coin from coin_list
Patric Hartmann
  • 738
  • 7
  • 24