(Map size is 25*25, the initial position of the snake is an array of map[3][3]~map[3][6], the snake head is map[3][6], the direction to the right)
#include <iostream>
#include <Windows.h>
#include <cstdlib>
#include <time.h>
#include <conio.h>
using namespace Std;
#define Max_wide 25
#define Max_high 25
Char Map[max_high][max_wide]; Map size
struct node
{
int x, y;
};
Node snack[(max_wide-2) * (max_high-2), food; Define the snake body and food coordinates
int Lenth, direct, live = 1; The length of the snake, direction and survival
int speech = 200;
void Initmap (node *n, int len) {//Initialize map
for (int i = 0; i < Max_high; i++)
for (int j = 0; J < Max_wide; J + +)
MAP[I][J] = ";
for (int i = 0; i < Max_high; i++)//Set wall around
Map[i][0] = map[i][max_wide-1] = ' | ';
for (int j = 0; J < Max_wide; J + +)
MAP[0][J] = map[max_high-1][j] = '-'; Set up and down walls
for (int j = 1; j < Len; j + +)
MAP[N[J].X][N[J].Y] = ' * '; Set up a snake body
MAP[N[0].X][N[0].Y] = ' # '; Set the snake Head
}
void Showmap () {//output array map
System ("CLS"); Clear Screen
for (int i = 0; i < Max_high; i++) {
for (int j = 0; J < Max_wide; J + +) {
cout << Map[i][j];
}
cout << Endl;
}
}
void Showfood () {//produce food, coordinates in map and not on snakes
Srand ((unsigned) time (NULL)); The point at which food is generated is random
do {
food.x = rand ()% Max_wide;
Food.y = rand ()% Max_high;
} while (Map[food.x][food.y]! = ");
MAP[FOOD.X][FOOD.Y] = ' o ';
}
int Updatagame () {//update array
Long start = clock ();
int A, B; Position of the snake head after the move is staged
while (!_kbhit () && (Clock ()-start <= speech)); Until there's a button or time is over, speech jumps out of the loop.
if (_kbhit ()) {
_getch (); The upper, lower, left, and right keys are two bytes, taking the second byte (72,80,75,77) respectively
Direct = _getch ();
}
Switch (direct)
{
Case://up
A = snack[0].x-1;
b = snack[0].y;
Break
Case://down
A = snack[0].x + 1;
b = snack[0].y;
Break
Case://left
A = snack[0].x;
b = snack[0].y-1;
Break
Case://right
A = snack[0].x;
b = snack[0].y + 1;
Break
Default
Break
}
if (a = = 0 | | a = = MAX_HIGH-1 | | b = = 0 | | b = = max_wide-1)//Wall
Live = 0;
if (map[a][b] = = ' * ')//bump itself
Live = 0;
MAP[SNACK[0].X][SNACK[0].Y] = ' * '; Put the snake's head into a snake body
MAP[SNACK[LENTH-1].X][SNACK[LENTH-1].Y] = "; Snake tail is empty
Map[a][b] = ' # '; Updated Snake head
for (int i = lenth-1; i > 0; i--)//update Snake array
Snack[i] = snack[i-1];
snack[0].x = A;
Snack[0].y = b;
if (a = = Food.x && b = = Food.y) {//Snake eats food
MAP[SNACK[LENTH-1].X][SNACK[LENTH-1].Y] = ' * '; Cancel the snake tail empty
lenth++;
Showfood (); Produce new food
Speech-= lenth; Accelerated
}
Showmap ();
return live;
}
int main () {
snack[0].x = 3;
SNACK[0].Y = 6;
Lenth = 4;
for (int i = 1; i < lenth; i++) {
snack[i].x = 3;
Snack[i].y = snack[i-1].y-1;
}
Direct = 77;
Initmap (snack, lenth);
Showfood ();
Showmap ();
do {
Updatagame ();
} while (live);
cout << "To be divided into:" << lenth-4 << Endl;
System ("pause");
return 0;
}
C + + Snake Games