I am trying to make a program where when I click an arrow key, the object moves constantly until I click another arrow key; or, when my object reaches a wall and crashes. For my code, I have made arrow key movements, I have assigned them in the program, and I have an object that moves. I have tried a do-while loop, however that does not work. I tried to put the do-while after my movement = _getch(), but that keeps moving until either x = 20 or y = 30. I cannot click another arrow for the object to change it's course of direction while the object is moving. Can anyone please help me out??? This is the code I have created so far. Thank You all for taking your time in looking at my code.

_ Zvjezdan
_
_
_
_

#include <iostream>
#include <string>
#include <windows.h>
#include <ctime>
#include <conio.h>

using namespace std;


void delay(long seconds) 
{
    clock_t time1 = clock();  // use clock time
    clock_t time2 = time1 + seconds;
    while(time1 < time2)
        time1 = clock();
    return;
}

int gotoxy(int x, int y) // used to make 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 object_() { cout << "-" << endl; }

#define UP_ARROW    72
#define LEFT_ARROW  75
#define DOWN_ARROW  80
#define RIGHT_ARROW 77

struct Player
{
    char letter1, letter2, letter3, letter4;
    char up_arrow, down_arrow, left_arrow, right_arrow, answer1;
};

int main()
{
    Player first_player;

    cout << "Do you want to use Letters[L/l] for movement or arrows[A/a] for movement? ";
    first_player.answer1 = _getch();
    cout << endl;

    if(first_player.answer1 == 'L' || first_player.answer1 == 'l')
    {
        cout << "What is your up movement? ";
        first_player.letter1 = _getch(); cout << first_player.letter1 << endl;

        cout << "What is your down movement? ";
        first_player.letter2 = _getch(); cout << first_player.letter2 << endl;

        cout << "What is your left movement? ";
        first_player.letter3 = _getch(); cout << first_player.letter3 << endl;

        cout << "What is your right movement? ";
        first_player.letter4 = _getch(); cout << first_player.letter4 << endl;
    }

    else if(first_player.answer1 == 'A' || first_player.answer1 == 'a')
    {
        cout << "Press the up arrow: ";
        first_player.up_arrow = _getch();

        if(_kbhit())
        {
            first_player.up_arrow = _getch();

            if(first_player.up_arrow == UP_ARROW)
            {
                first_player.up_arrow = UP_ARROW;
                cout << first_player.up_arrow << endl;
            }
        }


        cout << "Press the down arrow: ";
        first_player.down_arrow = _getch();

        if(_kbhit())
        {
            first_player.down_arrow = _getch();

            if(first_player.down_arrow == DOWN_ARROW)
            {
                first_player.down_arrow = DOWN_ARROW;
                cout << first_player.down_arrow << endl;
            }
        }


        cout << "Press the left arrow: ";
        first_player.left_arrow = _getch();

        if(_kbhit())
        {
            first_player.left_arrow = _getch();

            if(first_player.left_arrow == LEFT_ARROW)
            {
                first_player.left_arrow = LEFT_ARROW;
                cout << first_player.left_arrow << endl;
            }
        }


        cout << "Press the right arrow: ";
        first_player.right_arrow = _getch();

        if(_kbhit())
        {
            first_player.right_arrow = _getch();

            if(first_player.right_arrow == RIGHT_ARROW)
            {
                first_player.right_arrow = RIGHT_ARROW;
                cout << first_player.right_arrow << endl;
            }
        }
    }

    system("cls");
    system("mode 100,50");

    char movement;
    int x = 1, y = 1;

    if(first_player.answer1 == 'l' || first_player.answer1 == 'L')
    {
        gotoxy(x,y); object_();

        move_object: gotoxy(3,35); cout << "Where do you want to move? ";
        movement = _getch();
        cout << endl;

        // movement for letters

        do {
        if(movement == first_player.letter1)
        {
            y -= 1;
            gotoxy(x, y); object_();

            if( y == 0 || y == 30) { goto end_of_program; }
            movement = _getch();
        }

        if(movement == first_player.letter2)
        {
            y += 1;
            gotoxy(x, y); object_();

            if( y == 0 || y == 30) { goto end_of_program; }
            movement = _getch();
        }

        if(movement == first_player.letter3)
        {
            x -= 1;
            gotoxy(x, y); object_();

            if( x == 0 || x == 20) { goto end_of_program; }
            movement = _getch();
        }

        if(movement == first_player.letter4)
        {
            x += 1;
            gotoxy(x, y); object_();

            if( x == 0 || x == 20) { goto end_of_program; }
            movement = _getch();
        }

        } while (x != 0 || x != 20 || y != 0 || y != 30);
    }


    else if(first_player.answer1 == 'a' || first_player.answer1 == 'A')
    {
        gotoxy(x,y); object_();

        move_object_with_arrow: gotoxy(3,35); cout << "Where do you want to move? ";
        movement = _getch();
        cout << endl;

        // movement for arrows

        if(_kbhit())
        {
            do { 
            movement = _getch();

            if(movement == first_player.up_arrow)
            {
                y -= 1;
                gotoxy(x, y); object_();

                if( y == 0 || y == 30) { goto end_of_program; }
                movement = _getch();
            }

            if(movement == first_player.down_arrow)
            {
                y += 1;
                gotoxy(x, y); object_();

                if( y == 0 || y == 30) { goto end_of_program; }
                movement = _getch();
            }


            if(movement == first_player.left_arrow || movement == first_player.letter3)
            {
                x -= 1;
                gotoxy(x, y); object_();

                if( x == 0 || x == 20) { goto end_of_program; }
                movement = _getch();
            }

            if(movement == first_player.right_arrow || movement == first_player.letter4)
            {
                x += 1;
                gotoxy(x, y); object_();

                if( x == 0 || x == 20) { goto end_of_program; }
                movement = _getch();
            }

            } while (x != 0 || x != 20 || y != 0 || y != 30);
        }
    }

    end_of_program: gotoxy(3,43); system("pause");
    return 0;
}

Dani AI

Generated

— the core issues in the thread are (1) blocking input inside the movement loop, (2) incorrect loop/boundary logic, and (3) unnecessary busy-waiting/goto use. The right pattern is: keep a current-direction state, run a single game loop that moves the object every tick, and only poll the keyboard non‑blockingly to update that state.

Key points and fixes

  • Do not call _getch() as the loop’s step driver — it blocks. Use _kbhit() to check for input and read keys only when available, while the loop still advances the object each tick (use Sleep(ms) to control speed instead of a busy loop).
  • Arrow keys in the Windows console come as a two-byte sequence: the first _getch() returns 0 or 224, the second returns the actual code (72/80/75/77). When binding arrows, read both bytes and store the second value.
  • The posted while condition while (x != 0 || x != 20 || y != 0 || y != 30) is logically wrong (it is always true). Use a bounds check like while (x > 0 && x < MAX_X && y > 0 && y < MAX_Y) or break on collision.
  • Avoid goto for flow control — use a clear loop and break.

Minimal movement loop (concept)

int dir = 0; // 0:none, 1:up,2:down,3:left,4:right
const int MAX_X = 20, MAX_Y = 30;
int x = 1, y = 1;
drawAt(x,y);

while (x > 0 && x < MAX_X && y > 0 && y < MAX_Y) {
  if (_kbhit()) {
    int ch = _getch();
    if (ch == 0 || ch == 224) {
      int arrow = _getch();
      if (arrow == 72) dir = 1; else if (arrow == 80) dir = 2;
      else if (arrow == 75) dir = 3; else if (arrow == 77) dir = 4;
    } else {
      // map letter bindings to dir if needed
    }
  }
  clearAt(x,y);               // erase previous char
  switch(dir) { case 1: --y; break; case 2: ++y; break;
                 case 3: --x; break; case 4: ++x; break; }
  drawAt(x,y);                // draw at new pos
  Sleep(60);                  // control speed; tweak ms as needed
}

Troubleshooting notes

  • If movement seems unresponsive, reduce Sleep() and ensure _kbhit() branch runs.
  • To avoid trails, clear the old cell before drawing the new one.
  • For debugging, print the numeric values returned by _getch() when pressing keys to confirm what is being read.

This approach keeps the object moving continuously, lets a new arrow change direction immediately, avoids CPU-spinning delays, and fixes the loop/boundary logic seen in the original code.

By the way, gotoxy(num,num); is used for placing the object or word or whatever on the screen. Sorry, I didn't take that out. I thought I put the resize thing next to system("mode 100,50");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.