贪吃蛇(简易版)Leslie5205912著

# include <stdio.h>
# include <string.h>
# include <windows.h>
# include <stdlib.h>
# include <conio.h>
# include <time.h>
# include <math.h>
# include<iostream>
using namespace std;
#pragma warning(disable:4996)

#define de_lenth 5//蛇初始长度
#define Row_max 20//地图行
#define Line_max 30//地图列

char map[Row_max][Line_max];
int snake[Row_max][Line_max];
int Head_x = Line_max / 2;
int Head_y = Row_max / 2;
int Tail_x;
int Tail_y;
int Head_v = 5;//头部移动
int timen=6;//延时程度(蛇运动速度)
int food_x;
int food_y;
int score=0;//分数
char direct = 's';//蛇的运动方向 

void Creatmap(char map[Row_max][Line_max]); //设置地图
void Creatsnake(int snake[Row_max][Line_max]); //设置蛇
void Creatfood(char map[Row_max][Line_max], int snake[Row_max][Line_max]);//设置食物
bool Judgewall(int Head_y, int Head_x); //判断是否撞墙
void printmap(char map[Row_max][Line_max], int snake[Row_max][Line_max]);//打印地图和蛇
void Late();
void Run(char map[Row_max][Line_max], int snake[Row_max][Line_max]);//运行
void Movetail(); //移动尾巴
bool Eatfood(char map[Row_max][Line_max]); //判断是否吃到食物
void Result(); //初识页面

void Creatmap(char map[Row_max][Line_max]) {//画地图,分别画两行 # 号
for (int i = 0; i <= Line_max - 1; ++i){
map[Row_max - 1][i] = '#';
map[0][i] = '#';
}
for (int i = 1; i <= Row_max - 2; ++i) {
map[i][0] = '#';
map[i][Line_max - 1] = '#';
}
}

void Creatsnake(int snake[Row_max][Line_max]) {//画蛇
int n = de_lenth;
for (int i = Head_y; i >= Head_y - 5; --i) {
snake[i][Head_x] = n--;
if (i == Head_y - 5) {//给蛇尾巴赋位置
Tail_y = i;
Tail_x = Head_x;
}
}
}

void Creatfood(char map[Row_max][Line_max], int snake[Row_max][Line_max]) {//肯定减2啊傻逼
do{//随机数生成食物位置
food_x = rand() % (Line_max - 2) + 1;
food_y = rand() % (Row_max - 2) + 1;
} while (snake[food_y][food_x] != 0||map[food_y][food_x]=='#');//不能出现在蛇的身上,或在地图边
map[food_y][food_x] = '*';
}

bool Eatfood(char map[Row_max][Line_max]) {//判断是否吃到食物
if (map[Head_y][Head_x] == '*'){
map[Head_y][Head_x] = ' ';
return true;
}
else return false;
}

bool Judgewall(int Head_y,int Head_x) {//判断是否撞墙

if (Head_x == 0 || Head_x == Line_max - 1 || Head_y == 0 || Head_y == Row_max - 1)
return false;
else return true;
}

void printmap(char map[Row_max][Line_max], int snake[Row_max][Line_max]) {

for (int i = 0; i <= Row_max-1; ++i) {
for (int j = 0; j <= Line_max-1; ++j) {
//地图和蛇一起打印
if (snake[i][j] == 0)cout << map[i][j];
else {
if (snake[i][j] == Head_v)cout << "Q";
else cout << "*";
}//分别打印0,#,Q
}
cout << endl;
}
}

void Movetail() {//移动尾巴,下一步使尾巴变成0 

if (snake[Tail_y][Tail_x] + 1 == snake[Tail_y + 1][Tail_x]) {
snake[Tail_y][Tail_x] = 0;
++Tail_y;
}
else if (snake[Tail_y][Tail_x] + 1 == snake[Tail_y - 1][Tail_x]) {
snake[Tail_y][Tail_x] = 0;
--Tail_y;
}
else if (snake[Tail_y][Tail_x] + 1 == snake[Tail_y][Tail_x + 1]) {
snake[Tail_y][Tail_x] = 0;
++Tail_x;
}
else {
snake[Tail_y][Tail_x] = 0;
--Tail_x;
}
}

void Result() {

system("cls");//清屏操作
cout << "Game Over!" << endl;
cout << "Your score is " << score * 10 << " !" << endl << "Press any key to exit!" << endl;
getch();
}

void Run(char map[Row_max][Line_max], int snake[Row_max][Line_max]) {//蛇的移动

/*
上 -32 0xffffffe0 72 H
下 -32 0xffffffe0 80 P
左 -32 0xffffffe0 75 K
右 -32 0xffffffe0 77 M
*/
//方向键为组合键
char ch, sh;
while (1) {
if (Judgewall(Head_y,Head_x)) {
if (kbhit()) {//kbhit()函数判断键盘是否敲击,头文件"conio.h"
ch=getch();
if (ch == -32) {
sh=getch();
if (direct == 'a') {
if (sh == 'M')continue;
}
else if (direct == 'd') {
if (sh == 'K')continue;
}
else if (direct == 'w') {
if (sh == 'P')continue;
}
else {
if (sh == 'H')continue;
}
//转头
switch (sh) {
case'H':direct = 'w';
break;
case'P':direct = 's';
break;
case'K':direct = 'a';
break;
case 'M':direct = 'd';
break;
}
}
else {
if (direct == 'a') {
if (ch == 'd' || ch == 'D')continue;
}
else if (direct == 'd') {
if (ch == 'a' || ch == 'A')continue;
}
else if (direct == 'w') {
if (ch == 's' || ch == 'S')continue;
}
else {
if (ch == 'w' || ch == 'W')continue;
}
switch (ch) {
case 'a':case 'A': direct = 'a'; break;
case 'w':case 'W': direct = 'w'; break;
case 's':case 'S': direct = 's'; break;
case 'd':case 'D': direct = 'd'; break;
}

}
}
//蛇动一步
if (direct == 'a') {
if (snake[Head_y][Head_x - 1] != 0)return;//吃到自己

--Head_x;
if (Eatfood(map)) {//如果吃到了食物,尾巴不动,并加分数
++score;
snake[Head_y][Head_x] = ++Head_v;
Creatfood(map, snake);
}
else {//没吃到,只移动头和尾巴
snake[Head_y][Head_x] = ++Head_v;
Movetail();
}
}
else if (direct == 'd') {
if (snake[Head_y][Head_x + 1] != 0)return;

++Head_x;
if (Eatfood(map)) {
++score;
snake[Head_y][Head_x] = ++Head_v;
Creatfood(map, snake);
}
else {
snake[Head_y][Head_x] = ++Head_v;
Movetail();
}
}
else if (direct == 'w') {
if (snake[Head_y - 1][Head_x] != 0)return;

--Head_y;
if (Eatfood(map)) {
++score;
snake[Head_y][Head_x] = ++Head_v;
Creatfood(map, snake);
}
else {
snake[Head_y][Head_x] = ++Head_v;
Movetail();
}
}
else {
if (snake[Head_y + 1][Head_x] != 0)return;

++Head_y;
if (Eatfood(map)) {
++score;
snake[Head_y][Head_x] = ++Head_v;
Creatfood(map, snake);
}
else {
snake[Head_y][Head_x] = ++Head_v;
Movetail();
}
}
//gotoxy(30, 30);

//清屏
system("cls");
//打印一次
printmap(map, snake);
//延时
Late();
}
else return;
}
}

void Late() {//延时函数
for (int i = 0; i <= pow(18, timen); ++i)
;
}

int main() {

cout << "Press any key to start this game!" << endl;
getch();
Creatsnake(snake);
Creatmap(map);
Creatfood(map, snake);
Run(map, snake);
Result();
}

这是一条关于贪吃蛇小游戏的代码,但内容且十分简单,主要包括几个数组的调控,以及键盘输入对数组的影响。并涉及一些DOS命令的知识。简单地说是一个很不错的基础代码。但其中也涉及到多个库函数,一些不常见的库函数也说明笔者有良好的编程基础。但是,毕竟是简易版小游戏,优化不够彻底,部分地方偷工减料,单核心的地方,键盘的输入与数组的交互还是体现出来了。那么下面说一说我发现的几个点,希望能帮助大家的理解。

先说优点吧,

第一,基础库函数充足,基础知识丰富,有很对地方都值得我们借鉴。比如,conio.h头文件,就是整个程序的核心,但运用起来非常的简单方便,主要有做kbhit()、get()函数,取得输入信息。#pragma warning(disable:4996)的使用也有所查询,但不是特别的理解,根据其他作者的说法,我理解为解决一些配饰原因的警告灯问题。

第二,排版分布非常合理。以上是我减少了不必要的行数,原版的排版十分清晰,层次分明。首先他把全局变量定义,后声明函数,然后大致按顺序输入函数体,最后main运行。思路清晰,值得学习,减少了不必要的麻烦。并且在关键的地方进行注释。模块化思想体现的很好,虽然没有建立新的模块,因为没必要,但在一个模块中有单独的分布。

第三,模式值得我们学习。上述代码属于以函数体为主要内容的模式,减少了main的函数体,增大了整部程序的灵活性,我个人十分喜欢这样的模式,而且我认为这样更偏向与面向对象的程序,方便将来的处理更加复杂的代码。

第四,DOS命令的使用。方便快捷,而且方便简单的优化。

第五,对细节的处理。进行了简单的测试。尝试了按当前方向键和反方向键,都没有发生错误正常的运行。还是依赖于Run()函数,中间主要依赖于多次的 if 判定和 switch 的选择。但同时增加了过多运行时间。

关于不足的地方,(本人能力不能和这位大佬比较,只是一些小小的看法)

第一,毕竟还是一个简易版本,并不是说编程者能力不足,主要是为了我们这样的新手考虑,没有复杂的操作,所以优化就略显不足,或者说还有很大的上升空间。比较关键的是每次使用清屏操作会有闪屏出现,我认为这点可以通过其他的简单方式弥补。列入使用蛇身的重建,方法有些繁琐,在这里我就不写了。

第二,基础速度过快。游戏中蛇的移速过快,没有难度的调整。建议增减难度调整。代码如下:

void Late() {//延时函数
int n;
if(a==1)n=16;
else if(a==2)n=14;
else if(a==3)n=12;
for (int i = 0; i <= pow(n, timen); ++i);
}

int main() {
cout << "请选择难度。1:简单,2:中等。3:困难" << endl;
cin>>a;
}

总结:代码大部分为基础代码,Run函数为核心代码,比较难理解,建议多留意。经过简单的优化也是一款非常经典的游戏。还有一些其他的经典小游戏,五子棋,俄罗斯方块等,都非常适合我们现阶段的学习。

猜你喜欢

转载自www.cnblogs.com/Kissverter/p/10468365.html