简体   繁体   English

使用过剩重启蛇游戏

[英]Restart a snake game using glut

I wrote a c++ snake game using OpenGL and GLUT. 我使用OpenGL和GLUT编写了c ++蛇游戏。 The problem is that I implemented a small menu, with 2 buttons: new game, exit. 问题是我实现了一个带有2个按钮的小菜单:新游戏,退出。 I'm having a hard time with the "new game" part. 对于“新游戏”部分,我很难过。 I've been moving here and there lines of code and I don't know how it should be. 我一直在这里移动,那里的代码行,我不知道应该如何。 Main looks like this: 主要看起来像这样:

int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Aarghhh! O ramaaa !");
createMenu();      
glClearColor(0.0, 0.0, 0.0, 0.0);
glutIdleFunc(display_menu);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(dreptunghi);
glutSpecialFunc(player);
glutMainLoop();
return 0;
}

where init, createMenu and display_menu look like this: init,createMenu和display_menu如下所示:

 void init(void) {
    glClearColor(1.0, 1.0, 1.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0.0, 800.0, 0.0, 600.0);
    glShadeModel(GL_FLAT);
 }

 void menu(int num) {
    if (num == 0)
        exit(0);
    else {
        if (num == 1) {
            menu_value = num;           
        }
    }

    //glutPostRedisplay();
 }

 void createMenu(void) {
    glutCreateMenu(menu);
    glutAddMenuEntry("New game!", 1);
    glutAddMenuEntry("Exit", 0);    
    glutAttachMenu(GLUT_RIGHT_BUTTON);
 }

 void display_menu(void){
    if (menu_value == 1) {
        snake.clear();
        i = 30.0;
        j = 30.0;
        alpha = 1.0;
        value = -1;
        speed = 3;
        eaten = true;
        collided_food = false;
        collided_self = false;

        createMenu();
        glClearColor(0.0, 0.0, 0.0, 0.0);
        glutIdleFunc(display_menu);

        init();
        glutDisplayFunc(dreptunghi);
        glutReshapeFunc(reshape);
        glutSpecialFunc(player);
        //glutMainLoop();
    }
    glutSwapBuffers();
 }

The thing is: if I place the content of display_menu inside the if statement from menu function, it works, but I have to resize the window for the redrawing to happen (I realised that this happens because MainLoop expects an event, but I have no idea how to beat this). 关键是:如果将display_menu的内容放在菜单功能的if语句中,它可以工作,但是我必须调整窗口大小以进行重绘(我意识到这是因为MainLoop期望发生一个事件,但是我没有知道如何打败这个)。 If I keep it this way, it doesn't change anything to good, only to worse. 如果我这样坚持下去,它不会改变任何事情,只会变得更糟。 I'm new to this and I'm having troubles finding out how this works. 我是新来的人,我很难找出它是如何工作的。

If anybody has the same problem, this is how I solved it: 如果有人遇到相同的问题,这就是我的解决方法:

void menu(int num) {
if (num == 0) {
    if (engine)
        engine->drop();
    exit(0);
}
else {
    if (num == 1) {
        menu_value = num;   
        snake.clear();
        i = 30.0;
        j = 30.0;
        nou.set(i, j);
        snake.push_back(nou);
        alpha = 1.0;
        value = -1;
        speed = 3;
        eaten = true;
        collided_food = false;
        collided_self = false;

        createMenu();
        glClearColor(0.0, 0.0, 0.0, 0.0);

        glClearColor(1.0, 1.0, 1.0, 0.0);
        glutDisplayFunc(dreptunghi);
        glutReshapeFunc(reshape);
        glutSpecialFunc(player);
    }
}
glutSwapBuffers();
glutPostRedisplay();
}

while the init() function remains intact. 而init()函数保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM