Dev - C++ - Ayuda con Programas con Swchit y Juegos incluidos

 
Vista:
sin imagen de perfil
Val: 1
Ha aumentado su posición en 3 puestos en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

Ayuda con Programas con Swchit y Juegos incluidos

Publicado por Carlos (1 intervención) el 12/09/2019 21:21:52
Buenas Tardes Tengo como Tarea en la Universidad un Ejercicio para Casa con lo siguiente:

Programa que Calcule el Area de un Triangulo
Programa que Calcule el Area de un Trapecio
Programa que Calcule el Area de un Rectangulo
Juego de el Ahorgado
Juego de Naves

Todo esto debo hacerlo con Swicht, y donde tengo problemas es que el juego de naves se queda frezado y parpadea si alguien puede ayudarme de antemano muchas gracias.

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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
*/
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <windows.h>
#include <conio.h>
#include <list>
 
using namespace std;
 
double baset,alturat,areat;//variables para el triangulo
double base,basetr,alturatr,areatr;//variables para el trapecio
double baser,alturar,arear;//variables para el trapecio
 
#define ARRIBA 72
#define IZQUIERDA 75
#define DERECHA 77
#define ABAJO 80
 
#define palabra 12
char txt[9];
char datos[palabra][9]={
"carlos","programa","codigo","elvis",
"cristian","oscar","josue","caracter",
"comando","compilar","objeto","fichero"};
 
char dibuj[9][7]={
 "____",
 "|  |",
 "|  |",
 "|",
 "|",
 "|",
 "|",
 "|",
 "----"
};
 
struct pst{
 int pos;
 char part[5];
}parts[7]={{3,"  O"},{4," /"},{4,"|"},
{4,"\\"},{5,"  |"},{6," /"},{6," \\"}};
//sirve para ir pintando el dibujo del pichingo de ahorcado
 
//esta es la funcion que contiene el codido para el juego ahorcado
ahorcado(){
	int n,m,o,p,i,j,r,eq=0;char lc,tmp[10];
 
	srand(time(NULL));
	o=rand()%palabra;
	m=strlen(datos[o]);p=m-2;
 
	txt[0]=datos[o][0];txt[m-1]=datos[o][m-1];
	for(n=1;n<m-1;n++)txt[n]='_';txt[m]=0;
	tmp[0]=0;
	for(i=0;i<9;i++)printf("%s\n",dibuj[i]);
 
	do{
 
   for(j=0;j<strlen(tmp);j++){
     lc=tmp[j];r=p;
     for(n=1;n<m-1;n++)
       if(datos[o][n]==lc){
         txt[n]=lc;datos[o][n]='_';p--;
       }
     if(r==p){
       strcat(dibuj[parts[eq].pos],parts[eq].part);
       eq++;
     }
     for(i=0;i<9;i++)printf("%s\n",dibuj[i]);
   }
 //sirve para ir buscando las palabras en las establecidas y las agrega si existe
   if(eq>=7)break;
 
   if(p>0){
     printf("\nPalabra: %s - Ingrese 1 letra (* para terminar): ",txt);
     scanf("%10s",&tmp);
   }lc=tmp[0];
   system("cls");
 }while(lc!='*'&&p);
 //mientras no se ha completado el ahorcado pide ingresar letras al usuario
 if(!p)printf("\nPalabra: %s - FELICIDADES GANASTE!!!\n\n",txt);
 else if(eq>=7)printf("\n AHORCADO--PERDISTE EL JUEGO!!!\n\n");
 else printf("\nK c's Papá??? Recatate!!!\n\n");
 
 system("PAUSE");
}
 
 //juego de navess
 
void gotoxy(int x, int y){
 
	HANDLE hCon;
	hCon = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD dwPos;
	dwPos.X = x;
	dwPos.Y = y;
 
	SetConsoleCursorPosition(hCon, dwPos);
 
}
 
void OcultarCursor(){
	HANDLE hCon;
	hCon = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO cci;
	cci.dwSize = 50;
	cci.bVisible = FALSE;
 
	SetConsoleCursorInfo(hCon, &cci);
}
 
void pintar_limites(){
	for(int i = 2; i<78 ; i++){
		gotoxy(i,3); printf("%c",205);
		gotoxy(i,33); printf("%c",205);
	}
	for(int i=4 ; i< 33 ; i++){
		gotoxy(2,i); printf("%c",186);
		gotoxy(77,i); printf("%c",186);
	}
	gotoxy(2,3); printf("%c",201);
	gotoxy(2,33);printf("%c",200);
	gotoxy(77,3);printf("%c",187);
	gotoxy(77,33);printf("%c",188);
}
 
