采用双向链表结点模拟蛇身;
使用了C语言光标控制函数打印地图、蛇和食物;
/**************************
***************************
贪吃蛇游戏
C语言数据结构
作者:Dew
时间:2019年3月23日
版本:1.0
***************************
**************************/
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <conio.h>
#define N 30
#define TRUE 1
#define FALSE 0
#define TIME 300
#define LEFT 37
#define UP 38
#define RIGHT 39
#define DOWN 40
void initMap();
void showGame();
void initSnake();
void control(int key);
void updateSnake(int next_head_x, int next_head_y);
void gotoxy(int x, int y); //光标控制
void gameRun();
void moveUp();
void moveDown();
void moveLeft();
void moveRight();
void createFood();
void addSnakeNode(int cur_tail_x, int cur_tail_y);
void crashTest(int head_x, int head_y);
struct snakeNode {
int pos_x;
int pos_y;
snakeNode* pre;
snakeNode* next;
};
struct foodNode {
int pos_x;
int pos_y;
};
int map[N][N];
int next_head_x, next_head_y;
int cur_tail_x, cur_tail_y;
snakeNode* snake_head = (snakeNode *)malloc(sizeof(snakeNode));
snakeNode* snake_tail = (snakeNode *)malloc(sizeof(snakeNode));
foodNode* food = (foodNode *)malloc(sizeof(foodNode));
int main()
{
initSnake();
initMap();
gameRun();
return 0;
}
void initMap()
{
int i, j;
for(i = 0; i < N; i++)
for (j = 0; j < N; j++)
{
if (i == 0 || i == N - 1)
map[i][j] = 1;
else if (j == 0 || j == N - 1)
map[i][j] = 1;
else
map[i][j] = 0;
}
}
v