A C-language-written snake game

Source: Internet
Author: User
Tags goto rand sprintf

In the VC6.0 platform, through the C language and Easyx graphics library to write the greedy snake games, in order to use the graphics library function, the file is a. cpp file, using the C language, the game has a background music, code length of 500 lines, code and algorithms, are my own source, to share with interested friends to see.

Source:

#include <stdio.h>
#include <graphics.h>
#include <time.h>
#include <conio.h>
#include <easyx.h>


#define MaxLen 30
#define MAXFOD 6
#define EROR 1
#define OK 0
#pragma comment (lib, "Winmm.lib")


Enum direction
{
Left,up,right,down
};
int pos[maxlen][2];
int food[maxfod][2];
int lenfod=0;
int length=0;
int mark=0;
int highestm=0;
Direction DIT;
int snake ();
int DrawLine ();
int init ();
int Controlfood ();
int Savemark ();
Boolean isdead ();
Boolean Iswin ();
int Countmark ();


void Main ()
{
int i=0,count=1;
Srand ((unsigned) time (0));
Initgraph (600,900);
SetColor (yellow);
SetFont (30, 0, "italics");
mciSendString ("open./source/skycity.mp3 alias SkyCity", 0, 0, 0);
mciSendString ("Play SkyCity Repeat", 0, 0, 0);
Start
Cleardevice ();
Init ();
/*for (i=0;i<length;i++)
printf ("\n%d%d", pos[i][0],pos[i][1]);
printf ("\ n");
POS[0][1]-= 1;
for (i=length-1;i>0;i--)
{
POS[I][0]=POS[I-1][0];
POS[I][1]=POS[I-1][1];
}
Closegraph ();
for (i=0;i<length;i++)
printf ("\n%d%d", pos[i][0],pos[i][1]);
Sleep (50000);
Beginbatchdraw ();
DrawLine ();
Snake ();
Flushbatchdraw ();


while (true)
{
if (Kbhit ())
{
Switch (Getch ())
{

Case 27:
Savemark ();
Closegraph ();
Exit (OK);
Break
Case 72:
Case ' W ':
Case ' W ':
if (Dit!=down)
{
Dit=up;
for (i=length-1;i>0;i--)
{
POS[I][0]=POS[I-1][0];
POS[I][1]=POS[I-1][1];
}
POS[0][1]-= 1;
}
Break
Case 80:
Case ' s ':
Case ' S ':
if (dit!=up)
{
Dit=down;
for (i=length-1;i>0;i--)
{
POS[I][0]=POS[I-1][0];
POS[I][1]=POS[I-1][1];
}
POS[0][1] + + 1;
}
Break
Case 75:
Case ' a ':
Case ' A ':
if (dit!=right)
{
Dit=left;
for (i=length-1;i>0;i--)
{
POS[I][0]=POS[I-1][0];
POS[I][1]=POS[I-1][1];
}
Pos[0][0]-= 1;
}
Break
Case 77:
Case ' d ':
Case ' D ':
if (dit!=left)
{
Dit=right;
for (i=length-1;i>0;i--)
{
POS[I][0]=POS[I-1][0];
POS[I][1]=POS[I-1][1];
}
POS[0][0] + + 1;
}
Break
}
Cleardevice ();
Controlfood ();
DrawLine ();
Countmark ();
Snake ();
Count=1;
Flushbatchdraw ();
}
if (length<10)
{
if (count==20)
{
count=0;
}
}
else if (length<15&&length>9)
{
if (count==15)
count=0;
}
else if (length<20&&length>14)
{
if (count==10)
count=0;
}
Else
{
if (count==5)
count=0;
}
if (count==0)
{
for (i=length-1;i>0;i--)
{
POS[I][0]=POS[I-1][0];
POS[I][1]=POS[I-1][1];
}
Switch (DIT)
{
Case up:
POS[0][1]-= 1;
Break
Case down:
POS[0][1] + + 1;
Break
Case left:
Pos[0][0]-= 1;
Break
Case right:
POS[0][0] + + 1;
Break
}
Cleardevice ();
Controlfood ();
DrawLine ();
Countmark ();
Snake ();
Flushbatchdraw ();
}
if (Isdead ())
{
mciSendString ("Play Lose", 0, 0, 0);
Outtextxy (240,275, "lossing!");
Flushbatchdraw ();
while (true)
{
if (Kbhit ())
{
Switch (Getch ())
{
Case 13:
mciSendString ("Close Lose", 0, 0, 0);
mciSendString ("Close win", 0, 0, 0);
Goto start;
Case 27:
mciSendString ("Close SkyCity", 0, 0, 0);
Closegraph ();
Exit (OK);
}
}
}
}
if (Iswin ())
{
mciSendString ("Play Win", 0, 0, 0);
Outtextxy (240,275, "winning!");
Flushbatchdraw ();
while (true)
{
if (Kbhit ())
{
Switch (Getch ())
{
Case 13:
mciSendString ("Close Lose", 0, 0, 0);
mciSendString ("Close win", 0, 0, 0);
Goto start;
Case 27:
mciSendString ("Close SkyCity", 0, 0, 0);
Closegraph ();
Exit (OK);
}
}
}
}
count++;
Sleep (20);
}
}


int Snake ()
{
int i=0,j=0;
Setfillcolor (yellow);
for (i=0;i<length;i++)
Solidcircle (pos[i][0]*20+10,pos[i][1]*20+10,10);
Setfillcolor (RED);
i=0;
if (dit==left)
{
Solidcircle (pos[i][0]*20+5,pos[i][1]*20+5,3);
Solidcircle (pos[i][0]*20+5,pos[i][1]*20+15,3);
}
else if (dit==right)
{
Solidcircle (pos[i][0]*20+15,pos[i][1]*20+5,3);
Solidcircle (pos[i][0]*20+15,pos[i][1]*20+15,3);
}
else if (Dit==down)
{
Solidcircle (pos[i][0]*20+5,pos[i][1]*20+15,3);
Solidcircle (pos[i][0]*20+15,pos[i][1]*20+15,3);
}
Else
{
Solidcircle (pos[i][0]*20+5,pos[i][1]*20+5,3);
Solidcircle (pos[i][0]*20+15,pos[i][1]*20+5,3);
}
Setfillcolor (GREEN);
SetFont (18, 0, "italics");
for (i=0;i<lenfod;i++)
{
Solidcircle (food[i][0]*20+10,food[i][1]*20+10,7);
/*j=rand ()%2;
if (j==0)
{
Outtextxy (Food[i][0]*20+1,food[i][1]*20+1, "");
}
else if (j==1)
{
Outtextxy (Food[i][0]*20+1,food[i][1]*20+1, "");
}
Else
{
Outtextxy (Food[i][0]*20+1,food[i][1]*20+1, "");
}*/
}
SetFont (30, 0, "italics");
Solidcircle (food[i][0]*20+10,food[i][1]*20+10,7);
Setfillcolor (GREEN);
return OK;
}
int DrawLine ()
{
int i;
for (I=20;I&LT;600;I+=20)
{
Line (0,i,600,i);
Line (i,0,i,600);
}
Line (0,i,600,i);
Line (0,i+5,600,i+5);
return OK;
}
int init ()
{
int i,j,k,l;
FILE * FP;
mciSendString ("Open./source/lose.mp3 alias lose", 0, 0, 0);
mciSendString ("open./source/win.mp3 alias win", 0, 0, 0);
length=0;
Lenfod=0;
mark=0;
if ((Fp=fopen ("Highestscore.txt", "R")) ==null)
{
printf ("File open eror!\n");
Exit (Eror);
}
Else
{
FSCANF (FP, "%d", &highestm);
if (fclose (FP))
{
printf ("File close eror\n");
Exit (Eror);
}
}
Setlinecolor (GREEN);
Setfillcolor (yellow);
I=rand ()%10+10;
J=rand ()%10+10;
K=rand ()%4;
Pos[0][0] = i;
Pos[0][1] = j;
if (k==0)
{
for (i=1;i<5;i++)
{
pos[i][1]=pos[i-1][1]+1;
POS[I][0]=POS[I-1][0];
Dit=up;
}
}
else if (k==1)
{
for (i=1;i<5;i++)
{
Pos[i][0]=pos[i-1][0]-1;
POS[I][1]=POS[I-1][1];
Dit=right;
}
}
else if (k==2)
{
for (i=1;i<5;i++)
{
Pos[i][1]=pos[i-1][1]-1;
POS[I][0]=POS[I-1][0];
Dit=down;
}
}
Else
{
for (i=1;i<5;i++)
{
pos[i][0]=pos[i-1][0]+1;
POS[I][1]=POS[I-1][1];
Dit=left;
}
}
length=5;
K=rand ()%4+1;
for (l=0;l<k;l++)
{
I=rand ()%30;
J=rand ()%30;
Food[lenfod][0]=i;
Food[lenfod++][1]=j;
for (int m=0;m<length;m++)
if (I==pos[m][0]&&j==pos[m][1])
{
l--;
lenfod--;
Break
}
}
return OK;
}
int Controlfood ()
{
int i=0,j,k,l;
for (i=0;i<lenfod;i++)
{
if (Pos[0][0]==food[i][0]&&pos[0][1]==food[i][1])
{
if (length<10)
{
mark+=5;
}
else if (length<15)
{
mark+=10;
}
else if (length<20)
{
mark+=15;
}
else if (length<25)
{
mark+=20;
}
Else
{
mark+=25;
}
Break
}
}
if (I&LT;LENFOD)
{
for (j=i;j<lenfod-1;j++)
{
FOOD[J][0]=FOOD[J+1][0];
FOOD[J][1]=FOOD[J+1][1];
}
lenfod--;
length++;
POS[LENGTH-1][0]=POS[LENGTH-2][0]*2-POS[LENGTH-3][0];
POS[LENGTH-1][1]=POS[LENGTH-2][1]*2-POS[LENGTH-3][1];
}
if (lenfod<2)
{
K=rand ()%4+1;
for (l=0;l<k;l++)
{
I=rand ()%30;
J=rand ()%30;
Food[lenfod][0]=i;
Food[lenfod++][1]=j;
for (int m=0;m<length;m++)
if (I==pos[m][0]&&j==pos[m][1])
{
l--;
lenfod--;
Break
}
}
}
return OK;
}
Boolean Isdead ()
{
int i;
if (pos[0][0]<0| | pos[0][0]>30| | pos[0][1]<0| | POS[0][1]&GT;30)
{
Savemark ();
return true;
}
for (i=3;i<length;i++)
if (Pos[0][0]==pos[i][0]&&pos[0][1]==pos[i][1])
return true;
return false;
}
Boolean Iswin ()
{
if (Length==maxlen&&!isdead ())
{
Savemark ();
return true;
}
Else
return false;
}
int Countmark ()
{
Char num[10];
sprintf (num, "%d", highestm);
Outtextxy (60,610, "highest:");
Outtextxy (200,610,num);
Outtextxy (350,610, "Youscore:");
sprintf (num, "%d", MARK);
Outtextxy (490,610,num);
return OK;
}
int Savemark ()
{
FILE * FP;
if (mark>highestm)
{
if ((Fp=fopen ("Highestscore.txt", "W")) ==null)
{
printf ("File open eror!\n");
Exit (Eror);
}
Else
{
fprintf (FP, "%d", MARK);
if (fclose (FP))
{
printf ("File close eror\n");
Exit (Eror);
}
}
}
return OK;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.