Just after the C language basic study, I want to do the next write a console of the snake.
Head header File
#pragma once
#define _crt_secure_no_warnings
#include <stdio.h>
#include <stdlib.h>
# Include <windows.h>
#include <memory.h>
#include <windows.h>
#include <time.h>
#include <stdbool.h>
#include "Snake.h"
#include "map.h"
char ch;//used to hold the input character, the snake's direction of movement
Some related functions of snakes and global Variables for header files (Snake.h)
#pragma once
typedef struct LIST
{
//data field
int x;//array x
int y;//array y
struct list *next;//pointer field
} List;
typedef struct SNAKE
{
list *phead;//head node, in order to facilitate
the insertion of the snake head List *ptail;//tail node, in order to facilitate the removal of snake tail
}snake;
BOOL Init (Snake **shead);//Initialize
bool Insert (Snake *shead, int x, int y);//Insert
bool Deletesnake (Snake *shead);// Delete tail node
void setnewsnake (int changedirection_x, int changedirection_y);//Redraw Snake
void Snackmove ();//Snake movement
void Show (Snake *shead);//show
void Changsnake ();//Change the shape of the snake
void deathsnake (int new_x,int new_y);// Determine if the snake died
void Setreward ();//Set
the bonus BOOL risesnake (int new_x,int new_y);//Grow the snake body
int one;//control the number
of rewards Snake *mysnake;//Snake
Map display and initialization header file (map.h)
#include <windows.h>
#define X_MAX 25//array size
#define Y_MAX
int g_map[x_max][y_max];//map
void Buildmap ();//Initialize Boundary
void Showmap ();//Show Map
void Shownum ();//Display the number on the map
void Gotoxy (HANDLE hOut, int x , int y);//Reset Cursor position
Source file for the map (MAP.C)
#include "head.h"//1: Wall and Snake Body//0: Feasible//2: Serpent head Void Buildmap () {for (int i = 0; i < y_max;i++) {G_map[0][i] = 1;//First row
Set to wall g_map[x_max-1][i] = 1;//last row set to Wall} for (int i = 0; i < X_max; i++) {g_map[i][y_max-1] = 1;//last column set as wall G_map[i][0] = 1;//The first column is set to Wall}} void Shownum ()//Displays the number on the map {for (int i = 0; i < X_max; i++) {for (int j = 0; j < Y_max;
J + +) {printf ("%d", g_map[i][j]);
} printf ("\ n");
}} void Gotoxy (HANDLE hOut, int x, int y) {COORD pos; Pos. X = x;//Horizontal pos.
Y = y;//ordinate setconsolecursorposition (HOut, POS); } void Showmap () {HANDLE hOut = GetStdHandle (std_output_handle);//define monitor handle variable for (int i = 0; i < x_max;i++) {for
(int j = 0; J < y_max;j++)
{Gotoxy (HOut, j*2, i);
if (g_map[i][j]==1) {printf ("%s", "-");
} else if (g_map[i][j] = = 2) {printf ("%s", "0");
} else {printf ("%s", "" ");
}} printf ("\ n");
} Sleep (100);
if (ONE>50)//control reward existence time {one = 0;
} else {one++; }
}
Snake's source file (snake.c)
#pragma once #include "head.h" BOOL init (Snake **shead) {if (*shead) ==null) {(*shead) = (snake*) calloc (1, sizeof (
Snake));//assign memory to the snake if ((*shead) ==null) {printf ("failed to allocate memory \ n");
return 0;
} else {return 1;
}} else {return 0;
}} BOOL Insert (Snake *shead, int x, int y) {if (Shead ==null)//Not allocated memory {printf ("Snake does not allocate memory \ n");
return 0;
} List *p = calloc (1, sizeof (List));
if (p==null) {printf ("The body assignment failed \ n");
return 0;
} p->x = x;
P->y = y;
P->next = NULL;
if (Shead->phead = = NULL) {shead->phead = p;//Assignment snake head shead->ptail = p;//set tail return 1;
} else {p->next = shead->phead;
Shead->phead = p;
return 1;
} return 0;
} BOOL Deletesnake (Snake *shead)//Delete tail node {if (shead==null) {return 0;
} else {List *pold = shead->phead;
List *pnew = pold;
while (pold->next!=null) {pnew = Pold;
Pold = pold->next;
} free (pold);
Pold = NULL;
Pnew->next = NULL; shead-≫ptail = pnew;//Reset snake tail return 1;
}} void Show (Snake *shead)//display {List *p = shead->phead;
while (p) {printf ("%d,%d\n", P->x, P->y);
p = p->next;
}} BOOL risesnake (int new_x,int new_y)//Grow the Snake body {if (g_map[new_x][new_y]==2) {return 1;
} else {return 0; }} void Setnewsnake (int changedirection_x, int changedirection_y) {g_map[mysnake->phead->x][mysnake->phead- >y] = 1;//Sets the current snake head position to the body int new_x = mysnake->phead->x + changedirection_x;//new snake head coordinates int new_y = mysnake->phead-
>y + changedirection_y;//New snake head coordinates deathsnake (new_x, new_y); if (!risesnake (new_x, new_y))//No bonus to remove the snake body, otherwise do not have {g_map[mysnake->ptail->x][mysnake->ptail->y] = 0;// Clear 0 Deletesnake (mysnake) at the current end of the snake;//Remove the tail of the snake} else {one = 0;//set the bonus to 0 to reset the reward location} insert (Mysnake, new_x, new_y);//Place new location Set to Snake head g_map[mysnake->phead->x][mysnake->phead->y] = 2;//The new snake head corresponds to the coordinates set on the map} void Setreward ()//set Bonus {St atic int reward_x=1;//Bonus X-axis static int Reward_y=1;//Bonus Y-axis if (one==0) {time_t ts;
unsigned int num = time (&ts);
Srand (num); G_MAP[REWARD_X][REWARD_Y] = 0;//Initialize the value of the reward//(X_MAX-2) Exclude the bottom row of the Wall setting award, +1 exclude the top row of wall set bonus//(Y_MAX-2) Exclude the right column of the Wall setting award, +1 exclude the leftmost column of the wall set
Set bonus while (1) {reward_x = rand ()% (x_max-2) + 1;
reward_y = rand ()% (y_max-2) + 1;
if (g_map[reward_x][reward_y]==0)//Set reward is not a snake body or wall {g_map[reward_x][reward_y] = 2;
Break }}}} void Judgehaul (char oldch)//Determine if you want to change the direction {if (Oldch = = ' W ' | | oldch = = ' W ')//Move Up {if (ch== ' s ' | | ch=
= ' s ') {ch = oldch;//remains in the original direction}} else if (oldch = = ' s ' | | oldch = = ' s ')//Move Down {if (ch = = ' W ' | | ch = = ' W ')
{ch = oldch;//Keep the original direction}} else if (oldch = = ' a ' | | oldch = = ' a ')//move left {if (ch = = ' d ' | | ch = = ' d ') {ch = oldch;//Keep the original direction}} else if (OLDCH = = ' d ' | | oldch = = ' d ')//Move Right {if (ch = = ' A ' | | ch = = ' a ') {ch = oldch;//Keep the original direction}}} void Deathsna ke (int new_x, int new_y)//Determine if the snake is dead {if (g_map[new_X][new_y]==1)//encounters itself or wall death {System ("CLS");
for (int i = 0; i < 15;i++) {printf ("\ r \ n");
} for (int i = 0; i < i++) {printf ("%c", "");
} printf ("Sorry Game over\r\n");
Sleep (3000);
Exit (0); }} void Changsnake ()//Change the shape of the snake {int changedirection_x = 0;//change direction X int changedirection_y = 0;//change Direction Y if (ch = = ' W ' ||
ch = = ' W ')//move Up {changedirection_x =-1;
} else if (ch = = ' s ' | | ch = = ' s ')//Move Down {changedirection_x = 1;
} else if (ch = = ' A ' | | ch = = ' a ')//move Left {changedirection_y =-1;
} else if (ch = = ' d ' | | ch = = ' d ')//move Right {changedirection_y = 1;
} setnewsnake (changedirection_x, changedirection_y);
if (Kbhit ())//Gets the keyboard input stream {char OLDCH = ch;//Saves the old value because it needs to be judged by ch = getch ();
Judgehaul (OLDCH);
}} void Snackmove ()//Snake movement {while (true) {Setreward (); System ("CLS");//Refresh screen showmap ();//Show Map Changsnake ();//Change the position of the Snake}}
Main function
#include "head.h"
Void Help ()
{
printf ("Press number 1: Start game \ n");
printf ("By number 2: game operation \ n");
printf ("Press number 0: Exit game \ n");
}
void Gamestart ()
{
mysnake = NULL;
Init (&mysnake);//Initialize
the snake insert (mysnake, X_MAX/2, Y_MAX/2-2);//give the body initialization two positions
G_MAP[X_MAX/2][Y_MAX/2-2] = 1;
Insert (Mysnake, X_MAX/2, Y_MAX/2-1);
G_MAP[X_MAX/2][Y_MAX/2-1]=1;
Insert (Mysnake, X_MAX/2, Y_MAX/2);//Initialize the position of the snake head
g_map[x_max/2][y_max/2]=2;//in the position of the snake head to assign
ch = ' d ';//Initialize the direction of movement
Buildmap ();
System ("CLS");
}
int main ()
{help
();
int num = 0;
while (1)
{
scanf ("%d", &num);
Switch (num)
{case
1:
{
gamestart ();
Snackmove ();
}
break;
Case 0:
printf ("will close the game \ n");
Sleep (+);
return; Case 2:
printf ("Top: W, bottom: S, left: A, right: d\n");
Default: Help
();
break;
}
}
return 0;
}
Run: