Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Need Help Making PacMan Not Eat Walls (collision detection)
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
scottyrush13




PostPosted: Wed Jan 05, 2005 11:26 pm   Post subject: Need Help Making PacMan Not Eat Walls (collision detection)

I would like to start off by saying, i know that alot of people have asked the same question and then there are tutorials, but i still dont understand.

I have the beginnings of a pacman game (pacman moves and there is a level) but when pacman goes at a wall he goes right through it and eats it. Im new to programming and not very good (although i am learning).

If someone could give me the easiest possible explanation on how to solve my problem i would be grateful.

(if you need to see what i have so far you can PM me)
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed Jan 05, 2005 11:41 pm   Post subject: (No subject)

you need to figure out a way to see what's in front of pacman before moving. You can use tilemaps or whatdotcolour() or any other method of your choice.
scottyrush13




PostPosted: Wed Jan 05, 2005 11:55 pm   Post subject: whatdotcolour

say i was to use whatdocolour to detect where the walls were...how would i do it for this:

setscreen ("graphics:500;500")
drawfillbox (50,50,150,100, blue)
drawfillbox (50,100,100,150,blue)

(thats not the whole level thats just one of the objects)

i just need an example for this part so i can understand this whatdotcolour (i've never used it but i've read around the site and it sounds quite usefull)
lordofall




PostPosted: Thu Jan 06, 2005 1:29 am   Post subject: (No subject)

just use whatdotcolour to check if the spot right infront of pacman is blue, and if it is don't move him just make him stand still and move his mouth.

for example if pacman is moving left
then use whatdotcolor to check x-1 (u also do this when ur moving pacman likely so just check then)

so before moving put
if not whatdotcolor(x-1,y) = blue then
*move code*
end if
that way pacman won't move if the spot infront of him is blue

since pacman only goes in one direction u just check the spot right infront of him b4 moving, tho if u drew pacman as a circle you'll havta subtract or add his radius to his x or y cordinate to check b4 moving him.
scottyrush13




PostPosted: Thu Jan 06, 2005 8:00 am   Post subject: thank you

thanks, i think im starting to get this.

i think you diserve....

+5 Bits
scottyrush13




PostPosted: Thu Jan 06, 2005 3:10 pm   Post subject: omg

i thought i understood but when i went to do try it, it still does not work Sad Sad , i dont know if im using whatdotcolor right cause ive never used it before and when i asked my computer science teacher for help he told me that he wasnt going to help me cause its my isu (he didnt teach us anything through the year either, we just read notes out of a book Mad ).

if anyone could provide a simple example program of how whatdotcolour is used to stop pacman from eating any sort of wall, i would really appreciate the help, (also i have asked the smartest people in my class to help and they couldnt because our teacher never tought us this whatdotcolour)

thank you in advance to those who may help me, and thank you to those who have already helped me

::edit::
i have been told by multiple people that whatdotcolour is not the most efficiant way to work with collision detection, can someone provide an example of how i would do collision detection for pacman on the sample wall i provided above in an earlier post?
Cervantes




PostPosted: Thu Jan 06, 2005 4:29 pm   Post subject: (No subject)

Here's some quickly thrown together code to illustrate what you might be thinking about:

code:

setscreen ("graphics:500;500,offscreenonly")

var pacman :
    record
        x, y : int
        radius : int
    end record
pacman.x := 0
pacman.y := 100
pacman.radius := 10

var keys : array char of boolean
loop

    Input.KeyDown (keys)
    if keys (KEY_RIGHT_ARROW) then
        pacman.x += 1
    end if
    if keys (KEY_LEFT_ARROW) then
        pacman.x -= 1
    end if

    if whatdotcolour (pacman.x + pacman.radius + 1, pacman.y) = blue then
        pacman.x -= 1
    end if

    cls
    drawfilloval (pacman.x, pacman.y, pacman.radius, pacman.radius, yellow)
    drawfillbox (50, 50, 150, 100, blue)
    drawfillbox (50, 100, 100, 150, blue)
    View.Update
    delay (10)

end loop


Note, however, that I did not involve pacman's direction or anything like that. That's your job Wink
With pacman's direction, just check whatdotcolour in that one direction. A case statement might be helpful there.
scottyrush13




PostPosted: Thu Jan 06, 2005 6:35 pm   Post subject: (No subject)

