Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,665 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,159 people online right now. Registration is fast and FREE... Join Now!




PACMAN

 
Reply to this topicStart new topic

PACMAN

momina.k
post 3 Sep, 2007 - 09:22 AM
Post #1


New D.I.C Head

*
Joined: 19 Apr, 2007
Posts: 3


My Contributions


Hi,
Im a beginner and this is my first project, i have used very basic techniques,, lik 2-d arrays,,while loops,,
i m writing a brief code to show my logic of makin my game
i m handling my pacman(player) with arrow keys, n monsters will be moving around randomly,
my problem is that once i get a key it goes into main while loop,,then it gets key again n call rightghost(),,in rightghost() therez a while loop to move the monster,when the loop terminates,,it calls another ghost function for keep moving randomly,,,n it goes on in these while loops n never come back to get key again for my player,,I want that my ghosts will keep moving randomly n select their path accordingly when a wall comes(I have done it,but that is violating my main loop)n my pacman is controlled by my with keys aswell .can u plz solve my problem

CODE
int main()
{
ch=getch();

while(ch!=27)

ch=getch();
if(a==1)//declare for making condition
{
rightghost();//(calling my monster function)

}
a=0;//initialized it again so it wont call ghost after every key
if(key==77)
{
//code for moving my pacman
}
and code for rest of the keys,,left,upndown.

return 0;
}
NOW my monster function is
void righghost()

{
    while(arr[x][y]!='*')//[i]this is the itterative loop that is moving
                     // my ghost until a wall of asteriks come[/i]
    {

    code for movement
    }

    if(arr[x][y]=='*')
    {
                      a=rand()%3[i];//a random number is generated to
                 //help random movement of monster[/i]                       if(a==0)
            {
               leftghost[i]();//it will work on same condition
                         //as rightghost did i-e generate
                     // a random numbre if a wall of "*" comes[/i]            }
            if(a==1)
            {
                upghost();
            }
            if(a==2)
            {
                downghost();
            }
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 3 Sep, 2007 - 09:11 PM
Post #2


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,858



Thanked 47 times

Dream Kudos: 550
My Contributions


I find your question very hard to follow. I find your code even more so.

take for example the section:
QUOTE
CODE
while(ch!=27)
ch=getch();
This means "Do nothing until the user presses the escape key. Thats good, but then what follows? an if statement that uses an undeclared variable "a" and then another if statement using undeclared variable "key"

I guess this is supposed to be some kind of psuedo-code? Please clean up your question and/or code so that we can follow and offer advice. Based upon what I see, my advice is to work first on getting your program to read input keys from the user, then work on moving the character around a 2-d array... THEN work on the Ghosts. Don't try to tackle it all at once.
User is offlineProfile CardPM

Go to the top of the page

momina.k
post 4 Sep, 2007 - 06:35 AM
Post #3


New D.I.C Head

*
Joined: 19 Apr, 2007
Posts: 3


My Contributions


i have moved my pac man in 2-D array,,it is eating its food..sorry for the mistake it is (key=getch() andd while (key!=27), i have declared 'a' n rest of the variables as gobal variables..
in my game my pacman moves around with my keys ,,score is incremented, it eats food, but the Ghosts part is not working properly,,,i cant move ghost randomly while handling my pacman with keys,,what it does that once it goes into a Ghost function , it doesnt come back in main() to get key again, for that problem i tried to get key in ghost functions n called my key functions there,,it works for ONE ghost,,when there r 3 ghost it does not work...if u see my ghost code,,it is kind of a recursive call,,,the function is never going to stop,because it will keep on striking with wals" *" n generate random numbers and call the next random movement of ghost..

i hope it is more clear now?
i really appreciate ur help

This post has been edited by momina.k: 4 Sep, 2007 - 06:38 AM
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 4 Sep, 2007 - 06:54 AM
Post #4


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,858



Thanked 47 times

Dream Kudos: 550
My Contributions


your code for the ghost function seems incomplete...

You really should not put your packman loop inside of the ghost function. The function should exit and return back to your main loop.

So what is going on here usually is that you have a main loop that looks for key events. This loop really should have no other purpose other than to read in the keys and decide what to do with them. It is often a do-while loop since the last thing it checks is usually the exit sequence. When the user presses a key the loop will call the appropriate function to preform that action. Now background operations (like moving the ghosts) are placed either at the top or bottom of the loop so they execute every cycle.

CODE

do
{
ch=getch();
if (ch == UP) { do up }
else if (ch == DOWN) {do down }
...
else if (ch == 27) { break; } //exit the loop

MoveGhost();
} while (ch != 27); //redundant since the break line checks this condition, but still nice to see.


...

void MoveGhost()
{
int dir = random direction;
if (dir == NORTH) {gy++;}
else if (dir == SOUTH) {gy--; }
else if (dir == EAST) ...etc...

Test to see if Ghost position == user position
etc

return;
}
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 05:51AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month