一、贪吃蛇是什么?
贪吃蛇是一款经典的休闲游戏。有PC和手机等多平台版本,既简单又耐玩。玩家在游戏通过控制蛇头方向吃道具,从而使得蛇变得越来越长。
二、编写贪吃蛇
该游戏学要学会编写左右控制键,吃食物身体长度+1,将蛇头和蛇身以及米打印在控制台上,判断游戏的输赢。游戏设置等级,空格键暂停
三、游戏代码
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <cstring>
#include <cstdio>
#include <iostream>
#define N 22
using namespace std;
int gameover;
int x1, y1111; // 随机出米
int x, y;
long start;
//=======================================
//类的实现与应用initialize
//=======================================
//下面定义贪吃蛇的坐标类
class snake_position
{
public:
int x, y; //x表示行,y表示列
snake_position() {
};
void initialize(int&);//坐标初始化
};
snake_position position[(N - 2) * (N - 2) + 1]; //定义贪吃蛇坐标类数组,有(N-2)*(N-2)个坐标
void snake_position::initialize(int& j)
{
x = 1;
y = j;
}
//下面定义贪吃蛇的棋盘图
class snake_map
{
private:
char s[N][N];//定义贪吃蛇棋盘,包括墙壁。
int grade, length;
int gamespeed; //前进时间间隔
char direction; // 初始情况下,向右运动
int head, tail;
int score;
bool gameauto;
public:
snake_map(int h = 4, int t = 1, int l = 4, char d = 77, int s = 0) :length(l), direction(d), head(h), tail(t), score(s) {
}
void initialize(); //初始化函数
void show_game();
int updata_game();
void setpoint();
void getgrade();
void display();
};
//定义初始化函数,将贪吃蛇的棋盘图进行初始化
void snake_map::initialize()
{
int i, j;
for (i = 1