Problem with moving character around(using array for map)

This is a discussion on Problem with moving character around(using array for map) within the C++ Programming forums, part of the General Programming Boards category; I am using an array for my background, if you compile you will see what I mean... I want it ...

  1. #1
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138

    Problem with moving character around(using array for map)

    I am using an array for my background, if you compile you will see what I mean... I want it to put the char back where it was when the smiley moves forward, but when I compile its putting it one step down, or not at all . If anyone can tell me whats wrong I would be grateful.. Here is the code:

    Code:
    #include <iostream>
    #include <windows.h>
    #include <stdio.h> 
    #include <conio.h>
    #include <stdlib.h>
    int ch;
    static int get_code ( void )
    {
      int ch = getch();
    
      if ( ch == 0 || ch == 224 )
        ch = 256 + getch();
    
      return ch;
    }
    
    enum
    {
      KEY_ESC     = 27,
      ARROW_UP    = 256 + 72,
      ARROW_DOWN  = 256 + 80,
      ARROW_LEFT  = 256 + 75,
      ARROW_RIGHT = 256 + 77
    };
    
    
    
    
    
    
    
    
    using namespace std;
    
    int x=0,y=0;
    void gotoxy(int x, int y)             
    { 
    	HANDLE hConsoleOutput;
    hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    	COORD dwCursorPosition;
    
    	dwCursorPosition.X = x;
    	dwCursorPosition.Y = y;
    	
    	SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition);
    }
    
    
    
    char map[100][100] = {"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++",
    				      "---------------------------------------------------------",
    				      "fffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
                          "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr"
    } ;  
    int main ()
    {
    	
    	
    	gotoxy(0,0);
    	for (int j = 0; j < 4; j++) 
    	{
    		
    		cout << map[j] << endl;
    		
    	}
    	
    	while ( ( ch = get_code() ) != KEY_ESC ) {
        switch ( ch ) {
    		
        case ARROW_UP:
          gotoxy(x,y--);
    	  putchar(1);
          gotoxy(x,y++);
    	  putchar(map[y][x]);
    	  gotoxy(x,y--);
    	  break;
        case ARROW_DOWN:
          gotoxy(x,y++);
    	  putchar(1);
    	  gotoxy(x,y--);
    	  putchar(map[y][x]);
          gotoxy(x,y++);
          break;
        case ARROW_LEFT:
          gotoxy(x--,y);
    	  putchar(1);
    	  gotoxy(x++,y);
    	  putchar(map[y][x]);
    	  gotoxy(x--,y);
    	  break;
        case ARROW_RIGHT:
          gotoxy(x++,y);
    	  putchar(1);
    	  gotoxy(x--,y);
    	  putchar(map[y][x]);
    	  gotoxy(x++,y);
    	  
          break;
        }
    	}
    
    		
    		
    
    	  
    		  
    		  
    
        
    
    	
      return 0;
    }
    Last edited by o0obruceleeo0o; 04-25-2003 at 03:08 PM.

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    First, you are using the postfix autoincrement and decrement operators where you should be using prefix notation, (i.e. in your switch change x++ to ++x and the same with the minuses and the operations on y). Once you've done that, as long as you start with, for example the Right Arrow case, it sort of works.

    Your other problem is that you're position values can go negative and you are not handling that. If you start with the Left Arrow for example, it will screw up.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. REmoving a character from a character array
    By Bladactania in forum C Programming
    Replies: 3
    Last Post: 02-11-2009, 02:59 PM
  2. erase all characters in a character array?
    By diddy02 in forum C Programming
    Replies: 4
    Last Post: 11-20-2008, 05:30 PM
  3. Problem with Dynamically Increasing Array of Integers
    By laserlight in forum C++ Programming
    Replies: 30
    Last Post: 07-04-2008, 08:27 AM
  4. Help with 2-D character array
    By choykawairicky in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2004, 01:12 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21