?? snake.cpp
字號:
/* Game developed by me in my second year */
#include<dos.h>
#include<dir.h>
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define N 100 /* Max length of worm */
#define X1 200 /* Playing block coordinates */
#define Y1 200
#define X2 400
#define Y2 400
#define width 5 /* Width of the snake */
#define SPEEDSIZE 5 /* Speed of the whole game */
char *current_directory(char *path)
{
strcpy(path, "C:\\"); /* fill string with form of response: X:\ */
path[0] = 'A' + getdisk(); /* replace X with current drive letter */
getcurdir(0, path+3); /* fill rest of string with current directory */
return(path);
}
char level=1; /* Default level is the basic level */
void main()
{
void draw(int,int,int); /* This will draw one block of snake */
void score(int); /* To save record of top scores */
int menu(); /* To select level to play */
int i,driver = DETECT,mode,j,sco=0;
unsigned short int p[2][N],x=250,y=250,tx,ty,len=10,pc;
char b='a',a='2',flag='f',str[7],cheat[8];
char curdir[20];
current_directory(curdir);
strcat(curdir,"\\bgi");
initgraph(&driver,&mode,"..\\bgi");
w:
cleardevice();
x=y=250,len=10,sco=0,b='a',a='2';
for(i=0;i<len;i++)
{
p[0][i]=250;
p[1][i]=250+5*i;
}
flag=0;
j=0;
do
{
cleardevice();
settextstyle(7,0,4);
i=(i+25)%(100+2*getmaxx());
setcolor(i/100);
if(j==100)
sco=-1;
if(j==0)
sco=1;
j=j+sco;
sound(j*10);
outtextxy(getmaxx()-i,100,"I am sure that u will like this");
outtextxy(getmaxx()-i+700,200,"-Your friend Prithvi");
settextstyle(20,0,4);
setcolor(YELLOW);
outtextxy(100,400,"Press ESC to change settings or any other key to play");
delay(100);
nosound();
}while(!kbhit());
flag=getch();
sco=0;
cheat:
cleardevice();
if(flag==27)
level=menu();
flag='f';
settextstyle(0,0,0);
cleardevice();
setbkcolor(0);
setcolor(1);
outtextxy(2,2,"Use arrow keys to play ESC to stop");
outtextxy(2,12,"Press any key to start ");
for(i=0;i<3;i++)
{
line(X1+i,Y1+i,X2-i,Y1+i);
line(X1+i,Y1+i,X1+i,Y2-i);
line(X1+i,Y2-i,X2-i,Y2-i);
line(X2-i,Y1+i,X2-i,Y2-i);
}
outtextxy(10,200,"Score : ");
itoa(sco,str,10);
outtextxy(100,200,str);
setcolor(RED);
outtextxy(450,200,"Made by : Prithvi");
outtextxy(500,220,"267/COE/2001");
outtextxy(500,240,"N.S.I.T");
setcolor(1);
/* At this point you can enter the cheat code as "prithvi"
and the score will increase by 500 every
time you enter this but only upto 2000 */
cheat[0]=getch();
if(tolower(cheat[0])=='p')
{
pc=1;
while(pc<=6)
cheat[pc++]=getch();
cheat[pc]='\0';
if(strcmp(cheat,"prithvi")==0)
{
if(sco<2000)
sco=sco+500;
sound(1000);
delay(150);
sound(800);
delay(20);
sound(500);
delay(1300);
nosound();
goto cheat;
}
}
while(b!=27) /* Game starts from this point */
{
if(flag=='f') // Food is not present
{
tx=X1+5*(1+rand()%39);
ty=Y1+5*(1+rand()%39);
if(getpixel(tx,ty)!=0)
continue;
draw(tx,ty,GREEN);
flag='t';
}
if(kbhit())
{
b=getch();
if(b==75&&a!=77||b==80&&a!=72||b==77&&a!=75||b==72&&a!=80)
a=b;
}
switch(a)
{
case 75: x=x-width; // Left
break;
case 80: y=y+width; // Down
break;
case 77: x=x+width; // Right
break;
case 72: y=y-width; // Up
break;
}
if(x>X2-width/2||x<X1+width/2||y>Y2-width/2||y<Y1+width/2)
break;
draw(p[0][0],p[1][0],0);
for(j=0;j<len;j++)
{
p[0][j]=p[0][j+1];
p[1][j]=p[1][j+1];
}
p[0][len-1]=x;
p[1][len-1]=y;
if(getpixel(x,y)==5)
break;
if(getpixel(x,y)==GREEN)
{
itoa(sco,str,10);
setcolor(0);
outtextxy(100,200,str);
sound(500);
sco=sco+10*level*level;
setcolor(RED);
itoa(sco,str,10);
outtextxy(100,200,str);
draw(tx,ty,0);
len++;
flag='f';
}
draw(x,y,61);
delay(70-SPEEDSIZE*level);
nosound();
}
setcolor(YELLOW-4);
settextstyle(10,0,4);
outtextxy(150,250,"GAME OVER");
setcolor(GREEN);
circle(x,y,10);
circle(x,y,9);
sound(400);
delay(200);
sound(300);
delay(700);
nosound();
setcolor(BROWN);
settextstyle(7,0,3);
outtextxy(100,getmaxy()-35,"Press ESC to see scoreboard");
while(!kbhit());
while(getch()!=27);
fflush(stdin);
cleardevice();
score(sco);
i=0;
do
{
cleardevice();
settextstyle(7,0,4);
setcolor(i/40);
outtextxy(getmaxx()-i,200,"Press F1 to Play Another Game");
outtextxy(getmaxx()-i,300,"Press F2 to Exit the Game");
delay(70);
i=(i+10)%(2*getmaxx());
if(kbhit())
flag=getch();
}while(flag!=59&&flag!=60);
if(flag==59)
goto w;
}
void draw(int x,int y,int col)
{
int i,j;
for(i=-width/2;i<=width/2;i++)
for(j=-width/2;j<=width/2;j++)
putpixel(x+i,y+j,col);
}
int menu()
{
short int i=0;
char ch=77;
setcolor(61);
settextstyle(20,0,0);
outtextxy(150,20,"Adjust the level to play");
outtextxy(50,35," -> to increase <- to decrease ENTER to play");
line(150,301,340,301);
setfillstyle(1,WHITE);
bar(200-5+i*20,300-40-i*40,200+5+i*20,300);
i++;
do
{
ch=getch();
sound(600);
delay(100);
nosound();
if(ch==77&&i<5)
{
setfillstyle(1,WHITE);
bar(200-5+i*20,300-40-i*40,200+5+i*20,300);
i++;
}
if(ch==75&&i>1)
{
setfillstyle(1,BLACK);
bar(200-5+(i-1)*20,300-40-(i-1)*40,200+5+(i-1)*20,300);
i--;
}
}while(ch!=13);
cleardevice();
return i;
}
void score(int sco)
{
struct{
char name[10];
int score;
}data,tmp;
char fa,so[5],nm[10];
FILE *pt,*pp;
short int i=0,j,flag;
pt=fopen("c:\\raj1984.txt","rb");
if(pt==NULL)
{
pt=fopen("c:\\raj1984","wb+");
fclose(pt);
pt=fopen("c:\\raj1984","rb+");
}
while(fread(&data,sizeof(data),1,pt)==1)
{
i++;
if(data.score<sco)
{
flag=1;
break;
}
}
if(flag==1||i<5) /* It is eligible to get added in score list */
{
tmp.score=sco;
setcolor(RED);
settextstyle(7,0,2);
outtextxy(10,50,"Enter your name : ");
j=0;
strcpy(nm," ");
nm[j]=getch();
while(nm[j]!=13) // Enter
{
if(j<9||nm[j]==8)
{
setcolor(0);
outtextxy(250,50,nm);
setcolor(RED);
if(nm[j]==8&&j!=0) // Backspace
{
nm[j-1]=nm[j]=' ';
j=j-2;
if(j<0)
j=-1;
}
outtextxy(250,50,nm);
if(j<9)
j++;
}
fa=getch();
/* All printable chars or backspace or enter */
if((fa<127&&fa>31)||fa==8||fa==13)
nm[j]=fa;
}
while(j<9)
nm[j++]=' ';
nm[j]='\0';
strcpy(tmp.name,nm);
printf("%d",tmp.score);
getch();
rewind(pt);
pp=fopen("c:\\praj1984.txt","wb+");
if(pp==NULL)
{
printf("Error in writing data");
while(!kbhit());
exit(0);
}
fa='f';
rewind(pt);
j=0;
while(fread(&data,sizeof(data),1,pt)==1&&j<6)
{
if(data.score>=sco)
{
if(fwrite(&data,sizeof(data),1,pp)==1)
j++;
}
else
{
if(fa!='t')
{
if(fwrite(&tmp,sizeof(tmp),1,pp)==1)
j++;
fa='t';
}
if(fwrite(&data,sizeof(data),1,pp)==1)
j++;
}
}
if(fa!='t')
if(fwrite(&tmp,sizeof(tmp),1,pp)==1)
printf("\n%s",tmp.name);
getch();
fclose(pt);
fclose(pp);
remove("c:\\raj1984.txt");
rename("c:\\praj1984.txt","c:\\raj1984.txt");
pt=fopen("c:\\raj1984.txt","rb");
if(pt==NULL)
{
puts("Error");
exit(0);
}
}
cleardevice();
settextstyle(7,0,4);
outtextxy(200,50,"Highest scores");
i=0;
settextstyle(20,0,1);
rewind(pt);
while(fread(&data,sizeof(data),1,pt)==1)
{
outtextxy(220,150+i*20,data.name);
itoa(data.score,so,10);
outtextxy(500,150+i*20,so);
i++;
}
outtextxy(100,250+i*20,"Press ESC to Exit Scoreboard");
while(getch()!=27);
fclose(pt);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -