This is the enemy update code which updates the enemys position, it checks the value in
dirHorizontal
to see if its a
l
or
r
(left or right so it knows which direction it needs to go) and adds or subtracts from the ships
enemyX
position.
It starts off moving right, when it reaches position 50, it changes
dirHorizontal
to
l
to tell it we want to move left, then it rinses and repeats.
Timer is a simple countdown delay variable, basically what happens here is the function is run continuous but each time its run it subtracts 1 from timer, when the timer variable hits 0 it will perform the update on the ship, then reset the timer back to the delay value held in ENEMY_SPEED. What this does is delay the movement code so the ship doesn't run too quick.
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
|
if (enemyAlive)
{
enemyTimer--;
if (enemyTimer == 0)
{
// clear old
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;
}
}
|
||
means OR so in a IF statement you can do things like
1 2
|
if(name == "Michael" || name == "John")
//.... do something
|
The above would do something if the name variable contained Michael or John.
What I am doing in the next code is I am checking if the
bulletX
and
enemyX
are the same,
bulletY
and
enemyY
are the same and if they are we hit the ship with the bullet. The || statement is there because the ship is three characters wide therefore bullet would hit in 3 different x positions (hence the +1, +2)... see what I mean?
1 2 3 4 5 6 7 8 9
|
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();
}
|