class NAVE {
	int x, y;
	int corazones;
	int vidas;
public:
	NAVE(int _x, int _y, int _corazones, int _vidas): x(_x), y(_y),corazones(_corazones),vidas(_vidas){}
	int X (){ return x;}
	int Y (){ return y;}
	int VID(){ return vidas;}
	void COR(){corazones--;}
	void pintar();
	void borrar();
	void mover();
	void pintar_corazones();
	void morir();
};
 
void NAVE::pintar(){
	gotoxy(x,y); printf("  %c",30);
	gotoxy(x,y+1); printf(" %c%c%c",40,207,41);
	gotoxy(x,y+2); printf("%c%c %c%c",30,190,190,30);
 
}
 
void NAVE::borrar(){
	gotoxy(x,y); printf("     ");
	gotoxy(x,y+1); printf("     ");
	gotoxy(x,y+2); printf("     ");
}
 
void NAVE::mover(){
	if(kbhit()){
				char tecla = getch();
				borrar();
				if(tecla == IZQUIERDA && x>3) x--;
				if(tecla == DERECHA && x+6< 77) x++;
				if(tecla == ARRIBA && y >4) y--;
				if(tecla == ABAJO && y+3 <33
				) y++;
				if(tecla == 'e')corazones--;
				pintar();
				pintar_corazones();
		}
 
}
void NAVE::pintar_corazones(){
 
	gotoxy(50,2); printf("VIDAS %d",vidas);
	gotoxy(64,2); printf("salud");
	gotoxy(70,2); printf("      ");
	for(int i=0; i< corazones; i++){
 
		gotoxy(70+i,2); printf("%c",3);
	}
}
void NAVE::morir(){
	if(corazones == 0){
		borrar();
		gotoxy(x,y);  printf("   **   ");
		gotoxy(x,y+1); printf(" ****  ");
		gotoxy(x,y+2); printf("  **  ");
		Sleep(200);
 
		borrar();
		gotoxy(x,y);  printf(" * ** *");
		gotoxy(x,y+1); printf("  ****   ");
		gotoxy(x,y+2); printf(" * ** *");
		Sleep(200);
		borrar();
		vidas--;
		corazones = 3;
		pintar_corazones();
		pintar();
 
	}
 
}
 
class AST{
	int x,y;
	public:
		AST(int _x, int _y):x(_x),y(_y){}
		void pintar ();
		void mover();
		void choque(NAVE &N);
		int X(){ return x;}
		int Y(){ return y;}
 
};
 
void AST::pintar() {
	gotoxy(x,y); printf("%c",184);
}
 
void AST::mover(){
	gotoxy(x,y); printf(" ");
	y++;
	if(y> 32){
		x = rand()%71 + 4;
		y = 4;
 
	}
	pintar();
}
 
void AST::choque(NAVE &N){
	if(x >= N.X() && x < N.X()+6 && y >=N.Y() && y <= N.Y()+2)
	{
		N.COR();
		N.borrar();
		N.pintar();
		N.pintar_corazones();
		x = rand()%71 + 4;
		y = 4;
	}
}
class BALA {
	int x,y;
	public:
		BALA(int _x, int _y): x(_x), y(_y){}
		int X(){ return x; }
		int Y(){ return y; }
		void mover();
		bool fuera();
};
void BALA::mover(){
	gotoxy(x,y); printf(" ");
	y--;
	gotoxy(x,y); printf("*");
 
 
}
 
