C + + Snake Games

Source: Internet
Author: User

(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

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.