C语言贪吃蛇 第二条蛇吃不到食物 问什么

#include"snake1.h"
int result;

void main()
{
GameInit();
while (1)
{
while (!_kbhit()) //判断键盘是否按下
{
if (food.flag0 == 0 || food.flag1 == 0 || food.flag2 == 0)
{
GetFoodCoor();
}
BeginBatchDraw(); //画布,一批一批的
cleardevice();
SnakeMove();
DrawSnake();
EatFood();
DrawFood();
Sleep(100);
EndBatchDraw();
BreakSnake();
}
ChangeSnake();
}
getchar();
}

//设置背景,初始化小蛇
void GameInit()
{
srand((unsigned int)time(NULL)); //随机数因子

initgraph(WINDOW_LENGTH, WINDOW_WIDTH);         //创建窗口
setbkcolor(BGCOLOR);                //设置背景颜色,set backgrand color
cleardevice();                  //使用设置好的背景颜色

snake1.n = 1;               //初始化蛇的节数为1
snake1.ch = down;           //方向 向下
snake1.SnakeCoor[0].x = 0;      //蛇的坐标
snake1.SnakeCoor[0].y = 0;
rectangle(0, 0, SNAKESIZE, SNAKESIZE);

snake2.n = 1;
snake2.ch = up;
snake2.SnakeCoor[0].x = WINDOW_LENGTH - SNAKESIZE-2;
snake2.SnakeCoor[0].y = WINDOW_WIDTH - SNAKESIZE-2;
rectangle(WINDOW_LENGTH - SNAKESIZE - 2, WINDOW_WIDTH - SNAKESIZE - 2, WINDOW_LENGTH  - 2, WINDOW_WIDTH  - 2);

food.flag0 = 0;     //初始化食物刚开始没有
food.flag1 = 0;
food.flag2 = 0;

}

///动起来
void SnakeMove()
{
for (int i = snake1.n-1; i > 0; i--)
{
//只是使其坐标改变,并未画出蛇
//前一个坐标赋给后一个坐标,蛇头没有被赋值
snake1.SnakeCoor[i].x = snake1.SnakeCoor[i - 1].x;
snake1.SnakeCoor[i].y = snake1.SnakeCoor[i - 1].y;
}

for (int i = snake2.n - 1; i > 0; i--)
{
    //只是使其坐标改变,并未画出蛇
    //前一个坐标赋给后一个坐标,蛇头没有被赋值
    snake2.SnakeCoor[i].x = snake2.SnakeCoor[i - 1].x;
    snake2.SnakeCoor[i].y = snake2.SnakeCoor[i - 1].y;
}

//判断方向,定位蛇头
switch (snake1.ch)
{
case up:
{
           snake1.SnakeCoor[0].y -= SNAKESIZE;
           break;
}
case down:
{
             snake1.SnakeCoor[0].y += SNAKESIZE;
             break;
}
case left:
{
             snake1.SnakeCoor[0].x -= SNAKESIZE;
             break;
}
case right:
{
              snake1.SnakeCoor[0].x += SNAKESIZE;
              break;
}

}


switch (snake2.ch)
{
case up:
{
           snake2.SnakeCoor[0].y -= SNAKESIZE;
           break;
}
case down:
{
             snake2.SnakeCoor[0].y += SNAKESIZE;
             break;
}
case left:
{
             snake2.SnakeCoor[0].x -= SNAKESIZE;
             break;
}
case right:
{
              snake2.SnakeCoor[0].x += SNAKESIZE;
              break;
}

}

}

//画出来蛇
void DrawSnake()
{

for (int i = snake1.n - 1; i >= 0; i--)
{
    rectangle(snake1.SnakeCoor[i].x, snake1.SnakeCoor[i].y,
        snake1.SnakeCoor[i].x + SNAKESIZE, snake1.SnakeCoor[i].y + SNAKESIZE);
}


for (int i = snake2.n - 1; i >= 0; i--)
{
    rectangle(snake2.SnakeCoor[i].x, snake2.SnakeCoor[i].y,
        snake2.SnakeCoor[i].x + SNAKESIZE, snake2.SnakeCoor[i].y + SNAKESIZE);
}

}

//按键控制小蛇
void ChangeSnake()
{
int move1;
move1 = _getch();
switch (move1)
{
case up:
{
if (snake1.ch != down)
{
snake1.ch = up;
break;
}
}
case down:
{
if (snake1.ch != up)
{
snake1.ch = down;
break;
}
}
case left:
{
if (snake1.ch != right)
{
snake1.ch = left;
break;
}
}
case right:
{
if (snake1.ch != left)
{
snake1.ch = right;
break;
}
}

case 'w':
{
           if (snake2.ch != down)
           {
               snake2.ch = up;
               break;
           }
}
case 's':
{
             if (snake2.ch != up)
             {
                 snake2.ch = down;
                 break;
             }
}
case 'a':
{
             if (snake2.ch != right)
             {
                 snake2.ch = left;
                 break;
             }
}
case 'd':
{
              if (snake2.ch != left)
              {
                  snake2.ch = right;
                  break;
              }
}

}

}

//得到食物的坐标
void GetFoodCoor()
{ //出来三个食物
if (food.flag0 == 0)
{
food.FoodCoor[0].x = rand() % (WINDOW_LENGTH / SNAKESIZE) * SNAKESIZE;
food.FoodCoor[0].y = rand() % (WINDOW_WIDTH / SNAKESIZE)*SNAKESIZE;
food.flag0 = 1;
}
if (food.flag1 == 0)
{
food.FoodCoor[1].x = rand() % (WINDOW_LENGTH / SNAKESIZE) * SNAKESIZE;
food.FoodCoor[1].y = rand() % (WINDOW_WIDTH / SNAKESIZE)*SNAKESIZE;
food.flag1 = 1;
}
if (food.flag2 == 0)
{
food.FoodCoor[2].x = rand() % (WINDOW_LENGTH / SNAKESIZE) * SNAKESIZE;
food.FoodCoor[2].y = rand() % (WINDOW_WIDTH / SNAKESIZE)*SNAKESIZE;
food.flag2 = 1;
}
}

//画食物
void DrawFood()
{//根据一个矩形画出一个圆 圆角矩阵
roundrect(food.FoodCoor[0].x, food.FoodCoor[0].y,
food.FoodCoor[0].x + SNAKESIZE, food.FoodCoor[0].y + SNAKESIZE,
SNAKESIZE, SNAKESIZE);
roundrect(food.FoodCoor[1].x, food.FoodCoor[1].y,
food.FoodCoor[1].x + SNAKESIZE, food.FoodCoor[1].y + SNAKESIZE,
SNAKESIZE, SNAKESIZE);
roundrect(food.FoodCoor[2].x, food.FoodCoor[2].y,
food.FoodCoor[2].x + SNAKESIZE, food.FoodCoor[2].y + SNAKESIZE,
SNAKESIZE, SNAKESIZE);
}

//吃食物
void EatFood()
{
if (snake1.SnakeCoor[0].x == food.FoodCoor[0].x&&snake1.SnakeCoor[0].y == food.FoodCoor[0].y)
{
snake1.n++; //节数加1
food.flag0 = 0; //食物被吃掉 标志
}
if (snake2.SnakeCoor[0].x == food.FoodCoor[0].x&&snake2.SnakeCoor[0].y == food.FoodCoor[0].y)
{
snake2.n++; //节数加1
food.flag0 = 0; //食物被吃掉 标志
}
if (snake1.SnakeCoor[0].x == food.FoodCoor[1].x&&snake1.SnakeCoor[0].y == food.FoodCoor[1].y)
{
snake1.n++; //节数加1
food.flag1 = 0; //食物被吃掉 标志
}
if (snake2.SnakeCoor[0].x == food.FoodCoor[1].x&&snake2.SnakeCoor[0].y == food.FoodCoor[1].y)
{
snake2.n++; //节数加1
food.flag1 = 0; //食物被吃掉 标志
}
if (snake1.SnakeCoor[0].x == food.FoodCoor[2].x&&snake1.SnakeCoor[0].y == food.FoodCoor[2].y)
{
snake1.n++; //节数加1
food.flag2 = 0; //食物被吃掉 标志
}
if (snake2.SnakeCoor[0].x == food.FoodCoor[2].x&&snake2.SnakeCoor[0].y == food.FoodCoor[2].y)
{
snake2.n++; //节数加1
food.flag2 = 0; //食物被吃掉 标志
}

}

//碰撞检测
void BreakSnake()
{
//蛇头不能撞墙
if (snake1.SnakeCoor[0].xWINDOW_LENGTH || snake1.SnakeCoor[0].yWINDOW_WIDTH)
{
result = 0;
GameOver();
//玩家2胜
}
if (snake2.SnakeCoor[0].xWINDOW_LENGTH || snake2.SnakeCoor[0].yWINDOW_WIDTH)
{
result = 1;
GameOver();
//玩家1胜
}
//两个蛇不能碰撞在一起,自己的头可以撞自己身子
for (int i = snake1.n - 1; i >= 0; i--)
{
if (snake2.SnakeCoor[0].x == snake1.SnakeCoor[i].x&&snake2.SnakeCoor[0].y == snake1.SnakeCoor[i].y)
{
result = 1;
GameOver();
//玩家1胜
}
}
for (int i = snake2.n - 1; i >= 0; i--)
{
if (snake1.SnakeCoor[0].x == snake2.SnakeCoor[i].x&&snake1.SnakeCoor[0].y == snake2.SnakeCoor[i].y)
{
result = 0;
GameOver();
//玩家2胜
}
}
}

void GameOver()
{
cleardevice();
setcolor(BLACK);
settextstyle(50, 0, L"楷体");
if (result == 1)
{
outtextxy(100, 100, L"玩家一获胜");
}
if (result == 0)
{
outtextxy(100, 100, L"玩家二获胜");
}
_getch();
closegraph();
}

头文件
#pragma once
#include
#include //图形库
#include //随机数
#include
#include
//#DEFINE 不加分号
#define WINDOW_WIDTH 540 //宽
#define WINDOW_LENGTH 940 //长
#define BGCOLOR RGB(120,120,220) //背景颜色
#define SNAKEMAX 100 //蛇最大长度
#define SNAKESIZE 10 //蛇的大小

//枚举 列举一些方向
typedef enum Ch { down=80, up=72, left=75, right=77 }CH;

//蛇的坐标
typedef struct coor
{
int x;
int y;
}COOR;

//设置一条蛇的属性
typedef struct Snake
{
int n; //n节小蛇
COOR SnakeCoor[SNAKEMAX]; //最大长度
CH ch; //蛇的方向

}SNAKE;
SNAKE snake1, snake2; //两条蛇

typedef struct Food
{
COOR FoodCoor[3];
int flag0,flag1,flag2;
}FOOD;
FOOD food;

//函数声明
void GameInit();
void SnakeMove();

void DrawSnake();
void ChangeSnake();
void GetFoodCoor();
void DrawFood();
void EatFood();
void BreakSnake();
void GameOver();

查看全部
UNIONDONG
UNIONDONG
2017/06/05 10:06
  • c
  • 图形
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复