Mostrando entradas con la etiqueta tarea05. Mostrar todas las entradas
Mostrando entradas con la etiqueta tarea05. Mostrar todas las entradas

domingo, 28 de agosto de 2016

Ajedrez

Capturas de pantalla
En el tablero de ajedrez, la letra G se mueve y tiene los siguientes controles.
Controles
w : arriba
s : abajo
a : izquierda
d : derecha.

Código
#include </usr/include/GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define LONG_LADO 10//longitud del lado del cuadrado
#define TRANS_X 2.5//Traslacion en x inicial
#define TRANS_Y 7.5//Traslacion en y inicial
#define TRANS_Z 0.0//Traslacion en y inicial
#define SCALA_G 0.05// escala de la g

float radio=1,a1,a2;
float nx,ny,r,g,b,dx,dy,pi=3.141592653;
float trasX = TRANS_X, trasY = TRANS_Y, trasZ = TRANS_Z;

int puntos=0, ancho=500,alto=500,tam_px=1;
int rot = 0,tx=0,ty=0;

void circulo(float cx,float cy,float radio,float r,float g,float b,float a1,float a2){

    glBegin(GL_POLYGON);
    glColor3f(r,g,b);
    glVertex2f(cx, cy);
    for (float i=a1; i<a2; i+=0.01)
    {
        dx=radio*cos(i)+cx;
        dy=radio*sin(i)+cy;
        glVertex2f(dx, dy);
    }
    glEnd();
}

void tablero(){
    for(int colum = 0; colum < 10; colum++){
        for(int fila = 0; fila < 10; fila++){
            bool edo = true;
            if((colum > 0 && colum < 9) && (fila > 0 && fila < 9)){
                if(colum%2 != 0){//x impar                    
                    if(fila%2 != 0){//x impar
                        glColor3f(0,0,0);//negro
                    }else{//x par
                        glColor3f(1,1,1);//blanco        
                    }                    
                }else{//x par
                    if(fila%2 != 0){//x impar
                        glColor3f(1,1,1);//blanco        
                    }else{//x par
                        glColor3f(0,0,0);//negro
                    }                    
                }
            }else{
                glColor3f(0.282,0.52,0.93);
            }            
            glBegin(GL_QUADS);
                glVertex2i(colum, fila);
                glVertex2i(colum, fila + 1);
                glVertex2i(colum + 1,fila + 1);
                glVertex2i(colum + 1,fila);            
            glEnd();        
        }
    }
}

void dibujarG(){
    int cx=0;int cy=0;
    circulo(cx,cy,5,0.858,0.196,0.211,0.7854,2.7);
    circulo(cx,cy,5,0.956,0.76,0.05,2.65,3.65);
    circulo(cx,cy,5,0.235,0.73,0.33,3.65,5.49);
    circulo(cx,cy,5,0.282,0.52,0.93,5.48,2*3.22);
    circulo(cx,cy,3,1,1,1,0,2*3.1416);
    glColor3f(0.282,0.52,0.93);
    glBegin(GL_TRIANGLE_FAN);
        glVertex2f(0+cx,0.8+cy);glVertex2f(4.98+cx,0.8+cy);
        glVertex2f(4.93+cx,-1+cy);glVertex2f(0+cx,-1+cy);
    glEnd();
}

void display(void){
    glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();
        tablero();
    glPopMatrix();

    glPushMatrix();
        glTranslatef(trasX, trasY, trasZ);//x,y,z
        glScalef(SCALA_G, SCALA_G, SCALA_G);
        dibujarG();
    glPopMatrix();

    glFlush();

}

void key(unsigned char c, int x, int y)
{
    switch(c){
        case 27: //ESC
            exit(0);
            break;
        case 'w'://Arriba
            if(trasY < 8){
                trasY += 1;
            }
            break;
        case 's'://Abajo
            if(trasY > 2){
                trasY -= 1;
            }
            break;
        case 'a'://izquierda
            if(trasX > 2){
                trasX -= 1;
            }
            break;
        case 'd'://derecha
            if(trasX < 8){
                trasX += 1;
            }
            break;                          
        case 'r'://reset
            trasX = TRANS_X;
            trasY = TRANS_Y;
            trasZ = TRANS_Z;
            break;
    }
    display();
}

void Init()
{   glClearColor(1.0,1.0,1.0,0);
    gluOrtho2D(0,10,0,10);
    glPointSize(tam_px);
    glEnable(GL_POINT_SMOOTH);

}

int main(int argc, char **argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(ancho,alto);
    glutCreateWindow("Ajedrez");

    Init();

    glutDisplayFunc(display);
    glutKeyboardFunc(key);

    glutMainLoop();
    return 0;
}


Descargar archivo