Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdlib.h"
- #include "miniwin.h"
- #include <cmath>#include <cstdlib>
- #include <ctime>
- #include <list>
- using namespace miniwin;
- using namespace std;
- const int ANCHO=1280;
- const int ALTO=720;
- struct Coord
- {
- int x,y;
- Coord(int x0=0,int y0=0){x=x0; y=y0;}
- Coord(const Coord& c){x=c.x; y=c.y;}
- };
- struct Polar
- {
- int ro;
- float angle;
- Polar(int ro0=0,float ang=0.00){ro=ro0; angle=ang;}
- Polar(const Polar& p){ro=p.ro; angle=p.angle;}
- };
- class Pieza
- {
- private:
- Coord orig,veloc; //Coordenadas de origen de la pieza, Velocidad en 'X' y en 'Y';
- float angulo; //Ángulo de orientación de la pieza;
- Polar *perif; //Puntero a tabla (alojada en el heap) con los puntos
- //periféricos de la pieza en coordenadas polares;
- int tam,esc; //Tamaño de la array de periféricos y escala de la pieza (en %);
- bool colision; //Indica si está en colisión
- public:
- //Constructores
- Pieza(int x0=0,int y0=0,int ang=0,int p=0,int e=100,Coord v={0,0})
- {
- orig.x=x0;
- orig.y=y0;
- angulo=float(ang);
- tam=p;
- perif=new Polar[tam];
- veloc=v;
- esc=e;
- }
- //Setters y getters
- Coord coords(){return orig;}
- void coords(Coord& c){ orig=c; }
- //int coords(char c){return c=='x'? orig.x : (c=='y'? orig.y : -1);}
- void x (int c) { orig.x=c; }
- int x ( ) { return orig.x; }
- void y (int c) { orig.y=c; }
- int y ( ) { return orig.y; }
- int angle ( ) { return int(angulo);}
- void angle (int ang) {angulo=float(ang);}
- int periferico(){return tam;}
- Polar periferico(int id){return id<tam ? perif[id]:-1;}
- void periferico(int id,Polar valor){ id<tam ? perif[id]=valor:-1;}
- Coord velocidad ( ) { return veloc;}
- void velocidad (Coord v) {veloc=v;}
- int escala ( ) { return esc;}
- void escala (int e) {esc=e;}
- bool ha_chocado ( ) { return colision;}
- void ha_chocado (bool c) {colision=c;}
- void rota(int r)
- {
- angulo+=float(r);
- }
- void acelera()
- {
- Polar aux=Polar(2,angulo/180*M_PI);
- veloc.x+=aux.ro*cos(aux.angle);
- veloc.y+=aux.ro*sin(aux.angle);
- }
- void avanza()
- {
- orig.x+=veloc.x;
- orig.y+=veloc.y;
- if(orig.x<0) {orig.x=ANCHO;}
- else if(orig.x>ANCHO) {orig.x=0;}
- if(orig.y<0) {orig.y=ALTO;}
- else if(orig.y>ALTO) {orig.y=0;}
- }
- void pinta() const
- {
- Polar aux[tam];
- for(int i=0;i<tam;i++)
- {
- aux[i].ro=perif[i].ro*(esc/100);
- aux[i].angle=(perif[i].angle+angulo)/180*M_PI;
- }
- float xy[tam][2];
- for(int i=0;i<tam;i++)
- {
- xy[i][0]=float(orig.x+aux[i].ro*cos(aux[i].angle));
- xy[i][1]=float(orig.y+aux[i].ro*sin(aux[i].angle));
- }
- if(tam>1)
- {
- for(int i=0;i<tam-1;i++)
- {
- linea(xy[i][0],xy[i][1],xy[i+1][0],xy[i+1][1]);
- }
- linea(xy[tam-1][0],xy[tam-1][1],xy[0][0],xy[0][1]);
- }
- }
- virtual void actualiza(){};
- };
- class Nave: public Pieza
- {
- public:
- //Constructores
- Nave(int x0=0,int y0=0,int ang=0):Pieza(x0,y0,ang,4,100,Coord(0,0))
- {
- periferico(0,Polar(20,225.00));
- periferico(1,Polar(32,0.00));
- periferico(2,Polar(20,135.00));
- periferico(3,Polar(5,180.00));
- }
- virtual void actualiza()
- {
- if(ha_chocado())
- {
- //Se muere
- }
- avanza();
- pinta();
- }
- };
- class Asteroide: public Pieza
- {
- private:
- int r;
- public:
- //Constructores
- Asteroide():Pieza(-100,-100,0,9,2000,Coord(0,0))
- {
- periferico(0,Polar(4,45.00));
- periferico(1,Polar(4,90.00));
- periferico(2,Polar(4,135.00));
- periferico(3,Polar(4,180.00));
- periferico(4,Polar(4,225.00));
- periferico(5,Polar(4,270.00));
- periferico(6,Polar(4,315.00));
- periferico(7,Polar(1,337));
- periferico(8,Polar(4,360.00));
- Coord c,p;
- c=Coord(rand()%ANCHO,rand()%ALTO);
- p=Coord((rand()%4)-2,(rand()%4)-2);
- coords(c);
- velocidad(p);
- r=rand()%2;
- }
- virtual void actualiza(Pieza& nave)
- {
- if(ha_chocado())
- {
- //Se muere
- }
- rota(r>0?1:-1);
- avanza();
- pinta();
- if(colisiona(nave))
- {
- ha_chocado(true);
- nave.ha_chocado(true);
- }
- }
- bool colisiona(Pieza& nave)
- {
- //Se calcula si el asteroide choca contra la nave
- return true;
- }
- };
- //Utilidades y globales
- list<Asteroide*> A;
- list<Asteroide*>::iterator Ait;
- void frame(Nave& nave)
- {
- borra();
- nave.actualiza();
- for(Ait=A.begin();Ait!=A.end();Ait++)
- {
- (*Ait)->actualiza(nave);
- }
- refresca();
- }
- //End utilidades y globales
- int main()
- {
- srand(time(0)); //Ponemos la semilla
- vredimensiona(ANCHO, ALTO);
- Nave nave(ANCHO/2,ALTO/2,270);
- for(int i=0;i<5;i++)
- {
- A.push_back(new Asteroide());
- }
- frame(nave);
- int t;
- do
- {
- switch(t)
- {
- case IZQUIERDA:
- nave.rota(-3);
- break;
- case DERECHA:
- nave.rota(3);
- break;
- case ARRIBA:
- nave.acelera();
- break;
- case ABAJO:
- break;
- }
- frame(nave);
- espera(30);
- t=tecla();
- }while(t!=ESCAPE);
- refresca();
- vcierra();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment