Thread: constant movement using arrow keys

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    3

    constant movement using arrow keys

    Hi everyone. My name is Zvjezdan Veselinovic and I am studying Game and Simulation Programming at Rasmussen College over in New Port Richey, Florida. My question pertains to a "Tron-like" game that I am creating. It is called "Colors and Explosions". For my game, I have arrow key movement and a moving object. My question is: "How do I make it so that when I press an arrow key, my object will constantly move until I press another arrow key?" I need help on this. Any help would be appreciated, really.

    Code:
    // My "Things_in_background.h" header file code
    
    #include <iostream>
    #include <string>
    #include <windows.h> // resize the screen, gotoxy, delay, and so on.
    #include <iomanip> // GetAsyncKeyState(), GetKeyState(), and so on.
    #include <ctime>
    #include <conio.h> // _getch(), _kbhit()
    
    using namespace std;
    
    
    int gotoxy(int x, int y) // used to place things anywhere in the window
    {  
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        COORD point;
           point.X = x-1;
        point.Y = y-1;     
           SetConsoleCursorPosition(hConsole, point);
           return SetConsoleCursorPosition(hConsole, point);
    }
    
    
    void Continue_() { cout << "Press any key to continue... "; _getch(); }
    void Space_() { cout << "Press SPACE to continue... "; _getch(); }
    void Clear_Line() { gotoxy(5,35); cout << "                                                             " << endl; }
    
    void Up_arrow() { printf("%c", 30); }
    void Down_arrow() { printf("%c", 31); }
    void Left_arrow() { printf("%c", 17); }
    void Right_arrow() { printf("%c", 16); }
    
    void Object() { cout << "&" << endl; }

    Code:
    // my main_program.cpp file code
    
    #include "Things_in_background.h"
    
    struct Player_Presses
    {
        char up_arrow, down_arrow, left_arrow, right_arrow;
    };
    
    int main()
    {
        Player_Presses player1;
    
        char movement;
    
        int objectx = 2, objecty = 2;
    
    
    
        cout << "Press the Up arrow: "; player1.up_arrow = _getch();
    
        if(_kbhit())
        {
            player1.up_arrow = _getch(); Up_arrow();
    
            if(player1.up_arrow == GetAsyncKeyState(VK_UP))
            {
                player1.up_arrow = VK_UP;
            }
        } cout << endl << endl;
    
    
    
        cout << "Press the Down arrow: "; player1.down_arrow = _getch();
    
        if(_kbhit())
        {
            player1.down_arrow = _getch(); Down_arrow();
    
            if(player1.down_arrow == GetAsyncKeyState(VK_DOWN))
            {
                player1.down_arrow = VK_DOWN;
            }
        } cout << endl << endl;
    
    
    
        cout << "Press the Left arrow: "; player1.left_arrow = _getch();
    
        if(_kbhit())
        {
            player1.left_arrow = _getch(); Left_arrow();
    
            if(player1.left_arrow == GetAsyncKeyState(VK_LEFT))
            {
                player1.left_arrow = VK_LEFT;
            }
        } cout << endl << endl;
    
    
    
        cout << "Press the Right arrow: "; player1.right_arrow = _getch(); 
    
        if(_kbhit())
        {
            player1.right_arrow = _getch(); Right_arrow(); 
    
            if(player1.right_arrow == GetAsyncKeyState(VK_RIGHT))
            {
                player1.right_arrow = VK_RIGHT;
            }
        } cout << endl << endl << endl << endl;
    
    
    
        cout << "Your arrows are..." << endl << endl << endl;
    
        cout << "Up: " << player1.up_arrow << endl << endl;
        cout << "Down: " << player1.down_arrow << endl << endl;
        cout << "Left: " << player1.left_arrow << endl << endl;
        cout << "Right: " << player1.right_arrow << endl << endl;
    
        Space_(); system("cls");
    
        system("mode 100,40");
    
        gotoxy(5,35); Space_();
        Clear_Line();
    
        gotoxy(objectx,objecty); Object();
    
        a: gotoxy(5,35); cout << "Press any key to move piece: "; movement = _getch(); 
    
    
        if(_kbhit())
        {
            do {
            movement = _getch();
    
            if(movement == player1.up_arrow)
            {
                objecty--;
                gotoxy(objectx, objecty); Object();
    
                Clear_Line(); goto a;
            }
            
            if(movement == player1.down_arrow)
            {
                objecty++;
                gotoxy(objectx, objecty); Object();
                Clear_Line(); goto a;
            }
    
            if(movement == player1.left_arrow)
            {
                objectx--;
                gotoxy(objectx, objecty); Object();
                Clear_Line(); goto a;
            }
    
            if(movement == player1.right_arrow)
            {
                objectx++;
                gotoxy(objectx, objecty); Object();
                Clear_Line(); goto a;
            }
    
            } while(objectx != 20 || objectx != 1 || objecty != 1 || objecty != 25);
    
        } // end of _kbhit()
    
        gotoxy(5,35); Continue_();
        return 0;
    }
    Thank You all for taking your time at looking at my code.

    - Zvjezdan Veselinovic

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    my only advice here is do not use goto! break your do...while loop out into its own function and return from it in every case where you say goto a. then replace the a: line label with a while (true) loop.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    3
    so would I do a do whatever movement while previous movement != what I just typed in? Is that what you're going for?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Perhaps before you create a 100+ line program with 4 keys to deal with (which doesn't work), you prototype your idea with a single key.

    Such as
    - you run the program, it waits for a key press
    - you press a key
    - it prints
    Moving
    Moving
    Moving
    Moving
    Moving
    Moving
    Moving
    Moving
    - you press another key and it stops.

    Then you extend that to two keys, and print "Moving Up" or "Moving Down" as appropriate.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    3
    Ok. I'll try and prototype it. Thank You. =)

  6. #6
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    Personally I;d just use four boolians, one for each direction. When a key is pressed it sets that direction to true and all other to false. Your movement function should check the boolians rather than the keys when deciding how to move the object, hope it helps!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrow keys
    By Katushai in forum C++ Programming
    Replies: 1
    Last Post: 03-27-2005, 01:43 PM
  2. Movement with arrow keys
    By louis_mine in forum C Programming
    Replies: 3
    Last Post: 02-06-2005, 04:35 PM
  3. arrow keys, ctrl and alt keys
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2002, 03:53 PM
  4. Arrow Keys
    By drdroid33 in forum C++ Programming
    Replies: 1
    Last Post: 02-06-2002, 07:07 PM
  5. Arrow Keys and Such
    By Thantos in forum Game Programming
    Replies: 5
    Last Post: 10-25-2001, 05:40 PM

Tags for this Thread