The pure C language realizes the greedy snake game (VC6.0)

Source: Internet
Author: User
Tags rand

Transfer from C language network

Today I show you the C language to write the greedy snake game, let everyone play a game of their own write ~ is pure C language Oh ~vc6.0 development No problem

First, start the interface:

The game interface is as follows:

The code is as follows:

The author VC6.0, Test no problem, can copy the code directly to the VC6 source file, the suffix is. c file can be compiled by running ~

#include <stdio.h>
#include <time.h>
#include <windows.h>
#include <stdlib.h>
#define U 1
#define D 2
#define L 3
#define R 4//Snake State, U: up; D: down; L: Left R: Right
typedef struct SNAKE//A node of a snake body
{
int x;
int y;
struct SNAKE *next;
}snake;
Global variable//
int score=0,add=10;//total score with each eating food score.
int status,sleeptime=200;//The time interval of each run
Snake *head, *food;//snake head pointer, food pointer
Snake *q;//The use of the pointer when traversing the snake
int endgamestatus=0; The end of the game, 1: Hit the wall, 2: Biting himself; 3: actively quit the game.
Declare all functions//
void Pos ();
void Creatmap ();
void Initsnake ();
int biteself ();
void Createfood ();
void Cantcrosswall ();
void Snakemove ();
void Pause ();
void Gamecircle ();
void Welcometogame ();
void Endgame ();
void Gamestart ();
void Pos (int x,int y)/set cursor position
{
COORD POS;
HANDLE Houtput;
Pos. X=x;
Pos. Y=y;
Houtput=getstdhandle (Std_output_handle);
SetConsoleCursorPosition (Houtput,pos);
}
void Creatmap ()//Create Map
{
int i;
for (i=0;i<58;i+=2)//print up and down borders
{
Pos (i,0);
printf ("");
Pos (i,26);
printf ("");
}
for (i=1;i<26;i++)//print the left and right border {Pos (0,i); printf (""); Pos (56,i); printf (""); } void Initsnake ()//Initialize the snake body {snake *tail int i; tail= (snake*) malloc (sizeof (snake));//From the end of the snake, head interpolation, to X,y set the starting position//tail-> x=24;
tail->y=5;
tail->next=null;
for (i=1;i<=4;i++) {head= (snake*) malloc (sizeof (snake)); head->next=tail;
head->x=24+2*i;
head->y=5;
Tail=head;
}
while (Tail!=null)//from head to toe, output snake body
{
Pos (Tail->x,tail->y);
printf ("");
tail=tail->next;
}
}
int biteself ()//Decide whether to bite yourself
{
Snake *self;
self=head->next;
while (Self!=null)
{
if (self->x==head->x && self->y==head->y)
{
return 1;
}
self=self->next;
}
return 0;
}
void Createfood ()//random occurrence of food
{
Snake *food_1;
Srand ((unsigned) time (NULL));
food_1= (snake*) malloc (sizeof (snake));
while ((food_1->x%2)!=0)//Ensure that it is even, so that food can be with the snake head on its
{
Food_1->x=rand ()%52+2;
}
Food_1->y=rand ()%24+1;
Q=head;
while (Q->next==null)
{
if (q->x==food_1->x && q->y==food_1->y)//Judge whether the snake is coincident with the food
{
Free (food_1);
Createfood ();
}
q=q->next;
}
Pos (Food_1->x,food_1->y);
Food=food_1;
printf ("");
}
void Cantcrosswall ()//Can't wear wall
{
if (head->x==0 | | head->x==56 | | head->y==0 | | HEAD-&GT;Y==26)
{
Endgamestatus=1;
Endgame ();
}
}
void Snakemove ()//Snake forward, up u, down D, left L, right R
{
Snake * Nexthead;
Cantcrosswall ();
Nexthead= (snake*) malloc (sizeof (snake));
if (status==u)
{
nexthead->x=head->x;
nexthead->y=head->y-1;
if (nexthead->x==food->x && nexthead->y==food->y)//If Next there is food//
{
nexthead->next=head;
Head=nexthead;
Q=head;
while (Q!=null)
{
Pos (Q->x,q->y);
printf ("");
q=q->next;
}
Score=score+add;
Createfood ();
}
else//If there is no food//
{
nexthead->next=head;
Head=nexthead;
Q=head;
while (Q->next->next!=null)
{
Pos (Q->x,q->y);
printf ("");
q=q->next;
}
Pos (Q->next->x,q->next->y);
printf ("");
Free (q->next);
q->next=null;
}
}
if (Status==d)
{
nexthead->x=head->x;
nexthead->y=head->y+1;
if (nexthead->x==food->x && nexthead->y==food->y)//have food
{
nexthead->next=head;
Head=nexthead;
Q=head;
while (Q!=null)
{
Pos (Q->x,q->y);
printf ("");
q=q->next;
}
Score=score+add;
Createfood ();
}
else//No food
{
nexthead->next=head;
Head=nexthead;
Q=head;
while (Q->next->next!=null)
{
Pos (Q->x,q->y);
printf ("");
q=q->next;
}
Pos (Q->next->x,q->next->y);
printf ("");
Free (q->next);
q->next=null;
}
}
if (status==l)
{
nexthead->x=head->x-2;
nexthead->y=head->y;
if (nexthead->x==food->x && nexthead->y==food->y)//have food
{
nexthead->next=head;
Head=nexthead;
Q=head;
while (Q!=null)
{
Pos (Q->x,q->y);
printf ("");
q=q->next;
}
Score=score+add;
Createfood ();
}
else//No food
{
nexthead->next=head;
Head=nexthead;
Q=head;
while (Q->next->next!=null)
{
Pos (Q->x,q->y);
printf ("");
q=q->next;
}
Pos (Q->next->x,q->next->y);
printf ("");
Free (q->next);
q->next=null;
}
}
if (status==r)
{
nexthead->x=head->x+2;
nexthead->y=head->y;
if (nexthead->x==food->x && nexthead->y==food->y)//have food
{
nexthead->next=head;
Head=nexthead;
Q=head;
while (Q!=null)
{
Pos (Q->x,q->y);
printf ("");
q=q->next;
}
Score=score+add;
Createfood ();
}
else//No food
{
nexthead->next=head;
Head=nexthead;
Q=head;
while (Q->next->next!=null)
{
Pos (Q->x,q->y);
printf ("");
q=q->next;
}
Pos (Q->next->x,q->next->y);
printf ("");
Free (q->next);
q->next=null;
}
}
if (Biteself () ==1)//Judge If you will bite yourself
{
endgamestatus=2;
Endgame ();
}
}
void pause ()/suspend
{
while (1)
{
Sleep (300);
if (Getasynckeystate (vk_space))
{
Break
}
}
}
void Gamecircle ()//Control game
{
Pos (64,15);
printf ("Can't go through walls, can't bite yourself \ Nand");
Pos (64,16);
printf ("Control the movement of the Snake with ↑.↓.←.→");
Pos (64,17);
printf ("F1 for acceleration, F2 for deceleration");
Pos (64,18);
printf ("ESC: Quit the game. Space: Pause the game.");
Pos (64,20);
printf ("C Language Research Center www.dotcpp.com");
Status=r;
while (1)
{
Pos (64,10);
printf ("Score:%d", score);
Pos (64,11);
printf ("Score per food:%d points", add);
if (Getasynckeystate (vk_up) && Status!=d)
{
Status=u;
}
else if (getasynckeystate (vk_down) && status!=u)
{
Status=d;
}
else if (getasynckeystate (vk_left) && status!=r)
{
Status=l;
}
else if (getasynckeystate (vk_right) && status!=l)
{
Status=r;
}
else if (getasynckeystate (vk_space))
{
Pause ();
}
else if (getasynckeystate (Vk_escape))
{
endgamestatus=3;
Break
}
else if (getasynckeystate (VK_F1))
{
if (sleeptime>=50)
{
sleeptime=sleeptime-30;
add=add+2;
if (sleeptime==320)
{
add=2;//prevent it from being reduced to 1 and then added back.
}
}
}
else if (getasynckeystate (VK_F2))
{
if (sleeptime<350)
{
sleeptime=sleeptime+30;
Add=add-2;
if (sleeptime==350)
{
add=1; Guaranteed minimum divided into 1
}
}
}
Sleep (Sleeptime);
Snakemove ();
}
}
void Welcometogame ()//Start interface
{
Pos (40,12);
System ("Title C Language Research Center www.dotcpp.com");
printf ("Welcome to the gluttonous snake game.") ");
Pos (40,25);
printf ("C Language Research Center www.dotcpp.com.\n");
System ("pause");
System ("CLS");
Pos (25,12);
printf ("Control the movement of the snake with ↑.↓.←.→, F1 for acceleration, 2 for deceleration");
Pos (25,13);
printf ("Acceleration will get a higher score.") \ n ");
System ("pause");
System ("CLS");
}
void endgame ()/End Game
{
System ("CLS");
Pos (24,12);
if (endgamestatus==1)
{
printf ("Sorry, you hit the wall.") Game over. ");
}
else if (endgamestatus==2)
{
printf ("Sorry, you bit yourself.") Game over. ");
}
else if (endgamestatus==3)
{
printf ("Your game has ended.") ");
}
Pos (24,13);
printf ("Your score is%d\n", score);
Exit (0);
}
void Gamestart ()//game initialization
{
System ("mode con cols=100 lines=30");
Welcometogame ();
Creatmap ();
Initsnake ();
Createfood ();
}
int main ()
{
Gamestart ();
Gamecircle ();
Endgame ();
return 0;
}

Original address: http://www.dotcpp.com/wp/114.html

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.