Posted: Wed May 14, 2008 8:30 am Post subject: Pacman game problems
Hello.
I'm making a pacman game for a school assignment but having problems with collision and movement. The pacman is drawn using the following code:
code:
mouth := mouth mod 45 + 3
drawfillarc (x, y, 20, 20, mouth + dir, -mouth + dir, paccolor)
if dir= 0 or dir= 180 then
drawfilloval (x,y+10, 3, 3, black)
elsif dir= 90 then
drawfilloval (x+10,y, 3, 3, black)
elsif dir= 270 then
drawfilloval (x-10,y,3,3, black)
end if
%basic movement
Input.KeyDown (keys)
if keys (up_arrow) then
dir := 90
y := y + 5
end if
if keys (right_arrow) then
dir := 0
x := x + 5
end if
if keys (left_arrow) then
dir := 180
x := x - 5
end if
if keys (down_arrow) then
dir := 270
y := y - 5
end if
%Stopping diagonal movement
if keys (up_arrow) and keys (left_arrow) then
y := y - 5
x := x + 5
end if
if keys (up_arrow) and keys (right_arrow) then
y := y - 5
x := x - 5
end if
if keys (down_arrow) and keys (left_arrow) then
y := y + 5
x := x + 5
end if
if keys (down_arrow) and keys (right_arrow) then
y := y + 5
x := x - 5
end if
I would like to make it so that once a key is pressed, pacman moves non stop in one direction until the other key is pressed.
As for collision, I tried different ways and found the most efficient one to be using the View.WhatDotColor command, but I can't get it to work properly, pacman still moves through the borders.
Help is greatly appreciated.
Sponsor Sponsor
Insectoid
Posted: Wed May 14, 2008 10:12 am Post subject: RE:Pacman game problems
perhaps something like
code:
if key (key pressed) then
loop
y := y-5
exit when num (KEY_RIGHT_ARROW)
exit when num (KEY_LEFT_ARROW)
exit when num (KEY_UP_ARROW)
exit when num (KEY_DOWN_ARROW)
end loop
end if
you could also use the getch command, for pacman it makes no difference.
For collision detection, whatdotcolor is the best option in this scenario.
going diagonally is automatic with Input.KeyDown, so you don't need. And you don't really need to go diagonally in pacman, do you?
code:
if keys (down_arrow) and keys (right_arrow) then
y := y + 5
x := x - 5
end if
using a sprite as pacman is probably a better idea, so you don't need to redraw everything.
Posted: Wed May 14, 2008 3:09 pm Post subject: RE:Pacman game problems
For the boundaries, it would be something similar to this :
If the user clicks move
if Veiw.Whatdotcolor (pacmanx, pacmany) not=9 then
code for moving
end if
end if
do that for all the arrow keys. This makes it so that if the character is not on grey then he can move.
Insectoid
Posted: Wed May 14, 2008 4:35 pm Post subject: RE:Pacman game problems
I drew my maps in paint. This is a sample of my code (sort of...)
[code]
if key (KEY_UP_ARROW) and whatdotcolor (y + 10) then
y += 1
[/code
StealthArcher
Posted: Wed May 14, 2008 4:38 pm Post subject: RE:Pacman game problems
An interesting idea I heard was to have two pictures for each map:
-One your 'visual' map, the one everyone sees.
-Another with just pure black, and pure white, check for whatdotcolor on this one.
Then draw the visual map.
EDIT: I think you can still use sprites this way too.
Insectoid
Posted: Wed May 14, 2008 4:57 pm Post subject: RE:Pacman game problems
I guess that would work, if the top picture is a sprite (whatdotcolor ignores sprites, I believe...)
Partizan
Posted: Thu May 15, 2008 12:03 pm Post subject: Re: Pacman game problems
Thank you all for the help, I got collision and movement working
One last problem I'm having is calculating a proper score with the pills. Our teacher doesn't want us to use sprites so all the images have to be drawn using Turing, including the pacman.
Here is an example for my pills code:
code:
for i:390..570 by 20
drawfilloval (i, 50, 5, 5,red)
end for
And I have used the following to calculate the score:
code:
if whatdotcolor (pacx,pacy) = red then
drawfilloval (pacx,pacy,5,5,3)
score:=score+1
end if
Problem is, because the way my pacman is drawn*, it sometimes glitches and adds more than one point per pill, or sometimes doesn't even calculate the pills at all.
if dir= 0 or dir= 180 then
drawfilloval (pacx,pacy+10, 3, 3, black)
elsif dir= 90 then
drawfilloval (pacx+10,pacy, 3, 3, black)
elsif dir= 270 then
drawfilloval (pacx-10,pacy,3,3, black)
end if
Thank you all for the help.
Once I get this right I can pretty much polish my program and finish it.
Sponsor Sponsor
Insectoid
Posted: Thu May 15, 2008 5:54 pm Post subject: RE:Pacman game problems
What I did was draw the pictures in turing, then save them using Pic.ScreenSave I think..Then you could print that code and hand it in to the teacher while using the pictures it saved as your sprite.
My coins were drawn automaticaly using an if statement and a for loop ('if this space is big enouph and nothing else is there, draw a circle')
I used a procedure for collecting coins, sort of like
Posted: Sun May 18, 2008 2:53 pm Post subject: Re: Pacman game problems
Having a problem with my primitive ghost AI.
It works fine at first, but as soon as you complete the level (eat the red dot in the middle) the ghost refuses to go down.
Posted: Tue May 20, 2008 3:44 pm Post subject: RE:Pacman game problems
It seems that you aren't using sprites after all? did your teacher have a problem with you changing pictures drawn in turing into sprites?
Hmm, I just thought of a way to maybe have a self-contained program (draw & save pictures, then load the pictures in the same program)
For the AI, use something like:
code:
if pacx > ghostx then
ghostx += 1
end if
if pacx <ghostx then
ghostx -= 1
end if
if pacy > ghosty then
ghosty += 1
end if
if pacy < ghosty then
ghosty -= 1
end if
Wait, you are doing that! Your code is a bit weird to me, You seem to be making things a bit to complicated.
isaiahk9
Posted: Tue May 20, 2008 4:05 pm Post subject: RE:Pacman game problems
That would start out simple with insectoid's programming, to avoid the ghosts (they would usually walk into walls). However, multiple ghosts in different areas of the map with the same programming would be very difficult or impossible.
Insectoid
Posted: Tue May 20, 2008 4:19 pm Post subject: RE:Pacman game problems
Nah, You just have the same thing for every ghost! And as there only are 4 ghost (inky, blinky, pinky & clyde) it wouldn't be that hard. I have mine starting in each corner. They act completely independantly. You should google pacman to find out some of the special things that were in the original game ('scramble mode, speedy's special, etc.)
isaiahk9
Posted: Tue May 20, 2008 4:25 pm Post subject: RE:Pacman game problems
I meant if there was, about 20 ghosts then the levels would be impossible with each ghost having the type of AI you posted at 4:44 pm. But, using that AI, the ghosts would just run into the walls. It needs an if loop somewhere.
Insectoid
Posted: Tue May 20, 2008 4:42 pm Post subject: RE:Pacman game problems
*sigh* If you want real pathfinding AI, that's a few hundred extra lines of code. Otherwise, this simple AI is fine.
Since when does PacMan have 20 ghosts? If partizan wants to stay true to the original game, then he'll never have more than 4 ghosts.
Oh, how could I have posted at 4:44 if your post that mentioned that time was written before then? (I wrote that at 3:44)