0

I am building a pokemon game in python with pygame. I have got my map from Google and want my player to run on it but I want it to stop moving when it gets out of roads i.e I don't want it to move over walls or trees. How can I make my pygame player stop when he reaches certain parts in my map. My map is a single png image.

ROOP AMBER
  • 330
  • 1
  • 8

1 Answers1

1

You have to add shapes for the inaccessible areas, a collision map. A simple way would be combining elementary shapes like Rects and Circles. Make these shapes invisible for the player by creating another surface and blit it. Then check this answer on how collision works in pygame.

Attersson
  • 4,755
  • 1
  • 15
  • 29
  • So I should create a rectangle at that position where a wall is and than do spritecollide of my player with that rectangle and if it doesn't collide only than I should allow it to move – ROOP AMBER Mar 05 '22 at 10:37
  • That's right. You can also combine rectangles and other shapes and make them overlap. – Attersson Mar 05 '22 at 11:01
  • Which collide function would I have to use to collide a Rect object and a Sprite object – ROOP AMBER Mar 05 '22 at 14:31
  • `collide = rect.collidepoint(point)`, check the second link – Attersson Mar 05 '22 at 14:41
  • But I don't want to collide a point with a rect, I want to collide my player with my rect (for example let us take the rect as a house) – ROOP AMBER Mar 07 '22 at 15:21
  • use `rect.colliderect(my_rect)` – Attersson Mar 07 '22 at 15:53
  • but there is a problem in that, the player is never able to move out of the box and just stucks there forever while what I want is when player moves towards object for example towards right it should just not be able to move right and everything else – ROOP AMBER Mar 07 '22 at 20:42
  • I lack details to answer. Is the player going too far so it gets locked into collision? If that's the case, you have to bounce it a little back – Attersson Mar 07 '22 at 20:44
  • what I mean is that I did if not rect.collidederect: rect.x += 10, but when the player reaches the house or any object it is not able to move out as it is still colliding with the house and hence we get stuck there forever – ROOP AMBER Mar 07 '22 at 21:06
  • Thanks a lot, I got my answer from your latest comment that I should bounce it a little back. But please tell why was my question closed, the message is coming that my question is similar to other questions but all of the links provided by them of being same as mine are actually completely different – ROOP AMBER Mar 07 '22 at 21:19
  • I don't know. I think your question was legitimate, but the user who closed it saw some resemblance to another answer I think – Attersson Mar 07 '22 at 22:42
  • OH! Okay, should I try opening it or just proceed – ROOP AMBER Mar 08 '22 at 07:58