bool BALA::fuera(){
	if (y == 4) return true;
	return false;
}
juegoNaves(){
 
	OcultarCursor();
	pintar_limites();
	NAVE N(37,30,3,3);
	N.pintar();
	N.pintar_corazones();
 
	list<AST*> A;
	list<AST*>::iterator itA;
	for(int i=0; i<5; i++){
		A.push_back(new AST(rand()%75 + 3, rand()%5 + 4));
 
	}
 
 
	list<BALA*> B;
	list<BALA*>::iterator it;
 
 
	bool game_over = false;
	int puntos = 0;
	while(!game_over){
		gotoxy(4,2); printf("Puntos %d",puntos);
 
		if(kbhit()){
 
			char tecla = getch();
			if(tecla =='a')
			B.push_back(new BALA(N.X()+ 2, N.Y() -1));
		}
		for(it = B.begin(); it != B.end(); it++){
 
			(*it ) ->mover();
			if ((*it)->fuera()){
				gotoxy((*it)->X(), (*it)->Y()); printf(" ");
				delete(*it);
				it = B.erase(it);
 
			}
 
		}
		//gotoxy(x, y);printf("*");
	for(itA = A.begin(); itA != A.end(); itA++){
		(*itA)->mover();
		(*itA)->choque(N);
 
	}
 
	for(itA = A.begin(); itA != A.end(); itA++){
		for(it = B.begin(); it != B.end(); it++){
			if((*itA)-> X() == (*it)->X() && ( (*itA)->Y()+1 == (*it)->Y() || (*itA)->Y() == (*it)->Y() )){
 
				gotoxy((*it)->X(),(*it)->Y()); printf(" ");
				delete(*it);
				it = B.erase(it);
 
				A.push_back(new AST(rand()% 74 + 3, 4));
				gotoxy((*itA)->X(),(*itA)->Y()); printf(" ");
				delete (*itA);
				itA = A.erase(itA);
 
				puntos+=5;
 
			}
 
 
		}
 
 
	}
 
		if(N.VID() == 0) game_over = true;
 
		N.morir();
		N.mover();
		Sleep(30);
		system("cls");
 
	}
 
 
}
 
 
 
 
 
 
int main(int argc, char *argv[])
{
 
int opcion;//esta es la opcion que se leccionara el switch
cout<<"----Univerdiad Politecnica de Honduras----"<<endl;  //Comentario 
cout<<"----Examen de programacion III parcial----"<<endl;  //Comentario 
cout<<"-----------------Grupo #3-----------------"<<endl;  //Comentario 
 
cout<<"------------------------------------------"<<endl;
    do
    {
        cout<<"1. Calcular el Area de un Triangulo"<<endl;			//Menu para Calcular el area de un Triangulo
        cout<<"2. Calcular el Area de un Trapecio"<<endl;			//Menu para Calcular el area de un Trapecio
        cout<<"3. Calcular el Area de un Rectangulo"<<endl; 		//Menu para calcular el area de un Rectangulo
        cout<<"4. Juego del Ahorcado"<<endl;						//Juego del Ahorcado Palabra a Adivinar es "programa"
        cout<<"5. Juego de Naves"<<endl;							//Juego de Naves con Movimientos y 3 Vidas
        cout<<"6. Salir."<<endl;									//Salir del Menu
        cout<<"-----Introduzca una opcion (1-6)---------"<<endl;	//Opciones a seleccionar del 1-6
        cin>>opcion;
 
 
        switch (opcion)
        {
            case 1:													//Programa para Calcular el area de un Triangulo
                cout<<"Ingresa la Base del Triangulo:"<<endl;
                cin>>baset;
                cout<<"Ingresa la Altura del Triangulo:"<<endl;
                cin>>alturat;
                areat=(baset*alturat)/2;
                cout<<"El area del Triangulo es:"<<areat<<endl <<endl;
                cout<<"---------------------------"<<endl;
                break;
 
            case 2:													//Programa para Calcular el área de un Trapecio
                cout<<"Ingrese la Base principal del Trapecio:"<<endl;
                cin>>basetr;
                cout<<"Ingrese la otra Base del Trapecio:"<<endl;
                cin>>base;
                cout<<"Ingrese la Altura del Trapecio:"<<endl;
                cin>>alturatr;
                areatr=(basetr+base)*alturatr/2;
                cout<<"El Area del trapecio es:"<<areatr <<endl <<endl;
                cout<<"---------------------------"<<endl;
				break;
 
            case 3:													//Programa para Calcular el área de un Rectángulo
                cout<<"Ingresa la Base del Rectangulo"<<endl;
                cin>>baser;
                cout<<"Ingresa la Altura del Rectangulo"<<endl;
                cin>>alturar;
                arear=(baser*alturar);
                cout<<"El Area del Triangulo es:"<<arear;
                cout<<"---------------------------"<<endl;
                break;
            case 4:
                cout<<"########################################"<<endl;
				cout<<"##         JUEGO DEL AHORCADO         ##"<<endl;
				cout<<"########################################"<<endl;
				ahorcado();//esta es la función que ejecuta el juego del ahorcado
 
                break;
            case 5:
            	system("cls");//para limpiar el menú de opciones y solo aparezca el juego
               	juegoNaves();//función que ejecuta el código del juego de las naves
                break;
 
            default:
                cout << "......Numero fuera de rango....";
        }
    } while (opcion!=6);
    return 0;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder