Hi all : )
I'm coding a snake game, now the problem is to get the input from the user
if I used getch(); the snake will wait until she got an input from the user,
so how can I let the snake run normally and expect input in any time to change the snake's direction ?
Thanks : )

Recommended Answers

All 10 Replies

One way is to use threads. Another way is to use non-standard functions contained in conio.h -- but your compiler may or may not suppport them.

Member Avatar for iamthwee

kbhit()?

it doesn't work : (

aaaa......may be bios.h will work.....

# include "bios.h"
# include "ctype.h"
# include "stdio.h"
# include "stdlib.h"
# include "dos.h"
# include "graphics.h"
# include "conio.h"
int Level=18,SP=18; //SPEED
# define RIGHT 0x01
# define LEFT 0x02
# define CTRL 0x04
# define ALT 0x08

class Game
{
private:
int Bx,By,Count,Hits,Score;
int x,y,clr,bx,by;
public:
int X,Y;
int bomb,bomb1;
Game() {bomb=bomb1=0;x=-100; y=50; clr=WHITE;Count=3;Hits=0;
Score=0;
X=getmaxx()/2; Y=getmaxy()-5;}
void Plane(int);
void Man(int);
void Show();
};
void Win()
{
settextstyle(4,0,10);
outtextxy(100,getmaxy()/4,"You Win");
sound(600); delay(250);
sound(80); delay(210);
sound(150); delay(240);
sound(700); delay(1100);
nosound();
sleep(3);
exit(1);
}
void LevelClear(int no)
{
char str[15];
sprintf(str,"Level %d",no);
cleardevice();
settextstyle(3,0,8);
outtextxy(200,getmaxy()/4,str);
sleep(2);
cleardevice();
}
void Loose()
{
settextstyle(7,0,8);
outtextxy(100,getmaxy()/3,"Game Over!");
sound(700); delay(1100);
sound(150); delay(240);
sound(80); delay(210);
sound(600); delay(250);
nosound();
sleep(3);
exit(1);
}
void Sound()
{
for(int i=200;i>=1;i-=5)
{
for(int k=1;k<50;k++) sound(i*20);
delay(3);
}
for(;i<=60;i+=5);
{
sound(i*10);
delay(5);
}
nosound();
}
void Welcome()
{
setcolor(LIGHTGREEN);
settextstyle(4,0,7); outtextxy(100,getmaxy()/4,"ParvezMI Games");
delay(400); setcolor(WHITE);
settextstyle(7,0,4); outtextxy(250,+90+getmaxy()/4,"Presents");
delay(400); setcolor(YELLOW);
settextstyle(4,0,10); outtextxy(100,getmaxy()-190,"Air Base");
sleep(2);
cleardevice();
settextstyle(3,0,8);
outtextxy(200,getmaxy()/4,"Level 1");
sleep(2);
cleardevice();
}
void Game::Plane(int clr)
{
setcolor(clr);
sound(60);
line(x,y,x+150,y); line(x,y,x-8,y-30); line(x-8,y-30,x+8,y-30);
line(x+8,y-30,x+20,y-15); line(x+20,y-15,x+100,y-24);
line(x+100,y-24,x+130,y-24); line(x+130,y-24,x+150,y);
setfillstyle(5,clr);
bar(x+100,y-18,x+113,y-10);
bar(x+115,y-18,x+130,y-10);
line(x+60,y-5,x+30,y+20); line(x+30,y+20,x+45,y+20);
line(x+45,y+20,x+90,y-5);
if(bomb==1) {bx=x+100;by=y;bomb=2;}
else if(bomb==2) {
if(clr==15) setfillstyle(1,clr-3);
else setfillstyle(1,0);
setcolor(0);
fillellipse(bx,by,4,10);
if((bx>=X-25 && bx<=X+25) &&
(by>=Y-6 && by<=Y) && Count>=0)
{
setcolor(LIGHTRED);
settextstyle(8,0,10); outtextxy((bx-10)-25,getmaxy()-120,"*");
settextstyle(8,0,10); outtextxy((bx+10)-25,getmaxy()-120,"*");
setcolor(WHITE);
Sound();
Count--;
delay(500);
cleardevice();
bomb=0;
}
if(Count==0)
{
char str[15];
settextstyle(3,0,5);
sprintf(str,"Your Score : %d",Score);
outtextxy(190,300,str);
Loose();
}
}
}
void Game::Man(int clr)
{
setcolor(clr);
rectangle(X-25,Y-3,X+25,Y);
rectangle(X-3,Y-6,X+3,Y-3);
if(bomb1==1) {Bx=X;By=Y-8;bomb1=2;}
else if(bomb1==2) {
if(clr==15) setfillstyle(9,clr-1),setcolor(clr-1);
else setfillstyle(1,0);
bar3d(Bx-2,By-14,Bx+2,By-3,2,2);
if((Bx>=x && Bx<=x+150) &&
(By>=y-30 && By<=y) && Hits<=10)
{
setcolor(LIGHTRED);
settextstyle(8,0,10); outtextxy((Bx-10)-25,-70,"#");
settextstyle(8,0,10); outtextxy((Bx+10)-25,-70,"*");
setcolor(WHITE);
Sound();
Hits++; Score+=5;
bomb1=0;
delay(500);
cleardevice();
}
if(Hits==10) {
if(Level==SP) LevelClear(2),Level-=6; else
if(Level==SP-6) LevelClear(3),Level-=6; else
if(Level==SP-12) {
char str[15];
settextstyle(3,0,5);
sprintf(str,"High Score : %d",Score);
outtextxy(190,330,str);
Win();
}
Hits=0;
}
}
}
void Game::Show()
{
char str[15];
Plane(BLACK);
Man(BLACK);
if(x<=getmaxx()) x+=5; else x=-100;
if(bomb==2 && by<=getmaxy()) by+=12; else bomb=0; // by is Plane Bomb Speed
if(bomb1==2 && By>=5) By-=8; else bomb1=0; // By is User Bomb Speed

setcolor(WHITE); setfillstyle(1,8);
settextstyle(2,0,5);

bar3d(10,120,150,180,2,2);
sprintf(str,"Score : %d",Score); outtextxy(20,120,str);
sprintf(str,"Lifes : %d",Count); outtextxy(20,140,str);
if(Level==SP) outtextxy(20,160,"Level : 1"); else
if(Level==SP-6) outtextxy(20,160,"Level : 2"); else
if(Level==SP-12) outtextxy(20,160,"Level : 3");
Plane(WHITE);
Man(WHITE);
}

void main()
{
int gd=DETECT,gm; initgraph(&gd,&gm,"");
Welcome();
Game g;
randomize();
while(1)
{
g.Show();
if(rand()%10==1 && g.bomb==0) g.bomb=1;
///////////////////////////////
int modifiers;
g.Man(BLACK);
while (_bios_keybrd(_KEYBRD_READY) == 0);

modifiers = _bios_keybrd(_KEYBRD_SHIFTSTATUS);
if (modifiers)
{
if (modifiers & RIGHT) g.X+=8;
if (modifiers & LEFT) g.X-=8;
if (modifiers & CTRL) { if(g.bomb1==0) g.bomb1=1; }
if (modifiers & ALT) nosound(),exit(1);
}
g.Man(WHITE);
///////////////////////////////
delay(Level);
}
}

THIS IS A CODE OF GAME I DOWNLOADED FROM SOME WHERE STUDY IT U WILL GET THE ANS.

commented: what the hell is this -2
Member Avatar for iamthwee

@OP

Tell us what OS and compiler/IDE you are using so we can stop guessing.

kbhit()?

THANKS ALLOT it works now

what works now.....pls can you tell me the code u used....

what works now.....pls can you tell me the code u used....

With pleasure : )

if (kbhit())
   {
    direction = getch();
   }

and including "conio.h"

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.