#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#define MAXLENTH 2000
struct score {
int current_score;
char name[30];
int highest[5];
}history;
struct position {
short x,y;
struct position * pre,* next;
};
struct tm * lt;
typedef struct position snake;
typedef snake * point;
time_t t;
long long step = 0,pre_step = 0;
point Head, Tail, temp,te;
FILE * RankFile,*HistoryFile;
short snake_lenth, Food[2]; //记录蛇身长度,食物坐标
short game, a_or_m, speedx, speedy; //记录游戏状态,游戏模式,移动速度
char snake_dir = 'd', former_dir; //记录蛇头方向和前一次移动的方向
void goto_xy(SHORT, SHORT); //移动光标到x,y指示的坐标下
void over_game(void); //结束游戏
void begin_game(void); //开始游戏
void win_game(void); //赢得游戏
void draw_map(void); //打印地图
void draw_snake(void); //打印蛇身
void draw_menu(void); //打印菜单
void draw_food(void); //打印食物
void write_history(); //记录游戏历史
void change_dir_auto(void); //自动改变方向
void change_dir_mannul(void); //手动改变方向
void move_snake(); //移动蛇身
short check_game(); //检查游戏状态
void test(void); //测试接口
int main(int argc, char *argv[]) //主函数开始
{
// test();
HANDLE fd = GetStdHandle(STD_OUTPUT_HANDLE);//隐藏光标
CONSOLE_CURSOR_INFO cinfo; //隐藏光标
cinfo.bVisible = 0; //隐藏光标
cinfo.dwSize = 1; //隐藏光标
SetConsoleCursorInfo(fd, &cinfo); //隐藏光标
begin_game(); //开始游戏
while (1) {
game = check_game(); //判断游戏状态
if (game == 1) {
//吃到食物,增长蛇身
temp = (point)malloc(sizeof(snake));//尾插法增加蛇身节点
temp->x = Tail->x;temp->y = Tail->y;
Tail->next = temp;
temp->pre = Tail;
Tail = temp;
goto_xy(84, 8);
history.current_score += 10; //得分加+10
printf("%5d",history.current_score);//更新得分
draw_food(); //重新生成食物
move_snake(); //继续移动蛇身
snake_lenth++; //蛇身长度+1
}
else if (game == 2)
move_snake(); //移动蛇身
else if (game == 0)
over_game(); //结束游戏
else if (game == 3)
win_game(); //赢得游戏
else if (game == 4)
{
system("
C语言实现贪吃蛇(非图形界面)
最新推荐文章于 2021-05-24 02:03:35 发布