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 111 112 113 114 115 116
| // Insère la pièce dans le jeu
bool
Tetris::insert() {
if (
verify(
Pos2D(current_brick.get_position(), 15),
Pos2D(current_brick.get_width(), current_brick.get_height())
)
)
return true;
for (
int i = current_brick.get_position();
i < current_brick.get_position() + current_brick.get_width();
i++
)
for (int j = 15; j > 15 - current_brick.get_height(); j--) {
Case c = current_brick(
i - current_brick.get_position(),
15 - j
);
if (c == ACTIVE)
grid[i][15 - j] = ACTIVE;
}
return false;
}
/*
* Vérifie s'il est possible que la pièce descende.
* Renvoie true s'il y a un hit.
*/
bool
Tetris::verify(Pos2D p, Pos2D dim) {
for (int i = p.x; i < p.x + dim.x; i++)
for (int j = p.y - dim.y + 1; j <= p.y; j++)
if (grid[i][j] != EMPTY && current_brick(i - p.x, j - p.y + dim.y - 1) == ACTIVE)
return true;
return false;
}
// Finalise le déplacement de la brique
void
Tetris::set() {
for (int i = 0; i < 8; i++)
for (int j = 0; j < 16; j++)
if (grid[i][j] == ACTIVE)
grid[i][j] = blue ? BLUE : RED;
}
void
Tetris::unset() {
for (int i = 0; i < 8; i++)
for (int j = 0; j < 16; j++)
if (grid[i][j] == ACTIVE)
grid[i][j] = EMPTY;
}
// Déplace la brique
void
Tetris::place(Pos2D p, Pos2D dim) {
for (int i = p.x; i < p.x + p.x + dim.x; i++)
for (int j = p.y - dim.y + 1; j <= p.y; j++)
if (current_brick(i - p.x, j - (p.y - dim.y + 1)))
grid[i][j] = ACTIVE;
}
// Méthode clé : permet de faire tomber une brique dans le jeu
void
Tetris::fall() {
place_mode = false;
Pos2D p(current_brick.get_position(), 15);
Pos2D dim(current_brick.get_width(), current_brick.get_height());
// Insertion de la pi�ce
bool hit = insert();
p.y--;
while (!hit || p.y - current_brick.get_height() + 1) {
unset();
hit = verify(p, dim);
place(p, dim);
p.y--;
}
set();
}
/*
* Permet d'accéder à la matrice contenant la structure de
* la brique sans se préoccuper de la rotation
*/
Tetris::Case
Tetris::Brick::operator()(int x, int y) const {
int temp = x;
temp = x;
// Calcul de x et de y
if (rotation == 1) {
temp = x;
x = height - 1 - y;
y = temp;
}
else if (rotation == 2) {
x = width - 1 - x;
y = height - 1 - y;
}
else if (rotation == 3) {
x = y;
y = temp;
}
return brick_matrix[x * y + x];
} |
Partager