-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrabalho-M1-Jogo-Bomberman.cpp
244 lines (211 loc) · 8.53 KB
/
Trabalho-M1-Jogo-Bomberman.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
Bomberman
Trabalho M1
Miguel Angel Morán
Matheus Moritz Furlan
*/
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <time.h> ///contar o tempo
#define M_LINHAS 15
#define M_COLUNAS 15
using namespace std;
const int M = 3;
bool verificaColisao(int m[M_LINHAS][M_COLUNAS], int x, int y) {
if (m[x][y] == 0 || m[x][y] == 4) {
return true;
} else {
return false;
}
}
void bomba(int m[M_LINHAS][M_COLUNAS], int x, int y, bool *flagBomba){
if (!*flagBomba){ //verifica se a bomba já existe, se não existe, coloca uma bomba
*flagBomba = true; //altera a flag informando que agora existe uma bomba
m[x][y] = 3; // coloca a bomba no mapa
}
}
void movimentoInimigo(int m[M_LINHAS][M_COLUNAS], int &ex, int &ey) {
int direcao = rand() % 4; // escolhe uma direção aleatória
int passo = rand() % 3 + 1; // escolhe um número aleatório de passos
for (int i = 0; i < passo; i++) {
if (direcao == 0 && verificaColisao(m, ex - 1, ey)) {
ex--;
} else if (direcao == 1 && verificaColisao(m, ex + 1, ey)) {
ex++;
} else if (direcao == 2 && verificaColisao(m, ex, ey - 1)) {
ey--;
} else if (direcao == 3 && verificaColisao(m, ex, ey + 1)) {
ey++;
} else {
break;
}
}
}
int fimDeJogo() {
cout<<"\nFIM DE JOGO!";
return 0;
}
int main()
{
///ALERTA: N�O MODIFICAR O TRECHO DE C�DIGO, A SEGUIR.
//INICIO: COMANDOS PARA QUE O CURSOR N�O FIQUE PISCANDO NA TELA
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(out, &cursorInfo);
cursorInfo.bVisible = false; // set the cursor visibility
SetConsoleCursorInfo(out, &cursorInfo);
//FIM: COMANDOS PARA QUE O CURSOR N�O FIQUE PISCANDO NA TELA
//IN�CIO: COMANDOS PARA REPOSICIONAR O CURSOR NO IN�CIO DA TELA
short int CX=0, CY=0;
COORD coord;
coord.X = CX;
coord.Y = CY;
//FIM: COMANDOS PARA REPOSICIONAR O CURSOR NO IN�CIO DA TELA
///ALERTA: N�O MODIFICAR O TRECHO DE C�DIGO, ACIMA.
int m[M_LINHAS][M_COLUNAS]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,2,2,2,0,0,0,0,0,1,
1,0,1,0,1,0,1,2,1,2,1,0,1,0,1,
1,0,0,0,0,0,2,2,2,2,2,0,0,0,1,
1,0,1,0,1,0,1,2,1,2,1,0,1,0,1,
1,0,0,2,0,2,2,2,2,2,2,2,0,0,1,
1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,
1,2,2,2,2,2,2,2,2,2,2,2,2,2,1,
1,2,1,0,1,2,1,2,1,0,1,2,1,2,1,
1,0,0,0,2,2,2,2,2,2,2,2,0,0,1,
1,0,1,0,1,2,1,2,1,2,1,2,1,0,1,
1,0,0,0,0,2,2,2,2,2,2,2,0,0,1,
1,0,1,0,1,0,1,2,1,2,1,0,1,0,1,
1,0,0,0,0,0,2,2,2,2,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
};
//Posi��o inicial do personagem no console
int x=13, y=1;
//Variavel para tecla precionada
char tecla;
int ex1 = 1, ey1 = 1; // posição do primeiro inimigo
int ex2 = 13, ey2 = 13; // posição do segundo inimigo
int ex3 = 9, ey3 = 1; // posição do segundo inimigo
int contadorMovimentoInimigo = 0; // contador para controlar o movimento dos inimigos
//Variável para verificar se já existe bomba
bool flagBomba = false;
int bx, by; //localização da bomba
clock_t inicio, fim; //declaração das entidades de contagem de tempo para armazenar inicio e fim do período da bomba
while(true){
///Posiciona a escrita no início do console
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
///Imprime o jogo: mapa e personagem.
for(int i=0;i<M_LINHAS;i++){
for(int j=0;j<M_COLUNAS;j++){
if(i==x && j==y){
SetConsoleTextAttribute(out, 14);
cout<<char(2); //personagem
} else if (i==ex1 && j==ey1) {
SetConsoleTextAttribute(out, 10);
cout<<char(153); // primeiro inimigo
} else if (i==ex2 && j==ey2) {
SetConsoleTextAttribute(out, 13);
cout<<char(154); // segundo inimigo
} else if (i==ex3 && j==ey3) {
SetConsoleTextAttribute(out, 11);
cout<<char(152); // tersero inimigo
} else {
switch (m[i][j]){
case 0: SetConsoleTextAttribute(out, 2); /// cor do caminho
cout<<char(176); break; //caminho
case 1: SetConsoleTextAttribute(out, 17); /// cor da parede solida
cout<<char(219); break; //parede
case 2: cout<<"#"; break; //parede quebrável
case 3: SetConsoleTextAttribute(out, 8);/// cor da bomba
cout<<char(207); break; //bomba
case 4: SetConsoleTextAttribute(out, 12);/// cor quando explode a bomba
cout<<"*"; break; //explosão da bomba
//default: cout<<"-"; //erro
} //fim switch
}
SetConsoleTextAttribute(out, 15);
}
cout<<"\n";
} //fim for mapa
contadorMovimentoInimigo++;
if (contadorMovimentoInimigo == 50) { // move os inimigos a cada 10 iterações do loop
movimentoInimigo(m, ex1, ey1);
movimentoInimigo(m, ex2, ey2);
movimentoInimigo(m, ex3, ey3);
contadorMovimentoInimigo = 0;
}
if (m[x][y] == 4) { // condição de fim : explodiu com a bomba
return fimDeJogo();
}
if ((x == ex1 && y == ey1) || (x == ex2 && y == ey2) || (x == ex3 && y == ey3)) {
return fimDeJogo(); // o jogador colidiu com um inimigo
}
if (m[ex1][ey1] == 4) {
ex1 = -1; // o primeiro inimigo foi atingido pela explosão da bomba
}
if (m[ex2][ey2] == 4) {
ex2 = -1; // o segundo inimigo foi atingido pela explosão da bomba
}
if (m[ex3][ey3] == 4) {
ex3 = -1; // o terceiro inimigo foi atingido pela explosão da bomba
}
if (ex1 == -1 && ex2 == -1 && ex3) {
cout << "\nVOCÊ VENCEU!";
return 0; // verifica se o jogador matou todos os inimigos
}
///executa os movimentos
if ( _kbhit() ){
tecla = getch();
switch(tecla)
{
case 72: case 'w': ///cima
if (verificaColisao(m, x-1, y)){
x--;
}
break;
case 80: case 's': ///baixo
if (verificaColisao(m, x+1, y)){
x++;
}
break;
case 75:case 'a': ///esquerda
if (verificaColisao(m, x, y-1)){
y--;
}
break;
case 77: case 'd': ///direita
if (verificaColisao(m, x, y+1)) {
y++;
}
break;
case 32:
if (!flagBomba){
inicio = clock();
bx = x;
by = y;
}
bomba(m,x,y,&flagBomba);
break;
}
}
if (flagBomba) {
fim = clock();
if ((fim-inicio)/CLOCKS_PER_SEC == 3) {
m[bx][by]=4;
if (m[bx+1][by]==0 || m[bx+1][by]==2) m[bx+1][by] = 4;
if (m[bx-1][by]==0 || m[bx-1][by]==2) m[bx-1][by] = 4;
if (m[bx][by+1]==0 || m[bx][by+1]==2) m[bx][by+1] = 4;
if (m[bx][by-1]==0 || m[bx][by-1]==2) m[bx][by-1] = 4;
}
if ((fim-inicio)/CLOCKS_PER_SEC == 4) {
m[bx][by]=0;
if (m[bx+1][by]!=1) m[bx+1][by] = 0;
if (m[bx-1][by]!=1) m[bx-1][by] = 0;
if (m[bx][by+1]!=1) m[bx][by+1] = 0;
if (m[bx][by-1]!=1) m[bx][by-1] = 0;
flagBomba=false;
}
}
} //fim do laço do jogo
return 0;
} //fim main