Python Forum
Multiple wall collision in pacman - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: Multiple wall collision in pacman (/thread-20433.html)



Multiple wall collision in pacman - rustyjoe - Aug-10-2019

Hi, I'm trying to replicate pacman with pygame. I want to make the player move wherever he wants but on one condition: if he's too near the wall I have to avoid some keys pressed and allow him to go only in certain directions, where there aren't any walls but I encountered a problem with wall collision. I used spritecollide() function but it doesn't really work for my case. I tried to iterate through the entire list of walls, while I was comparing pacman rect coordinates but it produced some strange behaviours. So I came up with an idea, keep track of every coordinates I'm interested in but probably isn't a good solution because I should hardcode them so I'm wondering if you have some ideas or prompts of wall collision in pacman's style. Here is my entire code: https://github.com/rustyjoee/pacman


RE: Multiple wall collision in pacman - ichabod801 - Aug-10-2019

I don't know that I would go about PacMan as a wall collision issue. PacMan has a very limited path he can be on, I would just make sure he doesn't leave that path.


RE: Multiple wall collision in pacman - Windspar - Aug-10-2019

Collision. Start of simple. Remember last movement. If collide with any thing. Move it back.
How to use pygame sprite class the right way.


RE: Multiple wall collision in pacman - ThomasL - Aug-11-2019

How big/small are your moving steps? Can you move PacMan one pixel if you press a direction key?
I agree with ichabod801 and would no go with calculating wall collision but with keeping an eye on the track it´s moving.
A simple dictionary with a tuple of x/y coordinates as key and a tuple with the allowed directions as value would work
track = {(50, 50): (True, False, False, False)} so at x/y 50,50 movement allowed only North for example.
If done per pixel dictionary might habe some thousends keys but access is O(1)!
and you could do binning with the coordinates to reduce the size of the dictionary.


RE: Multiple wall collision in pacman - rustyjoe - Aug-11-2019

Thank you for the suggestion, I'll try with the dictionary