-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
136 lines (109 loc) · 2.75 KB
/
Game.cpp
File metadata and controls
136 lines (109 loc) · 2.75 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "Game.h"
#include <iostream>
#include "Board.h"
#include <conio2.h>
#include "TetrominoI.h"
#include "TetrominoL.h"
#include <ctime>
#include "TetrominoJ.h"
#include "TetrominoT.h"
#include "TetrominoZ.h"
#include "TetrominoS.h"
#include "TetrominoSquare.h"
using namespace std;
Board board;
Game::Game() {
board.printBoard();
gotoxy(15, 2);
cout << "Lineas: 0";
gotoxy(15,4);
cout << "Z: rotar a la izquierda" << endl;
gotoxy(15,5);
cout << "X: rotar a la derecha";
gotoxy(15,6);
cout << "Desplazamiento con las flechas direccionales";
currentTetromino = generateNextTetromino();
board.setCurrentTetromino(*currentTetromino);
while(playing){
if(kbhit()) {
int tecla=getch();
string directionToDetectCollision;
if(tecla == 120 || tecla == 88){
// X
// board.getCurrentTetromino()->rotateRight();
board.rotateTetrominoRightIfValid();
directionToDetectCollision = "right";
}else if(tecla == 122 || tecla == 90){
// Z
board.rotateTetrominoLeftIfValid();
directionToDetectCollision = "left";
}else if(tecla == 77){
// move Right
if(board.isValidRight()){
board.getCurrentTetromino()->moveRight();
}
directionToDetectCollision = "right";
}else if(tecla == 75){
// move Left
if(board.isValidLeft()){
board.getCurrentTetromino()->moveLeft();
}
directionToDetectCollision = "left";
}else if(tecla == 80){
// Down
board.getCurrentTetromino()->setPosition(board.getCurrentTetromino()->getXPosition(), board.getCurrentTetromino()->getYPosition() + 1);
directionToDetectCollision = "down";
}
//board.correctPositionWhenMovementIsNotValid(directionToDetectCollision);
}
board.clearBoard();
board.printTetromino();
if(board.currentTetrominoIsCollidingWithFixedPiece()){
board.fixCurrentTetromino();
board.checkLines();
isGameOver();
spawnTetromino();
}
board.printBoard();
if(tempo + step < clock()){
board.getCurrentTetromino()->fall();
tempo = clock();
}
}
}
Tetromino* Game::generateNextTetromino(){
srand(time(NULL));
int randomNumber = rand() % 7 + 1;
Tetromino *nextTetromino;
switch(randomNumber){
case 1:
nextTetromino = new TetrominoI();
break;
case 2:
nextTetromino = new TetrominoL();
break;
case 3:
nextTetromino = new TetrominoJ();
break;
case 4:
nextTetromino = new TetrominoT();
break;
case 5:
nextTetromino = new TetrominoZ();
break;
case 6:
nextTetromino = new TetrominoS();
break;
case 7:
nextTetromino = new TetrominoSquare();
break;
}
return nextTetromino;
}
void Game::spawnTetromino(){
currentTetromino = generateNextTetromino();
board.setCurrentTetromino(*currentTetromino);
}
void Game::isGameOver(){
playing = !board.isBoardFull();
}