void AddSnake()//游戏控制增加、减少长度
{
int key1, key2;
while (1) {
if (key1 = _getch()){
key2 = _getch();
}
switch (key2){
case 79:
// int i = 0;
for (int i = 1; i < 101; i++) {
snake.size++;
result += 100;
}
//存储蛇尾的坐标
lastX = snake.body[snake.size - 1].X;
lastY = snake.body[snake.size - 1].Y;
//蛇移动,前一节身体给后一节身体赋值
for (int i = snake.size - 1; i > 0; i--) {
snake.body[i].X = snake.body[i - 1].X;
snake.body[i].Y = snake.body[i - 1].Y;
}
snake.body[0].X += 100;
snake.body[0].Y += 100;
Sleep(speed);
break;
case 76:
for (int i = 1; i < 6; i--) {
snake.size--;
result -= 5;
}
//存储蛇尾的坐标
lastX = snake.body[snake.size - 1].X;
lastY = snake.body[snake.size - 1].Y;
//蛇移动,前一节身体给后一节身体赋值
for (int i = snake.size - 1; i > 0; i--) {
snake.body[i].X = snake.body[i - 1].X;
snake.body[i].Y = snake.body[i - 1].Y;
}
snake.body[0].X += 10;
snake.body[0].Y += 10;
Sleep(speed);
}
}
C语言写贪吃蛇,按键:P:长度加10,O:长度加100,L:长度减5;写了为什么没有执行啊?
不能增加长度也不能较少长度。救命!!