-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
184 lines (169 loc) · 4.19 KB
/
game.cpp
File metadata and controls
184 lines (169 loc) · 4.19 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "game.h"
#include <random>
Game::Game(){
grid = Grid();
blocks = GetAllBlocks();
currentBlock = GetRandomBlock();
nextBlock = GetRandomBlock();
gameOver = false;
score = 0;
level = 1;
lines = 0;
// Ponemos sonido a nuestro juego de tetris
InitAudioDevice();
music = LoadMusicStream("Sounds/original-tetris-theme.mp3");
PlayMusicStream(music);
rotateSound = LoadSound("Sounds/rotate.mp3");
clearSound = LoadSound("Sounds/clear.mp3");
gameSound = LoadSound("Sounds/game-over-tetris.mp3");
}
Game::~Game(){
UnloadSound(rotateSound);
UnloadSound(clearSound);
UnloadMusicStream(music);
UnloadSound(gameSound);
CloseAudioDevice();
}
Block Game::GetRandomBlock(){
if (blocks.empty()){
blocks = GetAllBlocks();
}
int randomIndex = rand() % blocks.size();
Block block = blocks[randomIndex];
blocks.erase(blocks.begin() + randomIndex);
return block;
}
std::vector<Block> Game::GetAllBlocks(){
return {IBlock(), JBlock(), LBlock(), OBlock(), SBlock(), TBlock(), ZBlock()};
}
void Game::Draw(){
grid.Draw();
currentBlock.Draw(11, 11); // Para darle la posicion de los bloques normales
switch (nextBlock.id){
case 3:
nextBlock.Draw(255, 505); // Bloque OBloque
break;
case 4:
nextBlock.Draw(255, 495); // Bloque IBlock
break;
default:
nextBlock.Draw(270, 485); // Pintar el proximo bloque en pantalla
break;
}
}
void Game::HandleInput(){
int keyPressed = GetKeyPressed();
if(gameOver && keyPressed != 0){
gameOver = false;
Reset();
}
switch (keyPressed){
case KEY_LEFT:
MoveBlockLeft();
break;
case KEY_RIGHT:
MoveBlockRight();
break;
case KEY_DOWN:
MoveBlockDown();
UpdateScore(0, 1);
break;
case KEY_UP:
RotateBlock();
break;
}
}
void Game::MoveBlockLeft(){
if (!gameOver){
currentBlock.Move(0, -1);
if (IsBlockOutside() || BlockFits() == false){
currentBlock.Move(0, 1);
}
}
}
void Game::MoveBlockRight(){
if (!gameOver){
currentBlock.Move(0, 1);
if (IsBlockOutside() || BlockFits() == false){
currentBlock.Move(0, -1);
}
}
}
void Game::MoveBlockDown(){
if (!gameOver){
currentBlock.Move(1, 0);
if (IsBlockOutside() || BlockFits() == false){
currentBlock.Move(-1, 0);
LockBlock();
}
}
}
bool Game::IsBlockOutside(){
std::vector<Position> tiles = currentBlock.GetCellPositions();
for (Position item : tiles){
if (grid.IsCellOutside(item.row, item.column)){
return true;
}
}
return false;
}
void Game::RotateBlock(){
if (!gameOver){
currentBlock.Rotate();
if (IsBlockOutside() || BlockFits() == false){
currentBlock.UndoRotation();
} else {
PlaySound(rotateSound);
}
}
}
void Game::LockBlock(){
std::vector<Position> tiles = currentBlock.GetCellPositions();
for (Position item : tiles){
grid.grid[item.row][item.column] = currentBlock.id;
}
currentBlock = nextBlock;
if(BlockFits() == false){
gameOver = true;
PlaySound(gameSound);
}
nextBlock = GetRandomBlock();
int rowsCleared = grid.ClearFullRows();
if (rowsCleared > 0){
PlaySound(clearSound);
UpdateScore(rowsCleared, 0);
lines += rowsCleared;
}
}
bool Game::BlockFits(){
std::vector<Position> tiles = currentBlock.GetCellPositions();
for (Position item : tiles){
if (grid.IsCellEmpty(item.row, item.column) == false){
return false;
}
}
return true;
}
void Game::Reset(){
grid.Initialize();
blocks = GetAllBlocks();
currentBlock = GetRandomBlock();
nextBlock = GetRandomBlock();
score = 0;
}
void Game::UpdateScore(int linesCleared, int moveDownPoints){
switch (linesCleared){
case 1:
score += 100;
break;
case 2:
score += 300;
break;
case 3:
score += 500;
break;
default:
break;
}
score += moveDownPoints;
}