Código de C/Visual C - Juego de naves en caracteres ASCII

Requerimientos

Necesitas tener el S.O. Windows, preferiblemente Windows 10, ya que en los demás S.O. se verá un poco mal.

Puedes descargar el setup del juego en mi web, puedes acceder mediante este link:
https://hdb-programming.github.io/web/downloads/hShip.html

Puedes acceder a su repositorio en GitHub con este link:
https://github.com/HDB-PROGRAMMING/hShip

Visita mi blog con este link:
https://nosgustaprogramar.blogspot.com/

1.3.0.0
estrellaestrellaestrellaestrellaestrella(5)

Actualizado el 30 de Agosto del 2020 (Publicado el 28 de Junio del 2020)gráfica de visualizaciones de la versión: 1.3.0.0
5.353 visualizaciones desde el 28 de Junio del 2020
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <conio.h>
#include <list>
// Aquí incluimos las librerías necesarias
 
using namespace std;
// Aquí usamos los namespaces necesarios
 
#define UP_KEY 72
#define DOWN_KEY 80
#define LEFT_KEY 75
#define RIGHT_KEY 77
#define DEBUG_MODE false
// Aquí definimos las constantes necesarias
 
void gotoxy(int x, int y) {
	HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD dwPos;
	dwPos.X = x;
	dwPos.Y = y;
	SetConsoleCursorPosition(hCon, dwPos);
}
 
void hideCursor() {
	HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO cci;
	cci.dwSize = 50;
	cci.bVisible = FALSE;
	SetConsoleCursorInfo(hCon, &cci);
}
 
class ship {
	int x, y, health, lives;
 
public:
	ship(int _x, int _y, int _health, int _lives) : x(_x), y(_y), health(_health), lives(_lives) {};
	int getX() { return x; };
	int getY() { return y; };
	int getLives() { return lives; };
	void decrementHealth() { health--; };
	void incrementLives() { lives++; };
	void show();
	void clean();
	void move();
	void showHealth();
	void die();
};
 
void ship::show() {
	gotoxy(x, y);     printf("  %c", 65);
	gotoxy(x, y + 1); printf(" %c%c%c", 40, 207, 41);
	gotoxy(x, y + 2); printf("%c%c %c%c", 174, 190, 190, 175);
}
 
void ship::clean() {
	gotoxy(x, y);     printf("      ");
	gotoxy(x, y + 1); printf("      ");
	gotoxy(x, y + 2); printf("      ");
}
 
void ship::move() {
	if (kbhit()) {
		char key = getch();
		clean();
		if ((key == LEFT_KEY) && x > 3) x--;
		if ((key == RIGHT_KEY) && x < 111) x++;
		if ((key == UP_KEY) && y > 4) y--;
		if ((key == DOWN_KEY) && y < 25) y++;
		show();
		showHealth();
	}
}
 
void drawLimits() {
	gotoxy(2, 3);    printf("%c", 201);
	gotoxy(117, 3);  printf("%c", 187);
	gotoxy(2, 28);   printf("%c", 200);
	gotoxy(117, 28); printf("%c", 188);
	for (int i = 3; i < 117; i++) {
		gotoxy(i, 3);  printf("%c", 205);
		gotoxy(i, 28); printf("%c", 205);
	}
 
	for (int i = 4; i < 28; i++) {
		gotoxy(2, i);   printf("%c", 186);
		gotoxy(117, i); printf("%c", 186);
	}
}
 
void ship::showHealth() {
	gotoxy(70, 1); printf("Vidas: %d", lives);
	gotoxy(90, 1); printf("Salud: ");
	gotoxy(97, 1); printf("      ");
	for (int i = 0; i < health; i++) {
		gotoxy(97 + i, 1); printf("X");
	}
}
 
void ship::die() {
	if (health <= 0) {
		clean();
		gotoxy(x, y);     printf("  **  ");
		gotoxy(x, y + 1); printf(" **** ");
		gotoxy(x, y + 2); printf("  **  ");
		Sleep(200);
		// Frame 1 de la animación de muerte
 
		clean();
		gotoxy(x, y);     printf("* ** *");
		gotoxy(x, y + 1); printf(" **** ");
		gotoxy(x, y + 2); printf("* ** *");
		Sleep(300);
		// Frame 2 de la animación de muerte
 
		clean();
		gotoxy(x, y);     printf("  **  ");
		gotoxy(x, y + 1); printf(" **** ");
		gotoxy(x, y + 2); printf("  **  ");
		Sleep(200);
		clean();
		// Frame 3 de la animación de muerte
 
		lives--; health = 3;
		showHealth();
		show();
	}
}
 
class asteroid {
	int x, y;
 
public:
	asteroid(int _x, int _y) : x(_x), y(_y) {};
	int getX() { return x; };
	int getY() { return y; };
	void show();
	void move();
	void clean();
	void colision(class ship& Ship);
};
 
void asteroid::show() {
	gotoxy(x, y); printf("%c", 184);
}
 
void asteroid::clean() {
	gotoxy(x, y); printf(" ");
}
 
void asteroid::move() {
	clean(); y++;
 
	if (y > 27) {
		x = rand() % 113 + 3;
		y = 4;
	}
 
	Sleep(5);
	show();
}
 
void asteroid::colision(ship& Ship) {
	if ((x >= Ship.getX() && x < Ship.getX() + 5) && (y >= Ship.getY() && y < Ship.getY() + 3)) {
		Ship.decrementHealth();
		Ship.clean();
		Ship.show();
		Ship.showHealth();
		x = rand() % 113 + 3;
		y = 4;
	}
}
 
class bullet {
	int x, y;
 
public:
	bullet(int _x, int _y) : x(_x), y(_y) {};
	int getX() { return x; };
	int getY() { return y; };
	void move();
	bool out();
};
 
void bullet::move() {
	gotoxy(x, y); printf(" ");
	y--;
	gotoxy(x, y); printf("%c", 186);
}
 
bool bullet::out() {
	if (y == 4) return true;
	return false;
}
 
int main() {
	bool game_over = false;
	int points = 0;
	ship Ship(57, 25, 3, 4);
	list<asteroid*> asteroids;
	list<asteroid*>::iterator itA;
	for (int i = 0; i < 5; i++) {
		asteroids.push_back(new asteroid(rand() % 115 + 2, rand() % 3 + 4));
	}
	list<bullet*> bullets;
	list<bullet*>::iterator it;
	// Aquí creamos las variables, objetos y listas
 
	hideCursor();
	system("title hShip, un juego de naves en caracteres ASCII");
	// Aquí cambiamos valores de la consola
 
	drawLimits();
	gotoxy(58, 14); printf("HSHIP");
	gotoxy(41, 15); printf("Pulsa cualquier tecla para iniciar el juego");
	system("pause> NUL");
	gotoxy(58, 14); printf("     ");
	gotoxy(41, 15); printf("                                           ");
 
	Ship.show();
	Ship.showHealth();
	drawLimits();
 
	while (!game_over) {
		gotoxy(7, 1); printf("Puntos: %d", points);
 
		if ((points % 2000 == 0) && points != 0) {
			if ((points % 4000 == 0) && points != 0) {
				asteroids.push_back(new asteroid(rand() % 115 + 2, rand() % 3 + 4));
			}
			Ship.incrementLives();
			points += 100;
		}
 
		if (kbhit()) {
			char key = getch();
			if (key == 'z' || key == ' ') {
				bullets.push_back(new bullet(Ship.getX() + 2, Ship.getY() - 1));
			}
			if (key == 'p') {
				gotoxy(58, 14); printf("HSHIP");
				gotoxy(38, 15); printf("Pulsa cualquier tecla para continuar con el juego");
				system("pause> NUL");
				gotoxy(58, 14); printf("     ");
				gotoxy(38, 15); printf("                                                 ");
			}
			if (key == 'l' && DEBUG_MODE) {
				points += 100;
			}
			if (key == 'e' && DEBUG_MODE) {
				Ship.decrementHealth();
			}
		}
 
		for (it = bullets.begin(); it != bullets.end(); it++) {
			(*it)->move();
			if ((*it)->out()) {
				gotoxy((*it)->getX(), (*it)->getY()); printf(" ");
				delete(*it);
				it = bullets.erase(it);
			}
		}
 
		Ship.move();
		Ship.showHealth();
		Ship.die();
		for (itA = asteroids.begin(); itA != asteroids.end(); itA++) {
			(*itA)->move();
			(*itA)->colision(Ship);
		}
 
		for (itA = asteroids.begin(); itA != asteroids.end(); itA++) {
			for (it = bullets.begin(); it != bullets.end(); it++) {
				if (((*itA)->getX() == (*it)->getX()) && ((*itA)->getY() == (*it)->getY() || (*itA)->getY() + 1 == (*it)->getY())) {
					gotoxy((*it)->getX(), (*it)->getY()); printf(" ");
					delete(*it);
					it = bullets.erase(it);
 
					asteroids.push_back(new asteroid(rand() % 115 + 2, rand() % 3 + 4));
					gotoxy((*itA)->getX(), (*itA)->getY()); printf(" ");
					delete(*itA);
					itA = asteroids.erase(itA);
 
					points += 100;
				}
			}
		}
		// Aquí el bucle para mantener activos a la nave y los asteroides
 
		if (Ship.getLives() <= 0) {
			game_over = true;
		}
 
		Sleep(30);
	}
 
	while (game_over) {
		gotoxy(55, 14); printf("GAME OVER");
		gotoxy(43, 15); printf("Pulsa ESPACIO para salir del juego");
 
		if (kbhit()) {
			char key = getch();
			if (key == ' ') {
				exit(0);
			}
		}
	}
 
	return 0;
}



Comentarios sobre la versión: 1.3.0.0 (5)

9 de Julio del 2020
estrellaestrellaestrellaestrellaestrella
yo tengo una pregunta (no soy programador)pero me apasiona la carrera con que programas puedo leer tu algoritmo
Responder
Imágen de perfil
10 de Julio del 2020
estrellaestrellaestrellaestrellaestrella
Si te refieres con qué programo, uso Vim y compilo por terminal, pero para comenzar te recomiendo fuertemente Visual Studio para Windows, Xcode para Mac y KDevelop para Linux
Responder
18 de Julio del 2020
estrellaestrellaestrellaestrellaestrella
mira tu juego es divertido pero cuando disparas y el disparo llega a los limites me carga un erro (estoy en visual studio) y se sierra todo el proceso
Responder
Imágen de perfil
19 de Julio del 2020
estrellaestrellaestrellaestrellaestrella
Ok, coméntame tu error y lo podré corregir. Gracias
Responder
5 de Enero del 2024
estrellaestrellaestrellaestrellaestrella
Me gusta jugar y escuchar música en Spotify apk gratis, porque aquí no hay anuncios.
Responder

Comentar la versión: 1.3.0.0

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s6330