最近沉迷于编写游戏,虽然都是简单的DOS版(之前尝试可视化编程失败了),井字棋,五子棋,移动迷宫之后,我又想出还有贪吃蛇没设计过。于是设计了以下程序。
程序
// This is a simple snake game.
#include <iostream>
#include <string>
#include <vector>
#include <cstddef>
#include <Windows.h>
#include <conio.h>
#include <random>
#include <ctime>
#include <deque>
using namespace std;
struct location {
unsigned x, y;
}loc1, loc2;
int state = 0;
int ret = 4; // head right by default
unsigned cnt = 0;
unsigned score = 0;
char p[25][30]; // the board
vector<location> vec;
deque<location> snake;
void p_def() // define p
{
for (int i = 0; i != 25; i++)
{
if (i != 0 && i != 24)
{
p[i][0] = p[i][29] = '*';
for (int j = 1; j != 29; j++)
p[i][j] = ' ';
}
else
{
for (int j = 0; j != 30; j++)
p[i][j] = '*';
}
}
p[13][5] = 'O'; // body
p[13][6] = '@'; // head
loc1.x = 13;
loc1.y = 6; // the initial location of the head of the snake
loc2.x = 13;
loc2.y = 5; // the initial location of the tail of the snake
snake.push_front(loc2); // store the first location of the snake tail
}
void input(char c1, char c2)
{
c1 = _getch();
c2 = _getch();
switch (c2)
{
case 72: // up
if (ret != 2)
ret = 1;
break;
case 80: // down
if (ret != 1)
ret = 2;
break;
case 75: // left
if (ret != 4)
ret = 3;
break;
case 77: // right
if (ret != 3)
ret = 4;
break;
default:
break;
}
}
void print()
{
for (int i = 0; i != 25; i++)
{
for (int j = 0; j != 30; j++)
std::cout << " " << p[i][j];
std::cout << endl;
}
}
void ran() // generate a random food for the snake to eat
{
default_random_engine random1, random2;
for (int i = 0; i != 644; i++)
{
unsigned ran1 = random1() % 23 + 1; // ranging from 1 to 23
unsigned ran2 = random2() % 28 + 1; // ranging from 1 to 28
location ran_l;
ran_l.x = ran1;
ran_l.y = ran2;
vec.push_back(ran_l);
}
}
void move()
{
switch (ret)
{
case 1: // up
if (p[loc1.x - 1][loc1.y] == 'O' || p[loc1.x - 1][loc1.y] == '*')
state = 1; // indicate the end of the game
else
{
if (p[loc1.x - 1][loc1.y] == '#')
{
score++;
snake.push_front(loc1);
}
else
{
p[snake.back().x][snake.back().y] = ' '; // delete the end of the tail
snake.pop_back();
snake.push_front(loc1); // former head -> front of the tail
}
}
--loc1.x;
break;
case 2: // down
if (p[loc1.x + 1][loc1.y] == 'O' || p[loc1.x + 1][loc1.y] == '*')
state = 1; // indicate the end of the game
else
{
if (p[loc1.x + 1][loc1.y] == '#')
{
score++;
snake.push_front(loc1);
}
else
{
p[snake.back().x][snake.back().y] = ' '; // delete the end of the tail
snake.pop_back();
snake.push_front(loc1); // former head -> front of the tail
}
}
++loc1.x;
break;
case 3: // left
if (p[loc1.x][loc1.y - 1] == 'O' || p[loc1.x][loc1.y - 1] == '*')
state = 1; // indicate the end of the game
else
{
if (p[loc1.x][loc1.y - 1] == '#')
{
score++;
snake.push_front(loc1);
}
else
{
p[snake.back().x][snake.back().y] = ' '; // delete the end of the tail
snake.pop_back();
snake.push_front(loc1); // former head -> front of the tail
}
}
--loc1.y;
break;
case 4: // right
if (p[loc1.x][loc1.y + 1] == 'O' || p[loc1.x][loc1.y + 1] == '*')
state = 1; // indicate the end of the game
else
{
if (p[loc1.x][loc1.y + 1] == '#')
{
score++;
snake.push_front(loc1);
}
else
{
p[snake.back().x][snake.back().y] = ' '; // delete the end of the tail
snake.pop_back();
snake.push_front(loc1); // former head -> front of the tail
}
}
++loc1.y;
break;
default: //others
break;
}
for (auto c : snake)
p[c.x][c.y] = 'O'; // print the tail
p[loc1.x][loc1.y] = '@'; // print the head
}
int main()
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut, &cci);
cci.bVisible = FALSE;
SetConsoleCursorInfo(hOut, &cci);
// The five lines above are used to hide the Console Cursor.
char c1 = '\0', c2 = '\0';
int time_set;
std::cout << "This is a simple snake game.\nProgrammer:Teddy van Jerry" << endl;
Sleep(300);
std::cout << "\nNow Loading";
for (int i = 0; i != 6; i++)
{
Sleep(200);
std::cout << ".";
}
Sleep(500); // stop for a while
std::cout << "\n\nYou can move the snake using ↑,↓,←,→." << endl;
std::cout << "\nYou can choose the speed the snake moves.\nVery slow(1) Slow(2) Medium(3) Fast(4) Very Fast(5) Very Very Fast(6)" << endl;
char choice;
choice = _getch();
switch (choice) // set the speed
{
case '1':
time_set = 1000;
break;
case '2':
time_set = 700;
break;
case '3':
time_set = 500;
break;
case '4':
time_set = 350;
break;
case '5':
time_set = 250;
break;
case '6':
time_set = 150;
break;
default:
time_set = 500;
break;
}
p_def(); // define p
cout << "\nNow get ready!\n" << endl;
Sleep(3500);
ran();
while (state == 0)
{
unsigned long clock_p = clock();
print();
std::cout << "\nYour current score is " << score << " .\n" << endl;
do
{
if (_kbhit()) // test if there is a click on the button
input(c1, c2);
} while (clock() - clock_p < time_set);
move();
if (p[(vec[cnt]).x][(vec[cnt]).y] == ' ')
p[(vec[cnt]).x][(vec[cnt]).y] = '#'; // generate food for the snake to eat
cnt++;
}
std::cout << "\nThe game is over.\nYour final score is " << score << " !" << endl;
std::cout << "\nALL RIGHTS RESERVED (c) 2020 Teddy van Jerry" << endl;
char anything;
cin >> anything;
return 0;
}
// ALL RIGHTS RESERVED (c) 2020 Teddy van Jerry
输出示例
(有些缺憾的是程序会有些闪,这个没办法)
分析
- 一开始写完问题多多,花了好久找bug。
然后又这样(怎么全是头而且身体被砍断?)
进而有这样(看着挺对,实际上身子动不了了)
- 可以看一下处理时间的函数,是在库
ctime
里的time()
函数,单位是毫秒,输出实际为long。 Sleep()
是库Windows.h
里的函数,就是暂停这么多毫秒。- 192-196行为取消光标的代码,网上找到的。
- 248行
_kbhit()
为判断是否键盘有输入而不输入。此处用_getch()
是没有用的(这两个函数均在库conio.h
里),我试过,这是之前那么多错误的原因之一。原来不加判断时程序会卡在输入,如果不输入是不会动的。 - 关于方向键的输入,详见我的博客 【C++ 程序】 移动迷宫游戏中的应用及分析。
- 蛇尾巴的存储是在
deque
里的,好处是两端可操作,输出方便。具体用法可在 CSDN 上搜索。 - 258-259行本无意义,目的在于单独使用 exe 文件时有时结束游戏时会直接退出不显示最后结果。加上这句无用的话即可解决。详见我的博客 关于 C++中 程序在其他设备上运行 的思考。
- 自定义随机位置函数
ran()
详见我的博客 【C++ 程序】 随机数。 - 定义函数详见我的博客 关于 C++中 定义函数造成影响 的思考。
- 此程序会闪,如果使用【C++ 程序】 贪吃蛇游戏(EasyX 图形界面)即可解决。
ALL RIGHTS RESERVED © 2020 Teddy van Jerry
欢迎转载,转载请注明出处。
See also
Teddy van Jerry 的导航页
【C++ 程序】 五子棋游戏(人 VS 人)
【C++ 程序】 井字棋游戏(人 VS 人)
【C++ 程序】 井字棋游戏(人 VS Lv1电脑)
【C++ 程序】 井字棋游戏(人 VS Lv2电脑)
【C++ 程序】 井字棋游戏(人 VS Lv3电脑)
【C++ 程序】 井字棋游戏(人 VS Lv3电脑)(战绩统计版)
【C++ 程序】 移动迷宫游戏
【C++ 程序】 随机数
【C++ 程序】 数字推盘游戏(15-puzzle)
【C++ 程序】 2048游戏
【C++ 程序】 井字棋游戏(人 VS 人)(EasyX 图形界面)
【C++ 程序】 井字棋游戏(人 VS Lv3电脑)(战绩统计版)(EasyX 图形界面)
【C++ 程序】 2048游戏(EasyX 图形界面)
【C++ 程序】 贪吃蛇游戏(EasyX 图形界面)