yay im on my way to completing the game!!!!

thank you A TON!!!! i now understand how whatdotcolour is used for programs like this!!!!

i cannot thank you enough for your help


::edit::
once again i hath been foiled by that little yellow guy!

i think it could have something to do with the case statement you told me would be useful---where exactly should i have a case statement...and i thought case statements were used for things like choosing an answer and having put "(so and so)"

please explain further, and im sorry for all of the question asking im doing, but i can assure you when i get better at programming i will try to help others, that like me now, are having problems with their programs on this site
Sponsor
Sponsor
Sponsor
sponsor
scottyrush13




PostPosted: Thu Jan 06, 2005 8:46 pm   Post subject: new problem

i dont know if i did it wrong but i used the whatdotcolour for my program to stop pacman from going right through the walls, and i can get him to now go through all of the walls but there are still parts he goes through...

for instance when going in an upward direction if he hits a wall he stops, but if hes going in a downward direction he goes through the wall....

i used whatdotcolour 4 times once for each direction, is that what im supposed to do? if so why does he go through some when heading in a left or down direction but not right and up????
scottyrush13




PostPosted: Thu Jan 06, 2005 9:28 pm   Post subject: yeesssssssssss

I DID IT!!!!! i just fiddled around a bit and fixed my own program, i still owe alot of thanks to Cervantes because if it wasnt for his help i dont think i would have figured this out!!!!!

Cervantes....you're the man!!!!!
Bored




PostPosted: Thu Jan 06, 2005 9:41 pm   Post subject: Re: new problem

scottyrush13 wrote:
i used whatdotcolour 4 times once for each direction, is that what im supposed to do? if so why does he go through some when heading in a left or down direction but not right and up????
When you used it for left and down did you remeber to put a minus sign where you had a plus sign in up and right because if you think about it when you travelling left and down the are in front of you has a lower coordanite than you do. For ex. if your at 100,10 trvaeling left the area in front of you would hav the same y co-ordinate and the x co-ordinate would be 100 minus the circle radius (lets say 15) minus one so the co-ordinates for the whatdotcolour would be 184.
code:
whatdotcolour (x-15-1,y)
%or
whatdotcolour (x-15-1,y)
Bored




PostPosted: Thu Jan 06, 2005 9:43 pm   Post subject: (No subject)

soz started writing b4 you posted scotty
Cervantes




PostPosted: Thu Jan 06, 2005 9:58 pm   Post subject: (No subject)

Awesome scotty! 8)
Actually, like bored, I started writing this a long time ago (had to disconnect from the internet for a while), but I figure I'll post it anyway. I think it'll help you speed your code up Smile

Re case: What I mean is use a case construct of pacman's direction, like this:
code:

case pacman_direction of

pacman_direction will be a string variable. It should be "up", "down", "left", or "right". (I don't know how you're doing this, but that's just how I'm choosing to do it)

Here's what the labels would look like:

code:

    label "up" :
        if whatdotcolour (pacmanX, pacmanY + pacmanRadius + 1) = wallColour then
            pacmanY -= pacmanMoveSpeed
        end if
    label "down" :
        if whatdotcolour (pacmanX, pacmanY - pacmanRadius - 1) = wallColour then
            pacmanY += pacmanMoveSpeed
        end if
    label "left" :
        if whatdotcolour (pacmanX - pacmanRadius - 1, pacmanY) = wallColour then
            pacmanX += pacmanMoveSpeed
        end if
    label "right" :
        if whatdotcolour (pacmanX + pacmanRadius + 1, pacmanY) = wallColour then
            pacmanX -= pacmanMoveSpeed
        end if
    label :

This way, you're not checking every side of pacman, you're only checking the spot in front of him (which could be north, south, east, or west, depending on which way pacman is facing). This will not only speed your program up, it'll fix some bugs that would occur if you checked all four sides at once and if pacman were in a corner, say.

Right-o?
-Cervantes
scottyrush13




PostPosted: Thu Jan 06, 2005 10:03 pm   Post subject: (No subject)

thats kind of like what i did but not quite...anyways i wanted to thank you for helpin me through this...lookin back i feel silly because its so simple i cant believe i didnt understand it Embarassed

oh well im a beginner, i'll get better Very Happy
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: