得分后,可以排名和删除排名相关操作的贪吃蛇
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#define BLOCK_SIZE 20
#define WIDTH 40
#define HEIGHT 30
int Blocks[HEIGHT][WIDTH] = { 0 };
char moveDirection;
int food_i, food_j;
int isFailure = 0; //是否游戏失败
int score;
// 定义用户信息结构体
typedef struct {
char name[20]; // 用户姓名
int score; // 用户成绩
} UserInfo;
// 定义链表节点结构体
typedef struct Node {
UserInfo user; // 用户信息
struct Node* next; // 指向下一个节点的指针
} Node;
// 定义链表头结构体
typedef struct {
Node* head; // 指向链表头节点的指针
int count; // 链表节点数
} List;
// 初始化链表
void initList(List* list) {
list->head = NULL;
list->count = 0;
}
// 添加新用户
void addUser(List* list, UserInfo user) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->user = user;
newNode->next = NULL;
if (list->head == NULL) {
list->head = newNode;
}
else {
Node* p = list->head;
Node* last = NULL;
while (p != NULL && p->user.score >= user.score) {
last = p;
p = p->next;
}
if (last == NULL) {
newNode->next = list->head;
list->head = newNode;
}
else {
newNode->next = p;
last->next = newNode;
}
}
list->count++;
}
// 删除用户
void deleteUser(List* list, char* name) {
Node* p = list->head;
Node* last = NULL;
while (p != NULL && strcmp(p->user.name, name) != 0) {
last = p;
p = p->next;
}
if (p == NULL) {
printf("User not found!\n");
return;
}
if (last == NULL) {
list->head = p->next;
}
else {
last->next = p->next;
}
free(p);
list->count--;
}
// 修改用户成绩
void modifyScore(List* list, char* name, int score) {
Node* p = list->head;
while (p != NULL && strcmp(p->user.name, name) != 0) {
p = p->next;
}
if (p == NULL) {
printf("User not found!\n");
return;
}
p->user.score = score;
}
// 查找用户排名
int findRank(List* list, char* name) {
int rank = 1;
Node* p = list->head;
while (p != NULL && strcmp(p->user.name, name) != 0) {
rank++;
p = p->next;
}
if (p == NULL) {
return -1; // 用户不存在,返回-1
}
return rank;
}
// 显示用户排名列表
void showList(List* list) {
printf("Rank\tName\tScore\n");
printf("-----------------------\n");
Node* p = list->head;
int rank = 1;
while (p != NULL) {
printf("%d\t%s\t%d\n", rank, p->user.name, p->user.score);
rank++;
p = p->next;
}
}
// 保存用户排名列表到文件
void saveList(List* list) {
FILE* fp = fopen("user_score.txt", "w");
Node* p = list->head;
while (p != NULL) {
fprintf(fp, "%s %d\n", p->user.name, p->user.score);
p = p->next;
}
fclose(fp);
}
// 从文件中读取用户排名列表
void loadList(List* list) {
FILE* fp = fopen("user_score.txt", "r");
if (fp == NULL) {
return;
}
char name[20];
int score;
while (fscanf(fp, "%s %d", name, &score) != EOF) {
UserInfo user;
strcpy(user.name, name);
user.score = score;
addUser(list, user);
}
fclose(fp);
}
void moveSnake() {
for (int i = 0; i < HEIGHT; i++)
for (int j = 0; j < WIDTH; j++)
if (Blocks[i][j] > 0) //大于0为小蛇元素
Blocks[i][j]++;
int oldTail_i, oldTail_j, oldHead_i, oldHead_j;
int max = 0;
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (max < Blocks[i][j]) {
max = Blocks[i][j];
oldTail_i = i;
oldTail_j = j;
}
if (Blocks[i][j] == 2) {
oldHead_i = i;
oldHead_j = j;
}
}
}
int newHead_i = oldHead_i;
int newHead_j = oldHead_j;
if (moveDirection == 'W')
newHead_i = oldHead_i - 1;
else if (moveDirection == 'S')
newHead_i = oldHead_i + 1;
else if (moveDirection == 'A')
newHead_j = oldHead_j - 1;
else if (moveDirection == 'D')
newHead_j = oldHead_j + 1;
//蛇头碰边界,或蛇头碰蛇身,失败
if (newHead_i >= HEIGHT || newHead_i < 0 || newHead_j >= WIDTH || newHead_j < 0 || Blocks[newHead_i][newHead_j]>0) {
isFailure = 1; //游戏失败
return;
}
Blocks[newHead_i][newHead_j] = 1;//新蛇头位置
if (newHead_i == food_i && newHead_j == food_j) {
score += 10;
food_i = rand() % (HEIGHT - 5) + 2;
food_j = rand() % (WIDTH - 5) + 2;
}
else {
Blocks[oldTail_i][oldTail_j] = 0;//蛇尾变空
}
}
void startup() {//初始化
int i;
score = 0;
isFailure = 0; //是否游戏失败
for (int i = 0; i < HEIGHT; i++)
for (int j = 0; j < WIDTH; j++)
Blocks[i][j] = 0;
Blocks[10][10] = 1;//画蛇头1
for (int i = 1; i <= 4; i++)
Blocks[10][10 - i] = i + 1;//向左依次4个蛇身,数值 2 3 4 5
moveDirection = 'D'; //初值右移
food_i = rand() % (HEIGHT - 5) + 2;//食物随机位置
food_j = rand() % (WIDTH - 5) + 2;
initgraph(WIDTH * BLOCK_SIZE, HEIGHT * BLOCK_SIZE);
setlinecolor(RGB(111, 111, 111));
BeginBatchDraw();//开始批量绘制
}
void show() {//绘制
cleardevice();
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (Blocks[i][j] > 0)
setfillcolor(HSVtoRGB( 10, 0.9, 1));
else
setfillcolor(RGB(111, 111, 111));
fillrectangle(j * BLOCK_SIZE, i * BLOCK_SIZE, (j + 1) * BLOCK_SIZE, (i + 1) * BLOCK_SIZE);
}
}
setfillcolor(RGB(0, 255, 0));//食物绿
fillrectangle(food_j * BLOCK_SIZE, food_i * BLOCK_SIZE, (food_j + 1) * BLOCK_SIZE, (food_i + 1) * BLOCK_SIZE);
if (isFailure) {
setbkmode(TRANSPARENT);//文本透明
settextcolor(RGB(255, 0, 0));
outtextxy(240, 220, _T("GAME OVER~!"));
}
FlushBatchDraw();//批量绘制
}
void updateWithoutInput() {
if (isFailure)
return;
static int waitIndex = 1;
waitIndex++;
if (waitIndex == 50) {
moveSnake();
waitIndex = 1;
}
}
void updateWithInput() {//输入影响方向
if (kbhit() && !isFailure) {
char input = getch();
const char* p;
input = toupper(input);
p = strchr("ASDW", input);
if (input == 'A' && moveDirection == 'D')
return;
if (input == 'D' && moveDirection == 'A')
return;
if (input == 'S' && moveDirection == 'W')
return;
if (input == 'W' && moveDirection == 'S')
return;
if (p) {
moveDirection = input;
moveSnake();
}
}
}
void Game() {
startup();
while (!isFailure) {
show();
updateWithoutInput();
updateWithInput();
}
show();
getch();
closegraph();
}
int main() {
List list;
initList(&list);
loadList(&list);
int choice;
UserInfo user;
int find;
do {
printf("\n1. 开始游戏\n");
printf("2. 删除用户\n");
printf("3. 显示所有信息\n");
printf("4. 保存所有信息\n");
printf("5. 退出\n");
printf("请选择: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("请输入玩家名: ");
scanf("%s", user.name);
Game();
user.score = score;
find=findRank(&list,user.name); //查找玩家
if(find!=-1) // 如果玩家存在
deleteUser(&list, user.name);//删除玩家
addUser(&list, user);//添加
printf("score = %d\n", user.score);
break;
case 2:
printf("Enter user name: ");
scanf("%s", user.name);
deleteUser(&list, user.name);
break;
case 3:
showList(&list);
break;
case 4:
saveList(&list);
printf("玩家成绩保存成功!\n");
break;
case 5:
break;
default:
printf("Invalid choice!\n");
break;
}
} while (choice != 5);
return 0;
}