#include<stdio.h>
#include<conio.h>
#include<windows.h>
#include<GL/glut.h>
#include<math.h>
#define WINDOW_SIZE 600
#define WINDOW_POS 100
#define ESC 27
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define START_POS 13
#define QUIT_POS 16
{
int aiStep[250];
int iNum=0;
GLint iPlayer=1;
GLint iEnd=0;
GLint aiBoard[15][15];
struct Point
{
int x;
int y;
}stPoint;
int main(int argc,char **argv)
{
glutInit(&argc, argv)
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(WINDOW_SIZE, WINDOW_SIZE);
glutInitWindowPosition(WINDOW_POS,WINDOW_POS);
iStart= ShowWelcome();
if(iStart)
{
ShowGuide();
glutCreatWindow("fivechess");
DrawBoard();
CtrlMenu();
NewGame();
glutDisplayFunc(DisplayGL);
glutKeyboardFunc(CtrlKeyboard);
glutMouseFunc(CtrlMouse);
glutMainLoop();
}
ShowEnd();
return 0;
}
void GotoXY( int x,int y )
int ChooseArrow();
int ShowWelcome();
void ShowEnd();
void showGuide();
void ShowWinner();
void GotoXY(int x,int y)
{
COORD c;
c.X=2*x;
c.Y=Y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
}
int ChooseArrow()
{
char cButton;
int iStart = -1;
while(iStart != 0 &&iStart !=1)
{
cButton = getch();
if(cButton ==UP || cButton ==DOWN)
{
if(stPoint.y==QUIT_POS)
{
stPoint.y==QUIT_POS;
GotoXY(10,START_POS);
printf(" ");
Goto(10,QUIT_POS);
printf("→");
GotoXY(12,QUIT_POS);
}
else if(stPoint.y=QUIT_POS)
{
stPoint.y=START_POS;
GotoXY(10,QUIT_POS);
printf("→");
GotoXY(12,START_POS);
}
}
if((cButton==START_POS&&stPoint.y==QUIT_POS)||cButton==ESC)
{iStart=0;
break;
}
if(cButton==START_POS&&stPoint.y==START_POS)
{
iStart=1;
break;
}
}
return iStart;
}
///261
int ShowWelcome()
{
int iStart=-1;
system("color 3F");
GotoXY(14,1);
printf("Welcome to play fivechess");
GotoXY(17,7);
printf("MAIN MENU");
GotoXY(15,START_POS);
PRINTF("***START GAME***");
GotoXY(15,QUIT_POS);
printf("***QUIT GAME***");
GotoXY(10,START_POS);
printf("--→");
stPoint.x=15;
stPoint.y=START_POS;
GotoXY(12,START_POS);
iStart=ChooseArrow();
return iStart;
}
////262 4
void ShowGuide()
{
system("cls");
system("color 4F");
GotoXY(6,5);
printf("BLACK");
Goto(6,9);
printf("PLAYER");
GotoXY(19,7);
printf("VS");
GotoXY(31,5);
printf("WHITE");
GotoXY(31,9);
printf("PLAYER");
GotoXY(6,18);
printf("1:You Can Right Button to Play Again,Undo or Exit!");
GotoXY(6,20);
printf("2:You Can use ESC to key to exit the gamme too!");
}
//////5
void ShowEnd()
{
system("cls");
system("color 6F");
GotoXY(16,3);
printf("GAME OVER!!!");
GotoXY(1,23);
}
///263 6
void ShowWinner()
{
system("cls");
GotoXY(15,8);
printf("congratulations!!!");
}
else
{
system("color 2F");
printf("WHITE PLAYER WIN THE GAME!!!");
}
}
int JudgeWin(int x,int y);
void TransPos(int *x,int *y);
void CtrlMouse(int iButton,int iState,int x,int y);
void NewGame();
int JudgeWin(int x, int y)
{
int aiDir[4][2]={0,1,1,0,1,1,1,-1};
for (int i=0; i<4; ++i)
{
int iTotal =1;
for(int j=1; aiBoard[x][y]== aiBoard[x +j*aiDir[i][0][y+ j* aiDir[i]
[1]];++j)
++iTotal;
for (int j= 1; aiBoard[x][y] == aiBoard[x+j* aiDir[i][0]][y
-j* aiDir[i][1]];++j)
++iTotal;
if(iTotal>= 5) return 1;
}
return 0;
void TransPos(int *x, int*y)
{
*y= WINDOW_SIZE-*y;
*x=(*x<<4)/( WINDOW_SIZE*1.0)-0.5;
*y=(*y<<4)/( WINDOW_SIZE*1.0)-0.5;
}
void CtrIMouse(int iButton, int iState, int x, int y)
{
switch(iButton)
{
case GLUT_LEFT_BUTTON;
if (! iEnd)
{
TransPos(&x, &y);
if(aiBoard[x][y]) break;
aistep[ iNum++]=x*100+y;
if(JudgeWin(x,y))
{iEnd=1;
ShowWinner();
}
iPlayer=-iPlayer;
glutPostRedisplay();
break;
}
}
void NewGame()
{
ShowGuide();
iNum=0;
iPlayer=1;
iEnd=0;
memset(aiBoard,0,sizeof(aiBoard));
}
void DisolayGL();
void DrawChess(int x, int y, int icolor );
void DrawBoard();
void DisplayGL()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 1, 1);
glDrawArrays(GL_LINES, 0, 60);
for (int i =0;i<15;i++)
{
for (int j=0; j<15;j++)
{if(aiOard[i][j])
DrawChess(i, j, aiBoard[i][j]);
}
}
glFlush();
}
void DrawChess(int x, int y, int iColor)
{
if (icolor == 1)
glColor3f(0,0,0);
else
glColor3f(1, 1, 1);
glBegin(GL_TRIANGLE_FAN);
for (int i =0; i<360; i++)
g1Vertex2f(0.37cos(i1.0)+x+1,0.37sin(i1.0)+y+1);
glEnd();
}
void DrawBoard()
{
glclearColor(0.1, 0.2, 0.3, 0);
glMatrixMode(GL_PROJECTION);
glOrtho(0,16,0,16,-1,1);
static GLint vertices[ 120];
int i, j;
for(i=0;i<15;i++)
{
j=i<<2;
vertices[j]=i+1;
vertices[j + 1]=1;
vertices[j + 2]=i+1;
vertices[j + 3]=15;
vertices[j + 60]=1;
vertices[j + 61]=i+1;
vertices[j + 62]=15;
vertices[j + 63]=i+1;
}
glEnableClientState( GL_VERTEX_ARRAY);
glVertexPointer(2, GL_INT, 0, vertices);
}
void CtrlKeyboard (unsigned char cKey, int x, int y);
void CtrlMenu();
void ManageItem(int iNumber);
void CtrlKeyboard(unsigned char cKey, int x, int y)
{
if (cKey== ESC)
{
ShowEnd();
exit(0);
}
}
void CtrlMenu()
{
int newGameMenu= glutCreateMenu( ManageItem);
glutAddMenuEntry("Play Again",1);
glutAddMenuEntry("Undo",2);
glutAddMenuEntry("Exit",3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void ManageItem( int iNumber)
{
switch(iNumber)
{
case 1:
NewGame();
break;
case 2:
if(iNum==0) break;
int iPrev= aistep[ iNum -1];
iNum--;
aiBoard[ iPrev/100][ iPrev % 100]=0;
iPlayer=-iPlayer;
break;
case 3:
ShowEnd();
exit(0);break;
}
glutPostRedisplay();
}
}
可以使用这个。基于EasyX的五子棋小游戏
https://juejin.cn/post/7026886605201735694
#define结束后的一个大括号是干啥呢
各个函数需要在main前进行申明
stPoint.y==QUIT_POS;改成 stPoint.y=QUIT_POS;