首先,您需要设计用户登录和注册的功能。可以使用文件存储用户信息,如用户名和密码等。在游戏结束后,可以将用户得分存储到文件中,并读取文件中的所有得分进行排名。设计一个用户友好的界面,让用户能够方便地进行游戏、登录和排名查看等操作。C语言源程序实现
时间: 2024-03-12 09:50:10 浏览: 57
Python实现的用户登录系统功能示例
5星 · 资源好评率100%
好的,下面是一个简单的C语言贪吃蛇程序,包括用户登录注册和得分排名功能,具体实现细节注释在代码中:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <time.h>
#define MAX_USERNAME_LEN 20
#define MAX_PASSWORD_LEN 20
#define MAX_SCORES 10
// 用户信息结构体
typedef struct {
char username[MAX_USERNAME_LEN + 1];
char password[MAX_PASSWORD_LEN + 1];
} UserInfo;
// 得分记录结构体
typedef struct {
char username[MAX_USERNAME_LEN + 1];
int score;
} ScoreRecord;
// 全局变量
UserInfo g_user; // 当前登录用户
int g_scores[MAX_SCORES] = {0}; // 所有用户的得分记录
int g_numScores = 0; // 当前得分记录数量
// 函数声明
void showMenu();
void login();
void registerUser();
void playGame();
void saveScore(int score);
void showRanking();
int main() {
showMenu();
return 0;
}
// 显示菜单
void showMenu() {
system("cls");
printf("************ Welcome to Snake Game ************\n");
printf("* *\n");
printf("* 1. Login *\n");
printf("* 2. Register *\n");
printf("* 3. Exit *\n");
printf("* *\n");
printf("************************************************\n\n");
int choice = 0;
do {
printf("Please enter your choice (1-3): ");
scanf("%d", &choice);
switch(choice) {
case 1:
login();
break;
case 2:
registerUser();
break;
case 3:
printf("\nGoodbye!\n");
exit(0);
default:
printf("\nInvalid choice, please try again.\n\n");
}
} while(choice < 1 || choice > 3);
}
// 用户登录
void login() {
system("cls");
printf("************ Login ************\n\n");
// 读取用户信息文件
FILE* fp = fopen("users.txt", "r");
if(fp == NULL) {
printf("Failed to open users.txt.\n");
return;
}
UserInfo user;
int found = 0;
while(fread(&user, sizeof(UserInfo), 1, fp)) {
if(strcmp(user.username, g_user.username) == 0 &&
strcmp(user.password, g_user.password) == 0) {
found = 1;
break;
}
}
fclose(fp);
if(found) {
printf("Login successful!\n");
playGame();
} else {
printf("Invalid username or password.\n");
}
printf("\nPress any key to continue...");
getch();
showMenu();
}
// 用户注册
void registerUser() {
system("cls");
printf("************ Register ************\n\n");
// 读取用户信息文件
FILE* fp = fopen("users.txt", "r");
if(fp == NULL) {
printf("Failed to open users.txt.\n");
return;
}
UserInfo user;
int found = 0;
while(fread(&user, sizeof(UserInfo), 1, fp)) {
if(strcmp(user.username, g_user.username) == 0) {
found = 1;
break;
}
}
fclose(fp);
if(found) {
printf("Username already exists, please try another one.\n");
} else {
// 写入用户信息文件
fp = fopen("users.txt", "a");
if(fp == NULL) {
printf("Failed to open users.txt.\n");
return;
}
fwrite(&g_user, sizeof(UserInfo), 1, fp);
fclose(fp);
printf("Register successful!\n");
playGame();
}
printf("\nPress any key to continue...");
getch();
showMenu();
}
// 游戏主逻辑
void playGame() {
system("cls");
printf("************ Snake Game ************\n\n");
// 游戏逻辑
int score = 0;
printf("\nYour score is %d.\n", score);
saveScore(score);
printf("\nPress any key to continue...");
getch();
showMenu();
}
// 保存得分记录
void saveScore(int score) {
// 读取得分记录文件
FILE* fp = fopen("scores.txt", "r");
if(fp == NULL) {
printf("Failed to open scores.txt.\n");
return;
}
ScoreRecord records[MAX_SCORES];
int i = 0;
while(fread(&records[i], sizeof(ScoreRecord), 1, fp)) {
i++;
}
fclose(fp);
// 更新得分记录
int j = 0;
while(j < i && strcmp(records[j].username, g_user.username) != 0) {
j++;
}
if(j == i) {
// 新用户
strcpy(records[i].username, g_user.username);
records[i].score = score;
i++;
if(i > MAX_SCORES) {
i = MAX_SCORES;
}
} else {
// 已有记录
if(score > records[j].score) {
records[j].score = score;
}
}
// 写入得分记录文件
fp = fopen("scores.txt", "w");
if(fp == NULL) {
printf("Failed to open scores.txt.\n");
return;
}
g_numScores = i;
for(j = 0; j < i; j++) {
fwrite(&records[j], sizeof(ScoreRecord), 1, fp);
g_scores[j] = records[j].score;
}
fclose(fp);
}
// 显示得分排名
void showRanking() {
system("cls");
printf("************ Ranking ************\n\n");
printf("Rank\tUsername\tScore\n");
printf("----\t--------\t-----\n");
// 按分数从高到低排序
int i, j;
for(i = 0; i < g_numScores - 1; i++) {
for(j = i + 1; j < g_numScores; j++) {
if(g_scores[j] > g_scores[i]) {
int temp = g_scores[i];
g_scores[i] = g_scores[j];
g_scores[j] = temp;
ScoreRecord tempRecord = {0};
memcpy(&tempRecord, &g_scores[i], sizeof(ScoreRecord));
memcpy(&g_scores[i], &g_scores[j], sizeof(ScoreRecord));
memcpy(&g_scores[j], &tempRecord, sizeof(ScoreRecord));
}
}
}
for(i = 0; i < g_numScores; i++) {
printf("%d\t%s\t\t%d\n", i + 1, g_scores[i].username, g_scores[i].score);
}
printf("\nPress any key to continue...");
getch();
showMenu();
}
```
注意,上面的代码只是一个简单的示例,实际应用中还需要考虑到更多的细节和安全性问题。
阅读全文