my code is showing some problem,

what i want it to do is that when the user hits space a ^ is shot from the position of the user and it moves +1 along y axis.
here is my code:

x2 = x; y2 = y // x = 50 and y is also = 50
char hit = getch();
while(true){
   if(hit = '36'){  // 36 is the value for space
    gotoxy(x,y);
      printf(" ");
x1 = lastx; y1 = lasty + 1; // lastx and lasty are the positions of the player
 gotoxy(x1,y1);
 std::cout<<"^";
 }
}

but its not working thanks again

Recommended Answers

All 9 Replies

I doubt it is compiling.

Line 4: You have used assignment = when you meant to use compare ==. '36' is not a valid character and it is not a number, if it compiled I expect it had the value 51 which is the ASCII value for the character 3. If 36 was the ASCII value of space then you want to use the value not a character/string of the value i.e. if (hit == 36) however 36 is actually the ASCII value for $ 32 is the ASCII value of space. A much better solution is to use the character ' ' not ASCII the value at all; this will protect you if your code happens to be ported to a system that uses an execution character set that is not ASCII (although that would be rather unusual these days) and is better practice in general giving if (hit == ' ')

Line 6/9: either use cout or use printf (preferably cout) but not both, they are not guaranteed to interoperate.

Finally your loop has no pauses, I imagin it will run so quickly you will not see anything happen. Also it has no end condition so once it's going you are stuck.

so i tried your method, its compiling but not working

if(hit == ' '){
//should i add loop here?
    gotoxy(x,y);
      printf(" ");
x2; y2 = lasty + 1;
 printf("^");
 Sleep(200);
 }

and for the loop where should i add, the for loop?

Describe what you mean by not working?

You already had everything in a loop, I think there is no need to add a second one.

well when i click space nothing shows up ( '^' does not show up)

Yes, you will need two loops if you want to be able to shoot repetitively; the outer one is your event loop, and the inner one is for animating the shot. You have the right idea for where you should put the loop; inside your if statement.

I have no idea what the rest of your code looks like. But let's try a little old fasion debugging here. Replace that section of code with this, and see if anything prints out on the screen.

printf("Ok, execution has reached this point.");
char hit = getchar();
if (hit == ' ') {
    printf("Space has been detected with getchar!\n");
}

Run the program and type <Space><Enter>. See if you can use that information to solve it and if not, post back with as much information you can.

Note: conio.h isn't a standard or well suported way of manipulationg text on a terminal. The only reason why you should be using it is if you're forced to for a (very questionable) school assignment.

no need, i solved the problem, check out my game...

#include <iostream>
#include <Windows.h>   
#include <conio.h>   
using namespace std;

#define BULLET_SPEED 70
#define ENEMY_SPEED 150

void gotoxy(int x, int y);

int main()
{

    // bullet variables **************************
    int bulletX = 0;
    int bulletY = 0;
    int timer = BULLET_SPEED;  // simple timer to slow bullet a bit
    bool Bulletalive = false;

    // enemy variables   **************************
    int enemyX = 0;
    int enemyY = 1;
    int enemyTimer = ENEMY_SPEED;
    int enemyAlive = true;
    char dirHorizontal = 'r';  

    bool playing = true;
    char ch;

    int shipXPos = 0, shipYPos = 23;

    do
    {       


        if (_kbhit())
        {

            ch = _getch();

            switch (ch)
            {
            case 'z':
                shipXPos--;
                if (shipXPos < 0) shipXPos = 0;
                break;
            case 'x':
                shipXPos++;
                if (shipXPos > 50) shipXPos = 50;
                break;
            case ' ':
                if (!Bulletalive)
                {
                    // found one, set its position to
                    // the ship, and set to active.
                    bulletX = shipXPos + 1;
                    bulletY = shipYPos;
                    Bulletalive = true;  
                }
                break;
            }       

            system("cls");
        }


        if (enemyAlive)
        {
            enemyTimer--;
            if (enemyTimer == 0)
            {

                gotoxy(enemyX, enemyY);
                cout << "   ";

                switch (dirHorizontal)
                {
                case 'l':
                    enemyX--;
                    if (enemyX == 0)
                        dirHorizontal = 'r';   // right
                    break;
                case 'r':
                    enemyX++;
                    if (enemyX == 50)
                        dirHorizontal = 'l';   // left
                    break;
                }

                gotoxy(enemyX, enemyY);
                cout << "###";
                enemyTimer = ENEMY_SPEED;
            }
        }

        if (Bulletalive)
        {
            timer--;
            if (timer == 0) {
                gotoxy(bulletX, bulletY);
                cout << " ";
                bulletY--;
                if (bulletY < 0)
                    Bulletalive = false;
                else
                {

                    gotoxy(bulletX, bulletY);
                    cout << "*";
                }
                timer = BULLET_SPEED;  // 10 seemed fine on this machine
            }
        }

        gotoxy(shipXPos, shipYPos);
        cout << " * ";
        gotoxy(shipXPos, shipYPos + 1);
        cout << "***";

        if (bulletX == enemyX && bulletY == enemyY && Bulletalive ||
            bulletX == enemyX + 1 && bulletY == enemyY && Bulletalive ||
            bulletX == enemyX + 2 && bulletY == enemyY && Bulletalive)
        {
            playing = false;
            gotoxy(20, 20);
            cout << "Well done you hit the enemy!";
            _getch();
        }

    } while (playing);

    return 0;

}


void gotoxy(int x, int y)
{
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

It's not working.

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.