Snake Games C + + command line version Instance code _c language

Source: Internet
Author: User
Tags clear screen rand


This example tells the greedy snake game C + + command line version of the implementation code, is very classic game. Share to everyone for your reference. The implementation methods are as follows:



As we all know, the snake game is a classic computer game.



The game is described as follows:



1. Greedy snake can be automatic straight forward, or the player can be manipulated by the direction of the greedy snake to move up and down, each advance a lattice.
2. The greedy snake in the stipulated area activity, when:



① a greedy snake touches a wall;



② snake head touching snake body or snake tail;



③ Player's keyboard input is not a directional key;



Command line displays "Game over!" and quit the game.



3. Greedy snake activities in the region each randomly produced a "peas", when the greedy snake eat "peas" after the snake body growth, automatic forward time shortened 100ms (default is 1000ms, and can not be less than 100ms). The length of a snake is 8 per improve a level.



C + + code is as follows:


##include <bios.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <stdlib.h>
#include <time.h>
Using namespace std;
 
Inline void display(char gsDomain[][22], int level, int moveSpeed)
{
System("cls"); //Clear screen
Cout << endl << endl;
For (int i = 0; i < 22; i++)
{
Cout << "\t";
For (int j = 0; j < 22; j++)
Cout << gsDomain[i][j] << " ";
If (i == 0)
{
Cout << "\tLevel:" << level;
}
Else if (i == 3)
{
Cout << "\t automatic forward time";
}
Else if (i == 5)
{
Cout << "\tinterval:" << moveSpeed << " ms";
}
 
Cout << endl;
}
}
 
Int main()
{
Char gsDomain[22][22]; // Snake activity area (including walls)
/ / Initialize the snake activity area (excluding the wall)
For (int i = 1; i <= 21; i++)
{
For (int j = 1; j <= 21; j++)
gsDomain[i][j] = ' ';
}
/ / Initialize the upper and lower walls of the Snake activity area
For (int i = 0; i < 22; i++)
gsDomain[0][i] = gsDomain[21][i] = '-';
/ / Initialize the left and right walls of the Snake activity area
For (int i = 1; i < 21; i++)
gsDomain[i][0] = gsDomain[i][21] = '|';
/ / Initialize the snake body
For (int i = 1; i <= 3; i++)
gsDomain[1][i] = '*';
/ / Initialize the snake head
gsDomain[1][4] = '#';
 
Int snake[2][100]; //record the coordinates of the position where the snake eats each time
For (int i = 0; i < 4; i++)
{
Snake[0][i] = 1; //record the x coordinate of the location of the snake
Snake[1][i] = i + 1; //record the y coordinate of the location of the snake
}
Int head = 3, tail = 0, length = 4;
 
Int beanX, beanY; //where the bean appears
Srand(time(0));
Do
{
beanX = rand() % 20 + 1;
beanY = rand() % 20 + 1;
} while (gsDomain[beanX][beanY] != ' ');
gsDomain[beanX][beanY] = '*'; //Doudou
 
Cout << "\n\n\t\t Snake game is about to begin!\n";
Long start;
Int level = 1, moveSpeed = 1000;
For (int i = 3; i >= 0; i--)
{
Start = clock();
While (clock() - start <= 1000){}
System("cls");
If (i)
{
Cout << "\n\n\t\tEnter the game countdown:" << i << endl;
}
Else
Display(gsDomain, level, moveSpeed);
}
 
Char direction = 77; //Grapian snake automatically goes straight to the right by default
While (true)
{
Bool timeFlag = true;
Int x, y;
Start = clock();
 
/ / If the time exceeds the automatic forward time or there is a key press on the keyboard, the loop is terminated.
While ((timeFlag = (clock() - start <= moveSpeed)) && !kbhit()){}
 
If (timeFlag)
{
//Read keyboard input when there is a key on the keyboard
Getch();
Direction = getch();
}
 
Switch (direction)
{
//up
Case 72: x = snake[0][head] - 1, y = snake[1][head];
Break;
//down
Case 80: x = snake[0][head] + 1, y = snake[1][head];
Break;
//left
Case 75: x = snake[0][head], y = snake[1][head] - 1;
Break;
//To the right
Case 77: x = snake[0][head], y = snake[1][head] + 1;
Break;
Default: cout << "\tGame Over!\n";
Return 0;
}
 
If (x == 0 || x == 21 || y == 0 || y == 21)
{
// Snake touches the wall
Cout << "\tGame Over!\n";
Return 0;
}
 
If (gsDomain[x][y] != ' ' && !(x == beanX && y == beanY))
{
//The Snake head of Snake touches the snake body or the snake tail
Cout << "\tGame Over!\n";
Return 0;
}
 
If (x == beanX && y == beanY)
{
// Eat peas
Length++; //length plus 1
If (length >= 8)
{
/ / Game upgrade processing
Length -= 8;
Level++;
If (moveSpeed > 100)
moveSpeed -= 100;
}
gsDomain[snake[0][head]][snake[1][head]] = '*';
gsDomain[x][y] = '#';
Head = (head + 1) % 100;
Snake[0][head] = x;
Snake[1][head] = y;
Do
{
beanX = rand() % 20 + 1;
beanY = rand() % 20 + 1;
} while (gsDomain[beanX][beanY] != ' ');
gsDomain[beanX][beanY] = '*';
 
Display(gsDomain, level, moveSpeed); //display on the screen
}
Else
{
//Do not eat peas
gsDomain[snake[0][tail]][snake[1][tail]] = ' '; //The snake tail moves forward one space
Tail = (tail + 1) % 100;
gsDomain[snake[0][head]][snake[1][head]] = '*';
Head = (head + 1) % 100;
Snake[0][head] = x;
Snake[1][head] = y;
gsDomain[x][y] = '#'; //The snake head moves forward one space
Display(gsDomain, level, moveSpeed); //display on the screen
}
}
 
Return 0;
} 


I hope that the examples described in this article will help you with the learning of C programming.


Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.