#include <graphics.h> #include <stdlib.h> #include <conio.h> #include <bios.h> #include <dos.h> #define N 50 #define A 10 #define bool int #define true 1 #define false 0 #define LEFT 0x4b00 #define RIGHT 0x4d00 #define DOWN 0x5000 #define UP 0x4800 #define ESC 0x011b int i,key=-1,score=0; char ch='A'; int gamespeed=50;/*speed=50,100*/ struct Color { int x; int y; int c; }; struct Food { int x; int y; bool need;/*true=1=no food and need to put food*/ }food; struct Snake { int x[N]; int y[N]; int node; int direction; bool dead;/*true=1=Esc,or alive false=0*/ }snake; void welcome(); void countdown(); void drawwall(); void mouse(); void gameover(); void gameplay(); void prscore(); void main() { int gd=VGA,gm=VGAHI; initgraph(&gd,&gm,"c:\\TC30\\BGI"); cleardevice(); do{ score=0; ch='A'; cleardevice(); welcome(); countdown(); drawwall(); gameplay(); gameover(); }while(ch=='r'); closegraph(); } void welcome() { char ch='A'; /*viewport1-B*/ setviewport(60,50,600,450,0); /*GREEDY SNAKE-B*/ setcolor(LIGHTRED); settextstyle(3,HORIZ_DIR,7); settextjustify(1,1); outtextxy(270,40,"GREEDY SNAKE"); /*GREEDY SNAKE-E*/ /*BY Sindy-B*/ setcolor(RED); settextstyle(3,HORIZ_DIR,3); settextjustify(1,1); outtextxy(430,90,"-BY Sindy-"); /*BY Sindy-E*/ /*Keys viewport-B*/ /*WELCOME-B*/ setviewport(65,240,585,360,0); setcolor(YELLOW); settextjustify(1,1); for(i=1;i<10;i++) { sound(100*i); settextstyle(3,HORIZ_DIR,i); outtextxy(260,20,"WELCOME!"); delay(gamespeed); clearviewport(); nosound(); } for(i=9;i>=1;i--) { sound(100*i); settextstyle(3,HORIZ_DIR,i); outtextxy(260,20,"WELCOME!"); delay(gamespeed); clearviewport(); nosound(); } /*welcome-E*/ /*Bottons-B*/ setcolor(YELLOW); setlinestyle(0,0,3); do { setfillstyle(1,DARKGRAY); /*key1*/ bar(10,10,250,110); settextstyle(3,HORIZ_DIR,4); outtextxy(130,60,"SLOW[s]"); /*key2*/ bar(270,10,510,110); settextstyle(3,HORIZ_DIR,4); outtextxy(390,60,"QUICK[q]"); do { ch=getch(); }while(ch!='s'&&ch!='q'); if(ch=='s') { setfillstyle(1,RED); /*key1*/ bar(10,10,250,110); settextstyle(3,HORIZ_DIR,4); outtextxy(130,60,"SLOW[s]"); gamespeed*=5; } else { setfillstyle(1,RED); /*key2*/ bar(270,10,510,110); settextstyle(3,HORIZ_DIR,4); outtextxy(390,60,"QUICK[q]"); } sound(1000); delay(gamespeed*2); nosound(); /*key3&4*/ setfillstyle(1,DARKGRAY); /*key3*/ bar(10,10,250,110); settextstyle(3,HORIZ_DIR,4); outtextxy(130,60,"PLAY[p]"); /*key4*/ bar(270,10,510,110); settextstyle(3,HORIZ_DIR,4); outtextxy(390,60,"BACK[b]"); do { ch=getch(); }while(ch!='p'&&ch!='b'); if(ch=='p') { setfillstyle(1,RED); /*key3*/ bar(10,10,250,110); settextstyle(3,HORIZ_DIR,4); outtextxy(130,60,"PLAY[p]"); } else { setfillstyle(1,RED); /*key2*/ bar(270,10,510,110); settextstyle(3,HORIZ_DIR,4); outtextxy(390,60,"BACK[b]"); } sound(1000); delay(gamespeed*2); nosound(); }while(ch=='b'); clearviewport(); /*Bottons-E*/ } void countdown() { setviewport(60,50,600,450,0); clearviewport(); setcolor(YELLOW); setlinestyle(3,0,3);/*(3,0,3)*/ setfillstyle(1,BLUE); settextstyle(3,HORIZ_DIR,9); settextjustify(1,1); for(i=1;i<=4;i++) { sound(100*i); /*circle*/ circle(270,180,80); floodfill(270,180,YELLOW); /*text*/ switch(i) { case 1: outtextxy(280,170,"3");break; case 2: outtextxy(280,170,"2");break; case 3: outtextxy(280,170,"1");break; case 4: outtextxy(280,170,"GO");break; } delay(gamespeed*2); clearviewport(); nosound(); } } void drawwall() { cleardevice(); setviewport(0,0,63*A+9,47*A+9,0); setcolor(GREEN); setlinestyle(0,0,1); setfillstyle(1,LIGHTGREEN); for(i=5;i<=60;i+=1) { rectangle(i*A,4*A,i*A+9,4*A+9); floodfill(i*A+5,4*A+5,GREEN); delay(gamespeed/10); } for(i=4;i<=45;i+=1) { rectangle(60*A,i*A,60*A+9,i*A+9); floodfill(60*A+5,i*A+5,GREEN); delay(gamespeed/10); } for(i=60;i>=5;i-=1) { rectangle(i*A,45*A,i*A+9,45*A+9); floodfill(i*A+5,45*A+5,GREEN); delay(gamespeed/10); } for(i=45;i>=4;i-=1) { rectangle(5*A,i*A,5*A+9,i*A+9); floodfill(5*A+5,i*A+5,GREEN); delay(gamespeed/20); } } void gameplay() { setviewport(0,0,63*A+9,47*A+9,0); randomize();/*??*/ /*initialize food and snake*/ food.need=true;/*1 no food,0 has food*/ snake.dead=false;/*false=0 alive, 1 dead*/ snake.direction=1;/*1r,2l,3u,4d*/ snake.x[0]=10;snake.y[0]=10;/*two nodes*/ snake.x[1]=9;snake.y[1]=10; snake.node=2; prscore(); /*or over: [Esc]&[q]*/ do { if(kbhit()) { key=bioskey(0); if(key==ESC) break; else if(key==UP&&snake.direction!=4) snake.direction=3; else if(key==RIGHT&&snake.direction!=2) snake.direction=1; else if(key==LEFT&&snake.direction!=1) snake.direction=2; else if(key==DOWN&&snake.direction!=3) snake.direction=4; } /*set food*/ if(food.need==true) { do{ food.x=rand()%54+6;/*randomize,*/ food.y=rand()%40+5; food.need=false; for(i=0;i<snake.node;i++) if(snake.x[i]==food.x&&snake.y[i]==food.y) { food.need=true; break; } }while(food.need==true); setcolor(BLUE); setfillstyle(SOLID_FILL,LIGHTBLUE); setlinestyle(0,0,1); rectangle(food.x*A,food.y*A,food.x*A+9,food.y*A+9); floodfill(food.x*A+5,food.y*A+5,BLUE); } /*snake move*/ for(i=snake.node-1;i>0;i--) { snake.x[i]=snake.x[i-1]; snake.y[i]=snake.y[i-1]; } switch(snake.direction) { case 1:snake.x[0]+=1;break; case 2:snake.x[0]-=1;break; case 3:snake.y[0]-=1;break; case 4:snake.y[0]+=1;break; } /*dead or not */ for(i=3;i<snake.node;i++) { if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0]) { snake.dead=true; break; } } if(snake.x[0]<6||snake.x[0]>59||snake.y[0]<5||snake.y[0]>44) snake.dead=true; if(snake.dead==true)/*!!*/ break; /*food&score*/ if(snake.x[0]==food.x&&snake.y[0]==food.y) { /*erase food*/ setfillstyle(SOLID_FILL,BLACK); bar(food.x*A-1,food.y*A-1,food.x*A+10,food.y*A+10); snake.x[snake.node]=-2;snake.y[snake.node]=-2;/*?*/ /*node & score*/ snake.node++; food.need=true; score+=10; prscore(); } /*draw snake*/ setcolor(RED); setfillstyle(SOLID_FILL,LIGHTRED); setlinestyle(0,0,1); for(i=0;i<snake.node;i++) { rectangle(snake.x[i]*A,snake.y[i]*A,snake.x[i]*A+9,snake.y[i]*A+9); floodfill(snake.x[i]*A+5,snake.y[i]*A+5,RED); } delay(gamespeed); /*erase snake*/ setfillstyle(SOLID_FILL,BLACK); bar(snake.x[snake.node-1]*A-1,snake.y[snake.node-1]*A-1,snake.x[snake.node-1]*A+10,snake.y[snake.node-1]*A+10); }while(snake.dead==false);/*endwhile(1)*/ cleardevice(); } void gameover() { prscore(); /*GAMEOVER!-B*/ setviewport(65,140,585,260,0); setcolor(YELLOW); settextjustify(1,1); for(i=1;i<10;i++) { sound(100*i); settextstyle(3,HORIZ_DIR,i); outtextxy(260,20,"Game Over!"); delay(gamespeed); clearviewport(); nosound(); } settextstyle(3,HORIZ_DIR,9); outtextxy(260,20,"Game Over!"); /*bottons*/ setviewport(0,0,639,479,0); setcolor(YELLOW); setlinestyle(0,0,3); setfillstyle(1,DARKGRAY); settextjustify(1,1); settextstyle(3,HORIZ_DIR,4); /*key5*/ bar(75,250,315,350); outtextxy(195,300,"REPLAY[r]"); /*key6*/ bar(335,250,575,350); outtextxy(455,300,"QUIT[q]"); mouse(); if(ch=='r') { setfillstyle(1,RED); /*key5*/ bar(75,250,315,350); settextstyle(3,HORIZ_DIR,4); outtextxy(195,300,"REPLAY[r]"); } else { setfillstyle(1,RED); /*key6*/ bar(335,250,575,350); settextstyle(3,HORIZ_DIR,4); outtextxy(455,300,"QUIT[q]"); } sound(1000); delay(gamespeed*2); nosound(); } void mouse() { union REGS regs; struct Color color[16]; struct Color * p=color; int xnew,ynew,button;/*pb=press left button*/ int x0,y0; setviewport(0,0,639,479,0); /*initialize */ regs.x.ax=0; int86(0x33,®s,®s); if(!regs.x.ax) { outtextxy(319,239,"no mouse"); exit(0); } regs.x.ax=7;/*(0~639,0~199),x scale*/ regs.x.cx=0; regs.x.dx=639; int86(0x33,®s,®s); regs.x.ax=8;/*y scale*/ regs.x.cx=0; regs.x.dx=479; int86(0x33,®s,®s); /*readms click left key??change ch->draw mouse->*/ /*read pixel*/ regs.x.ax=3; int86(0x33,®s,®s); xnew=regs.x.cx; ynew=regs.x.dx; x0=xnew; y0=ynew; /*remember color*/ for(p=color;p<color+16;p++) { p->x=xnew+(p-color)%4; p->y=ynew+(p-color)/4; p->c=getpixel(p->x,p->y); } /*draw mouse(white),*/ for(p=color;p<color+16;p++) putpixel(p->x,p->y,WHITE); /*move or not*/ do { /*read pixel*/ do { regs.x.ax=3; int86(0x33,®s,®s); xnew=regs.x.cx; ynew=regs.x.dx; regs.x.ax=5; regs.x.bx=0;/*left pressed=1,others=0*/ int86(0x33,®s,®s); button=regs.x.bx; }while(xnew==x0&&ynew==y0&&button==0); if(xnew!=x0||ynew!=y0) { x0=xnew;y0=ynew; /*erase mouse*/ for(p=color;p<color+16;p++) putpixel(p->x,p->y,p->c); /*remember color*/ for(p=color;p<color+16;p++) { p->x=xnew+(p-color)%4; p->y=ynew+(p-color)/4; p->c=getpixel(p->x,p->y); } /*draw mouse(white),*/ for(p=color;p<color+16;p++) putpixel(p->x,p->y,WHITE); } if(xnew>=75&&xnew<=315&&ynew>=250&&ynew<=350&&button==1) ch='r'; if(xnew>=335&&xnew<=575&&ynew>=250&&ynew<=350&&button==1) ch='q'; }while(ch!='r'&&ch!='q'); regs.x.ax=2; int86(0x33,®s,®s);/*int86x(0x33,&r,&r,&s)*/ /*mouse-E*/ } void prscore() { char str[10]; setviewport(0,0,615,465,0); setcolor(BLACK); settextjustify(1,1); settextstyle(0,0,2); /*bottons*/ setfillstyle(SOLID_FILL,YELLOW); bar(60,5,240,35); outtextxy(150,20," \30\31\32\33[Esc]"); /*snake*/ setfillstyle(SOLID_FILL,WHITE); bar(240,5,420,35); outtextxy(330,20,"\1 SNAKE \1"); /*score*/ setfillstyle(SOLID_FILL,YELLOW); bar(420,5,600,35); sprintf(str,"SCORE:%4d",score); outtextxy(510,20,str); }