Thread: Maze game collision problem

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    3

    Maze game collision problem

    ok well
    i have a problem and hoped someone could help me fix it.
    when i go into a wall it gives me the error "wall blah blah" thats ok but you can press up into the wall it does not print the caracter but you can say go into wall and go left 3 spaces and press down you will come out of wall as if by magic.
    basicly i need to stop my caracter entering the wall??
    thanks kempy1000

    Code:
    //*****************************************************************
    //                       THE QUEST
    //          Name: Map Loader
    //   Description: Loads Maps Into an Array From An Txt File
    //*****************************************************************
    #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
    #define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
    
    #define VK_A 0x41
    #define VK_B 0x42
    #define VK_C 0x43
    #define VK_D 0x44
    #define VK_E 0x45
    #define VK_F 0x46
    #define VK_G 0x47
    #define VK_H 0x48                                   // defines for Keys
    #define VK_I 0x49
    #define VK_J 0x4A
    #define VK_K 0x4B
    #define VK_L 0x4C
    #define VK_M 0x4D
    #define VK_N 0x4E
    #define VK_O 0x4F
    #define VK_P 0x50
    #define VK_Q 0x51
    #define VK_R 0x52
    #define VK_S 0x53
    #define VK_T 0x54
    #define VK_U 0x55
    #define VK_V 0x56
    #define VK_W 0x57
    #define VK_X 0x58
    #define VK_Y 0x59
    #define VK_Z 0x5A
    
    #include <cstdio>
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    //************* MAP *************
    const int GSIZE = 20;
    //*******************************
    //********** OBJECTS ************
    char wall = 178;
    int woods = 5;
    //*******************************
    
    void gotoxy(int x, int y)
    {
      COORD coord;
      coord.X = x;
      coord.Y = y;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }//END
    
    char map[GSIZE][GSIZE] =
    {
         178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,
         178,178,178,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,178,178,
         178,178,178,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,178,178,
         178,178,178,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,178,178,
         178,178,178,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,178,178,
         178,178,178,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,178,178,
         178,178,178,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,178,178,
         178,178,178,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,178,178,
         178,178,178,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,178,178,
         178,178,178,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,178,178,
         178,178,178,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,178,178,
         178,178,178,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,178,178,
         178,178,178,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,178,178,
         178,178,178,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,178,178,
         178,178,178,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,178,178,
         178,178,178,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,178,178,
         178,178,178,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,178,178,
         178,178,178,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,178,178,
         178,178,178,0,0,0,0,0,0,0,0,0,0,0,0,0,0,178,178,178,
         178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,
         
    };
    
    void PrintMap()
    { 
      for(int i = 0; i < GSIZE; i++)
      {
              for(int j = 0; j < GSIZE; j++) 
              {
                      cout << map[j][i];
              }
              cout << "\n";
      }
    }//END
    
    void PrintPlayer(int player_x, int player_y, int move)
    {
         gotoxy(player_x, player_y);
         cout << (char)2;
    }//END
    
    int CheckMove(int player_x, int player_y, int move)
    {
         if(map[player_x][player_y] == 0)
         {
              PrintPlayer(player_x, player_y, move);         
         }
         else
         {
              gotoxy(25,5);
              cout << "There Is A Wall In The Way\n";
         }
    }//END
    
    void Move(int player_x, int player_y)
    {
         
         bool endfunction = false;
         
         do{
         int move = 0; 
         bool endloop = false;
         
         do{
         
            if (KEY_DOWN(VK_UP))              // If key pressed is the UP arrow
            {
                endloop = true;
                player_y--;
                move = 1;
                Sleep(200);
            }
    
            else if (KEY_DOWN(VK_DOWN))       // If key pressed is the Down arrow
            {
                 endloop = true;
                 player_y++;
                 move = 2;
                 Sleep(200);
            }
    
            else if (KEY_DOWN(VK_LEFT))       // If key pressed is the LEFT arrow
            {
                 endloop = true;
                 player_x--;
                 move = 3;
                 Sleep(200);
            }
    
            else if (KEY_DOWN(VK_RIGHT))      // If key pressed is the RIGHT arrow
            {
                 endloop = true;
                 player_x++;
                 move = 4;
                 Sleep(200);
            }
    
            else if (KEY_DOWN(VK_RETURN))     // If key pressed is the RETURN key (Enter)
            {
                 endloop = true;
                 endfunction = true;
                 Sleep(200);
            }
         
         }while(!endloop);
         
         CheckMove(player_x, player_y, move);
         
         }while(!endfunction);
         
    }//END
    
    int main()
    {
        int player_x = 5;
        int player_y = 5;
        int move = 0;
        //LoadMap();
        PrintMap();
        PrintPlayer(player_x, player_y, move);
        Move(player_x, player_y);
    }//END

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    In the else part of your CheckMove() function you have to set the position back to the previous position.
    BTW you declared CheckMove() to return an int but you don't return anything.
    Kurt

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Make CheckMove() return a value, valid move or invalid move. Where you move the character (eg, player_y--) call CheckMove() to see it if was a valid move. If it wasn't, move the character back again.

    Or make CheckMove() take two arguments, the x and y of the new position. It then sets the position if it's valid (you might want to rename the function in this case).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman game problem
    By piradie in forum C Programming
    Replies: 9
    Last Post: 12-30-2008, 04:29 PM
  2. Game Engine Link Prob
    By swgh in forum Game Programming
    Replies: 2
    Last Post: 01-26-2006, 12:14 AM
  3. Maze game, complete with maze editor and an example maze!
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-20-2002, 03:27 AM
  4. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM