Devc++ Snake

Source: Internet
Author: User

A simple version of the snake program written using devc++, however, does not include the graphics library until it is configured, so you need to configure the new environment yourself

-------------------------------------------------------------------Split Line--------------------------------------------------- -------------------------------

#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
Main ()
{
int X[200]={200,210,220},y[200]={60,60,60},i,n=3,d=3,key;
int food_x,food_y,flag=0,score=0;
Char str[100];
Initwindow (640,480);
SetColor (RED);
Outtextxy (50,30, "greedysnake1.0");
Outtextxy (400,30, "score:");//Initialize fractional display
Itoa (score,str,10);
Outtextxy (450,30,STR);
Rectangle (50,50,500,400);
SetColor (white);//Draw the initial snake
for (i=0;i<n;i++)
Rectangle (x[i],y[i],x[i]+10,y[i]+10);
while (1)
{
if (flag==0)//Hair Food
{
Srand ((unsigned) time (NULL));
Food_x=rand ()%400+50;
Food_y=rand ()%300+50;
while (food_x%10!=0) food_x++;
while (food_y%10!=0) food_y++;
flag=1;
Rectangle (food_x,food_y,food_x+10,food_y+10);
}
if (food_x==x[0]&&food_y==y[0])//Eat food
{
SetColor (BLACK);
Rectangle (food_x,food_y,food_x+10,food_y+10);
n++;
flag=0;
score+=100;
Itoa (score,str,10);//integer fraction converted to string
SetColor (white);//Display Score
Outtextxy (450,30,STR);
}
Sleep (200);//Rub the snake
SetColor (BLACK);
for (i=0;i<n;i++)
Rectangle (x[i],y[i],x[i]+10,y[i]+10);
for (i=n-1;i>=1;i--)//Mobile
{
X[I]=X[I-1];
Y[I]=Y[I-1];
}
if (Kbhit ()!=0)//If a keystroke occurs, the statement body is executed
{
Key=getch ();
Switch (key)
{
Case ' W ':d =1;break;
Case ' s ':d =2;break;
Case ' a ':d =3;break;
Case ' d ':d =4;break;
Case ' P ': getch ();
}
}
Switch (d)//control the direction of the snake head
{
Case 1:y[0]-=10;break;
Case 2:y[0]+=10;break;
Case 3:x[0]-=10;break;
Case 4:x[0]+=10;break;
}
SetColor (white);//Draw a snake
for (i=0;i<n;i++)
Rectangle (x[i],y[i],x[i]+10,y[i]+10);
if (x[0]<=50&&x[0]>=0| | x[0]<=640&&x[0]>=500| | y[0]<=50&&y[0]>=0| | y[0]<=480&&y[0]>=400)//Wall dead
{
Break
}
}
Closegraph ();
}

Devc++ Snake

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.