Python Forum
Multiple wall collision in pacman
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple wall collision in pacman
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Collision. Start of simple. Remember last movement. If collide with any thing. Move it back.
How to use pygame sprite class the right way.
99 percent of computer problems exists between chair and keyboard.
Reply
#4
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.
Reply
#5
Thank you for the suggestion, I'll try with the dictionary
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [PyGame] Problem with collision of player and enemy Nekotrooper 1 601 Dec-08-2023, 03:29 PM
Last Post: deanhystad
  can't get collision detection to work in platform game chairmanme0wme0w 10 3,663 Aug-19-2022, 03:51 PM
Last Post: deanhystad
  [PyGame] Rect object penetrates wall on one side Jan_97 4 2,463 Dec-30-2021, 11:08 PM
Last Post: Jan_97
  Make a wall in turtle module Jan_97 2 7,023 Aug-18-2021, 08:47 PM
Last Post: Jan_97
  [PyGame] Collision in not happening onizuka 3 3,360 Sep-07-2020, 11:30 AM
Last Post: metulburr
  [PyGame] No collision detection onizuka 6 3,596 Aug-18-2020, 01:29 PM
Last Post: onizuka
  Problem with collision detection... michael1789 4 3,219 Nov-12-2019, 07:49 PM
Last Post: michael1789
  Arcade Collision Problem randor 0 2,656 Oct-28-2019, 11:17 PM
Last Post: randor
  Creating Pacman Labyrinth alehak 2 2,321 Oct-20-2019, 01:09 AM
Last Post: ichabod801
  drawing, moving, and collision problems (pygame) SheeppOSU 26 14,443 Apr-22-2019, 03:09 AM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020