VPN termux about China.net
Snake part1
#include <stdio.h>
#include <time.h>
#include <GL/glut.h>
#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4
GLint lvl = 1;
GLbyte keys = 0;
GLint _w = 400;
GLint _h = 350;
GLint _Giw = 10;
GLint _Gih = 30;
GLint _Gfw = 270;
GLint _Gfh = 240;
GLint tamanho = 0;
GLint body[2][30] = {{}};
GLint oldX[2] = {};
GLint oldY[2] = {};
struct SnakeHead {
GLint x;
GLint y;
} head ={10, 20};
void snake(){
int i;
//head
glPushMatrix();
glTranslatef(head.x, head.y,0.0);
glColor3f(1, 0, 0);
glRectf(10.0f, 10.0f, 20.0f, 20.0f);
glPopMatrix();
// boody
for(i=0; i<tamanho; i++){
glPushMatrix();
glTranslatef(body[0][i],body[1][i],0.0);
glColor3f(1, 0, 0);
glRectf(10.0f, 10.0f, 20.0f, 20.0f);
glPopMatrix();
}
}
void go(int value){
int i;
oldX[1] = head.x;
oldY[1] = head.y;
switch(keys){
case RIGHT:
head.x += 10;
if(head.x > _Gfw-20)head.x = _Giw-10;
break;
case LEFT:
head.x-= 10;
if(head.x < 0) head.x = _Gfw-20;
break;
case UP:
head.y += 10;
if(head.y > _Gfh-20)head.y = _Gih-10;
break;
case DOWN:
head.y -= 10;
if(head.y < 20) head.y = _Gfh-20;
break;
}
// if(colisao()) gameOver = true;
for(i = 0; i<tamanho; i++){
oldX[0] = oldX[1];
oldY[0] = oldY[1];
oldX[1] = body[0][i];
oldY[1] = body[1][i];
body[0][i] = oldX[0];
body[1][i] = oldY[0];
}
glutTimerFunc(140-lvl*8, go, 0);
}
void keybord(int key, int x, int y){
switch(key) {
case GLUT_KEY_RIGHT :
if(keys != LEFT)
keys = RIGHT;
break;
case GLUT_KEY_LEFT :
if(keys != RIGHT)
keys = LEFT;
break;
case GLUT_KEY_UP :
if(keys != DOWN)
keys = UP;
break;
case GLUT_KEY_DOWN :
if(keys != UP)
keys = DOWN;
break;
}
}
void Display(void){
glClear(GL_COLOR_BUFFER_BIT);
snake();
glutPostRedisplay();
glutSwapBuffers();
}
void Reshape(GLsizei w, GLsizei h){
glViewport(0, 0, _w, _h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
gluOrtho2D (0.0f, 250.0f, 0.0f, 250.0f*_h/_w);
else
gluOrtho2D (0.0f, 250.0f*_w/_h, 0.0f, 250.0f);
}
void init (void){
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(_w,_h);
glutInitWindowPosition(500,400);
glutCreateWindow("snake");
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutSpecialFunc(keybord);
go(0);
init();
glutMainLoop();
}
Subscribe to:
Posts (Atom)
No comments:
Post a Comment