Вы находитесь на странице: 1из 10

/

/snakegamei
nc

#i
ncl
ude<stdio.h>
#i
ncl
ude<ti
me. h>
#i
ncl
ude<stdli
b. h>
#i
ncl
ude<conio. h>
#i
ncl
ude<ti
me. h>
#i
ncl
ude<cty
pe. h>
#i
ncl
ude<ti
me. h>
#i
ncl
ude<windows. h>
#i
ncl
ude<process. h>

#def
ineUP72
#def
ineDOWN80
#def
ineLEFT75
#def
ineRIGHT77

i
ntlength;
i
ntbend_ no;
i
ntlen;
charkey ;
voi
dr ecor d()
;
voi
dload( );
i
ntli
fe;
voi
dDel ay (l
ongdouble);
voi
dMov e();
voi
dFood( )
;
i
ntScor e();
voi
dPr int();
voi
dgot oxy (i
ntx,i
nty);
voi
dGot oXY( i
ntx,
inty
);
voi
dBend( )
;
voi
dBoar der();
voi
dDown( );
voi
dLef t();
voi
dUp( );
voi
dRi ght ()
;
voi
dExi tGame( );
i
ntScor eonl y(
);

str
uctcoordi
nate{
i
ntx;
i
nty;
i
ntdi
rect
ion;
};

t
ypedefst
ructcoor
dinat
ecoor
dinat
e;

coor
dinat
ehead,
bend[
500]
,
food,
body
[30]
;

