初级贪吃蛇

1.实现蛇的移动。

#include  
#include 
#include 

#define SNAKE_MAX_LENGTH 20
#define SNAKE_HEAD 'H'
#define SNAKE_BODY 'X'
#define VLANK_CELL ' '
#define SNAKE_FOOD '$'
#define WALL_SELL '*'
char map[12][12] = {
  "************",
  "*XXXXH     *",
  "*          *",
  "*          *",
  "*          *",
  "*          *",
  "*          *",
  "*          *",
  "*          *",
  "*          *",
  "************",
};

int snakeX[SNAKE_MAX_LENGTH]={1,2,3,4,5};
int snakeY[SNAKE_MAX_LENGTH]={1,1,1,1,1};
int snakeLength = 5;
int gameRunning=1;

void snakeMove(int snakeX[], int snakeY[]){
int headX= snakeX[snakeLength-1];
    int headY= snakeY[snakeLength-1];
char control=_getch();
    switch (control) {
    case 'w':
        snakeY[snakeLength-1]--;
    break;
            case 'a':
    snakeX[snakeLength-1]--;
        break;
    case 's':
            snakeY[snakeLength-1]++;
        break;
    case 'd':
        snakeX[snakeLength-1]++;
    break;
        default:
        return;
    }
    if (map[snakeY[snakeLength-1]][snakeX[snakeLength-1]] != VLANK_CELL && map[snakeY[snakeLength-1]][snakeX[snakeLength-1]] != SNAKE_FOOD) {
gameOver();}
map[snakeY[snakeLength-1]][snakeX[snakeLength-1]]='H';
map[headY][headX]='X';
map[snakeY[0]][snakeX[0]]=' ';
int i;
for(i=0;i

}
}

void gameOver() {
printf("GAME OVER!!\n");
gameRunning = 0;
}

int main()
{
output();
while (gameRunning) {
    snakeMove(snakeX,snakeY);
}
printf("GAME OVER!!\n");
}

这样就基本实现了蛇的移动。

2.吃食物和增长蛇身。

#include 
#include 
#include 

char BLANK_CHAR      = ' ';
char WALL_CHAR       = '*';
char SNAKE_HEAD_CHAR = 'H';
char SNAKE_BODY_CHAR = 'X';
char FOOD_CHAR       = '$';

char map[12][13] = {
  "************",
  "*          *",
  "*          *",
"*          *",
"*          *",
  "*          *",
  "*          *",
  "*          *",
  "*          *",
  "*          *",
  "*          *",
  "************",
};

int snakeHeadX = 1, snakeHeadY = 1;
int snakeBodyX[100] = {0}, snakeBodyY[100] = {0};
int snakeBodyLen = 0;
int snakeTailIndex = -1;
int willBeLonger = 0;

int foodX = 0, foodY = 0;

int gameRunning = 1;

void printMap() {
  system("cls");
  int i = 0 ;
  for (i = 0; i < 12; ++i) {
    printf("%s\n", map[i]);
      }
}

void spawnFood() {
  // Random food position
          foodX = rand() % 10 + 1;
  foodY = rand() % 10 + 1;
  while (map[foodX][foodY] != BLANK_CHAR) {
    foodX = rand() % 10 + 1;
foodY = rand() % 10 + 1;
  }
      map[foodX][foodY] = FOOD_CHAR;
}

void initGame() {
  // Initialize snake
      snakeHeadX = snakeHeadY = 1;
  map[snakeHeadX][snakeHeadY] = SNAKE_HEAD_CHAR;
  snakeBodyLen = 0;
      gameRunning = 1;
  snakeTailIndex = -1;
  willBeLonger = 0;
  // Initialize food
  spawnFood();
  // Initialize game
  gameRunning = 1;

  printMap();
}

void gameOver() {
system("cls");
printf("GAME OVER!!\n");
gameRunning = 0;
}

void snakeMove(char control) {
      map[snakeHeadX][snakeHeadY] = BLANK_CHAR;
  // record the previous snake head position
  int prevSnakeHeadX = snakeHeadX;
  int prevSnakeHeadY = snakeHeadY;
  switch (control) {
   case 'w':
snakeHeadX--;
break;
   case 'a':
snakeHeadY--;
break;
   case 's':
snakeHeadX++;
break;
   case 'd':
snakeHeadY++;
break;
   default:
return;
  }
          if (map[snakeHeadX][snakeHeadY] != BLANK_CHAR && map[snakeHeadX][snakeHeadY] != FOOD_CHAR) {
    // DIED
gameOver();
  }map[snakeHeadX][snakeHeadY] = SNAKE_HEAD_CHAR;

  int moved = 0;
  // If willBeLonger, then make it longer
  if (willBeLonger) {
willBeLonger = 0;
moved = 1;
// make space
int i;
for (i = snakeBodyLen - 1; i > snakeTailIndex; --i) {
  snakeBodyX[i + 1] = snakeBodyX[i];
  snakeBodyY[i + 1] = snakeBodyY[i];
}
snakeBodyX[snakeTailIndex + 1] = prevSnakeHeadX;
snakeBodyY[snakeTailIndex + 1] = prevSnakeHeadY;
if (snakeTailIndex < 0) snakeTailIndex = 0;
map[prevSnakeHeadX][prevSnakeHeadY] = SNAKE_BODY_CHAR;
snakeBodyLen++;
  }

  // Check if ate food
  if (snakeHeadX == foodX && snakeHeadY == foodY) {
willBeLonger = 1;
spawnFood();
  }

  // Head has already moved, **Move the body**
      // if "no body" here or added tail just now, then it needn't move
  if (snakeBodyLen <= 0 || moved) return;
  map[snakeBodyX[snakeTailIndex]][snakeBodyY[snakeTailIndex]] = BLANK_CHAR;
  snakeBodyX[snakeTailIndex] = prevSnakeHeadX;
  snakeBodyY[snakeTailIndex] = prevSnakeHeadY;
  map[prevSnakeHeadX][prevSnakeHeadY] = SNAKE_BODY_CHAR;
  // Move tail index
  snakeTailIndex = (snakeTailIndex - 1 + snakeBodyLen) % snakeBodyLen;
}

int main() {
  // Initialize random seed
  srand(time(NULL));

  initGame();

  char control = 0;
  while (gameRunning) {
control=_getch(); // " %c": the space is used to skip blank character, like '\n'
snakeMove(control);
printMap();
}

这样就完成了基本的通过wasd来控制的贪吃蛇游戏了。

你可能感兴趣的:(初级贪吃蛇)