Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9143185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:57:38+00:00 2026-06-17T09:57:38+00:00

It is a rather simple snake game. It just gets hanged. When I try

  • 0

It is a rather simple snake game. It just gets hanged.
When I try to execute it, it runs for a while and then hangs in some moments.
Please Tell me what to do?
This is turbo C++ version 3.0 and please do not tell me to use new version. And don’t call me outdated, I know that.

#include<conio.h>
#include<iostream.h>
#include<dos.h>
#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int x,y,a,b,flag,i,discx,discy,sc,maxx,maxy,same;
short int arr[100][2];
char ch,check;
void shift();
int introscreen();
void condition();
void gameover();
void score();
void randomapple();

int introscreen()
    {
     outtextxy(maxx/2,30,"SNAKE");
     outtextxy((maxx/2)-10,50,"INSTRUCTIONS");
     outtextxy(5,70,"W A S D represent Top Right Bottom Left respectively.");
     outtextxy(5,90,"Press Spacebar to quit any moment");
     outtextxy(5,110,"To play Press Space else anything else");
     char take=getch();
     if(take==' ')
     return 1;
     else
     return 0;
     }
void randomapple()
{same=0;
while(1)
{
 a=random(maxx-5);
 b=random(maxy-5);
 for(i=0;i<flag;i++)
 {if((abs(a-arr[i][0]<=5)&&abs(b-arr[i][1]<=5))||a<=5||b<=5)
  same=1;
 }
 if(same==0) break;
 }
 setcolor(RED);
 outtextxy(a,b,"0");
 setcolor(WHITE);
}
void shift()
    {discx=arr[flag-1][0];
     discy=arr[flag-1][1];
     for(i=flag-1;i>0;i--)
        {
         arr[i][0]=arr[i-1][0];
         arr[i][1]=arr[i-1][1];
         }
     arr[0][0]=x;
     arr[0][1]=y;
     }
void printsnake()
    {
     setcolor(BLACK);
     outtextxy(discx,discy,"X");
     setcolor(GREEN);
     outtextxy(x,y,"X");
     setcolor(RED);
     outtextxy(a,b,"0");
     rectangle(0,0,maxx,maxy);
     delay(5);
    }
void condition()
    {
     switch(ch)
        {
         case'd':x++;
             break;
         case'a':x--;
             break;
         case'w':y--;
             break;
         case's':y++;
             break;
         case' ':exit(1);

        }
     }
    void score()
    {if((abs(x-a)<=5)&&(abs(y-b)<=5))
     {sc++;
     if(flag<500)
     flag=flag+10;
     setcolor(BLACK);
     outtextxy(a,b,"0");
     randomapple();
     }

    if(sc==100)
    {
        clrscr();
        cout<<"You are the champion tonight!!!!";
        getch();
        exit(1);
    }
    }
void collision()
    {
     if(x<=5||y<=5||maxx-x<=5||maxy-y<=5)
     gameover();
     for(i=1;i<flag;i++)
     {
      if(arr[0][0]==arr[i][0]&&arr[0][1]==arr[i][1])
      gameover();
     }
    }
void gameover()
    {textcolor(BLACK);
     clrscr();
     textcolor(WHITE);
     cout<<"YOU LOSE!\n";
     cout<<"GAME OVER\n";
     cout<<"Final Score = "<<sc;
     getch();
     getch();
     getch();
     getch();
     exit(1);
    }
void main()
    {
     randomize();
     int gdriver = DETECT, gmode, errorcode;
     /* initialize graphics mode */
     initgraph(&gdriver, &gmode, "");
     maxx=500;
     maxy=400;
     x=maxx/2;
     y=maxy/2;
     ch='d';
     sc=0;
     if(introscreen())
     {
     textcolor(BLACK);
     clrscr();
     rectangle(0,0,maxx,maxy);
     flag=20;
     setcolor(GREEN);
     for(i=19;i>=0;i--)
     {arr[i][0]=x-i;
      arr[i][1]=y-i;
      outtextxy(x-i,y-i,"X");
     }
     setcolor(WHITE);
     randomapple();
     while(1)
     {
        if(kbhit())
        {
            check=getch();
            switch(check)
            {
            case 'a':if(ch!='d') ch=check;
                break;
            case 'd':if(ch!='a') ch=check;
                break;
            case 'w':if(ch!='s') ch=check;
                break;
            case 's':if(ch!='w') ch=check;
                break;
            case ' ':ch=check;
                break;
            }

        }
        condition();
        shift();
        printsnake();
        collision();
        score();
     }
     }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:57:39+00:00Added an answer on June 17, 2026 at 9:57 am

    From what I see, the problem lies in randomapple function. If you look closer on it, if you set same to 1 then it stays 1 forever. You should reset it every iteration of that while.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A rather simple question that I'm sure there's an answer to, I just can't
I have a rather simple query that for some reason is not using any
I think its a rather simple question but I couldn't really find an answer
I have a rather simple ListView row: <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=fill_parent android:layout_height=wrap_content
In a rather simple ASP.NET application where the user can upload a image, mostly
My question is rather simple. Let's suppose I'm executing the algorithm A star (search
my question is rather simple, if you have an pure virtual class (interface) but
I have a rather simple question that I could normally debug myself, but I
Have a rather simple question. Does anyone knows if i can use jparallax both
I have following rather simple query select count(*) from tbl t1, tbl t2 For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.