Thread: Weird console game logic error...?

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Weird console game logic error...?

    (WinXP, Borland C++ Free Command Line Compiler)
    Ok, I made a short console game, you can move a dot around the screen. But, my barriers only work for up & down, left and right dont work for some odd reason. I have looked all around my code, and I tried swapping their values, but I dont understand why the barriers dont work, its just a simple if() function...

    Well heres my code, its actualy pritty short:

    Code:
    #include <windows.h>
    #include <iostream>
    #include <conio.h>
    using namespace std;
    
    enum {
     ESC_KEY = 27,
     UP_ARROW = 256+72,
     DOWN_ARROW = 256+80,
     LEFT_ARROW = 256+75,
     RIGHT_ARROW = 256+77
    };
    
    int get_arrow();
    
    HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
    WORD Color;
    CONSOLE_SCREEN_BUFFER_INFO inf;  
    CONSOLE_CURSOR_INFO Cur;
    
    int main() {
     COORD Pos;
     COORD Pos2;
     int Counter;
     int c2;
     int buffer;
     int grid_x;
     int grid_y;
     
     clrscr();
     GetConsoleScreenBufferInfo(h, &inf);
     Color = inf.wAttributes; 
     Cur.bVisible = FALSE;
     Cur.dwSize = 1;
    
     SetConsoleTextAttribute(h, BACKGROUND_RED | BACKGROUND_INTENSITY);
     SetConsoleCursorInfo(h, &Cur);
     SetConsoleTitle("Hi");
    
    
     for(Counter=0; Counter < 80; Counter++)
      for(c2=0; c2 < 25; c2++) {
       Pos.X = Counter;
       Pos.Y = c2;
       SetConsoleCursorPosition(h, Pos);
        if(Counter == 10 && c2 == 13)
         cout << "*" << endl;
        else
         cout << " " << endl;
     }
     Pos2.X = 0;
     Pos2.Y = 0;
     SetConsoleCursorPosition(h, Pos2);
    
     grid_x = 10;
     grid_y = 13;
    
     for(;;) {
      buffer = get_arrow();
      if(buffer == ESC_KEY) {
       clrscr();
       SetConsoleTextAttribute(h, Color);
       exit(0);
      }
      if(buffer == DOWN_ARROW)
       if(grid_y == 23)
        cout << "";
       else
        grid_y++;
      if(buffer == UP_ARROW)
       if(grid_y == 1)
        cout << "";
       else
        grid_y--;
      if(buffer == LEFT_ARROW)
       if(grid_y == 80)
        cout << "";
       else
        grid_x--;
      if(buffer == RIGHT_ARROW)
       if(grid_y == 1)
        cout << "";
       else
        grid_x++;
       for(Counter=0; Counter < 80; Counter++)
        for(c2=0; c2 < 25; c2++) {
         Pos.X = Counter;
         Pos.Y = c2;
         SetConsoleCursorPosition(h, Pos);
         if(Counter == grid_x && c2 == grid_y)
          cout << "*" << endl;
         else
          cout << " " << endl;
         SetConsoleCursorPosition(h, Pos2);
        }
       }
    }
    
    int get_arrow() {
     int ch = getch();
     
     if(ch == 0 || ch == 224)
      ch = 256 + getch();
     return ch;
    }

    I'm guessing I messed up somewhere simple, but I just cant find my problem.... I have looked at this four three hours, but I'm not getting anywhere, no matter what value I give it, it just lets me bring it off the screen... Any ideas?

    Thank you very much in advanced!
    Last edited by Blackroot; 01-07-2006 at 06:35 AM.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
      if(buffer == LEFT_ARROW) 
       if(grid_y == 80)  // here you want to check for 1
        cout << "";
       else
        grid_x--;
      if(buffer == RIGHT_ARROW)
       if(grid_y == 1)// here you want to check for 80
        cout << "";
       else
        grid_x++;
    Looks like you have mixed up the logic for left and right
    Kurt

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Ahhh... How in hell did I miss that? Tyvm kurt!
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. very weird .h problem
    By royuco77 in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 07:55 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM