一、程序设计目标
1. 能够统计当次游戏的成绩,并对历史最高分进行记录与展示;
2. 创建一张23*23的地图,四周墙体用"□"实现,中间用"■"实现;
3. 在地图中生成一条蛇,蛇由蛇头和蛇身组成,头为"¤"符号,蛇身为"★"符号;
4. 在地图中的随机位置生成一苹果,由"●"符号实现;
5. 按W、S、A、D四个键代表上、下、左、右四个方向,以此控制蛇头前进的方向,同时蛇身随蛇头方向移动;
6. 当蛇吃到苹果后(蛇头的坐标与苹果的坐标重合),苹果消失,表示其被蛇吃掉,蛇身长度增加1节,得分增加10分,再在地图中的随机位置生成一个新的苹果;
7. 当蛇头撞墙或咬到蛇身时(蛇头的坐标与围墙或蛇身的坐标重合),游戏终止,提示游戏结束;
二、函数模块讲解(附代码)
1. 头文件
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
2. 变量初始化
int wall[30][30]; //创建围墙
int snake[900][2] = { 0 }; //创建蛇
int apple[2] = { 0 }; //创建苹果
int poison[2] = { 0 }; //创建毒药
int ch = 'a'; //创建初始选择变量
int lastch = 'a'; //创建末尾选择变量
int L = 3; //初始蛇长度
int score = 0; //初始得分
int change = 0; //判断蛇头方向是否改变的变量
int HighScore; //历史最高得分
int flag = 0; //记录吃下苹果数目
int apple2[2] = { 0 }; //创建苹果2
char name[5] = { 0 }; //历史得分面板人员名字
3. 光标移动函数
void gotoxy(int x, int y)
{
COORD pos;
pos.X = 2 * x;
pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);//获得控制台光标位置
}
4. 颜色设置函数
void color(int a)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);
}
5. 建立墙体与得分面板
void makewall()
{
int i, j;
for (i = 0; i < 30; i++)
{
for (j = 0; j < 30; j++)
{
wall[i][j] = 1;
}
}
for (i = 0; i < 30; i++)
{
wall[0][i] = 0;
wall[29][i] = 0;
wall[i][0] = 0;
wall[i][29] = 0;
}
color(11);
for (i = 0; i < 30; i++)
{
for (j = 0; j < 30; j++)
{
if (wall[i][j] == 1)
{
gotoxy(i, j);
printf("■");
}
else
{
gotoxy(i, j);
printf("□");
}
}
}
File_out();
gotoxy(33, 5);
color(11);
printf("☆最高记录☆:%s %d", name, HighScore);
color(14);
gotoxy(33, 15);
printf("当前得分:0");
}
6. 初始化蛇、苹果、毒药
void snake_apple_poison()
{
int i;
color(14);
snake[0][0] = 14;
snake[0][1] = 14;
gotoxy(snake[0][0], snake[0][1]);
printf("¤");
for (i = 1; i < L; i++)
{
snake[i][0] = snake[0][0] + i;
snake[i][1] = snake[0][1];
gotoxy(snake[i][0], snake[i][1]);
printf("★");
}
color(12);
srand((unsigned)time(NULL));
apple[0] = (rand() * 2) % 28 + 1;
apple[1] = (rand() * 3) % 28 + 1;
int j;
for (j = 1; j < L; j++)
{
if (apple[0] == snake[j][0] && apple[1] == snake[j][1])
{
srand((unsigned)time(NULL));
apple[0] = (rand() * 2) % 28 + 1;
apple[1] = (rand() * 3) % 28 + 1;
}
}
gotoxy(apple[0], apple[1]);
printf("●");
color(10);
srand((unsigned)time(NULL));
poison[0] = (rand() * 5) % 28 + 1;
poison[1] = (rand() * 7) % 28 + 1;
int k;
for (k = 1; k < L; k++)
{
if ((poison[0] == snake[k][0] && poison[1] == snake[k][1]) || (poison[0] == apple[0] && poison[1] == apple[1]))
{
srand((unsigned)time(NULL));
poison[0] = (rand() * 5) % 28 + 1;
poison[1] = (rand() * 7) % 28 + 1;
}
}
gotoxy(poison[0], poison[1]);
printf("●");
}
7. 蛇移动函数
void movesnake()
{
if (_kbhit())
{
ch = _getch();
if ((ch != 'w') && (ch != 'a') && (ch != 's') && (ch != 'd'))
ch = lastch;
}
if (!change)
{
gotoxy(snake[L - 1][0], snake[L - 1][1]);
color(11);
printf("■");
}
for (int i = L - 1; i > 0; i--)
{
snake[i][0] = snake[i - 1][0];
snake[i][1] = snake[i - 1][1];
}
if (lastch == 'w' && ch == 's')
ch = 'w';
if (lastch == 's' && ch == 'w')
ch == 's';
if (lastch == 'a' && ch == 'd')
ch = 'a';
if (lastch == 'd' && ch == 'a')
ch = 'd';
switch (ch)
{
case'w':
snake[0][1]--;
break;
case's':
snake[0][1]++;
break;
case'a':
snake[0][0]--;
break;
case'd':
snake[0][0]++;
break;
}
gotoxy(snake[0][0], snake[0][1]);
color(14);
printf("¤");
for (int i = 1; i < L - 1; i++)
{
gotoxy(snake[