-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bomb.cpp
79 lines (67 loc) · 1.97 KB
/
Bomb.cpp
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
#include "Bomb.h"
#include "TetrisBoard.h"
#include "ScoreBar.h"
Bomb::Bomb()
{
SIZE = 1;
shape[0].setY(Point::START_Y);
shape[0].setX(Point::START_X);
setTextColor(whichColor(BOMB));
setShape(BOMB);
setTexture('@');
}
int Bomb::move(int direction, TetrisBoard& board) {
if (checkBomb(direction, board)) // the bomb needs to explode
return TetrisBoard::BOMB_EXPLODED;
if (board.checkPos(this, direction) == TetrisBoard::BOTTOM_ENCOUNTER)
return TetrisBoard::MOVE_FAIL;
shape[0].draw(BLANK_SPACE);
if (direction != UP) { // the shape won't rotate
shape[0].move(direction);
}
setTextColor(whichColor());
shape[0].draw(getTexture());
return TetrisBoard::MOVED_SUCCESSFULLY;
}
bool Bomb::checkBomb(int direction, TetrisBoard& board) {
int x = shape[0].getX(), y = shape[0].getY();
shape[0].draw(getTexture());
// checks if the bomb can explode in the direction entered
if ((direction == LEFT && board.checkBoard(x - 1, y) == true) ||
(direction == RIGHT && board.checkBoard(x + 1, y) == true) ||
(board.checkBoard(x, y + 1) == true) || y >= Board_Gap + ROWS - 1)
{
board.sethowManyDeleted(activateBomb(x, y, board)); // update the pieces that have been blown
return true;
}
gotoxy(x, y);
return false;
}
int Bomb::activateBomb(int x, int y, TetrisBoard& board) {
int tempX, tempY = y - 1, howManyBombed = 0;
for (int i = 0; i < EXPLOSION_AREA; i++)
{
tempX = x - 1;
for (int j = 0; j < EXPLOSION_AREA; j++) {
// if it's within the board limits and a shape has been met, update the counter (howManyBombed)
if (tempX > 0 && tempX <= COLUMNS && tempY > Board_Gap && tempY < ROWS + Board_Gap) {
if (board.getCoord(tempX, tempY))
howManyBombed++;
// explosion effect
if (j % 2 == 0)
setTextColor(YELLOW);
else
setTextColor(RED);
gotoxy(tempX, tempY);
cout << "*";
Sleep(50);
gotoxy(tempX, tempY);
cout << BLANK_SPACE;
board.setCoord(tempX, tempY, 0);
}
tempX++;
}
tempY++;
}
return howManyBombed;
}