2d packman
Blitz3D Forums/Blitz3D Beginners Area/2d packman
| ||
I want to make a 2d packman game where packman is one image and the walls are another image. How would I get packman to not go through the walls? |
| ||
there are many possibilities... you could do a simple readpixelfast around your packmans current position from the map image. or you could setup collision boxes and use blitz collision detection. or you could setup a memorybank with the walls data and peek from that. or or or ;) |
| ||
The best way to do this (imo) is not to use collisions at all, but have a 2D array holding the wall data. All your sprites (PacMan, ghosts etc.) should be made the same size (e.g. 32x32) so then you set up an array with each element saying whether that 32x32 area of the screen is a wall or a space. Give your PacMan and Ghosts a position in this array so that when they need to make a change in direction, you can simply reference the array to see if there's a wall in the way or not. Remember: PacMan and the ghosts can only change direction when they're exactly aligned to a position in the grid array. |
| ||
Thanks for the advice. After reading big10p's post, I tried out using an array, it worked great, but this project is not so much how to get a good final product as to learn about the blitz 2d commands. So, does anybody know how to do this with the blitz ImagesCollide command? |
| ||
Consider using ImagesCollide with Pacman and the Ghosts to test for player death - the 2d array solution is really the best for wall collision for this type of game. |
| ||
Have the 'gaps' (e.g. the bits pacman walks on) the maskcolour for the maze and use imagescollide? |
| ||
Yes, but is there a realy simple way to find out which side the collision was on so that I can prohibit movement in that direction? |
| ||
Why do you need to know which side the collision was on? All you need to know is that the pacman collided with a non-transparent pixel of the maze. Check the movement key from the player, check whether Pacman's new position would collide with the wall image. If it does don't allow the move. |
| ||
Heres the entire code I once wrote for a PacMan game in QuickBASIC. It should give you some ideas on how to set up and execute such a game. Note that it uses no external images, nor does it make use of the vertical refresh, so it's written to take advantage of 'dirty rectangles' to minimize screen flicker. You can actually play it from my Projects Gallery on my website. ![]() |
| ||
Thanks everyone! I think I have it figured out now. |
| ||
Just in case anyone wanted it, here is some very simple code that I started out with.Graphics 640,480,0,2 SetBuffer BackBuffer() packman=LoadImage("packman.bmp") maze=LoadImage("maze.bmp") x#=320 y#=240 While Not KeyDown(1) Cls DrawImage maze,0,0 If KeyDown(203) If Not ImagesCollide(packman,x-1,y,0,maze,0,0,0) Then x=x-1 EndIf If KeyDown(205) If Not ImagesCollide(packman,x+1,y,0,maze,0,0,0) Then x=x+1 EndIf If KeyDown(200) If Not ImagesCollide(packman,x,y-1,0,maze,0,0,0) Then y=y-1 EndIf If KeyDown(208) If Not ImagesCollide(packman,x,y+1,0,maze,0,0,0) Then y=y+1 EndIf DrawImage packman,x,y Flip Wend End |
| ||
Or, that could be rewritten as; figure out where we want to move to Local dest_x = x Local dest_y = y If KeyDown(203) Then dest_x = dest_x - 1 If KeyDown(205) Then dest_x = dest_x + 1 If KeyDown(200) Then dest_y = dest_y - 1 If KeyDown(208) Then dest_y = dest_y + 1 ; attempt to move there If Not ImagesCollide(packman,dest_x,dest_y,0,maze,0,0,0) Then x = dest_x y = dest_y EndIf |
| ||
Except that in 'true' Pacman, the pacman is always on the move unless it hits a wall... |