#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void gotoxy(int x, int y)
{
COORD pos = {x, y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); // 获取标准输出设备句柄
SetConsoleCursorPosition(hOut, pos); // 两个参数分别是指定哪个窗体,具体位置
}
// 隐藏光标
void HideCursor()
{
CONSOLE_CURSOR_INFO curInfo; // 定义光标信息的结构体变量
curInfo.dwSize = 1; // 如果没赋值的话,光标隐藏无效
curInfo.bVisible = FALSE; // 将光标设置为不可见
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); // 获取控制台句柄
SetConsoleCursorInfo(handle, &curInfo); // 设置光标信息
}
int position[25][51];
int game = 1;
#define head 1;
#define body 2;
#define wall 3
#define kong 0
#define fu 4
int flag = 0;
int score = 0;
int max = 0;
int width;
int height;
int Time = 0;
struct
{ // 每一节蛇的坐标
int x[100];
int y[100];
int len; // 蛇长
int speed; //
} snake;
struct RECORD
{
int score;
char name[20];
int level;
int Long;
char time[100];
struct RECORD *next;
};
char uname[20];
int level = 1;
char MenuText[9][20] = {
"[A] Begin",
"[B] Username",
"[C] Gamedifficulty",
"[D] History data",
"[E] line",
"[F] Exit"};
void name()
{
printf("Please enter your name: ");
gets(uname);
fflush(stdin);
}
void difficulty()
{
printf("choose your gamedifficuty:\n");
printf("1.simple(default) 2.middle 3.difficult\n");
char key;
scanf("%c", &key);
fflush(stdin);
if (key == 1)
{
level = 1;
snake.speed = 3000;
}
else if (key == 2)
{
level = 2;
snake.speed = 2000;
}
else
{
level = 3;
snake.speed = 1000;
}
}
void history()
{
FILE *fp1 = NULL;
fp1 = fopen("record.txt", "r");
char data1[100];
while (fgets(data1, sizeof(data1), fp1) != NULL)
{
// 输出每一行读取的字符串
printf("%s", data1);
}
printf("\n");
fclose(fp1);
fp1 = NULL;
}
void displaymenu(int hilight)
{
int i;
printf("\n======================\n");
for (i = 0; i < 6; i++)
{
if (i == hilight - 1)
printf("<<%s>>\n", MenuText[i]);
else
printf(" %s\n", MenuText[i]);
}
printf("======================\n");
}
void drawMap()
{
for (int i = 0; i <= height; i++)
{
position[i][0] = wall;
gotoxy(0, i);
printf("#");
position[i][width] = wall;
gotoxy(width, i);
printf("#");
}
for (int i = 0; i <= width; i += 2) // 上下 #占用两个字符
{
position[0][i] = wall;
position[0][i + 1] = wall;
gotoxy(i, 0);
printf("#");
position[height][i] = wall;
position[0][i + 1] = wall;
gotoxy(i, height);
printf("#");
}
snake.len = 0;
snake.x[0] = 25; // 开始蛇头放屏幕中间
snake.y[0] = height / 2;
position[snake.y[0]][snake.x[0]] = head;
gotoxy(snake.x[0], snake.y[0]);
printf("@"); // 一节 x=2
}
// 随机生成食物
void Food()
{
int i, j;
do
{
// 随机生成食物的横纵坐标
i = rand() % (height - 1) + 1;
j = rand() % (width - 1) + 1;
if (j % 2 == 0)
j++;
} while (position[i][j] != kong); // 确保生成食物的位置为空,若不为空则重新生成
position[i][j] = fu; // 将食物位置进行标记
// color(12); // 颜色设置为红色
gotoxy(j, i);
printf("$"); // 打印食物
}
void sadd(int e)
{
snake.len++;
snake.x[snake.len] = snake.x[snake.len - 1];
snake.y[snake.len] = snake.y[snake.len - 1] + e;
position[snake.y[snake.len]][snake.x[snake.len]] = body;
gotoxy(snake.x[snake.len], snake.y[snake.len]);
printf("*");
}
void hadd(int e)
{
snake.len++;
snake.x[snake.len] = snake.x[snake.len - 1] + e;
snake.y[snake.len] = snake.y[snake.len - 1];
position[snake.y[snake.len]][snake.x[snake.len]] = body;
gotoxy(snake.x[snake.len], snake.y[snake.len]);
printf("*");
}
void move()
{
for (int i = 0; i <= height; i++)
{
for (int j = 0; j <= width; j++)
{
if (position[i][j] == 0)
{
gotoxy(j, i);
printf(" ");
}
}
}
}
void prints()
{
for (int i = 0; i <= snake.len; i++)
{
gotoxy(snake.x[i], snake.y[i]);
if (i == 0)
printf("@");
else
printf("*");
}
}
void scheck(int e)
{
if (position[snake.y[0]][snake.x[0]] == fu || position[snake.y[0]][snake.x[0]] == fu)
{
sadd(e);
score += 10;
gotoxy(0, height + 1);
printf("recent score:%d", score);
flag = 1;
}
}
void hcheck(int e)
{
if (position[snake.y[0]][snake.x[0]] == fu || position[snake.y[0]][snake.x[0]] == fu)
{
hadd(e);
score += 10;
gotoxy(0, height + 1);
printf("recent score:%d", score);
flag = 1;
}
}
void Read(struct RECORD *p)
{
FILE *pf = fopen("record.txt", "a+"); // 以只写的方式打开文件
// fseek(pf, 0, SEEK_SET); // 使文件指针pf指向文件开头;
struct RECORD *new = (struct RECORD *)malloc(sizeof(struct RECORD));
struct RECORD *copy2 = p;
while (copy2->next != NULL)
{
copy2 = copy2->next;
}
copy2->next = new;
new->next = NULL;
new->level = level;
fprintf(pf, " ");
fprintf(pf, "%d ", level);
new->Long = Time;
fprintf(pf, " ");
fprintf(pf, "%ds", Time);
*(new->name) = uname[20];
fprintf(pf, " ");
fprintf(pf, "%s ", uname);
new->score = score;
fprintf(pf, " ");
fprintf(pf, "%d ", score);
time_t t;
struct tm dt;
time(&t);
localtime_s(&dt, &t);
snprintf(new->time, sizeof(new->time), "%04d-%02d-%02d %02d:%02d:%02d",
dt.tm_year + 1900, dt.tm_mon + 1, dt.tm_mday,
dt.tm_hour, dt.tm_min, dt.tm_sec);
fprintf(pf, "%s\n", new->time);
fclose(pf); // 关闭文件
pf = NULL; // 文件指针及时置空
}
void line(struct RECORD *p)
{
struct RECORD *Copy, *pre, *j, *t, *leader;
Copy = p->next;
t = p;
if (Copy == NULL)
{
return;
}
else if (Copy->next == NULL)
{
printf("%s,%d,%d,%d,%s", Copy->name, Copy->level, Copy->Long, Copy->score, Copy->time);
return;
}
pre = Copy->next;
if (pre->next == NULL)
{
if (pre->score > Copy->score)
{
printf("%s,%d,%d,%d,%s", pre->name, pre->level, pre->Long, pre->score, pre->time);
printf("%s,%d,%d,%d,%s", Copy->name, Copy->level, Copy->Long, Copy->score, Copy->time);
}
else
{
printf("%s,%d,%d,%d,%s", Copy->name, Copy->level, Copy->Long, Copy->score, Copy->time);
printf("%s,%d,%d,%d,%s", pre->name, pre->level, pre->Long, pre->score, pre->time);
}
}
else
{
j = pre->next;
for (; Copy != NULL; Copy = Copy->next)
{
while (j != NULL)
{
if (pre->score < j->score)
{
pre->next = j->next;
j->next = pre;
if (t == p)
{
Copy->next = j;
t = j;
}
else
{
t->next = j;
t = j;
}
j = pre->next;
}
else
{
t = pre;
pre = j;
j = j->next;
}
}
}
}
leader = p->next;
for (; leader != NULL; leader = leader->next)
{
printf("%s,%d,%d,%d,%s", leader->name, leader->level, leader->Long, leader->score, leader->time);
printf("");
}
}
void ifsafe(struct RECORD *p)
{
if (position[snake.y[0]][snake.x[0] - 1] == wall || position[snake.y[0]][snake.x[0] + 1] == wall || position[snake.y[0] - 1][snake.x[0]] == wall || position[snake.y[0] + 1][snake.x[0]] == wall)
{
system("cls");
printf("failure");
game = 0;
Read(p);
if (score > max)
{
max = score;
}
}
}
void GAME(struct RECORD *p)
{
if (level == 1)
height = 14, width = 30;
else if (level == 2)
height = 20, width = 40;
else
height = 24, width = 50;
HideCursor();
int x, y;
char key = 'w';
system("cls");
Food();
drawMap();
int count = 5;
for (; game;)
{
gotoxy(0, height + 1);
printf("recent score:%d ", score);
printf("histroy score:%d", max);
switch (key)
{
case 'W': // 往上走 y--
case 'w':
for (; game;)
{
Time += snake.speed / 1000; // 蛇每次移动需要的时间
if (flag)
{
for (int i = snake.len; i >= 0; i--)
{
if (i == 0)
{
if (snake.len == 0)
{
position[snake.y[i]][snake.x[i]] = kong;
}
snake.y[i]--;
position[snake.y[i]][snake.x[i]] = head;
}
else
{
if (i == snake.len)
{
position[snake.y[i]][snake.x[i]] = kong;
}
snake.x[i] = snake.x[i - 1];
snake.y[i] = snake.y[i - 1];
position[snake.y[i]][snake.x[i]] = body;
}
}
move();
prints();
}
else
{
position[snake.y[0]][snake.x[0]] = kong;
move();
snake.y[0]--;
scheck(1);
position[snake.y[0]][snake.x[0]] = head;
gotoxy(snake.x[0], snake.y[0]);
printf("@");
}
ifsafe(p);
if (kbhit())
{
key = getch();
fflush(stdin);
break;
}
Sleep(snake.speed);
}
break;
case 'S':
case 's':
for (; game;)
{
Time += snake.speed / 1000;
if (flag)
{
for (int i = snake.len; i >= 0; i--)
{
if (i == 0)
{
if (snake.len == 0)
{
position[snake.y[i]][snake.x[i]] = kong;
}
snake.y[i]++;
position[snake.y[i]][snake.x[i]] = head;
}
else
{
if (i == snake.len)
{
position[snake.y[i]][snake.x[i]] = kong;
}
snake.x[i] = snake.x[i - 1];
snake.y[i] = snake.y[i - 1];
position[snake.y[i]][snake.x[i]] = body;
}
}
move();
prints();
}
else
{
position[snake.y[0]][snake.x[0]] = kong;
move();
snake.y[0]++;
scheck(-1);
position[snake.y[0]][snake.x[0]] = head;
gotoxy(snake.x[0], snake.y[0]);
printf("@");
}
ifsafe(p);
if (kbhit())
{
key = getch();
fflush(stdin);
break;
}
Sleep(snake.speed);
}
break;
case 'A':
case 'a':
for (; game;)
{
Time += snake.speed / 1000;
if (flag)
{
for (int i = snake.len; i >= 0; i--)
{
if (i == 0)
{
if (snake.len == 0)
{
position[snake.y[i]][snake.x[i]] = kong;
}
snake.x[i] -= 2;
position[snake.y[i]][snake.x[i]] = head;
}
else
{
if (i == snake.len)
{
position[snake.y[i]][snake.x[i]] = kong;
}
snake.x[i] = snake.x[i - 1];
snake.y[i] = snake.y[i - 1];
position[snake.y[i]][snake.x[i]] = body;
}
}
move();
prints();
}
else
{
position[snake.y[0]][snake.x[0]] = kong;
move();
snake.x[0] -= 2;
hcheck(2);
position[snake.y[0]][snake.x[0]] = head;
gotoxy(snake.x[0], snake.y[0]);
printf("@");
}
ifsafe(p);
if (kbhit())
{
key = getch();
fflush(stdin);
break;
}
Sleep(snake.speed);
}
break;
case 'd':
case 'D':
for (; game;)
{
Time += snake.speed / 1000;
if (flag)
{
for (int i = snake.len; i >= 0; i--)
{
if (i == 0)
{
if (snake.len == 0)
{
position[snake.y[i]][snake.x[i]] = kong;
}
snake.x[i] += 2;
position[snake.y[i]][snake.x[i]] = head;
}
else
{
if (i == snake.len)
{
position[snake.y[i]][snake.x[i]] = kong;
}
snake.x[i] = snake.x[i - 1];
snake.y[i] = snake.y[i - 1];
position[snake.y[i]][snake.x[i]] = body;
}
}
move();
prints();
}
else
{
position[snake.y[0]][snake.x[0]] = kong;
move();
snake.x[0] += 2;
hcheck(-2);
position[snake.y[0]][snake.x[0]] = head;
gotoxy(snake.x[0], snake.y[0]);
printf("@");
}
ifsafe(p);
if (kbhit())
{
key = getch();
fflush(stdin);
break;
}
Sleep(snake.speed);
}
break;
}
}
}
// void getscore()
// {
// FILE *fp1 = NULL;
// fp1 = fopen("record.txt", "r");
// int num1;
// int num2;
// while (fscanf(fp1, "%d", &num1) == 1)
// {
// if (fscanf(fp1, "%d", &num2) == 1)
// {
// max = num1;
// break;
// }
// if (num2 > num1)
// {
// max = num2;
// }
// }
// fclose(fp1);
// }
void start(struct RECORD *p)
{
int sel = 1;
displaymenu(1);
for (;;)
{
if (kbhit())
{
if (GetAsyncKeyState(VK_UP))
{ // 判断是否<上>按键点击
sel = (sel > 1) ? sel - 1 : sel;
system("cls"); // 清屏
displaymenu(sel); // 显示菜单
// fflush(stdin);
getch();
getch();
}
else if (GetAsyncKeyState(VK_DOWN))
{ // 判断是否<下>按键点击
sel = (sel < 6) ? sel + 1 : sel;
system("cls");
displaymenu(sel);
// fflush(stdin);
getch();
getch();
}
else
{
char c = getch();
fflush(stdin);
if (c == '\r')
{
switch (sel)
{
case 1:
GAME(p);
break;
case 2:
name();
break;
case 3:
difficulty();
break;
case 4:
history();
break;
case 5:
line(p);
break;
case 6:
break;
default:
break;
}
}
else
{
switch (c)
{
case 'A':
sel = c - 16 - '0';
system("cls");
displaymenu(sel);
GAME(p);
break;
case 'B':
sel = c - 16 - '0';
system("cls");
displaymenu(sel);
name();
break;
case 'C':
sel = c - 16 - '0';
system("cls");
displaymenu(sel);
difficulty();
break;
case 'D':
sel = c - 16 - '0';
system("cls");
displaymenu(sel);
history();
break;
case 'E':
sel = c - 16 - '0';
system("cls");
displaymenu(sel);
line(p);
break;
case 'F':
break;
default:
break;
}
}
}
}
}
}
int main()
{
struct RECORD *p = (struct RECORD *)malloc(sizeof(struct RECORD));//每次创建都会造成原链表丢失
p->next = NULL;
start(p);
return 0;
}
标签:int,len,链表,重新启动,score,丢失,printf,position,snake From: https://blog.csdn.net/2401_87555017/article/details/144847306