Click here to Skip to main content
15,908,842 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
why the (game over) show whan the shape reach to the Border???


C#
// 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;
}
Posted
Comments
Mohibur Rashid 29-May-12 21:27pm    
Why don't you debug?
Anderso0on 29-May-12 21:39pm    
code is correct but must in the first update library glutin visual stdio
i want know the message box why show if the shape reach to the border
Sergey Alexandrovich Kryukov 29-May-12 23:50pm    
Huh?
--SA
Sergey Alexandrovich Kryukov 29-May-12 23:52pm    
Not clear. Everything should be explained: the goal of it, what it does, what's expected, what's going wrong (but properly explained), in what lined of code...
--SA
[no name] 30-May-12 2:19am    
One would have to draw the conclusion that you have cut and pasted this code and don't know what it does. Not really what the forum is for.

1 solution

Is this not your own code?

In either case, the answer is very simple.
It's this if statement that kills the game.

C++
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);
    }


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

Ive just woken up and dont feel like trying to build it. From a quick glance, it would seem that you can prevent the game ending prematurely by changing the above if statement.

C++
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);
    }
 
Share this answer
 
Comments
Sandeep Mewara 30-May-12 13:52pm    
5+ for effort put in answering the lazy-homework question!
enhzflep 30-May-12 13:57pm    
Thank-you. I supposed that since (some) effort was shown, it wouldn't be _so_ awful of me to indicate that the solution could be as simple as removing 4 characters from the source code.
Cheers.
Sandeep Mewara 30-May-12 14:16pm    
:thumbsup: :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900