

-----------------------------------

Partizan

Wed May 14, 2008 8:30 am


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:

    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 
    
    delay(35)   
    drawfillarc (x, y, 20, 20, mouth + dir, -mouth + dir, black)


and the movement is as follows:

%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.


-----------------------------------

Insectoid

Wed May 14, 2008 10:12 am


RE:Pacman game problems

-----------------------------------

perhaps something like


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? 


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. 

Check out my PacMan blog, [url=http://compsci.ca/v3/viewtopic.php?t=18054]Here


-----------------------------------

Partizan

Wed May 14, 2008 10:33 am


RE:Pacman game problems

-----------------------------------

Thanks for the help.
How would I go about using whatdotcolor for my type of pacman though? 

Say I have the following border:

    drawfillbox (0, 0, 20, 420, 9)
    drawfillbox (20, 420, 620, 380, 9)
    drawfillbox (640, 400, 620, 20, 9)
    drawfillbox (640, 0, 20, 20, 9)



-----------------------------------

isaiahk9

Wed May 14, 2008 3:09 pm


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

Wed May 14, 2008 4:35 pm


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

Wed May 14, 2008 4:38 pm


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

Wed May 14, 2008 4:57 pm


RE:Pacman game problems

-----------------------------------

I guess that would work, if the top picture is a sprite (whatdotcolor ignores sprites, I believe...)


-----------------------------------

Partizan

Thu May 15, 2008 12:03 pm


Re: Pacman game problems

-----------------------------------

Thank you all for the help, I got collision and movement working  :D 
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:

for i:390..570 by 20
drawfilloval (i, 50, 5, 5,red)
end for


And I have used the following to calculate the score:

    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.




*

mouth := mouth mod 45 + 3
    drawfillarc (pacx, pacy, 20, 20, mouth + dir, -mouth + dir, paccolor)
    
    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 
    
    delay(50) 
    drawfillarc (pacx, pacy, 20, 20, mouth + dir, -mouth + dir, 3)   

 
Thank you all for the help.
Once I get this right I can pretty much polish my program and finish it.


-----------------------------------

Insectoid

Thu May 15, 2008 5:54 pm


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

if whatdotcolor (pacx, pacy) = (coincolor) then
     Draw.Fill (pathcolor, pathcolor)
     points += 10
end if



-----------------------------------

Partizan

Sun May 18, 2008 2:53 pm


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.


-----------------------------------

Insectoid

Tue May 20, 2008 3:44 pm


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:


if pacx > ghostx then 
     ghostx += 1
end if

if pacx  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

Tue May 20, 2008 4:05 pm


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

Tue May 20, 2008 4:19 pm


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

Tue May 20, 2008 4:25 pm


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

Tue May 20, 2008 4:42 pm


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)


-----------------------------------

isaiahk9

Tue May 20, 2008 6:25 pm


RE:Pacman game problems

-----------------------------------

I didn't mean pathfinding.  I was just saying that partizan could use View.WhatDotColor or some other method to glitch less.

I meant if he didn't care - most knock offs just increase difficulty by increasing the number of ghosts.

My time for compsci is messed up - I definitely gave myself the wrong time zone.


-----------------------------------

Insectoid

Tue May 20, 2008 7:32 pm


RE:Pacman game problems

-----------------------------------

Well of course he's going to do wall collision (with whichever method he chooses to use)
In my example I was just doing the bare ass of the work. Frankly, I thought it was very obvious.

I don't do knockoffs, they are a mystery to me. I clone as best I can.


-----------------------------------

isaiahk9

Wed May 21, 2008 5:51 am


RE:Pacman game problems

-----------------------------------

Yes, I wield to your logic.  Just pointing out the obvious in case anybody didn't see it.


-----------------------------------

Insectoid

Wed May 21, 2008 7:53 am


RE:Pacman game problems

-----------------------------------

you wield my logic? Can't be too effective...(exploiting typoes...)

I suppose the solution to the whole problem could be that a variable needs to be reset...

What do you think, partizan? 
What do you think of this conversation?
Do you feel excluded?
Have we helped...at all? Seems all we've done is argue...lol


-----------------------------------

Partizan

Sat May 24, 2008 1:03 pm


RE:Pacman game problems

-----------------------------------

Haha, well, I didn't get an answer for several days so I went and found out on my own, it's ok though, you guys helped a lot before :D. Everything works pretty well, except I had to implement my own Pacman style of gameplay.
Right now I am trying to figure out how to have the highscores write the variables onto a file and them load them when called upon.
Shouldn't be too hard though.


-----------------------------------

Sean

Sat May 24, 2008 1:17 pm


Re: Pacman game problems

-----------------------------------

Read up on: Read and Write functions.

Edit: Open and Close will be needed aswell


-----------------------------------

Partizan

Sat May 24, 2008 1:44 pm


RE:Pacman game problems

-----------------------------------

Thanks, got it working.
Except I used the put and get functions to write into the file and to output from the file instead of read and write.

Sweet, I'm all set for my computer science class. I guess it's Counter Strike until the end of the year, lol. 
 :lol:

