忽然心血来潮,自己写了一个贪吃蛇的程序,后面有彩蛋偶!
按照习惯,我先使用宏定义定义了一些内容
#define UP 72 // 向上方向键 72
#define DOWN 80 // 向下方向键 80
#define RIGHT 77 // 向右方向键 77
#define LEFT 75 // 向左方向键 75
#define Enter 13 //回车键 13
#define null 32 //空格键
按照常规,我先定义一下我的蛇,还有食物,这了需要控制台输出句柄,定义坐标值
struct Point { //定义坐标
int x, y;
};
struct Food{ //食物的结构体定义
struct Point position; //食物的位置
int addFood; //判断是否要出现食物的变量
int direction; //食物的方向
}food;
struct Snake {
struct Point nodes[MAX_LENGTH]; //蛇身数组,保存每节蛇身的坐标
int length; //当前蛇长
int direction; //蛇头运动方向
} snake;
接着我写出我的菜单函数,因为是控制台程序,为了不让他显得太low,就伪造一个界面,结果是这样的
是根据光标的移动来选择操作,根据菜单函数的返回值,来决定下一步操作。
switch (menu()) //游戏选项
{
case 0:
gamePlay();
break;
case 1:
Setspeed();
break;
case 2:
gamerand();
break;
case 3:
close();
break;
}
其中有一个gamerand();函数,这是其中的彩蛋!我之后会提到。
接着我就要框出我的游戏范围,这个用whlie循环就好,我就不多说了,还有就是画蛇,之前蛇的定义为数组形式,这里用循环也可以画出来,我认为最主要的环节就是让蛇动起来,我采用的方式是画出新的蛇头,擦出旧的蛇尾,
void moveSnake(){
lastx = snake.nodes[snake.length - 1].x;
lasty = snake.nodes[snake.length - 1].y;
gotoxy(lastx, lasty);
printf("%2s","");
for (int k = snake.length - 1; k > 0; k--)
{
snake.nodes[k ].x = snake.nodes[k-1].x;
snake.nodes[k ].y = snake.nodes[k-1].y;
}
switch (snake.direction)
{
case UP:snake.nodes[0].y -= 1; break;
case DOWN:snake.nodes[0].y += 1; break;
case LEFT:snake.nodes[0].x -= 1; break;
case RIGHT:snake.nodes[0].x += 1; break;
}
SetConsoleTextAttribute(hout, FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoxy(snake.nodes[0].x, snake.nodes[0].y);
printf("■");
SetConsoleTextAttribute(hout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
gotoxy(snake.nodes[1].x, snake.nodes[1].y);
printf("■");
}
画出游戏区域和范围之后我就要画出我的食物了,在创造食物的过程中我使用了rand函数,来随机出一个食物的位置,判断食物是否在边界,或者蛇身上,然后画出食物的位置。
void createFood(){
int i;
label:
food.position.x = left + 10 * ((int)rand() % 11);
food.position.y = top + 10 * ((int)rand() % 11);
for (i = 0; i
{
if (snake.nodes[i].x == food.position.x&&snake.nodes[i].y == food.position.y||food.position.x<=left+4||food.position.x>=right-4||food.position.y<=top+4||food.position.y>=bottom-4)
{
goto label;
}
}
}
其中我的蛇的速度是用Sleep函数实现的,还有一个功能是设置速度,我使用的是全局变量,
int Setspeed(){
system("cls");
system("color 3c");
gotoxy(15, 9);
printf("┏━━━━━━━┓");
gotoxy(15, 10);
printf("┃%2s%s%2s┃", "", "请输入速度", "");
gotoxy(15,11);
printf("┃%2s%8s%4s┃", "", "(1-10)", "");
gotoxy(15, 12);
printf("┃%2s%10s%2s┃", "", "", "");
gotoxy(15,13);
printf("┗━━━━━━━┛");
gotoxy(19, 12);
scanf_s("%d", &SET_SPEED);
return 0;
}
其他的我认为就没有什么难点了,下来我们接着说之前的那个gamerand函数,只是我定义的一个可以根据你吃的食物的增多,来增大游戏难度,主要是提高蛇的移动速度,在游戏区域内增加新的墙,来增大游戏难度,新墙的生成我也使用到了rand函数,来随机墙的X,Y。其中也要判断新的墙是否在游戏范围内,还要不在蛇身上,食物上以及不在已经生成的墙上,这了就要用到多次判断,如果重叠就重新生成。
void RAND(){
for (; dw < DRAWWALL.much; dw++){
L1: DRAWWALL.wall[dw].x = (left +( 4 * rand() % 10)*4);
DRAWWALL.wall[dw].y = (top + (3 * rand() % 10)*5);
for (int i = 0; i < snake.length; i++)
{
if (DRAWWALL.wall[dw].x == snake.nodes[i].x && DRAWWALL.wall[dw].y == snake.nodes[i].y) goto L1;
}
if (DRAWWALL.wall[dw].x <= left || DRAWWALL.wall[dw].x >= right || DRAWWALL.wall[dw].y <= top || DRAWWALL.wall[dw].y >= bottom) goto L1;
for (int j = 0; j < dw; j++){
if (DRAWWALL.wall[j].x == DRAWWALL.wall[dw].x&&DRAWWALL.wall[j].y == DRAWWALL.wall[dw].y) goto L1;
}
}
}
void ADDwall(){
SetConsoleTextAttribute(hout, BACKGROUND_RED | BACKGROUND_BLUE);
gotoxy(27, 1);
printf("%-4d", level);
DRAWWALL.much += 5;
SetConsoleTextAttribute(hout, FOREGROUND_RED);
RAND();
for (dw-=5; dw< level * 5; dw++)
{
gotoxy(DRAWWALL.wall[dw].x, DRAWWALL.wall[dw].y);
printf("■");
}
level++;
}
最后,我想在贪吃蛇中加点好玩的,让食物也动起来,我写了食物的移动函数
void fooddir(){
int i;
i = ((rand() * 11) % 4);
switch (i)
{
case 0: food.direction = UP; break;
case 1:food.direction = DOWN; break;
case 2:food.direction = LEFT; break;
case 3:food.direction = right; break;
default:
break;
}
}
void movefood(){
lastfx = food.position.x;
lastfy = food.position.y;
label: if (food.position.x<=left+2||food.position.x>=right-2||food.position.y<=top+2||food.position.y>=bottom-2)
{
fooddir();
goto label;
}
for (int i = 0; i < snake.length;i++){
if (food.position.x == snake.nodes[i].x&&food.position.y == snake.nodes[i].y)
{
fooddir();
goto label;
}
}
switch (food.direction)
{
case UP:food.position.y -= 1; break;
case DOWN:food.position.y += 1; break;
case LEFT:food.position.x -= 1; break;
case RIGHT:food.position.x += 1; break;
}
}
这样我的贪吃蛇就和以前的不一样了。
最后吧源码贡献给大家,这是不带食物移动的,如果需要可以自行将以上内容添加进去
#include
#include
#include
#include
#include"resource.h"
/*-----------------------------------------------------------------------------宏定义*/
#define TRUE 1
#define FALSE 0
#define ESC 27
#define UP 72 // 向上方向键 72
#define DOWN 80 // 向下方向键 80
#define RIGHT 77 // 向右方向键 77
#define LEFT 75 // 向左方向键 75
#define Enter 13 //回车键 13
#define null 32
#define MAX_LENGTH 100 //蛇身最大长度
#define top 2
#define left 2
#define bottom 32
#define right 35
#define GAME_SPEED 15
#define MAX_ADD 200
/*----------------------------------------------------------------------------全局变量*/
HANDLE hout;
int SET_SPEED = 1;
int Score = 0;
int level = 1;
int keyCode;
int lastx, lasty;
int lastfx, lastfy;
int dw=0;
/*-------------------------------------------------------------------------结构体声明*/
struct Point { //定义坐标
int x, y;
};
struct Snake {
struct Point nodes[MAX_LENGTH]; //蛇身数组,保存每节蛇身的坐标
int length; //当前蛇长
int direction; //蛇头运动方向
} snake;
struct Wall{
struct Point wall[MAX_ADD];
int much;
}DRAWWALL;
/*---------------------------------------------------------------------------函数声明*/
int gamePlay(void); //游戏主循环
void init(void); //初始化
int menu(void); //主菜单
int Setspeed(void); //设置速度
void createFood(void); //创造一个食物
void drawWall(void); //画墙
void drawFood(void); //画出食物
void drawSnake(void); //画出蛇
void drawScore(void); //画出分数
int touchWall(void); //判断是否碰到墙
int touchSelf(void); //判断是否碰到自己
void button(void); //键盘输入
void moveSnake(void); //移动蛇
int oppositeDirection(int keyCode); //判断是否方向有误
int foodEat(void); //判断是否吃到食物
void expandSnake(void); //把蛇增长一节
int gameOver(void); //游戏结束
void close(void); //关闭游戏
void gotoxy(int, int);
void gamerand(); //挑战模式
void scorerand();
void RAND(); //随机函数
void ADDwall(); //增加墙
int touchAdd();
int randover();
/*-----------------------------------------------------------------------------主函数*/
int main(){
hout = GetStdHandle(STD_OUTPUT_HANDLE); // 获取控制台输出句柄
SetConsoleTitleA("贪吃蛇————LSX");
CONSOLE_CURSOR_INFO cursorInfo = { 1, FALSE }; //设置
SetConsoleCursorInfo(hout, &cursorInfo); // 设置光标隐藏
while (1)
{
init();
//system("color 50");
switch (menu()) //游戏选项
{
case 0:
gamePlay();
break;
case 1:
Setspeed();
break;
case 2:
gamerand();
break;
case 3:
close();
break;
}
}
}
/*-----------------------------------------------------------------------------主菜单*/
int menu(){
int ch, index = 0;
static const char *modeItem[] = { "→开始游戏", "→设置速度", "→挑战模式", "→退出" };
system("cls");
drawWall();
SetConsoleTextAttribute(hout, FOREGROUND_GREEN);
gotoxy(15, 4);
printf("┏━━━━━━━┓");
gotoxy(15, 5);
printf("┃%2s%s%2s┃", "", "■贪吃蛇■", "");
gotoxy(15, 6);
printf("┃%2s%10s%2s┃", "", "", "");
gotoxy(15, 7);
printf("┃%2s%s%2s┃", "", "★刘少雄★", "");
gotoxy(15, 8);
printf("┗━━━━━━━┛");
SetConsoleTextAttribute(hout, 0xF0);
gotoxy(16, 14);
printf("%2s%s%2s", "", modeItem[0], "");
SetConsoleTextAttribute(hout, 0x0F);
gotoxy(16, 16);
printf("%2s%s%2s", "", modeItem[1], "");
SetConsoleTextAttribute(hout, 0x0F);
gotoxy(16, 18);
printf("%2s%s%2s", "", modeItem[2], "");
SetConsoleTextAttribute(hout, 0x0F);
gotoxy(16, 20);
printf("%2s%s%2s", "", modeItem[3], "");
do
{
ch = _getch();
switch (ch)
{
case 's': case 'S': case '2': case 80: // 下
if (index == 0)
{
index = 1;
SetConsoleTextAttribute(hout, 0x0F);
gotoxy(16, 14);
printf("%2s%s%2s", "", modeItem[0], "");
SetConsoleTextAttribute(hout, 0xF0);
gotoxy(16, 16);
printf("%2s%s%2s", "", modeItem[1], "");
}
else if (index == 1)
{
index = 2;
SetConsoleTextAttribute(hout, 0x0F);
gotoxy(16, 16);
printf("%2s%s%2s", "", modeItem[1], "");
SetConsoleTextAttribute(hout, 0xF0);
gotoxy(16, 18);
printf("%2s%s%2s", "", modeItem[2], "");
}
else if (index == 2)
{
index = 3;
SetConsoleTextAttribute(hout, 0xF0);
gotoxy(16, 20);
printf("%2s%s%2s", "", modeItem[3], "");
SetConsoleTextAttribute(hout, 0x0F);
gotoxy(16, 18);
printf("%2s%s%2s", "", modeItem[2], "");
}
else if (index == 3)
{
index = 0;
SetConsoleTextAttribute(hout, 0xF0);
gotoxy(16, 14);
printf("%2s%s%2s", "", modeItem[0], "");
SetConsoleTextAttribute(hout, 0x0F);
gotoxy(16, 20);
printf("%2s%s%2s", "", modeItem[3], "");
}
break;
case 'w': case 'W': case '8': case 72: // 上
if (index == 0)
{
index = 3;
SetConsoleTextAttribute(hout, 0x0F);
gotoxy(16, 14);
printf("%2s%s%2s", "", modeItem[0], "");
SetConsoleTextAttribute(hout, 0xF0);
gotoxy(16, 20);
printf("%2s%s%2s", "", modeItem[3], "");
}
else if (index == 1)
{
index = 0;
SetConsoleTextAttribute(hout, 0xF0);
gotoxy(16, 14);
printf("%2s%s%2s", "", modeItem[0], "");
SetConsoleTextAttribute(hout, 0x0F);
gotoxy(16, 16);
printf("%2s%s%2s", "", modeItem[1], "");
}
else if (index == 2)
{
index = 1;
SetConsoleTextAttribute(hout, 0xF0);
gotoxy(16, 16);
printf("%2s%s%2s", "", modeItem[1], "");
SetConsoleTextAttribute(hout, 0x0F);
gotoxy(16, 18);
printf("%2s%s%2s", "", modeItem[2], "");
}
else if (index == 3)
{
index = 2;
SetConsoleTextAttribute(hout, 0xF0);
gotoxy(16, 18);
printf("%2s%s%2s", "", modeItem[2], "");
SetConsoleTextAttribute(hout, 0x0F);
gotoxy(16, 20);
printf("%2s%s%2s", "", modeItem[3], "");
}
break;
case ' ': case 13:
return index;
break;
}
}
while (1);
}
/*-----------------------------------------------------------------------------初始化*/
void init(){
snake.nodes[0].x = 16;
snake.nodes[0].y = 16;
snake.nodes[1].x = 16;
snake.nodes[1].y = 17;
snake.direction = UP;
snake.length = 2;
Score = 0;
food.addFood = 0;
}
/*------------------------------------------------------------------------------画墙*/
void drawWall(){
int i;
for ( i = 3; i < right; i++)
{
SetConsoleTextAttribute(hout, FOREGROUND_RED);
gotoxy(i, 2);
printf("━━");
gotoxy(i, 32);
printf("━━");
}
gotoxy(2, 2);
printf("┏");
gotoxy(2, 32);
printf("┗");
gotoxy(35, 2);
printf("┓");
gotoxy(35, 32);
printf("┛");
for ( i = 3; i 2, i);
printf("┃");
gotoxy(35, i);
printf("┃");
}
}
/*--------------------------------------------------------------------------设置速度*/
int Setspeed(){
system("cls");
system("color 3c");
gotoxy(15, 9);
printf("┏━━━━━━━┓");
gotoxy(15, 10);
printf("┃%2s%s%2s┃", "", "请输入速度", "");
gotoxy(15,11);
printf("┃%2s%8s%4s┃", "", "(1-10)", "");
gotoxy(15, 12);
printf("┃%2s%10s%2s┃", "", "", "");
gotoxy(15,13);
printf("┗━━━━━━━┛");
gotoxy(19, 12);
scanf_s("%d", &SET_SPEED);
return 0;
}
/*--------------------------------------------------------------------------位置定义*/
void gotoxy(int x, int y)
{
static COORD cd;
cd.X = (int)(x << 1);
cd.Y = y;
SetConsoleCursorPosition(hout, cd);
}
/*------------------------------------------------------------------------游戏主循环*/
int gamePlay(){
int ch;
system("cls");
//system("color 50");
drawScore(); //输出分数
drawWall(); //画墙
drawSnake();
createFood();
food.addFood = 1;
drawFood();
fooddir();
do
{
if (touchWall() || touchSelf()){
gameOver();
return 0;
}
if (!food.addFood)
{
createFood();
food.addFood = 1;
drawFood();
}
for (int d = (22 - SET_SPEED*2); d >= 0; d--)
{
Sleep(GAME_SPEED);
}
if (_kbhit())
{
button();
if (keyCode = ESC)
{
gameOver;
}
if (keyCode == null){
_getch();
}
}
moveSnake();
if (foodEat()){
food.addFood = FALSE;
Score += 1;
drawScore();
expandSnake();
}
} while (1);
}
/*----------------------------------------------------------------------创造一个食物*/
void createFood(){
int i;
label:
food.position.x = left + 10 * ((int)rand() % 11);
food.position.y = top + 10 * ((int)rand() % 11); //创造食物,利用rand随机函数
for (i = 0; i length ; i++)
{
if (snake.nodes[i].x == food.position.x&&snake.nodes[i].y == food.position.y||food.position.x<=left+4||food.position.x>=right-4||food.position.y<=top+4||food.position.y>=bottom-4)//判断食物是否存在
{
goto label;
}
}
}
/*-------------------------------------------------------------------------画出食物*/
void drawFood(){
SetConsoleTextAttribute(hout, FOREGROUND_GREEN);
gotoxy(food.position.x, food.position.y);
printf("■");
gotoxy(lastfx, lastfy);
printf(" ");
}
/*--------------------------------------------------------------------------画出蛇*/
void drawSnake(){
SetConsoleTextAttribute(hout, FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoxy(snake.nodes[0].x, snake.nodes[0].y);
printf("■");
SetConsoleTextAttribute(hout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
gotoxy(snake.nodes[snake.length - 1].x, snake.nodes[snake.length - 1].y);
printf("■");
}
/*------------------------------------------------------------------------画出分数*/
void drawScore(){
SetConsoleTextAttribute(hout, BACKGROUND_RED | BACKGROUND_BLUE);
gotoxy(15, 1);
printf("%6s%-4d","Score:",Score);
}
/*------------------------------------------------------------------------分数等级*/
void scorerand(){
SetConsoleTextAttribute(hout, BACKGROUND_RED | BACKGROUND_BLUE);
gotoxy(15, 1);
printf("%6s%4d\t%6s%4d", "Score:", Score, "Level:", level);
}
/*------------------------------------------------------------------判断是否碰到墙*/
int touchWall(){
int x1 = snake.nodes[0].x;
int y1 = snake.nodes[0].y;
if (x12 || x1>right-2 || y12 || y1>bottom-2)
{
return TRUE;
}
else
{
return FALSE;
}
}
/*----------------------------------------------------------------判断是否碰到自己*/
int touchSelf(){
for (int i = 1; i < snake.length; i++)
{
if (snake.nodes[0].x == snake.nodes[i].x&&snake.nodes[0].y == snake.nodes[i].y)
{
return TRUE;
}
}
return FALSE;
}
/*------------------------------------------------------------------------键盘输入*/
void button(){
keyCode = (_getch()); //将按键从控制台中取出并保存到key中
if (keyCode == UP || keyCode == DOWN || keyCode == LEFT || keyCode == RIGHT ){
lastx = snake.nodes[snake.length - 1].x;
lasty = snake.nodes[snake.length - 1].y;
if (oppositeDirection(keyCode))
{
snake.direction = keyCode;
}
}
}
/*-------------------------------------------------------------------------移动蛇*/
void moveSnake(){
lastx = snake.nodes[snake.length - 1].x;
lasty = snake.nodes[snake.length - 1].y;
gotoxy(lastx, lasty);
printf("%2s","");
for (int k = snake.length - 1; k > 0; k--)
{
snake.nodes[k ].x = snake.nodes[k-1].x;
snake.nodes[k ].y = snake.nodes[k-1].y;
}
switch (snake.direction)
{
case UP:snake.nodes[0].y -= 1; break;
case DOWN:snake.nodes[0].y += 1; break;
case LEFT:snake.nodes[0].x -= 1; break;
case RIGHT:snake.nodes[0].x += 1; break;
}
SetConsoleTextAttribute(hout, FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoxy(snake.nodes[0].x, snake.nodes[0].y);
printf("■");
SetConsoleTextAttribute(hout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
gotoxy(snake.nodes[1].x, snake.nodes[1].y);
printf("■");
} //移动蛇身
/*---------------------------------------------------------------判断是否方向有误*/
int oppositeDirection(int keyCode) {
if (keyCode == DOWN && snake.direction == DOWN){
return 0;
}
else if (keyCode == UP && snake.direction == UP) {
return 0;
}
else if (keyCode == RIGHT && snake.direction == RIGHT){
return 0;
}
else if (keyCode == LEFT && snake.direction == LEFT){
return 0;
}
if (keyCode == DOWN && snake.direction == UP){
return 0;
}
else if (keyCode == UP && snake.direction == DOWN) {
return 0;
}
else if (keyCode == RIGHT && snake.direction == LEFT){
return 0;
}
else if (keyCode == LEFT && snake.direction == RIGHT){
return 0;
}
else
return 1;
}
/*---------------------------------------------------------------判断是否吃到食物*/
int foodEat(){
if (snake.nodes[0].x == food.position.x&&snake.nodes[0].y == food.position.y)
{
return TRUE;
}
return FALSE;
} //判断食物是否被吃
/*------------------------------------------------------------------把蛇增长一节*/
void expandSnake() {
if (keyCode == UP && snake.direction != DOWN){
snake.direction = UP;
lastx -= 1;
}
else if (keyCode == DOWN && snake.direction != UP){
snake.direction = DOWN;
lastx += 1;
}
else if (keyCode == LEFT && snake.direction != RIGHT){
snake.direction = LEFT;
lasty -= 1;
}
else if (keyCode == RIGHT && snake.direction != LEFT){
snake.direction = RIGHT;
lasty += 1;
}
snake.nodes[snake.length].x = lastx;
snake.nodes[snake.length].y = lasty;
snake.length++;
}
/*---------------------------------------------------------------------游戏结束*/
int gameOver(){
SetConsoleTextAttribute(hout, FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoxy(15, 13);
printf("┏━━━━━━━━┓");
gotoxy(15,14);
printf("┃%3s%s%4s┃", "", "GAME OVER", "");
gotoxy(15, 15);
printf("┃%1s%s%1s┃", "", "Your Score is:", "");
gotoxy(15, 16);
printf("┃%6s%4d%6s┃", "", Score, "");
gotoxy(15, 17);
printf("┗━━━━━━━━┛");
if ((_getch(keyCode))== ESC)
{
close();
}
else
{
return 0;
}
}
/*---------------------------------------------------------------------游戏结束*/
int randover(){
SetConsoleTextAttribute(hout, FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoxy(15, 13);
printf("┏━━━━━━━━┓");
gotoxy(15, 14);
printf("┃%3s%s%4s┃", "", "GAME OVER", "");
gotoxy(15, 15);
printf("┃%1s%s%1s┃", "", "Your Score is:", "");
gotoxy(15, 16);
printf("┃%6s%4d%6s┃", "", Score, "");
gotoxy(15, 17);
printf("┃%1s%s%1s┃", "", "Your Level is:", "");
gotoxy(15, 18);
printf("┃%6s%4d%6s┃", "", level, "");
gotoxy(15, 19);
printf("┗━━━━━━━━┛");
if ((_getch(keyCode)) == ESC)
{
close();
}
else
{
return 0;
}
}
/*---------------------------------------------------------------------关闭游戏*/
void close(){
exit(1);
}
/*-------------------------------------------------------------------------挑战模式*/
void gamerand(){
DRAWWALL.much = 0;
int ch;
system("cls");
system("color 0c");
SET_SPEED = 1;
scorerand(); //输出分数
drawWall(); //画墙
drawSnake();
createFood();
food.addFood = 1;
drawFood();
fooddir();
do
{
if (touchWall() || touchSelf()||touchAdd()){
randover();
return 0;
}
if (!food.addFood)
{
createFood();
food.addFood = 1;
drawFood();
}
for (int d = (22 - SET_SPEED * 2); d >= 0; d--)
{
Sleep(GAME_SPEED);
}
if (_kbhit())
{
button();
if (keyCode == ESC){
randover();
}
if (keyCode == null){
_getch();
}
}
moveSnake();
if (foodEat()){
food.addFood = FALSE;
Score += 1;
drawScore();
expandSnake();
if ((Score%5) == 0)
{
SET_SPEED++;
ADDwall();
}
}
} while (1);
}
/*--------------------------------------------------------------------------随机函数*/
void RAND(){
for (; dw < DRAWWALL.much; dw++){
L1: DRAWWALL.wall[dw].x = (left +( 4 * rand() % 10)*4);
DRAWWALL.wall[dw].y = (top + (3 * rand() % 10)*5);
for (int i = 0; i < snake.length; i++)
{
if (DRAWWALL.wall[dw].x == snake.nodes[i].x && DRAWWALL.wall[dw].y == snake.nodes[i].y) goto L1;
}
if (DRAWWALL.wall[dw].x <= left || DRAWWALL.wall[dw].x >= right || DRAWWALL.wall[dw].y <= top || DRAWWALL.wall[dw].y >= bottom) goto L1;
for (int j = 0; j < dw; j++){
if (DRAWWALL.wall[j].x == DRAWWALL.wall[dw].x&&DRAWWALL.wall[j].y == DRAWWALL.wall[dw].y) goto L1;
}
}
}
/*--------------------------------------------------------------------------增加墙*/
void ADDwall(){
SetConsoleTextAttribute(hout, BACKGROUND_RED | BACKGROUND_BLUE);
gotoxy(27, 1);
printf("%-4d", level);
DRAWWALL.much += 5;
SetConsoleTextAttribute(hout, FOREGROUND_RED);
RAND();
for (dw-=5; dw< level * 5; dw++)
{
gotoxy(DRAWWALL.wall[dw].x, DRAWWALL.wall[dw].y);
printf("■");
}
level++;
}
/*----------------------------------------------------------------判断是否碰到新墙*/
int touchAdd(){
int x1 = snake.nodes[0].x;
int y1 = snake.nodes[0].y;
for (int i = 0; i if (x1 == DRAWWALL.wall[i].x && y1==DRAWWALL.wall[i].y)
{
return TRUE;
}
}
return FALSE;
}