OpenGL偶尔写一写还是挺好玩的
可执行文件下载
wsad控制左边方框,上下左右控制右边方框,Esc退出,空格键重开。
右边方框的目的是到达左边底线
左边方框的目的是捉住右边方框
#include <stdlib.h>
#include <windows.h>
#include <gl/glut.h>
#define HORIZONAL_RADIUS 3200
#define VERTICAL_RADIUS 2000
#define CHASER_RADIUS 100
#define ESCAPER_RADIUS 100
#define CHASER_SPEED 1
#define ESCAPER_SPEED 1
int chaserPos[]={CHASER_RADIUS - HORIZONAL_RADIUS, 0};
int escaperPos[]={HORIZONAL_RADIUS - ESCAPER_RADIUS, 0};
int chaserSpeed[]={0, 0};
int escaperSpeed[]={0, 0};
void restart();
void nullCallback(){}
void nullKeyboardCallback(unsigned char key,int x,int y)
{
if(key == 27)
exit(0);
if(key == ' ')
restart();
}
void nullSpecialCallback(int key,int x,int y){}
int abs(int x)
{
if(x<0)return -x;
return x;
}
void drawSquare(int center[], int radius)
{
glBegin(GL_POLYGON);
glVertex2i(center[0] - radius, center[1] - radius);
glVertex2i(center[0] - radius, center[1] + radius);
glVertex2i(center[0] + radius, center[1] + radius);
glVertex2i(center[0] + radius, center[1] - radius);
glEnd();
}
void displayRoles()
{
glClearColor(1.0,1.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glColor3f(1.0, 0, 0);
drawSquare(chaserPos,CHASER_RADIUS);
glColor3f(0, 0, 1.0);
drawSquare(escaperPos,ESCAPER_RADIUS);
glPopMatrix();
glutSwapBuffers();
}
void checkPos(int center[], int radius)
{
if(center[0] - radius < - HORIZONAL_RADIUS)
center[0] = radius - HORIZONAL_RADIUS;
if(center[0] + radius > HORIZONAL_RADIUS)
center[0] = HORIZONAL_RADIUS - radius;
if(center[1] - radius < - VERTICAL_RADIUS)
center[1] = radius - VERTICAL_RADIUS;
if(center[1] + radius > VERTICAL_RADIUS)
center[1] = VERTICAL_RADIUS - radius;
}
void keyboardCallback(unsigned char key,int x,int y)
{
switch(key)
{
case 27:
exit(0);
break;
case ' ':
restart();
break;
case 'A':case 'a':
chaserSpeed[0] = -1;
break;
case 'D':case 'd':
chaserSpeed[0] = 1;
break;
case 'S':case 's':
chaserSpeed[1] = -1;
break;
case 'W':case 'w':
chaserSpeed[1] = 1;
break;
}
}
void specialCallback(int key,int x,int y)
{
switch(key)
{
case GLUT_KEY_LEFT :
escaperSpeed[0] = -1;
break;
case GLUT_KEY_RIGHT :
escaperSpeed[0] = 1;
break;
case GLUT_KEY_DOWN :
escaperSpeed[1] = -1;
break;
case GLUT_KEY_UP :
escaperSpeed[1] = 1;
break;
}
}
void keyboardUpCallback(unsigned char key,int x,int y)
{
switch(key)
{
case 27:
exit(0);
break;
case 'A':case 'a':
case 'D':case 'd':
chaserSpeed[0] = 0;
break;
case 'S':case 's':
case 'W':case 'w':
chaserSpeed[1] = 0;
break;
}
}
void specialUpCallback(int key,int x,int y)
{
switch(key)
{
case GLUT_KEY_LEFT :
case GLUT_KEY_RIGHT :
escaperSpeed[0] = 0;
break;
case GLUT_KEY_DOWN :
case GLUT_KEY_UP :
escaperSpeed[1] = 0;
break;
}
}
void escaperWins()
{
char* s="Escaper Wins!";
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glColor3f(0, 1.0, 1.0);
drawSquare(chaserPos,CHASER_RADIUS);
glColor3f(1.0, 1.0, 0);
drawSquare(escaperPos,ESCAPER_RADIUS);
glColor3f(1.0, 1.0, 1.0);
glRasterPos2f(-400, 0);
while(*s)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *(s++));
glPopMatrix();
glutSwapBuffers();
}
void chaserWins()
{
char* s="Chaser Wins!";
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glColor3f(0, 1.0, 1.0);
drawSquare(chaserPos,CHASER_RADIUS);
glColor3f(1.0, 1.0, 0);
drawSquare(escaperPos,ESCAPER_RADIUS);
glColor3f(1.0, 1.0, 1.0);
glRasterPos2f(-400, 0);
while(*s)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *(s++));
glPopMatrix();
glutSwapBuffers();
}
void checkResult()
{
if(escaperPos[0] - ESCAPER_RADIUS == - HORIZONAL_RADIUS)
{
glutDisplayFunc(escaperWins);
glutKeyboardFunc(nullKeyboardCallback);
glutSpecialFunc(nullSpecialCallback);
glutKeyboardUpFunc(nullKeyboardCallback);
glutSpecialUpFunc(nullSpecialCallback);
glutIdleFunc(nullCallback);
glutPostRedisplay();
}
if(abs(escaperPos[0] - chaserPos[0]) < ESCAPER_RADIUS + CHASER_RADIUS
&& abs(escaperPos[1] - chaserPos[1]) < ESCAPER_RADIUS + CHASER_RADIUS)
{
glutDisplayFunc(chaserWins);
glutKeyboardFunc(nullKeyboardCallback);
glutSpecialFunc(nullSpecialCallback);
glutKeyboardUpFunc(nullKeyboardCallback);
glutSpecialUpFunc(nullSpecialCallback);
glutIdleFunc(nullCallback);
glutPostRedisplay();
}
}
void idleCallback()
{
chaserPos[0]+=chaserSpeed[0]*CHASER_SPEED;
chaserPos[1]+=chaserSpeed[1]*CHASER_SPEED;
checkPos(chaserPos, CHASER_RADIUS);
escaperPos[0]+=escaperSpeed[0]*ESCAPER_SPEED;
escaperPos[1]+=escaperSpeed[1]*ESCAPER_SPEED;
checkPos(escaperPos, ESCAPER_RADIUS);
checkResult();
glutPostRedisplay();
}
void restart()
{
glutDisplayFunc(displayRoles);
glutKeyboardFunc(keyboardCallback);
glutSpecialFunc(specialCallback);
glutKeyboardUpFunc(keyboardUpCallback);
glutSpecialUpFunc(specialUpCallback);
glutIdleFunc(idleCallback);
chaserPos[0] = CHASER_RADIUS - HORIZONAL_RADIUS;
chaserPos[1] = 0;
escaperPos[0] = HORIZONAL_RADIUS - ESCAPER_RADIUS;
escaperPos[1] = 0;
chaserSpeed[0]=chaserSpeed[1]=0;
escaperSpeed[0]=escaperSpeed[1]=0;
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutCreateWindow("CK Chaser");
ShowCursor(0);
gluOrtho2D(-HORIZONAL_RADIUS, HORIZONAL_RADIUS, -VERTICAL_RADIUS, VERTICAL_RADIUS);
glutFullScreen();
glutIgnoreKeyRepeat(1);
restart();
glutMainLoop();
return 0;
}