本文介绍了这段代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(游戏结束)为什么显示形状达到边界?


why the (game over) show whan the shape reach to the Border???


// snake.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include<glut.h>
#include<Windows.h>
int N=3;
int n=0;

struct
{
    int x ;
    int y ;

}S[100];

int s=10;
void Display(void)
{

    glClear(GL_COLOR_BUFFER_BIT);

    for(int i=0;i<N;++i)
    {
        if(i == 0)
        {
            glColor3f(1,0,0);
        }
        else
        {
            glColor3f(1,.5,0);
        }


        glRectf(S[i].x-10,S[i].y-10,S[i].x+10,S[i].y+10);

    }

    glutSwapBuffers();
}

void timers(int = 0 )
{
    Display();


        if(n == 0 && s % 10 == 0)
        {
            for(int i=N-1;i>0;--i)
            {
                S[i].x=S[i-1].x;
                S[i].y=S[i-1].y;
            }
            S[0].y-=20;

            s=1;
        }
        if(n == 1 && s % 10 == 0)
        {
            for(int i=N-1;i>0;--i)
            {
                S[i].x=S[i-1].x;
                S[i].y=S[i-1].y;
            }
            S[0].x+=20;
            s=1;
        }
        if(n == 2 && s % 10 == 0)
        {
            for(int i=N-1;i>0;--i)
            {
                S[i].x=S[i-1].x;
                S[i].y=S[i-1].y;
            }
            S[0].y+=20;
            s=1;
        }
        if(n == 3 && s % 10 == 0)
        {
            for(int i=N-1;i>0;--i)
            {
                S[i].x=S[i-1].x;
                S[i].y=S[i-1].y;
            }
            S[0].x-=20;
            s=1;
        }

    if(S[0].x<=0||S[0].y<=0     ||S[0].x>=600,S[0].x>=600)
    {

        MessageBox(NULL,L"Game over",L"Game over",MB_OK);
        exit(0);
    }



        if(GetAsyncKeyState(VK_LEFT))
        n=3;
        if(GetAsyncKeyState(VK_RIGHT))
        n=1;
        if(GetAsyncKeyState(VK_UP))
        n=0;
        if(GetAsyncKeyState(VK_DOWN))
        n=2;


    if(s<10)
    {
        s++;
    }

    glutTimerFunc(10,timers,0);
}



int _tmain(int argc,char **argv)
{
    S[0].x=50;
    S[0].y=200;
    for(int i=1;i<N;++i)
    {
        S[i].x=S[i-1].x;
        S[i].y=S[i-1].y+20;
    }
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE );
    glutInitWindowSize(600,600);
    glutInitWindowPosition(0,0);
    glutCreateWindow("Bousha");
    glLoadIdentity();
    glMatrixMode(GL_PROJECTION);
    glOrtho(0,500,500,0,0,1);


    glutDisplayFunc(Display);
    timers();

    glutMainLoop();
    return 0;
}

推荐答案

if(S[0].x<=0||S[0].y<=0     ||S[0].x>=600,S[0].x>=600)
    {
        MessageBox(NULL,L"Game over",L"Game over",MB_OK);
        exit(0);
    }



鉴于该文件名为snake.cpp,我将假定这是蛇的经典游戏.

事实是,您可以平行于边缘单元中的墙移动.当满足以下两个条件之一时,游戏结束:
下一步之后:


  • 蛇的头将占据一个已经被其身体占据的单元格
  • 蛇的头将占据一个壁单元,而不是游戏板单元 li>



    Given that the file''s called snake.cpp, I''ll assume that this is the classic game of snake.

    The thing is, you can travel parallel to the walls in the edge cells. The game ends when one of the two conditions are met:
    After the next move:


    • The snake''s head will occupy a cell already occupied by it''s body
    • The snakes head will occupy a cell that is a wall cell, rather than a game-board cell
    • if( (S[0].x < 0) || (S[0].y < 0) || (S[0].x > 600) || (S[0].x > 600) )
          {
              MessageBox(NULL,L"Game over",L"Game over",MB_OK);
              exit(0);
          }


      这篇关于这段代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 13:48
查看更多