0

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;
}
1
Contributor
1
Reply
2
Views
5 Years
Discussion Span
Last Post by zvjezdan.veselinovic
0

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");

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.