先上一下效果图
欢迎界面
游戏界面
结束界面
大致思路
1.游戏分为四个部分:场景,蛇,食物和交互
2.打印整个地图以及提示(完成场景)
3.食物需要的函数:打印食物,改变食物坐标
4.蛇需要的函数:初始位置,移动,改变方向(键盘监听)
5.交互函数:蛇吃食物(加长,改变食物坐标),游戏结束(蛇撞墙,碰到自己)
移动通过删除尾巴,改变头坐标实现。加长则不删除尾巴。
完整代码(有详细注释)
#include <iostream>
#include <windows.h>
#include <cstdlib> //定义杂项函数及内存分配函数
#include <cstdio> //定义输入/输出函数
#include <string> //字符串处理
#include <conio.h> //主要是一些用户通过按键盘产生的对应操作
#include <ctime> //定义关于时间的函数
using namespace std;
int snake[1000][2]; //蛇每一节x,y坐标
int length = 3;
int food[2] = {20,15}; //食物x,y坐标
int a = 1; //x坐标变化
int b = 0; //y坐标变化
int s = 1; //主函数循环
int speed = 500; //间隔时间(速度)
int score = 0;
int level = 1;
void start();
void hide();
void gotoxy(short x, short y);
void drawWall();
void drawSnake();
void changeSnake();
void drawFood();
void changeDirection();
void eatFood();
void clear();
void gameOver();
int main()
{
start();
hide();
drawWall();
drawSnake();
drawFood();
while(s)
{
changeDirection();
changeSnake();
eatFood();
gameOver();
}
clear();
return 0;
}
void hide() //隐藏光标
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息
CursorInfo.bVisible = false; //隐藏控制台光标
SetConsoleCursorInfo(handle, &CursorInfo);
}
void gotoxy(short x, short y) //变换坐标
{
COORD position = { x, y };
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, position);
}
void start() //欢迎界面
{
gotoxy(20, 10);
cout << "welcome to snake";
gotoxy(20, 20);
cout << "Press enter to start the game.";
cin.get();
system("cls");
}
void drawWall() //初始化打印边界和提示
{
for (int i=1;i<=20;i++)
{
gotoxy(0, i);
cout << "#";
gotoxy(40, i);
cout << "#";
}
for (int l=0;l<=40;l++)
{
gotoxy(l, 0);
cout << "#";
gotoxy(l, 20);
cout << "#";
}
gotoxy(50, 10);
cout << "press w,s,a,d to change direction.";
gotoxy(50,14);
cout << "score: " << score << endl;
gotoxy(50,16);
cout << "level: " << level;
gotoxy(50, 18);
cout << "press Space to stop ,press Enter to continue";
}
void drawSnake() //初始化蛇
{
snake[0][0] = 10;
snake[0][1] = 10;
snake[1][0] = 9;
snake[1][1] = 10;
snake[2][0] = 8;
snake[2][1] = 10;
gotoxy(snake[0][0],snake[0][1]);
cout << "*";
gotoxy(snake[1][0], snake[1][1]);
cout << "+";
gotoxy(snake[2][0], snake[2][1]);
cout << "+";
}
void drawFood() //打印食物
{
gotoxy(food[0], food[1]);
cout <<"@";
}
void changeFood() //改变食物坐标
{
int f = 1;
while(f)
{
srand((int)time(0));
food[0] = rand() % 39 + 1;
srand((int)time(0));
food[1] = rand() % 19 + 1;
for (int i = length-1; i > 0; i--)
{
if(snake[i][0] == food[0] && snake[i][1] == food[1])
{
f = 1;
break;
}
else
f = 0;
}
}
drawFood();
}
void eatFood() //蛇吃食物
{
if(snake[0][0] == food[0] && snake[0][1] == food[1])
{
gotoxy(snake[length-1][0],snake[length-1][1]); //加分
cout << "+";
length += 1;
score += 1;
gotoxy(50,14);
cout << "score: " << score << endl;
if(score!= 0 && score%5 == 0) //加等级
{
level += 1;
if(speed !=100)
speed -= 100;
gotoxy(50,16);
cout << "level: " << level;
}
changeFood();
}
gotoxy(0,0);
cout << "#";
}
void changeDirection() //键盘控制改变行进方向
{
if (kbhit()!=0)
{
char ch = getch();
switch (ch)
{
case 'w':
case 'W':
if(b == 0) //防止反方向改变,下同
{
a = 0;
b = -1;
}
break;
case 's':
case 'S':
if(b == 0)
{
a = 0;
b = 1;
}
break;
case 'a':
case 'A':
if(a == 0)
{
a = -1;
b = 0;
}
break;
case 'd':
case 'D':
if(a == 0)
{
a = 1;
b = 0;
}
break;
case ' ':
cin.get();
break;
default:
break;
}
}
}
void changeSnake() //改变蛇位置
{
gotoxy(snake[length-1][0],snake[length-1][1]);
cout << " "; //清除尾巴
for (int i = length-1; i > 0; i--) //改变身体坐标
{
for (int j = 0; j < 2; j++)
{
snake[i][j] = snake[i - 1][j];
}
}
snake[0][0] += a;
snake[0][1] += b; //改变蛇头坐标
gotoxy(snake[0][0],snake[0][1]);
cout << "*";
gotoxy(snake[1][0],snake[1][1]);
cout << "+";
Sleep(speed);
}
void gameOver() //游戏结束
{
if(snake[0][0] == 0 || snake[0][0] == 40 || snake[0][1] == 0 || snake[0][1] == 20) //撞墙
{
s = 0;
system("cls");
}
for (int i = length-1; i > 0; i--) //碰到自己
{
if(snake[0][0] == snake[i][0] && snake[0][1] == snake[i][1])
{
s = 0;
system("cls");
}
}
}
void clear()
{
gotoxy(20, 10);
cout << "Game over!!!" << endl;
gotoxy(20, 12);
cout << "your score: " << score << endl;
gotoxy(20, 14);
cout << "Welcome to play again, press Enter to continue";
gotoxy(20, 20);
cin.get();
}