lunes, 20 de febrero de 2017

Tablero de ajedrez

Tablero de Ajedrez

Ricardo Alvarez Hernandez



Código

#include <windows.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <C:\GLUT\include\GL\glut.h>
#include <stdlib.h>

float radio=1,a1,a2;
float nx,ny,r,g,b,dx,dy,pi=3.141592653;
float trasX = 1.5, trasY = 1.5, trasZ = 0,escala=0.05;

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 = 1; colum < 9; colum++){
        for(int fila = 1; fila < 9; 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(1,0,0);
            }
            glBegin(GL_QUADS);
                glVertex2i(colum, fila);
                glVertex2i(colum, fila + 1);
                glVertex2i(colum + 1,fila + 1);
                glVertex2i(colum + 1,fila);
            glEnd();
            glBegin(GL_LINE_STRIP);
                glVertex2i(1,1);
                glVertex2i(9,1);
                glVertex2i(9,9);
                glVertex2i(1,9);
                glVertex2i(1,1);
            glEnd();
        }
    }
}

void Google(){
    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(escala, escala, escala);
        Google();
    glPopMatrix();

    glFlush();

}

void key(unsigned char c, int x, int y)
{
    switch(c){
        case 27: //ESC
            exit(0);
            break;
        case 'w'://mover a la Arriba
            if(trasY < 8){
                trasY += 1;
            }
            break;
        case 's'://mover a la Abajo
            if(trasY > 2){
                trasY -= 1;
            }
            break;
        case 'a'://mover a la izquierda
            if(trasX > 2){
                trasX -= 1;
            }
            break;
        case 'd'://mover a la derecha
            if(trasX < 8){
                trasX += 1;
            }
            break;
        case 'r'://reset
            trasX = 1.5;
            trasY = 1.5;
            trasZ = 0;
            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("Tablero de Ajedrez");

    Init();

    glutDisplayFunc(display);
    glutKeyboardFunc(key);

    glutMainLoop();
    return 0;
}




No hay comentarios:

Publicar un comentario