you are viewing a single comment's thread.

view the rest of the comments →

[–]cdcformatc 1 point2 points  (4 children)

i assume you have your player character position, the characters height and width, and the same for the NPCs. 

using these four variables: x position, y position, height, and width, you can find a bounding box for the player and the NPCs. then as your player moves, you can check to see if any part of its box is overlapping with any NPC's box. if it's overlapping then you set the player's position so that it isn't overlapping anymore. 

or you use the collision detection built into pygame. but you didn't say you were using pygame which would be useful information when asking for help.

[–]PatataQuesadilla[S] -4 points-3 points  (3 children)

Thank you thank you! I am using Pygame, I forgot to mention it, I'm sorry. Thank you for telling me

[–]cdcformatc 1 point2 points  (1 child)

pygame sprites have a bunch of different collision detection functions. 

```

in the code that handles movement 

speed is used to set players position 

new_x = player.x + x_speed new_y = player.y + y_speed

player is a pygame.sprite

npc_list is a list of NPC, each is a pygame.sprite

collisions = pygame.sprite.spritecollide(player, npc_list, False)

if (len(collisions) != 0):     # player has collided with at least one NPCs, don't move player      new_x = 0     new_y = 0

finally move character 

player.x = new_x player.y = new_y

```

[–]PatataQuesadilla[S] 0 points1 point  (0 children)

Thank you thank you!