假期写的贪吃蛇小游戏,基于EasyX图形库
因为是第一次使用makedown语法,所以写的可能有点low,在下游戏所有的文件资源在最下面。
代码展示
自己定义头文件:gcffile.h
#pragma once
#include <stdio.h>
#include <graphics.h>
#include <string.h>
#include <stdbool.h>
//背景图片
IMAGE bk[11];
int bkNum;
const int WIDE = 1920 / 2;
const int HEIGHT = 1200 / 2;
char username[20] = "";
bool isbreak = false;
//mode = 0,为普通模式;mode = 1,为穿墙模式,mode = 2,为无敌模式,mode = 3为精英版的普通模式。。。
int mode = -1;
void UserMessage()
{
FILE* fp_ruser = fopen("信息\\user.txt", "r");
if (fp_ruser == NULL)
{
char user[21] = "";
InputBox(user, 20, "请输入登录名:", NULL, 0, 300, 0, false);
strcpy(username, user);
char password1[21] = "";
char password2[21] = "";
InputBox(password1, 20, "请输入密码:", NULL, 0, 300, 0, false);
InputBox(password2, 20, "请确认密码:", NULL, 0, 300, 0, false);
while (strcmp(password1, password2) != 0)
{
InputBox(password1, 20, "两次密码不一致\n请重新输入:", NULL, 0, 300, 0, false);
InputBox(password2, 20, "请确认密码:", NULL, 0, 300, 0, false);
}
FILE* fp_wuser = fopen("信息\\user.txt", "a");
strcat(user, "\n");
fputs(user, fp_wuser);
fputs(password1, fp_wuser);
fclose(fp_wuser);
}
else
{
char user[40] = "";
char password1[21] = "";
char password[21] = "";
fgets(user, 20, fp_ruser);
user[strlen(user) - 1] = '\0';
strcpy(username, user);
strcat(user,"\n请输入密码:");
fgets(password, 20, fp_ruser);
InputBox(password1, 20, user, NULL, 0, 300, 0, false);
while (strcmp(password1, password) != 0)
{
InputBox(password1, 20, "密码错误\n请重新输入:", NULL, 0, 300, 0, false);
}
fclose(fp_ruser);
}
}
/********************游戏开始界面*************************/
int modeSelect = 0;
void BeginGraph()
{
settextcolor(WHITE);
settextstyle(30, 0, "仿宋");
setbkmode(1);
outtextxy(WIDE / 2 - 50, HEIGHT / 3 , "普通模式");
outtextxy(WIDE / 2 - 50, HEIGHT / 3 + 90, "精英模式");
outtextxy(WIDE / 2 - 50, HEIGHT / 3 + 180, "无聊模式");
outtextxy(WIDE / 2 - 50, HEIGHT / 3 + 270, "游戏帮助");
setlinecolor(RED);
switch (modeSelect)
{
case 0:
rectangle(WIDE / 2 - 80, HEIGHT / 3 - 10, WIDE / 2 + 100, HEIGHT / 3 + 40);
break;
case 1:
rectangle(WIDE / 2 - 80, HEIGHT / 3 + 80, WIDE / 2 + 100, HEIGHT / 3 + 130);
break;
case 2:
rectangle(WIDE / 2 - 80, HEIGHT / 3 + 170, WIDE / 2 + 100, HEIGHT / 3 + 220);
break;
case 3:
rectangle(WIDE / 2 - 80, HEIGHT / 3 + 260, WIDE / 2 + 100, HEIGHT / 3 + 310);
break;
}
}
//普通模式
int generalMode = 0;
void General()
{
settextcolor(WHITE);
settextstyle(30, 0, "仿宋");
outtextxy(WIDE / 2 - 50, HEIGHT / 3, "普通模式");
outtextxy(WIDE / 2 - 50, HEIGHT / 3 + 90, "穿墙模式");
outtextxy(WIDE / 2 - 50, HEIGHT / 3 + 180, "无敌模式");
setlinecolor(RED);
switch (generalMode)
{
case 0:
rectangle(WIDE / 2 - 80, HEIGHT / 3 - 10, WIDE / 2 + 100, HEIGHT / 3 + 40);
break;
case 1:
rectangle(WIDE / 2 - 80, HEIGHT / 3 + 80, WIDE / 2 + 100, HEIGHT / 3 + 130);
break;
case 2:
rectangle(WIDE / 2 - 80, HEIGHT / 3 + 170, WIDE / 2 + 100, HEIGHT / 3 + 220);
break;
}
}
void CtrlGeneral()
{
if (_kbhit())
{
char ch = _getch();
switch (ch)
{
case 80://xia
if (generalMode < 2)
{
generalMode++;
}
else
{
generalMode