i
ntmai
n()
{

charkey
;
Pr
int
();

sy
stem(
"cl
s")
;

l
oad(
);

l
engt
h=5;

head.
x=25;

head.
y=20;

head.
dir
ect
ion=RI
GHT;

Boar
der
();

Food(
);/
/togener
atef
oodcoor
dinat
esi
nit
ial
l
y

l
i
fe=3;
//numberofext
ral
i
ves

bend[
0]=head;

Mov
e()
; /
/ini
ti
ali
ngi
nit
ial
bendcoor
dinat
e

r
etur
n0;

v
oidMove()
{
i
nta,
i;

do{

Food()
;
ff
lush(
stdi
n);

l
en=0;

f
or(
i=0;
i
<30;
i
++)

body
[i
].
x=0;

body
[i
].
y=0;

i
f(
i==l
engt
h)

br
eak;

Del
ay(
lengt
h);
Boar
der
();

i
f(
head.
dir
ect
ion==RI
GHT)

Ri
ght
();

el
sei
f(
head.
dir
ect
ion==LEFT)

Lef
t()
;

el
sei
f(
head.
dir
ect
ion==DOWN)

Down(
);

el
sei
f(
head.
dir
ect
ion==UP)

Up(
);

Exi
tGame(
);

}
whi
l
e(!
kbhi
t(
));

a=get
ch(
);

i
f(
a==27)

sy
stem(
"cl
s")
;

exi
t(
0);

}
key
=get
ch(
);

i
f((key
==RI
GHT&&head.di
rect
ion!
=LEFT&&head.dir
ecti
on!
=RIGHT)
||
(key
==LEFT&&head.
dir
ecti
on!
=
RIGHT&&head.
dir
ecti
on!
=LEFT)||
(key
==UP&&head.dir
ecti
on!
=DOWN&&head.di
rect
ion!
=UP)|
|(
key=
=DOWN&&head.dir
ecti
on!
=UP&&head. di
rect
ion!
=DOWN) )

bend_
no++;

bend[
bend_
no]
=head;

head.
dir
ect
ion=key
;

i
f(
key
==UP)

head.
y--
;

i
f(
key
==DOWN)
head.
y++;

i
f(
key
==RI
GHT)

head.
x++;

i
f(
key
==LEFT)

head.
x--
;

Mov
e()
;

el
sei
f(
key
==27)

sy
stem(
"cl
s")
;

exi
t(
0);

el
se

pr
int
f("
\a"
);

Mov
e()
;

}
}

v
oidgot
oxy
(intx,
inty
)
{

COORDcoor
d;

coor
d.X=x;

coor
d.Y=y
;

Set
Consol
eCur
sor
Posi
ti
on(
Get
StdHandl
e(STD_
OUTPUT_
HANDLE)
,coor
d);

}
v
oidGotoXY(intx,i
nty)
{
HANDLEa;
COORDb;
ff
lush(
stdout)
;
b.
X=x;
b.
Y=y ;
a=Get StdHandle(
STD_
OUTPUT_
HANDLE)
;
Set Consol eCur sorPosition(a,b);
}
voidload( ){
i
ntr ow, col,
r ,
c,q;
got oxy(36,14) ;
pri
nt f("
loadi ng..."
);
got oxy(30,15) ;
for(r=1;r<=20; r++) {
for(q=0; q<=100000000; q++) ;
//t
odi spl
aythechar act
erslowly
pri
nt f("
%c" ,177) ;}
getch( );
}
voidDown( )
{
i
nti ;
for(i=0;i
<=( head. y-bend[ bend_ no].
y)&&len<l
ength;i
++)
{
Got oXY( head. x,head. y-
i);
{
i
f(len==0)
pr i
ntf(
"v");
el
se
pr i
ntf(
"*");
}
body [l
en] .
x=head. x;
body [l
en] .
y =head. y-i;
len++;
}
Bend( )
;
i
f(!kbhi t
())
head. y++;
}
voidDel ay (
longdoubl ek)
{
Scor e();
l
ongdoubl ei ;
for(i=0;i
<=( 10000000) ;i++) ;
}
voidExitGame( )
{
i
nti ,check=0;
for(i=4;i
<lengt h;i++) / /startswith4becausei tneedsmi nimum 4el
ementtotouchit
sown
body
{
if(
body [
0].x==body [i]
.x&&body [0].
y==body[i
].
y)
{
check++; / /
check'sv alueincreasesast hecoor di
nat
esofheadisequaltoany
otherbodycoor dinat e
}
if(
i==lengt h||check! =0)
break;
}
i
f(head. x<=10| |
head. x>=70| |head.y<=10||
head.y>=30|
|check!=0)
{
li
fe--;
i
f(li
fe>=0)
{
head. x=25;
head. y=20;
bend_ no=0;
head. directi
on=RI
GHT;
Mov e( );
}
else
{
system( "cl
s")
;
printf("Alll
ivescompl
eted\
nBet
terLuckNextTi
me!
!!
\nPr
essanykeyt
oqui
tthe
game\n")
;
recor d();
exit(0);
}
}
}
voi
dFood( )
{
if
(head.x==food. x&&head.y
==f
ood.y)
{
l
engt h++;
ti
me_ ta;
a=time(0) ;
srand(a);
food.x=rand( )%70;
i
f(food.x<=10)
food. x+=11;
food.y=rand( )%30;
i
f(food.y<=10)

f
ood.y+=11;
}
el
sei
f(f
ood.x==0)/*t
ocr
eat
efoodf
ort
hef
ir
stt
imecozgl
obal
var
iabl
ear
eini
ti
ali
zedwi
th0*
/
{
f
ood.x=rand()
%70;
i
f(
food.x<=10)
f
ood.x+=11;
f
ood.y=rand()
%30;
i
f(
food.y<=10)
f
ood.y+=11;
}
}
v
oidLef
t()
{
i
nti
;
f
or(
i=0;
i<=(
bend[bend_ no].
x-head.
x)&&l
en<l
engt
h;i
++)
{
GotoXY((
head. x+i
),head.y)
;
{
if
(len==0)
pri
ntf("
<")
;
else
pri
ntf("
*")
;
}
body
[l
en]
.x=head.
x+i
;
body
[l
en]
.y=head.
y;
l
en++;
}
Bend();
i
f(!
kbhit(
))
head.x-
-;

}
v
oidRight()
{
i
nti;
for(
i=0;i
<=( head. x-bend[ bend_ no] .
x)&&len<length;i
++)
{
//Got oXY( (head. x-i)
,head. y);
body [l
en] .x=head. x-i;
body [l
en] .y=head. y;
Got oXY( body [
len].x,
body [len].
y );
{
i
f(len==0)
pr i
nt f("
>");
else
pr i
nt f("
*");
}
/*body [len].x=head. x-i;
body [l
en] .y=head. y;*/
len++;
}
Bend( )
;
i
f(!
kbhi t
())
head. x++;
}
v
oidBend( )
{
i
nti,j
,dif
f;
for(
i=bend_ no;i>=0&&l en<l ength;i-
-)
{
i
f(bend[ i].
x==bend[ i-
1].x)
{
diff =bend[ i]
.y-bend[i-1].
y;
i
f (diff<0)
for(j
=1;j<=(-diff)
;j++)
{
body [l
en] .
x=bend[i].
x;
body [l
en] .
y=bend[i].
y+j;
Got oXY( body [
len]
.x,
body [
len].
y);
pri
nt f(
"*");
l
en++;
i
f(len==lengt h)
break;
}
elsei f(
diff>0)
for(j
=1;j<=di f
f;j
++)
{
/*
Got oXY( bend[i]
.x,
(bend[
i].
y-j
))
;
pri
ntf("
*");
*/
body[l
en] .
x=bend[ i
].
x;
body[l
en] .
y=bend[ i
].
y-j
;
GotoXY( body [
len]
.x,
body [
len]
.y)
;
pri
ntf("
*");
l
en++;
i
f(l
en==lengt h)
break;
}
}
el
seif
(bend[i]
.y==bend[ i
-1].y)
{
di
ff=bend[i].
x-bend[ i
-1].
x;
i
f(di
ff<0)
for(
j=1;j<=(-diff)
&&l en<length;j++)
{
/*Got oXY( (
bend[ i]
.x+j)
,bend[i]
.y)
;
printf("*");
*/
body [
len].x=bend[ i].
x+j;
body [
len].y=bend[ i].
y;
Got oXY( body [
len].x,
body [
len].
y);
printf("*"
);
len++;
if(
len==l engt h)
break;
}
el
sei f(
diff
>0)
for(
j=1;j
<=di ff&&len<l ength;j
++)
{
/*Got oXY( (
bend[ i]
.x-j
),
bend[ i
].
y);
printf("*"
);*/
body [
len].x=bend[ i
].x-j
;
body [
len].y=bend[ i
].y;
Got oXY( body [
len].
x,body [
len].
y);
printf("*
");
len++;
if(
len==l engt h)
break;
}
}
}
}
v
oidBoar der()
{
system( "cl
s")
;
inti
;
GotoXY( food.x,
food.y
); /
*di
spl
ayi
ngf
ood*
/
printf(
"F"
);
for(
i=10;i<71;
i++)
{
Got oXY(i,
10);
pri
ntf(
"!
");
Got oXY(i,
30);
pri
ntf(
"!
");
}
f
or(
i=10;
i<31;
i++)
{
GotoXY(10,i
);
pri
ntf(
"!
")
;
GotoXY(70,i
);
pri
ntf(
"!
");
}
}
voidPr int()
{
//Got oXY( 10, 12);
print f
("\tWel comet ot hemi ni Snakegame. (pressanykeyt oconti
nue)\n")
;
getch( );
syst em( "
cls" );
print f
("\tGamei nst ruct i
ons: \n") ;
print f
("\n->Usear rowkey st omov ethesnake. \n\ n->Youwi l
lbepr
ov i
dedf oodsattheseveral
coordinat esoft hescr eenwhi chy ouhav et oeat.Ever yti
mey oueatafoodt helengthofthesnake
wil
lbei ncr easedby1el ementandt hust hescor e.
\ n\n->Herey ouar
epr ovidedwiththreeli
ves.
Yourl i
f ewi lldecr easeasy ouhi tt hewal lorsnake' sbody .\n\n-
>YOucanpauset hegamei nit
s
middlebypr essi nganykey .Tocont inuet hepausedgamepr essanyotherkeyonceagai n\n\
n->If
youwantt oexi tpr essesc.\ n");
print f
("\n\ nPr essanykeyt opl aygame. ..
")
;
if(
get ch( )==27)
exit(0);
}
voidrecor d(){
charpl name[ 20] ,
npl name[ 20] ,
cha, c;
inti,j,
px;
FILE* i
nf o;
info=f open( " recor d.txt",
"a+" )
;
get ch() ;
syst em( "
cls" );
print f
("Ent ery ourname\ n" )
;
scanf ("%[ ^\n]" ,
plname) ;
//*** ****** *
* ** *
*** ****** *
for(j=0;plname[ j]
!='\0';j
++) {//toconv ertthefirstletteraft
erspacetocapital
nplname[ 0]=t oupper (plname[ 0]);
if(
pl name[ j-
1] ==''){
nplname[ j
]=t oupper (plname[ j
]);
nplname[ j
-1]=pl name[ j
-1];
}
elsenpl name[ j]
=plname[ j
];
}
nplname[ j
]='\0' ;
//*** ****** *
* ** *
*** ****** *
* ****
//sdf printf(inf o,
"\t\t\tPlay ersLi st \n")
;
fprintf(info,"Pl ayerName: %s\ n" ,nplname) ;
//fordat eandt ime

t
ime_tmy ti
me;
myti
me=t i
me( NULL) ;
f
pri
ntf(info,
"PlayedDat e:
%s",
ctime(&my t
ime));
/
/*** *
*********
* *
*****
*****
*
f
printf(i
nfo,"
Score:%d\n",
px=Scoreonl
y())
;//
cal
lscoret
odispl
ayscor
e
/
/fprintf
(info,
"\nLevel
:%d\n",
10);
//cal
llev
eltodi
splayl
evel
for(
i=0;i
<=50; i++)
fpri
ntf(i
nf o,"%c",'
_'
);
fpri
ntf(i
nf o,"\n");
fcl
ose( i
nf o);
pri
ntf("wannaseepastr ecordspr
ess'
y'
\n"
);
cha=get ch( );
system( "cls");
i
f(cha==' y'
){
i
nfo=f open( "record.t
xt"
,"
r"
);
do{
put char (c=getc(
inf
o));
}whi l
e( c!=EOF);}
fclose(info) ;
}
i
ntScor e()
{
intscor e;
Got oXY( 20,8);
scor e=lengt h-5;
printf("
SCORE: %d" ,
(l
ength-5)
);
scor e=lengt h-5;
Got oXY( 50,8);
printf("
Life: %d" ,
li
fe);
returnscor e;
}
i
ntScor eonly()
{
i
ntscor e=Scor e();
system( "cls");
ret
ur nscor e;
}
voidUp( )
{
inti;
for(i
=0; i
<=(bend[ bend_no].y
-head.
y)&&l
en<l
engt
h;i
++)
{
Got oXY( head.x,
head.y+i)
;
{
i
f(l
en==0)
pr
intf(
"^"
);
el
se
pr
intf(
"*"
);
}
body [l
en] .
x=head.x;
body [l
en] .
y=head.y+i
;
len++;
}
Bend( )
;
if(
!kbhi t
())
head. y--
;
}

Вам также может понравиться