I saw this tutorial on Youtube, how a guy made the snake game to be played on the console. Then I had an idea to make a game myself (just to understand the logic), so I started to copy some parts of the snake game and changed them so I can make the Alien shot game. So there's the ship at the bottom (in this case it's the * but I will make it look like a triangle later) , I made it move only left and right with A-D but not up-down, also I made it not cross the border. Also comparing with the snake game, I made it that the ship moves only for one block (character) for each time I press A or D. Now the problem is with the bullets. For the sake of simplicity I tried to make only one bullet. I wanted that whenever I press P, the bullet must be shot and move UP on it's own. But when you try the code, you'll see that I must press the P for each block the bullet (in this case the + ) moves. Also when I press P, everything I press after that also makes the bullet move up, before the P I can move only the ship, after P the bullet is being moved by every key. Can someone help me please?
So the goal is press P once, and the bullet will be shot at that actual width position of the ship but change the height by moving up ALONE, so no further pressing will be needed. Just help me with this case, I will figure out the rest.
p.s
You can watch the snake game tutorial here if you need to compare my code with his.
https://www.youtube.com/watch?v=E_-lMZDi7Uw
Also the full source code of the snake game is here:
http://paste4btc.com/Lu9Cvpd9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
bool gameover;
const int width = 40;
const int height = 20;
int x, y, i, j , p, o;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN};
eDirection ship;
eDirection point1;
void Setup()
{
gameover = false;
ship = STOP;
point1 = STOP;
x = width / 2;
y = height-1;
p = height - 2;
o = width / 2;
}
void Draw()
{
system("cls"); //system("clear");
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j == 0)
cout << "#";
if (i == y && j == x)
cout << "*";
else if (i == p && j == o)
cout << "+";
else
cout << " ";
if (j == width-1)
cout << "#";
}
cout << endl;
}
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
}
void Input()
{
switch (_getch())
{
case 'a':
ship = LEFT;
break;
case 'd':
ship = RIGHT;
break;
case 'x':
gameover = true;
break;
case 'p':
point1 = UP;
default:
ship = STOP;
break;
}
}
void Logic()
{
switch (ship)
{
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
default:
break;
}
switch (point1)
{
case UP:
p--;
break;
}
if (x < 0)
x = 0;
if (x > width-1)
x = width-1;
}
int main()
{
Setup();
{
while (!gameover)
{
Draw();
Input();
Logic();
Sleep(10);
}
}
return 0;
}
|