Demo entry 6718314

demo

   

Submitted by anonymous on Mar 03, 2018 at 18:05
Language: C++. Code size: 6.3 kB.

#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

int board[3][3];    //画出操作的3*3的版面
int turn;            // 当前玩家序号
int result;            // 游戏的结果值
bool over;            // 判断游戏是否结束

/*
 绘制网格线的循环
 */
void Intialize()
{
    turn=1;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
            board[i][j]=0;
    }
}
/*
 游戏结束时是否重新开始游戏
 */
void OnKeyPress(unsigned char key,int x,int y)
{
    switch(key)
    {
        case 'y':         //判断重新开始游戏,改变over的初始值,重新画网格
            if(over==true)
            {
                over=false;
                Intialize();
            }
            break;
        case 'n':      //判断结束游戏,跳出界面
            if(over==true)
            {
                exit(0);
            }
            break;
        default:
            exit(0);
    }
}

/*
 确认玩家鼠标点击区域
 */
void OnMouseClick(int button,int state,int x,int y)
{
    if(over==false && button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
    {
        if(turn==1)
        {
            if(board[(y-50)/100][x/100]==0)
            {
                board[(y-50)/100][x/100]=1;
                turn=2;
            }
        }
        else if(turn==2)
        {
            if(board[(y-50)/100][x/100]==0)
            {
                board[(y-50)/100][x/100]=2;
                turn=1;
            }
        }
    }
}

/*
 绘制提示字符串
 */
void DrawString(void *font,const char s[],float x,float y)
{
    unsigned int i;
    glRasterPos2f(x,y);
    for(i=0;i<strlen(s);i++)
    {
        glutBitmapCharacter(font,s[i]);
    }
}

/*
 绘制网格线
 */
void DrawLines()
{
    glBegin(GL_LINES);
    glColor3f(0,0,0);
    
    glVertex2f(100,50);
    glVertex2f(100,340);
    glVertex2f(200,340);
    glVertex2f(200,50);
    
    glVertex2f(0,150);
    glVertex2f(300,150);
    glVertex2f(0,250);
    glVertex2f(300,250);
    
    glEnd();
}

/*
 画圈
 */
void DrawCircle(float cx, float cy, float r, int num_segments)
{
    glBegin(GL_LINE_LOOP);
    for (int i = 0; i < num_segments; i++)
    {
        float theta = 2.0f * 3.1415926f * float(i) / float(num_segments);//得到当前角度
        float x = r * cosf(theta);//当前角度x方向的值
        float y = r * sinf(theta);//当前角度y方向的值
        glVertex2f(x + cx, y + cy);//输出顶点
    }
    glEnd();
}

/*
画圈和叉的具体画法
 */
void DrawXO()
{
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            if(board[i][j]==1)
            {
                glBegin(GL_LINES);
                glVertex2f(50 + j * 100 - 25, 100 + i * 100 - 25);
                glVertex2f(50 + j * 100 + 25, 100 + i * 100 + 25);
                glVertex2f(50 + j * 100 - 25, 100 + i * 100 + 25);
                glVertex2f(50 + j * 100 + 25, 100 + i * 100 - 25);
                glEnd();
            }
            else if(board[i][j]==2)
            {
                
                DrawCircle(50 + j*100 , 100 + i*100 , 25 , 15);
            }
        }
    }
}

/*
 判断胜利的方法
 */
bool CheckWinner()
{
    int i, j;
    // 水平方向的检验
    for(i=0;i<3;i++)
    {
        for(j=1;j<3;j++)
        {
            if(board[i][0]!=0 && board[i][0]==board[i][j])
            {
                if(j==2)
                {
                    return true;
                }
            }
            else
                break;
        }
    }
    // 垂直方向检验
    for(i=0;i<3;i++)
    {
        for(j=1;j<3;j++)
        {
            if(board[0][i]!=0 && board[0][i]==board[j][i])
            {
                if(j==2)
                    return true;
            }
            else
                break;
        }
    }
    //对角线方向检验
    if((board[0][0]!=0 && board[0][0]==board[1][1] && board[0][0]==board[2][2])
       || (board[2][0]!=0 && board[2][0]==board[1][1] && board[2][0]==board[0][2]))
        return true;
    return false;
}

/*
 判断网格是否为空
 */
bool CheckIfDraw()
{
    int i, j;
    bool draw;
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            if(board[i][j]==0)
                return false;
        }
    }
    return true;
}

/*
 界面显示功能
 */
void Display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glClearColor(1, 1, 1, 1);
    glColor3f(0, 0, 0);
    if(turn == 1)
        DrawString(GLUT_BITMAP_HELVETICA_18, "player1's turn", 100, 30);
    else
        DrawString(GLUT_BITMAP_HELVETICA_18, "player2's turn", 100, 30);
    
    DrawLines();
    DrawXO();
    
    if(CheckWinner() == true)
    {
        if(turn == 1)
        {
            over = true;
            result = 2;
        }
        else
        {
            over = true;
            result = 1;
        }
    }
    else if(CheckIfDraw() == true)
    {
        over = true;
        result = 0;
    }
    if(over == true)
    {
        DrawString(GLUT_BITMAP_HELVETICA_18, "Game Over !", 100, 160);
        if(result == 0)
            DrawString(GLUT_BITMAP_HELVETICA_18, "It's Draw", 110, 185);
        if(result == 1)
            DrawString(GLUT_BITMAP_HELVETICA_18, "Play1 Win !", 95, 185);
        if(result == 2)
            DrawString(GLUT_BITMAP_HELVETICA_18, "Play2 win !", 95, 185);
        DrawString(GLUT_BITMAP_HELVETICA_18, "Do you want to continue (y/n)", 40, 210);
    }
    glutSwapBuffers();
}

/*
 改变窗口形状时,里面绘制不变形
 */
void Reshape(int x, int y)
{
    glViewport(0, 0, x, y);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, x, y, 0, 0, 1);
    glMatrixMode(GL_MODELVIEW);
}

/*
 主函数
 */
int main(int argc, char **argv)
{
    Intialize(); //初始化网格数组
    glutInit(&argc,argv); //初始化GLUT库
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);  //初始化显示模式:RGB颜色模型,双缓冲
    glutInitWindowPosition(550,200); //初始化窗口位置
    glutInitWindowSize(300,350);  //初始化窗口大小
    glutCreateWindow("九宫格小游戏"); //窗口命名
    glutReshapeFunc(Reshape); //注册绘制回调函数
    glutDisplayFunc(Display); //注册重绘回调函数
    glutKeyboardFunc(OnKeyPress); //注册按键回调函数
    glutMouseFunc(OnMouseClick);  //注册鼠标回调函数
    glutIdleFunc(Display);  //注册全局回调函数
    glutMainLoop();  //glut事件处理循环
    return 0;
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).