Ayuda!!!!!!!
#include <stdio.h>
#include "glut.h"
#include "windows.h"
#include <math.h>
#include <time.h>
#include <stdlib.h>
enum color{ROJO,VERDE,AZUL,AMARILLO,CYAN,MAGENTA,BLANCO,NEGRO};
// Variables punto de vista
float altura=10;
float anchura=16;
float profundidad=25;
// Variables pelota
typedef struct _pelota
{
float rad_pelota;
float x_pelota;
float y_pelota;
float vel_x;
float vel_y;
int color_pelota;
}pelota;
// Variables jugador
typedef struct _jugador
{
float x_jugador;
float rad_jugador;
float y_jugador;
float vel_x_jugador;
float vel_y_jugador;
float max_rango;
float min_rango;
int color_jugador;
}jugador;
typedef struct _porteria
{
float x;
float y;
float ancho;
float color_porteria;
}porteria;
//variables gol
int gr=0,ga=0;
//LISTA ENCADENADA DE LOS RIVALES
typedef struct _rival
{
jugador datos_rival;
struct _rival* next;
}rival;
rival* lista_rival=NULL;
int max_num_rivales=3;
//Funcines que crean la lista de rivales
void CrearLista();
void AddRival(rival* ins);
void DibujarLista(rival *ins);
//Funciones para la lista de rivales
void DibujarRivales(rival *primerrival);
void MoverRivales(rival *primerrival);
void ColisonesRivales(rival *primerrival);
//Variables globales
int estado=0;
pelota balon;
jugador player;
jugador rivales[3];
porteria meta[2];
num_max_rivales=3;
//Funciones de inicio
void InitVentana(int argc, char **argv);
void Color(int i);
//Funciones de diseño
void DibujaPelota(pelota p);
void DibujaJugador(jugador j);
void DibujaRivales(rival *primerrival);
void DibujaPorteria(porteria por);
void DibujaMarcador();
//Funcion de movimiento de jugador
void MueveJugador(jugador *j,float t);
// Funcion movimiento pelota
void MuevePelota(pelota* p,float t);
//Funcion Goles
void Goles(pelota *p);
//Funcion juego finaliza
void AcabaJuego(int g1,int g2);
void PrintText(float x, float y, char *cad);
void ColisionPelotas(pelota* p1,jugador* p2);
//Callbacks
void OnDibuja(void);
void OnTimer(int value); //funcion de animacion
void OnSpecialKeyboardDown(int key, int x, int y); //Teclas especiales (Cursor...)
void OnSpecialKeyboardUp(int key, int x, int y); //Teclas especiales (Cursor...)
void OnKeyboardDown(unsigned char key, int x, int y); //Teclas normales
void OnKeyboardDown(unsigned char key, int x, int y);
int main(int argc,char* argv[])
{/********************************************/
srand(time(NULL));
// Inicialización de variables
balon.x_pelota=0;
balon.y_pelota=altura/2;
balon.color_pelota=BLANCO;
balon.vel_x=0;
balon.vel_y=0;
balon.rad_pelota=0.2;
player.x_jugador=-anchura/2+1;
player.y_jugador=altura/2;
player.vel_x_jugador=0;
player.vel_y_jugador=0;
player.color_jugador=ROJO;
player.rad_jugador=0.5;
player.max_rango=100;
player.min_rango=0;
rivales[0].x_jugador=anchura/2-1;
rivales[0].y_jugador=altura/2;
rivales[0].vel_x_jugador=0;
rivales[0].vel_y_jugador=10;
rivales[0].color_jugador=AZUL;
rivales[0].rad_jugador=0.5;
rivales[0].max_rango=75;
rivales[0].min_rango=25;
rivales[1].x_jugador=anchura/2-2.5;
rivales[1].y_jugador=altura/2+1;
rivales[1].vel_x_jugador=0;
rivales[1].vel_y_jugador=15;
rivales[1].color_jugador=AZUL;
rivales[1].rad_jugador=0.5;
rivales[1].max_rango=90;
rivales[1].min_rango=50;
rivales[2].x_jugador=anchura/2-3;
rivales[2].y_jugador=altura/2-1;
rivales[2].vel_x_jugador=0;
rivales[2].vel_y_jugador=-10;
rivales[2].color_jugador=AZUL;
rivales[2].rad_jugador=0.5;
rivales[2].max_rango=50;
rivales[2].min_rango=10;
meta[0].x=-anchura/2;
meta[0].y=altura/2;
meta[0].ancho=3;
meta[0].color_porteria=BLANCO;
meta[1].x=anchura/2;
meta[1].y=altura/2;
meta[1].ancho=3;
meta[1].color_porteria=BLANCO;
CrearLista();
InitVentana(argc, argv);
glutDisplayFunc(OnDibuja);
glutTimerFunc(50,OnTimer,0);
glutSpecialFunc(OnSpecialKeyboardDown);
glutKeyboardFunc(OnKeyboardDown);
glutSpecialUpFunc(OnSpecialKeyboardUp);
glutMainLoop();
return 0;
}/**************************************************/
void AddRival(rival *ins)
{/*************************************************/
rival *aux=lista_rival;
lista_rival=ins;
ins->next=aux;
}/************************************************/
void CrearLista()
{
rival *aux=(rival*)malloc(sizeof(rival));
int i;
for (i=0;i<num_max_rivales;i++)
{
aux->datos_rival.x_jugador=rivales[i].x_jugador;
aux->datos_rival.y_jugador=rivales[i].y_jugador;
aux->datos_rival.vel_x_jugador=rivales[i].vel_x_jugador;
aux->datos_rival.vel_y_jugador=rivales[i].vel_y_jugador;
aux->datos_rival.color_jugador=rivales[i].color_jugador;
aux->datos_rival.max_rango=rivales[i].max_rango;
aux->datos_rival.min_rango=rivales[i].min_rango;
aux->datos_rival.rad_jugador=rivales[i].rad_jugador;
AddRival(aux);
}
}
void DibujarLista(rival *ins)
{
rival *aux=ins;
while (aux!=NULL)
{
DibujaRivales(aux);
aux=aux->next;
}
}
void DibujaRivales(rival *primerrival)
{
Color(primerrival->datos_rival.color_jugador);
glTranslatef(primerrival->datos_rival.x_jugador,primerrival->datos_rival.y_jugador,0);
glutSolidSphere(primerrival->datos_rival.rad_jugador,12,12);
glTranslatef(-primerrival->datos_rival.x_jugador,-primerrival->datos_rival.y_jugador,0);
}
void Color(int i)
{/**************************************************************/
switch(i)
{
case ROJO:glColor3f(1,0,0);break;
case VERDE:glColor3f(0,1,0);break;
case AZUL:glColor3f(0,0,1); break;
case AMARILLO:glColor3f(1,1,0);break;
case CYAN:glColor3f(0,1,1);break;
case MAGENTA:glColor3f(1,0,1);break;
case BLANCO:glColor3f(1,1,1);break;
case NEGRO:glColor3f(0,0,0);break;
default:glColor3f(1,1,0);break;
}
}/**************************************************************/
void InitVentana(int argc, char **argv)
{/**************************************************************/
float alto,ancho;
//Creacion y definicion de la ventana
glutInit(&argc, argv);
glutInitWindowSize(800,600);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Futbol");
//Habilitamos las luces, la renderizacion y el color de los materiales
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
ancho=glutGet(GLUT_WINDOW_WIDTH);
alto=glutGet(GLUT_WINDOW_HEIGHT);
//definimos la proyeccion
glMatrixMode(GL_PROJECTION);
gluPerspective( 45.0, ancho/alto, 0.1, 50);
}/**************************************************************/
void OnDibuja(void)
{/**************************************************************/
int i;
//Borrado de la pantalla
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Para definir el punto de vista
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, altura/2, profundidad, // posicion del ojo (0,-4,1)
0.0, altura/2, 0.0, // hacia que punto mira (0,0,0)
0.0, 1.0, 0.0); // sentido positivo es el de las y(0,1,0)
//Dibujar campo
Color(VERDE);
glNormal3f(0,0,1);
glBegin(GL_POLYGON);
glVertex3f(anchura/2,altura,0);// vertice arriba drcha
glVertex3f(anchura/2,0,0);//abajo drcha
glVertex3f(-anchura/2,0,0);
glVertex3f(-anchura/2,altura,0);
glEnd();
//Dibujar una pelota
Color(balon.color_pelota);
DibujaPelota(balon);
//Dibujar el jugador manejado por teclado
DibujaJugador(player);
DibujarLista(lista_rival);
// Dibujar las porterias
for (i=0;i<2;i++)
{
DibujaPorteria(meta[i]);
}
//Dibujar el marcador
DibujaMarcador();
glutSwapBuffers();
}/**************************************************************/
void DibujaPelota(pelota p)
{/**************************************************************/
glTranslatef(p.x_pelota,p.y_pelota,0);
glutSolidSphere(p.rad_pelota,12,12);
glTranslatef(-p.x_pelota,-p.y_pelota,0);
}/**************************************************************/
void DibujaJugador(jugador j)
{/**************************************************************/
Color(j.color_jugador);
glTranslatef(j.x_jugador,j.y_jugador,0);
glutSolidSphere(j.rad_jugador,12,12);
glTranslatef(-j.x_jugador,-j.y_jugador,0);
}/**************************************************************/
void DibujaPorteria(porteria m)
{/**************************************************************/
Color(m.color_porteria);
glNormal3f(0,0,1);
glBegin(GL_POLYGON);
glVertex3f(m.x+0.2,m.y+m.ancho/2,0);
glVertex3f(m.x+0.2,m.y-m.ancho/2,0);
glVertex3f(m.x-0.2,m.y-m.ancho/2,0);
glVertex3f(m.x-0.2,m.y+m.ancho/2,0);
glEnd();
}/**************************************************************/
void DibujaMarcador()
{/******************************************/
char cad[20];
Color(CYAN);
glNormal3f(0,0,1);
glBegin(GL_POLYGON);
glVertex3f(-3,12.5,0);
glVertex3f(-3,16.5,0);
glVertex3f(3,16.5,0);
glVertex3f(3,12.5,0);
glEnd();
sprintf(cad,"Local %dn",ga);
//PrintText(-3.0f,14.5f,cad);
sprintf(cad,"Vist %dn",gr);
//PrintText(0.5f,14.5f,cad);
}/******************************************************/
void MueveJugador(jugador *j,float t)
{/************************************************************/
j->y_jugador+=j->vel_y_jugador*t;
if(j->y_jugador>altura)
{
j->y_jugador=altura;
j->vel_y_jugador=0;
}
//Salida por el lado izquierdo
if(j->y_jugador<0)
{
j->y_jugador=0;
j->vel_y_jugador=0;
}
}/*************************************************************/
void MuevePelota(pelota* p,float t)
{/**************************************************************/
//caida libre
p->x_pelota+=p->vel_x*t;
p->y_pelota+=p->vel_y*t;//-4.9*t*t;
//Rebote con el suelo
if(p->y_pelota-p->rad_pelota<0.0f)
{
p->y_pelota=p->rad_pelota;
p->vel_y=-p->vel_y;
}
//Rebote con el techo
if(p->y_pelota+p->rad_pelota>altura)
{
p->y_pelota=altura-p->rad_pelota;
p->vel_y=-p->vel_y;
}
//Rebote con la pared derecha
if(p->x_pelota+p->rad_pelota>anchura/2)
{
p->x_pelota=anchura/2-p->rad_pelota;
p->vel_x=-p->vel_x;
}
//Rebote con la pared izquierda
if(p->x_pelota-p->rad_pelota<-anchura/2)
{
p->x_pelota=-anchura/2+p->rad_pelota;
p->vel_x=-p->vel_x;
}
}/**************************************************************/
void Goles(pelota *p)
{/**************************************************************/
if ((p->x_pelota-p->rad_pelota)<-anchura/2)
{
if (((p->y_pelota-p->rad_pelota)>(altura/2-meta[0].ancho/2))&&((p->y_pelota-p->rad_pelota)<(altura/2+meta[0].ancho/2)))
{
p->vel_x=0;
p->vel_y=0;
gr++;
estado=0;
p->x_pelota=0;
p->y_pelota=altura/2;
}
}
if ((p->x_pelota-p->rad_pelota)>anchura/2)
{
if (((p->y_pelota-p->rad_pelota)>(altura/2-meta[1].ancho/2))&&((p->y_pelota-p->rad_pelota)<(altura/2+meta[1].ancho/2)))
{
p->vel_x=0;
p->vel_y=0;
ga++;
estado=0;
p->x_pelota=0;
p->y_pelota=altura/2;
}
}
}/**************************************************************/
void ColisionPelotas(pelota* p1,jugador* p2)
{
float incx,incy,th1,th2;
float u1x,u1y,u2x,u2y,v1x,v1y,v2x,v2y; //velocidades en coordenadas del sistema de referencia relativo antes (u) y despues (v) del choque
float py,ey,a,b,c;
float disc,modv1,modv2,fi1,fi2;
//Los parametros de las ecuaciones
float x1=p1->x_pelota;
float y1=p1->y_pelota;
float r1=p1->rad_pelota;
float m1=r1;
float vx1=p1->vel_x;
float vy1=p1->vel_y;
//El modulo y argumento de la velocidad de la pelota1
float v1=sqrt(vx1*vx1+vy1*vy1);
float ang1=atan2(vy1,vx1);
float x2=p2->x_jugador;
float y2=p2->y_jugador;
float r2=p2->rad_jugador;
float m2=r2;
float velx2=p2->vel_x_jugador;
float vely2=p2->vel_y_jugador;
//El modulo y argumento de la velocidad de la pelota2
float v2=sqrt(velx2*velx2+vely2*vely2);
float ang2=atan2(vely2,velx2);
//Chequeo de que colisionan
float dx=x2-x1;
float dy=y2-y1;
//Modulo y argumento del vector de distancia entre centros
float d=sqrt(dx*dx+dy*dy);
float angd=atan2(dy,dx);
float dist=sqrt((r1+r2)*(r1+r2));
float dif=d-dist;
if(dif<0)//si hay colision
{
angd=angd-3.14159/2;
//Separamos las esferas, lo que se han incrustado
//la mitad cada una
incx=dif/2*cos(angd);
incy=dif/2*sin(angd);
p1->x_pelota+=incx;
p1->y_pelota+=incy;
p2->x_jugador-=incx;
p2->y_jugador-=incy;
//El angulo de las velocidades en el sistema relativo antes del choque
th1=ang1-angd;
th2=ang2-angd;
//Las componentes de las velocidades en el sistema relativo antes del choque
u1x=v1*cos(th1);
u1y=v1*sin(th1);
u2x=v2*cos(th2);
u2y=v2*sin(th2);
//la componente en X del sistema relativo no cambia
v1x=u1x;
v2x=u2x;
py=m1*u1y+m2*u2y;//Cantidad de movimiento inicial ejey
ey=m1*u1y*u1y+m2*u2y*u2y;//Energia cinetica inicial ejey
//los coeficientes de la ecuacion cuadrada
a=m2*m2+m1*m2;
b=-2*py*m2;
c=py*py-m1*ey;
disc=b*b-4*a*c;
if(disc<0)
{
printf("Disc Neg=%fn",disc);
disc=0;
}
//las nuevas velocidades segun el eje Y relativo
v2y=(-b+sqrt(disc))/(2*a);
v1y=(py-m2*v2y)/m1;
//Modulo y argumento de las velocidades en coordenadas absolutas
modv1=sqrt(v1x*v1x+v1y*v1y);
modv2=sqrt(v2x*v2x+v2y*v2y);
fi1=angd+atan2(v1y,v1x);
fi2=angd+atan2(v2y,v2x);
//Velocidades en absolutas despues del choque en componentes
p1->vel_x=modv1*cos(fi1);
p1->vel_y=modv1*sin(fi1);
p2->vel_x_jugador=modv2*cos(fi2);
p2->vel_y_jugador=modv2*sin(fi2);
}
}
void AcabaJuego(int g1,int g2)
{
if ((g1==5)||(g2==5))
{
if (g1<g2)
{
printf("El resultado ha sido:%d,%d n",g2,g1);
printf("Has ganado n");
exit(0);
}
if (g1>g2)
{
printf("El resultado ha sido:%d,%d n",g2,g1);
printf("Has perdido n");
exit(0);
}
}
}
void OnTimer(int value)
{/**************************************************************/
MueveJugador(&player,0.025);
if(estado==1)
{
MuevePelota(&balon,0.025);
ColisionPelotas(&balon,&player);
Goles(&balon);
}
AcabaJuego(gr,ga);
//Cuando hemos terminado todas los movimientos
glutTimerFunc(50,OnTimer,0);//al finalizar, le decimos que dentro de 25ms
//vuelva a llamar otra vez a esta funcion OnTimer()
//(si queremos que cada 25ms se este llamando esta funcion)
glutPostRedisplay();//le mandamos que redibuje
}/**************************************************************/
void OnSpecialKeyboardDown(int key, int x, int y)
{/**************************************************************/
if(key==GLUT_KEY_DOWN)
{
player.vel_y_jugador=-15.0f;
}
if(key==GLUT_KEY_UP)
{
player.vel_y_jugador=15.0f;
}
}/**************************************************************/
void OnSpecialKeyboardUp(int key, int x, int y)
{/**************************************************************/
if(key==GLUT_KEY_UP)
{
player.vel_y_jugador=0.0f;
}
if(key==GLUT_KEY_DOWN)
{
player.vel_y_jugador=0.0f;
}
}/**************************************************************/
void OnKeyboardDown(unsigned char key, int x, int y)
{/**************************************************************/
if(key==' '&& estado==0)
{
estado=1;
balon.vel_x=rand()%8+1;
balon.vel_y=rand()%8+1;
}
}/**************************************************************/
Hola les pongo el codigo de un juego q he hecho con visual c, pero tengo un problema y es q me compila bien pero al ejecutarse se queda bloqueado a que puede ser debido?.Gracias.
#include "glut.h"
#include "windows.h"
#include <math.h>
#include <time.h>
#include <stdlib.h>
enum color{ROJO,VERDE,AZUL,AMARILLO,CYAN,MAGENTA,BLANCO,NEGRO};
// Variables punto de vista
float altura=10;
float anchura=16;
float profundidad=25;
// Variables pelota
typedef struct _pelota
{
float rad_pelota;
float x_pelota;
float y_pelota;
float vel_x;
float vel_y;
int color_pelota;
}pelota;
// Variables jugador
typedef struct _jugador
{
float x_jugador;
float rad_jugador;
float y_jugador;
float vel_x_jugador;
float vel_y_jugador;
float max_rango;
float min_rango;
int color_jugador;
}jugador;
typedef struct _porteria
{
float x;
float y;
float ancho;
float color_porteria;
}porteria;
//variables gol
int gr=0,ga=0;
//LISTA ENCADENADA DE LOS RIVALES
typedef struct _rival
{
jugador datos_rival;
struct _rival* next;
}rival;
rival* lista_rival=NULL;
int max_num_rivales=3;
//Funcines que crean la lista de rivales
void CrearLista();
void AddRival(rival* ins);
void DibujarLista(rival *ins);
//Funciones para la lista de rivales
void DibujarRivales(rival *primerrival);
void MoverRivales(rival *primerrival);
void ColisonesRivales(rival *primerrival);
//Variables globales
int estado=0;
pelota balon;
jugador player;
jugador rivales[3];
porteria meta[2];
num_max_rivales=3;
//Funciones de inicio
void InitVentana(int argc, char **argv);
void Color(int i);
//Funciones de diseño
void DibujaPelota(pelota p);
void DibujaJugador(jugador j);
void DibujaRivales(rival *primerrival);
void DibujaPorteria(porteria por);
void DibujaMarcador();
//Funcion de movimiento de jugador
void MueveJugador(jugador *j,float t);
// Funcion movimiento pelota
void MuevePelota(pelota* p,float t);
//Funcion Goles
void Goles(pelota *p);
//Funcion juego finaliza
void AcabaJuego(int g1,int g2);
void PrintText(float x, float y, char *cad);
void ColisionPelotas(pelota* p1,jugador* p2);
//Callbacks
void OnDibuja(void);
void OnTimer(int value); //funcion de animacion
void OnSpecialKeyboardDown(int key, int x, int y); //Teclas especiales (Cursor...)
void OnSpecialKeyboardUp(int key, int x, int y); //Teclas especiales (Cursor...)
void OnKeyboardDown(unsigned char key, int x, int y); //Teclas normales
void OnKeyboardDown(unsigned char key, int x, int y);
int main(int argc,char* argv[])
{/********************************************/
srand(time(NULL));
// Inicialización de variables
balon.x_pelota=0;
balon.y_pelota=altura/2;
balon.color_pelota=BLANCO;
balon.vel_x=0;
balon.vel_y=0;
balon.rad_pelota=0.2;
player.x_jugador=-anchura/2+1;
player.y_jugador=altura/2;
player.vel_x_jugador=0;
player.vel_y_jugador=0;
player.color_jugador=ROJO;
player.rad_jugador=0.5;
player.max_rango=100;
player.min_rango=0;
rivales[0].x_jugador=anchura/2-1;
rivales[0].y_jugador=altura/2;
rivales[0].vel_x_jugador=0;
rivales[0].vel_y_jugador=10;
rivales[0].color_jugador=AZUL;
rivales[0].rad_jugador=0.5;
rivales[0].max_rango=75;
rivales[0].min_rango=25;
rivales[1].x_jugador=anchura/2-2.5;
rivales[1].y_jugador=altura/2+1;
rivales[1].vel_x_jugador=0;
rivales[1].vel_y_jugador=15;
rivales[1].color_jugador=AZUL;
rivales[1].rad_jugador=0.5;
rivales[1].max_rango=90;
rivales[1].min_rango=50;
rivales[2].x_jugador=anchura/2-3;
rivales[2].y_jugador=altura/2-1;
rivales[2].vel_x_jugador=0;
rivales[2].vel_y_jugador=-10;
rivales[2].color_jugador=AZUL;
rivales[2].rad_jugador=0.5;
rivales[2].max_rango=50;
rivales[2].min_rango=10;
meta[0].x=-anchura/2;
meta[0].y=altura/2;
meta[0].ancho=3;
meta[0].color_porteria=BLANCO;
meta[1].x=anchura/2;
meta[1].y=altura/2;
meta[1].ancho=3;
meta[1].color_porteria=BLANCO;
CrearLista();
InitVentana(argc, argv);
glutDisplayFunc(OnDibuja);
glutTimerFunc(50,OnTimer,0);
glutSpecialFunc(OnSpecialKeyboardDown);
glutKeyboardFunc(OnKeyboardDown);
glutSpecialUpFunc(OnSpecialKeyboardUp);
glutMainLoop();
return 0;
}/**************************************************/
void AddRival(rival *ins)
{/*************************************************/
rival *aux=lista_rival;
lista_rival=ins;
ins->next=aux;
}/************************************************/
void CrearLista()
{
rival *aux=(rival*)malloc(sizeof(rival));
int i;
for (i=0;i<num_max_rivales;i++)
{
aux->datos_rival.x_jugador=rivales[i].x_jugador;
aux->datos_rival.y_jugador=rivales[i].y_jugador;
aux->datos_rival.vel_x_jugador=rivales[i].vel_x_jugador;
aux->datos_rival.vel_y_jugador=rivales[i].vel_y_jugador;
aux->datos_rival.color_jugador=rivales[i].color_jugador;
aux->datos_rival.max_rango=rivales[i].max_rango;
aux->datos_rival.min_rango=rivales[i].min_rango;
aux->datos_rival.rad_jugador=rivales[i].rad_jugador;
AddRival(aux);
}
}
void DibujarLista(rival *ins)
{
rival *aux=ins;
while (aux!=NULL)
{
DibujaRivales(aux);
aux=aux->next;
}
}
void DibujaRivales(rival *primerrival)
{
Color(primerrival->datos_rival.color_jugador);
glTranslatef(primerrival->datos_rival.x_jugador,primerrival->datos_rival.y_jugador,0);
glutSolidSphere(primerrival->datos_rival.rad_jugador,12,12);
glTranslatef(-primerrival->datos_rival.x_jugador,-primerrival->datos_rival.y_jugador,0);
}
void Color(int i)
{/**************************************************************/
switch(i)
{
case ROJO:glColor3f(1,0,0);break;
case VERDE:glColor3f(0,1,0);break;
case AZUL:glColor3f(0,0,1); break;
case AMARILLO:glColor3f(1,1,0);break;
case CYAN:glColor3f(0,1,1);break;
case MAGENTA:glColor3f(1,0,1);break;
case BLANCO:glColor3f(1,1,1);break;
case NEGRO:glColor3f(0,0,0);break;
default:glColor3f(1,1,0);break;
}
}/**************************************************************/
void InitVentana(int argc, char **argv)
{/**************************************************************/
float alto,ancho;
//Creacion y definicion de la ventana
glutInit(&argc, argv);
glutInitWindowSize(800,600);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Futbol");
//Habilitamos las luces, la renderizacion y el color de los materiales
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
ancho=glutGet(GLUT_WINDOW_WIDTH);
alto=glutGet(GLUT_WINDOW_HEIGHT);
//definimos la proyeccion
glMatrixMode(GL_PROJECTION);
gluPerspective( 45.0, ancho/alto, 0.1, 50);
}/**************************************************************/
void OnDibuja(void)
{/**************************************************************/
int i;
//Borrado de la pantalla
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Para definir el punto de vista
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, altura/2, profundidad, // posicion del ojo (0,-4,1)
0.0, altura/2, 0.0, // hacia que punto mira (0,0,0)
0.0, 1.0, 0.0); // sentido positivo es el de las y(0,1,0)
//Dibujar campo
Color(VERDE);
glNormal3f(0,0,1);
glBegin(GL_POLYGON);
glVertex3f(anchura/2,altura,0);// vertice arriba drcha
glVertex3f(anchura/2,0,0);//abajo drcha
glVertex3f(-anchura/2,0,0);
glVertex3f(-anchura/2,altura,0);
glEnd();
//Dibujar una pelota
Color(balon.color_pelota);
DibujaPelota(balon);
//Dibujar el jugador manejado por teclado
DibujaJugador(player);
DibujarLista(lista_rival);
// Dibujar las porterias
for (i=0;i<2;i++)
{
DibujaPorteria(meta[i]);
}
//Dibujar el marcador
DibujaMarcador();
glutSwapBuffers();
}/**************************************************************/
void DibujaPelota(pelota p)
{/**************************************************************/
glTranslatef(p.x_pelota,p.y_pelota,0);
glutSolidSphere(p.rad_pelota,12,12);
glTranslatef(-p.x_pelota,-p.y_pelota,0);
}/**************************************************************/
void DibujaJugador(jugador j)
{/**************************************************************/
Color(j.color_jugador);
glTranslatef(j.x_jugador,j.y_jugador,0);
glutSolidSphere(j.rad_jugador,12,12);
glTranslatef(-j.x_jugador,-j.y_jugador,0);
}/**************************************************************/
void DibujaPorteria(porteria m)
{/**************************************************************/
Color(m.color_porteria);
glNormal3f(0,0,1);
glBegin(GL_POLYGON);
glVertex3f(m.x+0.2,m.y+m.ancho/2,0);
glVertex3f(m.x+0.2,m.y-m.ancho/2,0);
glVertex3f(m.x-0.2,m.y-m.ancho/2,0);
glVertex3f(m.x-0.2,m.y+m.ancho/2,0);
glEnd();
}/**************************************************************/
void DibujaMarcador()
{/******************************************/
char cad[20];
Color(CYAN);
glNormal3f(0,0,1);
glBegin(GL_POLYGON);
glVertex3f(-3,12.5,0);
glVertex3f(-3,16.5,0);
glVertex3f(3,16.5,0);
glVertex3f(3,12.5,0);
glEnd();
sprintf(cad,"Local %dn",ga);
//PrintText(-3.0f,14.5f,cad);
sprintf(cad,"Vist %dn",gr);
//PrintText(0.5f,14.5f,cad);
}/******************************************************/
void MueveJugador(jugador *j,float t)
{/************************************************************/
j->y_jugador+=j->vel_y_jugador*t;
if(j->y_jugador>altura)
{
j->y_jugador=altura;
j->vel_y_jugador=0;
}
//Salida por el lado izquierdo
if(j->y_jugador<0)
{
j->y_jugador=0;
j->vel_y_jugador=0;
}
}/*************************************************************/
void MuevePelota(pelota* p,float t)
{/**************************************************************/
//caida libre
p->x_pelota+=p->vel_x*t;
p->y_pelota+=p->vel_y*t;//-4.9*t*t;
//Rebote con el suelo
if(p->y_pelota-p->rad_pelota<0.0f)
{
p->y_pelota=p->rad_pelota;
p->vel_y=-p->vel_y;
}
//Rebote con el techo
if(p->y_pelota+p->rad_pelota>altura)
{
p->y_pelota=altura-p->rad_pelota;
p->vel_y=-p->vel_y;
}
//Rebote con la pared derecha
if(p->x_pelota+p->rad_pelota>anchura/2)
{
p->x_pelota=anchura/2-p->rad_pelota;
p->vel_x=-p->vel_x;
}
//Rebote con la pared izquierda
if(p->x_pelota-p->rad_pelota<-anchura/2)
{
p->x_pelota=-anchura/2+p->rad_pelota;
p->vel_x=-p->vel_x;
}
}/**************************************************************/
void Goles(pelota *p)
{/**************************************************************/
if ((p->x_pelota-p->rad_pelota)<-anchura/2)
{
if (((p->y_pelota-p->rad_pelota)>(altura/2-meta[0].ancho/2))&&((p->y_pelota-p->rad_pelota)<(altura/2+meta[0].ancho/2)))
{
p->vel_x=0;
p->vel_y=0;
gr++;
estado=0;
p->x_pelota=0;
p->y_pelota=altura/2;
}
}
if ((p->x_pelota-p->rad_pelota)>anchura/2)
{
if (((p->y_pelota-p->rad_pelota)>(altura/2-meta[1].ancho/2))&&((p->y_pelota-p->rad_pelota)<(altura/2+meta[1].ancho/2)))
{
p->vel_x=0;
p->vel_y=0;
ga++;
estado=0;
p->x_pelota=0;
p->y_pelota=altura/2;
}
}
}/**************************************************************/
void ColisionPelotas(pelota* p1,jugador* p2)
{
float incx,incy,th1,th2;
float u1x,u1y,u2x,u2y,v1x,v1y,v2x,v2y; //velocidades en coordenadas del sistema de referencia relativo antes (u) y despues (v) del choque
float py,ey,a,b,c;
float disc,modv1,modv2,fi1,fi2;
//Los parametros de las ecuaciones
float x1=p1->x_pelota;
float y1=p1->y_pelota;
float r1=p1->rad_pelota;
float m1=r1;
float vx1=p1->vel_x;
float vy1=p1->vel_y;
//El modulo y argumento de la velocidad de la pelota1
float v1=sqrt(vx1*vx1+vy1*vy1);
float ang1=atan2(vy1,vx1);
float x2=p2->x_jugador;
float y2=p2->y_jugador;
float r2=p2->rad_jugador;
float m2=r2;
float velx2=p2->vel_x_jugador;
float vely2=p2->vel_y_jugador;
//El modulo y argumento de la velocidad de la pelota2
float v2=sqrt(velx2*velx2+vely2*vely2);
float ang2=atan2(vely2,velx2);
//Chequeo de que colisionan
float dx=x2-x1;
float dy=y2-y1;
//Modulo y argumento del vector de distancia entre centros
float d=sqrt(dx*dx+dy*dy);
float angd=atan2(dy,dx);
float dist=sqrt((r1+r2)*(r1+r2));
float dif=d-dist;
if(dif<0)//si hay colision
{
angd=angd-3.14159/2;
//Separamos las esferas, lo que se han incrustado
//la mitad cada una
incx=dif/2*cos(angd);
incy=dif/2*sin(angd);
p1->x_pelota+=incx;
p1->y_pelota+=incy;
p2->x_jugador-=incx;
p2->y_jugador-=incy;
//El angulo de las velocidades en el sistema relativo antes del choque
th1=ang1-angd;
th2=ang2-angd;
//Las componentes de las velocidades en el sistema relativo antes del choque
u1x=v1*cos(th1);
u1y=v1*sin(th1);
u2x=v2*cos(th2);
u2y=v2*sin(th2);
//la componente en X del sistema relativo no cambia
v1x=u1x;
v2x=u2x;
py=m1*u1y+m2*u2y;//Cantidad de movimiento inicial ejey
ey=m1*u1y*u1y+m2*u2y*u2y;//Energia cinetica inicial ejey
//los coeficientes de la ecuacion cuadrada
a=m2*m2+m1*m2;
b=-2*py*m2;
c=py*py-m1*ey;
disc=b*b-4*a*c;
if(disc<0)
{
printf("Disc Neg=%fn",disc);
disc=0;
}
//las nuevas velocidades segun el eje Y relativo
v2y=(-b+sqrt(disc))/(2*a);
v1y=(py-m2*v2y)/m1;
//Modulo y argumento de las velocidades en coordenadas absolutas
modv1=sqrt(v1x*v1x+v1y*v1y);
modv2=sqrt(v2x*v2x+v2y*v2y);
fi1=angd+atan2(v1y,v1x);
fi2=angd+atan2(v2y,v2x);
//Velocidades en absolutas despues del choque en componentes
p1->vel_x=modv1*cos(fi1);
p1->vel_y=modv1*sin(fi1);
p2->vel_x_jugador=modv2*cos(fi2);
p2->vel_y_jugador=modv2*sin(fi2);
}
}
void AcabaJuego(int g1,int g2)
{
if ((g1==5)||(g2==5))
{
if (g1<g2)
{
printf("El resultado ha sido:%d,%d n",g2,g1);
printf("Has ganado n");
exit(0);
}
if (g1>g2)
{
printf("El resultado ha sido:%d,%d n",g2,g1);
printf("Has perdido n");
exit(0);
}
}
}
void OnTimer(int value)
{/**************************************************************/
MueveJugador(&player,0.025);
if(estado==1)
{
MuevePelota(&balon,0.025);
ColisionPelotas(&balon,&player);
Goles(&balon);
}
AcabaJuego(gr,ga);
//Cuando hemos terminado todas los movimientos
glutTimerFunc(50,OnTimer,0);//al finalizar, le decimos que dentro de 25ms
//vuelva a llamar otra vez a esta funcion OnTimer()
//(si queremos que cada 25ms se este llamando esta funcion)
glutPostRedisplay();//le mandamos que redibuje
}/**************************************************************/
void OnSpecialKeyboardDown(int key, int x, int y)
{/**************************************************************/
if(key==GLUT_KEY_DOWN)
{
player.vel_y_jugador=-15.0f;
}
if(key==GLUT_KEY_UP)
{
player.vel_y_jugador=15.0f;
}
}/**************************************************************/
void OnSpecialKeyboardUp(int key, int x, int y)
{/**************************************************************/
if(key==GLUT_KEY_UP)
{
player.vel_y_jugador=0.0f;
}
if(key==GLUT_KEY_DOWN)
{
player.vel_y_jugador=0.0f;
}
}/**************************************************************/
void OnKeyboardDown(unsigned char key, int x, int y)
{/**************************************************************/
if(key==' '&& estado==0)
{
estado=1;
balon.vel_x=rand()%8+1;
balon.vel_y=rand()%8+1;
}
}/**************************************************************/
Hola les pongo el codigo de un juego q he hecho con visual c, pero tengo un problema y es q me compila bien pero al ejecutarse se queda bloqueado a que puede ser debido?.Gracias.
