我的小站——半生瓜のblog
相关文章——C语言实现推箱子小游戏
推箱子
编译软件:VS2019
下载:代码文件我已经上传到GitHub
链接
下载最新的那个!(文件夹形式的)
注意:
-
fopen scanf编译器认为的不安全报错 解决:项目属性-C/C+±预处理器-预处理定义-编辑-加入_CRT_SECURE_NO_WARNINGS
-
图形库函数xxx等问题 解决:项目属性-高级-字符集-使用多字节字符集
-
图形窗口注意贴图顺序以及参数 -
文件读取问题——链接 -
相关有帮助的文章——鼠标操作
代码实现
注意:
- 被注释的代码大多数是命令行窗口的,少量是根本就没用的代码(没删)。
- 注释很多
#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<string.h>
#define ROW 10
#define COL 10
#define Space 0
#define Wall 1
#define Final 2
#define Box 3
#define Player 4
#define RecordPlayerNums 10
typedef struct RpArry
{
char name[64];
int steps;
}RpArry;
IMAGE img[6];
void Loadimage()
{
for (int i = 0; i < 6; i++)
{
char temFileName[50] = { 0 };
sprintf(temFileName, "./images/%d.bmp", i);
loadimage(img + i, temFileName, 63, 63);
}
}
RpArry rparry[RecordPlayerNums];
int RankPerson = 0;
int LEVEL = 0;
int Step = 0;
int FilePersonNums = 0;
void MainMenu()
{
printf("------------------------\n");
printf("------》推箱子《-------\n");
printf("------0.退出-----------\n");
printf("------1.开始游戏-------\n");
printf("------2.查看排行榜------\n");
printf("------------------------\n");
}
int Map[6][ROW][COL] =
{
{
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,1,1,0,0,0,0},
{0,0,0,1,2,1,0,0,0,0},
{0,0,0,1,3,1,1,1,1,0},
{0,1,1,1,0,0,3,2,1,0},
{0,1,2,3,0,4,1,1,1,0},
{0,1,1,1,1,3,1,0,0,0},
{0,0,0,0,1,2,1,0,0,0},
{0,0,0,0,1,1,1,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
},
{
{0,0,0,0,0,0,0,0,0,0},
{0,0,1,1,0,0,1,1,0,0},
{0,1,0,2,1,1,2,0,1,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1,0,0,1,3,1,0},
{0,1,0,0,0,0,0,0,1,0},
{0,1,0,1,1,1,1,0,1,0},
{0,1,0,0,0,0,0,0,1,0},
{0,0,1,0,4,0,0,1,0,0},
{0,0,0,1,1,1,1,0,0,0},
{0,0,0,0,0,0,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,0},
{0,1,2,3,0,0,3,2,1,0},
{0,1,0,1,0,0,1,0,1,0},
{0,1,0,1,0,0,1,0,1,0},
{0,1,0,1,4,0,1,0,1,0},
{0,1,0,1,0,0,1,0,1,0},
{0,1,2,3,0,0,3,2,1,0},
{0,1,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0}
},
};
void ShowMap()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (Map[LEVEL][i][j] == Space)
{
putimage(j * 63, i * 63, img + 0);
printf(" ");
}
else if (Map[LEVEL][i][j] == Wall)
{
putimage(j * 63, i * 63, img + 1);
printf("▊");
}
else if (Map[LEVEL][i][j] == Final)
{
putimage(j * 63, i * 63, img + 2);
printf("☆");
}
else if (Map[LEVEL][i][j] == Box)
{
putimage(j * 63, i * 63, img + 3);
printf("★");
}
else if (Map[LEVEL][i][j] == Player)
{
putimage(j * 63, i * 63, img + 4);
printf("♀");
}
else if (Map[LEVEL][i][j] == Box + Final)
{
putimage(j * 63, i * 63, img + 5);
printf("★");
}
else if (Map[LEVEL][i][j] == Final + Player)
{
putimage(j * 63, i * 63, img + 4);
printf("♂");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
void ReSetLevel()
{
for (int s = 0; s < 10; s++)
{
for (int b = 0; b < 10; b++)
{
Map[LEVEL][s][b] = FakeMap[LEVEL][s][b];
}
}
}
void PushBox()
{
int i = 0;
int j = 0;
int flag = 0;
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
if (Map[LEVEL][i][j] == Player || Map[LEVEL][i][j] == Player + Final)
{
flag = 1;
break;
}
}
if (flag == 1)
{
break;
}
}
if (LEVEL < 4)
{
printf("按空格重置关卡\n");
printf("按回车跳过关卡\n");
printf("当前步数%d", Step);
}
char KeyBorad = _getch();
switch (KeyBorad)
{
case 'w':
case 'W':
case 72:
{
if (Map[LEVEL][i - 1][j] == Space || Map[LEVEL][i - 1][j] == Final)
{
Map[LEVEL][i - 1][j] += Player;
Map[LEVEL][i][j] -= Player;
Step++;
}
else if (Map[LEVEL][i - 1][j] == Box)
{
if (Map[LEVEL][i - 2][j] == Space || Map[LEVEL][i - 2][j] == Final)
{
Map[LEVEL][i - 2][j] += Box;
Map[LEVEL][i - 1][j] -= Box;
Map[LEVEL][i - 1][j] += Player;
Map[LEVEL][i][j] -= Player;
Step++;
}
}
break;
}
case 's':
case 'S':
case 80:
{
if (Map[LEVEL][i + 1][j] == Space || Map[LEVEL][i + 1][j] == Final)
{
Map[LEVEL][i + 1][j] += Player;
Map[LEVEL][i][j] -= Player;
Step++;
}
else if (Map[LEVEL][i + 1][j] == Box)
{
if (Map[LEVEL][i + 2][j] == Space || Map[LEVEL][i + 2][j] == Final)
{
Map[LEVEL][i + 2][j] += Box;
Map[LEVEL][i + 1][j] -= Box;
Map[LEVEL][i + 1][j] += Player;
Map[LEVEL][i][j] -= Player;
Step++;
}
}
break;
}
case 'a':
case'A':
case 75:
{
if (Map[LEVEL][i][j - 1] == Space || Map[LEVEL][i][j - 1] == Final)
{
Map[LEVEL][i][j - 1] += Player;
Map[LEVEL][i][j] -= Player;
Step++;
}
else if (Map[LEVEL][i][j - 1] == Box)
{
if (Map[LEVEL][i][j - 2] == Space || Map[LEVEL][i][j - 2] == Final)
{
Map[LEVEL][i][j - 2] += Box;
Map[LEVEL][i][j - 1] -= Box;
Map[LEVEL][i][j - 1] += Player;
Map[LEVEL][i][j] -= Player;
Step++;
}
}
break;
}
case 'd':
case 'D':
case 77:
{
if (Map[LEVEL][i][j + 1] == Space || Map[LEVEL][i][j + 1] == Final)
{
Map[LEVEL][i][j + 1] += Player;
Map[LEVEL][i][j] -= Player;
Step++;
}
else if (Map[LEVEL][i][j + 1] == Box)
{
if (Map[LEVEL][i][j + 2] == Space || Map[LEVEL][i][j + 2] == Final)
{
Map[LEVEL][i][j + 2] += Box;
Map[LEVEL][i][j + 1] -= Box;
Map[LEVEL][i][j + 1] += Player;
Map[LEVEL][i][j] -= Player;
Step++;
}
}
break;
}
case 32:
{
ReSetLevel();
ShowMap();
break;
}
case 13:
if (LEVEL < 5)
{
LEVEL++;
}
break;
}
}
bool PassLevel()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (Map[LEVEL][i][j] == Box)
{
return false;
}
}
}
ReSetLevel();
return true;
}
void PopWindow();
void StartGame()
{
ShowMap();
settextstyle(30, 0, NULL);
settextcolor(RGB(0, 0, 0));
setbkmode(TRANSPARENT);
outtextxy(0, 0, "当前步数:");
outtextxy(200,0,"第");
char q[5];
sprintf(q, "%d", LEVEL+1);
outtextxy(300,0, q);
outtextxy(380,0,"关");
char s[5];
sprintf(s, "%d", Step);
outtextxy(0, 30, s);
outtextxy(0, 570, "空格键重置当前关卡");
outtextxy(0, 600, "回车键跳过当前关卡");
if (PassLevel())
{
LEVEL++;
if (LEVEL > 5)
{
printf("恭喜你%s,完成了游戏\n", rparry[RankPerson].name);
printf("走了%d步\n", Step);
PopWindow();
}
}
PushBox();
}
void SavePlayerRecord()
{
FILE* fp = fopen("PlayerRecord.txt", "a");
if (fp == NULL)
{
printf("读取文件失败\n");
return;
}
fprintf(fp, "\t%s\t%d\n", rparry[RankPerson].name, rparry[RankPerson].steps);
fclose(fp);
system("pause");
system("cls");
}
void ReadRecordFile()
{
FILE* fp = fopen("PlayerRecord.txt", "r");
if (fp == NULL)
{
return;
}
for (int i = 0; i < RecordPlayerNums; i++)
{
fscanf(fp, "%s%d", rparry[i].name, &rparry[i].steps);
}
for (int i = 0; i < RecordPlayerNums; i++)
{
if (rparry[i].steps != 0)
{
RankPerson = ++FilePersonNums;
}
}
fclose(fp);
}
void RecordBubbSort()
{
for (int i = 0; i < RankPerson; i++)
{
RpArry Temprparry;
for (int j = 1; j < RankPerson - i; j++)
{
if (rparry[j - 1].steps >= rparry[j].steps)
{
Temprparry = rparry[j];
rparry[j] = rparry[j - 1];
rparry[j - 1] = Temprparry;
}
}
}
}
void DeletePlayerRecord()
{
remove("PlayerRecord.txt");
RankPerson = 0;
printf("清空完毕!\n");
}
void GraphInputName();
void GraphRankPage();
void GraphFirImagesPg()
{
MOUSEMSG m;
initgraph(ROW * 63, COL * 63);
Loadimage();
setbkcolor(BLACK);
cleardevice();
IMAGE picture1;
loadimage(&picture1, "./topbc.jpg", 630, 630);
putimage(0, 0, &picture1);
setfillcolor(GREEN);
fillrectangle(230, 405, 400, 430);
fillrectangle(230, 445, 400, 470);
fillrectangle(230, 485, 400, 510);
settextstyle(85, 0, "黑体");
char Title[] = "推箱子";
char Select1[] = "开始游戏";
char Select2[] = "排行榜";
char Select3[] = "退出游戏";
outtextxy(210, 150, Title);
settextstyle(30, 0, NULL);
setbkmode(TRANSPARENT);
outtextxy(252, 405, Select1);
outtextxy(268, 445, Select2);
outtextxy(255, 485, Select3);
while (1)
{
m = GetMouseMsg();
if (m.x >= 230 && m.x <= 400 && m.y >= 405 && m.y <= 430)
{
setlinecolor(RED);
rectangle(220, 400, 410, 440);
if (m.uMsg == WM_LBUTTONDOWN)
{
GraphInputName();
cleardevice();
while (LEVEL < 6)
{
StartGame();
}
rparry[RankPerson].steps = Step;
SavePlayerRecord();
LEVEL = 0;
Step = 0;
RankPerson++;
ReSetLevel();
cleardevice();
GraphFirImagesPg();
}
}
else if (m.x >= 230 && m.x <= 400 && m.y >= 445 && m.y <= 470)
{
setlinecolor(RED);
rectangle(220, 440, 410, 480);
if (m.uMsg == WM_LBUTTONDOWN)
{
cleardevice();
GraphRankPage();
}
}
else if (m.x >= 230 && m.x <= 400 && m.y >= 485 && m.y <= 510)
{
setlinecolor(RED);
rectangle(220, 480, 410, 520);
if (m.uMsg == WM_LBUTTONDOWN)
{
exit(0);
break;
}
}
else
{
setlinecolor(BLACK);
rectangle(220, 400, 410, 440);
rectangle(220, 440, 410, 480);
rectangle(220, 480, 410, 520);
}
}
}
void GraphInputName()
{
TCHAR InputWindow[] = _T("请输入你的昵称");
InputBox(rparry[RankPerson].name, 10, InputWindow);
}
void PopWindow()
{
HWND hnd = GetHWnd();
int isok = MessageBox(hnd, "恭喜通关", "提示", MB_OK);
}
void GraphDeleteRankPage();
void GraphRankPage()
{
MOUSEMSG m1;
cleardevice();
setbkcolor(BLACK);
cleardevice();
setfillcolor(GREEN);
fillrectangle(440, 90, 580, 140);
fillrectangle(440, 150, 580, 200);
settextstyle(30, 0, "黑体");
setbkmode(TRANSPARENT);
char Delete[10] = { "清空数据" };
char Back[10] = { "返回" };
outtextxy(480, 158, Back);
outtextxy(450, 100, Delete);
outtextxy(60, 100, "排名");
outtextxy(220, 100, "昵称");
outtextxy(350, 100, "步数");
if (RankPerson == 0)
{
settextstyle(50, 0, "黑体");
char TIPS[20] = { "当前记录为空!" };
outtextxy(130, 238, TIPS);
}
RecordBubbSort();
for (int i = 1; i <= RankPerson; i++)
{
char a[5];
sprintf(a, "%d", rparry[i - 1].steps);
char b[5];
sprintf(b, "%d", i);
outtextxy(60, 100 + (50 * i), b);
outtextxy(220, 100 + (50 * i), rparry[i - 1].name);
outtextxy(350, 100 + (50 * i), a);
}
while (1)
{
m1 = GetMouseMsg();
if (m1.x >= 440 && m1.x <= 580 && m1.y >= 85 && m1.y <= 150)
{
rectangle(430, 85, 590, 150);
setlinecolor(RED);
if (m1.uMsg == WM_LBUTTONDOWN)
{
GraphDeleteRankPage();
cleardevice();
GraphRankPage();
}
}
else if (m1.x>=440 && m1.x<=580&&m1.y >=150 && m1.y<200)
{
rectangle(430, 145, 590, 210);
setlinecolor(RED);
if (m1.uMsg == WM_LBUTTONDOWN)
{
cleardevice();
GraphFirImagesPg();
}
}
else
{
setlinecolor(BLACK);
rectangle(430, 85, 590, 150);
rectangle(430, 145, 590, 210);
}
}
}
void GraphDeleteRankPage()
{
HWND hnd = GetHWnd();
int isok = MessageBox(hnd, "确认清空?", "提示", MB_OKCANCEL);
if (isok == IDOK)
{
int tips1 = MessageBox(hnd, "清空成功!", "提示", MB_OK);
DeletePlayerRecord();
}
else if (isok == IDCANCEL)
{
}
}
int main(void)
{
ReadRecordFile();
GraphFirImagesPg();
return 0;
}
我的小站——半生瓜のblog
相关文章——C语言实现推箱子小游戏
推箱子
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-akMHKVyn-1626320559280)(终极推箱子.assets/image-20210715114111312.png)]
编译软件:VS2019
下载:代码文件我已经上传到GitHub
链接
下载最新的那个!(文件夹形式的)
注意:
-
fopen scanf编译器认为的不安全报错 解决:项目属性-C/C+±预处理器-预处理定义-编辑-加入_CRT_SECURE_NO_WARNINGS [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MXGHhW9G-1626320559283)(终极推箱子.assets/image-20210715102026788.png)] -
图形库函数xxx等问题 解决:项目属性-高级-字符集-使用多字节字符集 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Vo9GyhDf-1626320559284)(终极推箱子.assets/image-20210715101904943.png)] -
图形窗口注意贴图顺序以及参数 -
文件读取问题——链接 -
相关有帮助的文章——鼠标操作
代码实现
注意:
- 被注释的代码大多数是命令行窗口的,少量是根本就没用的代码(没删)。
- 注释很多
#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<string.h>
#define ROW 10
#define COL 10
#define Space 0
#define Wall 1
#define Final 2
#define Box 3
#define Player 4
#define RecordPlayerNums 10
typedef struct RpArry
{
char name[64];
int steps;
}RpArry;
IMAGE img[6];
void Loadimage()
{
for (int i = 0; i < 6; i++)
{
char temFileName[50] = { 0 };
sprintf(temFileName, "./images/%d.bmp", i);
loadimage(img + i, temFileName, 63, 63);
}
}
RpArry rparry[RecordPlayerNums];
int RankPerson = 0;
int LEVEL = 0;
int Step = 0;
int FilePersonNums = 0;
void MainMenu()
{
printf("------------------------\n");
printf("------》推箱子《-------\n");
printf("------0.退出-----------\n");
printf("------1.开始游戏-------\n");
printf("------2.查看排行榜------\n");
printf("------------------------\n");
}
int Map[6][ROW][COL] =
{
{
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,1,1,0,0,0,0},
{0,0,0,1,2,1,0,0,0,0},
{0,0,0,1,3,1,1,1,1,0},
{0,1,1,1,0,0,3,2,1,0},
{0,1,2,3,0,4,1,1,1,0},
{0,1,1,1,1,3,1,0,0,0},
{0,0,0,0,1,2,1,0,0,0},
{0,0,0,0,1,1,1,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
},
{
{0,0,0,0,0,0,0,0,0,0},
{0,0,1,1,0,0,1,1,0,0},
{0,1,0,2,1,1,2,0,1,0},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,3,0,3,0,0,0,1},
{1,0,0,0,4,0,0,0,0,1},
{0,1,0,3,0,3,0,0,1,0},
{0,0,1,2,0,0,2,1,0,0},
{0,0,0,1,0,0,1,0,0,0},
{0,0,0,0,1,1,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,0},
{0,1,2,0,0,0,0,2,1,0},
{0,1,0,0,0,0,0,0,1,0},
{0,1,0,3,0,3,0,0,1,0},
{0,1,0,0,4,0,0,0,1,0},
{0,1,0,3,0,3,0,0,1,0},
{0,1,2,0,0,0,0,2,1,0},
{0,1,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,1,1,1,0,0,0},
{0,1,1,1,0,2,1,0,0,0},
{0,1,0,0,0,0,1,1,1,0},
{0,1,2,0,0,3,3,0,1,0},
{0,1,1,2,0,0,3,4,1,0},
{0,0,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,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1,0},
{0,1,0,3,0,3,0,0,1,0},
{0,1,0,0,4,0,0,0,1,0},
{0,1,0,3,0,3,0,0,1,0},
{0,1,2,0,0,0,0,2,1,0},
{0,1,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,1,1,1,0,0,0},
{0,1,1,1,0,2,1,0,0,0},
{0,1,0,0,0,0,1,1,1,0},
{0,1,2,0,0,3,3,0,1,0},
{0,1,1,2,0,0,3,4,1,0},
{0,0,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0,0,0},
{0,0,1,1,1,1,1,1,0,0},
{0,1,2,2,3,0,0,2,1,0},
{0,1,3,1,0,0,1,3,1,0},
{0,1,0,0,0,0,0,0,1,0},
{0,1,0,1,1,1,1,0,1,0},
{0,1,0,0,0,0,0,0,1,0},
{0,0,1,0,4,0,0,1,0,0},
{0,0,0,1,1,1,1,0,0,0},
{0,0,0,0,0,0,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,0},
{0,1,2,3,0,0,3,2,1,0},
{0,1,0,1,0,0,1,0,1,0},
{0,1,0,1,0,0,1,0,1,0},
{0,1,0,1,4,0,1,0,1,0},
{0,1,0,1,0,0,1,0,1,0},
{0,1,2,3,0,0,3,2,1,0},
{0,1,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0}
},
};
void ShowMap()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (Map[LEVEL][i][j] == Space)
{
putimage(j * 63, i * 63, img + 0);
printf(" ");
}
else if (Map[LEVEL][i][j] == Wall)
{
putimage(j * 63, i * 63, img + 1);
printf("▊");
}
else if (Map[LEVEL][i][j] == Final)
{
putimage(j * 63, i * 63, img + 2);
printf("☆");
}
else if (Map[LEVEL][i][j] == Box)
{
putimage(j * 63, i * 63, img + 3);
printf("★");
}
else if (Map[LEVEL][i][j] == Player)
{
putimage(j * 63, i * 63, img + 4);
printf("♀");
}
else if (Map[LEVEL][i][j] == Box + Final)
{
putimage(j * 63, i * 63, img + 5);
printf("★");
}
else if (Map[LEVEL][i][j] == Final + Player)
{
putimage(j * 63, i * 63, img + 4);
printf("♂");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
void ReSetLevel()
{
for (int s = 0; s < 10; s++)
{
for (int b = 0; b < 10; b++)
{
Map[LEVEL][s][b] = FakeMap[LEVEL][s][b];
}
}
}
void PushBox()
{
int i = 0;
int j = 0;
int flag = 0;
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
if (Map[LEVEL][i][j] == Player || Map[LEVEL][i][j] == Player + Final)
{
flag = 1;
break;
}
}
if (flag == 1)
{
break;
}
}
if (LEVEL < 4)
{
printf("按空格重置关卡\n");
printf("按回车跳过关卡\n");
printf("当前步数%d", Step);
}
char KeyBorad = _getch();
switch (KeyBorad)
{
case 'w':
case 'W':
case 72:
{
if (Map[LEVEL][i - 1][j] == Space || Map[LEVEL][i - 1][j] == Final)
{
Map[LEVEL][i - 1][j] += Player;
Map[LEVEL][i][j] -= Player;
Step++;
}
else if (Map[LEVEL][i - 1][j] == Box)
{
if (Map[LEVEL][i - 2][j] == Space || Map[LEVEL][i - 2][j] == Final)
{
Map[LEVEL][i - 2][j] += Box;
Map[LEVEL][i - 1][j] -= Box;
Map[LEVEL][i - 1][j] += Player;
Map[LEVEL][i][j] -= Player;
Step++;
}
}
break;
}
case 's':
case 'S':
case 80:
{
if (Map[LEVEL][i + 1][j] == Space || Map[LEVEL][i + 1][j] == Final)
{
Map[LEVEL][i + 1][j] += Player;
Map[LEVEL][i][j] -= Player;
Step++;
}
else if (Map[LEVEL][i + 1][j] == Box)
{
if (Map[LEVEL][i + 2][j] == Space || Map[LEVEL][i + 2][j] == Final)
{
Map[LEVEL][i + 2][j] += Box;
Map[LEVEL][i + 1][j] -= Box;
Map[LEVEL][i + 1][j] += Player;
Map[LEVEL][i][j] -= Player;
Step++;
}
}
break;
}
case 'a':
case'A':
case 75:
{
if (Map[LEVEL][i][j - 1] == Space || Map[LEVEL][i][j - 1] == Final)
{
Map[LEVEL][i][j - 1] += Player;
Map[LEVEL][i][j] -= Player;
Step++;
}
else if (Map[LEVEL][i][j - 1] == Box)
{
if (Map[LEVEL][i][j - 2] == Space || Map[LEVEL][i][j - 2] == Final)
{
Map[LEVEL][i][j - 2] += Box;
Map[LEVEL][i][j - 1] -= Box;
Map[LEVEL][i][j - 1] += Player;
Map[LEVEL][i][j] -= Player;
Step++;
}
}
break;
}
case 'd':
case 'D':
case 77:
{
if (Map[LEVEL][i][j + 1] == Space || Map[LEVEL][i][j + 1] == Final)
{
Map[LEVEL][i][j + 1] += Player;
Map[LEVEL][i][j] -= Player;
Step++;
}
else if (Map[LEVEL][i][j + 1] == Box)
{
if (Map[LEVEL][i][j + 2] == Space || Map[LEVEL][i][j + 2] == Final)
{
Map[LEVEL][i][j + 2] += Box;
Map[LEVEL][i][j + 1] -= Box;
Map[LEVEL][i][j + 1] += Player;
Map[LEVEL][i][j] -= Player;
Step++;
}
}
break;
}
case 32:
{
ReSetLevel();
ShowMap();
break;
}
case 13:
if (LEVEL < 5)
{
LEVEL++;
}
break;
}
}
bool PassLevel()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (Map[LEVEL][i][j] == Box)
{
return false;
}
}
}
ReSetLevel();
return true;
}
void PopWindow();
void StartGame()
{
ShowMap();
settextstyle(30, 0, NULL);
settextcolor(RGB(0, 0, 0));
setbkmode(TRANSPARENT);
outtextxy(0, 0, "当前步数:");
outtextxy(200,0,"第");
char q[5];
sprintf(q, "%d", LEVEL+1);
outtextxy(300,0, q);
outtextxy(380,0,"关");
char s[5];
sprintf(s, "%d", Step);
outtextxy(0, 30, s);
outtextxy(0, 570, "空格键重置当前关卡");
outtextxy(0, 600, "回车键跳过当前关卡");
if (PassLevel())
{
LEVEL++;
if (LEVEL > 5)
{
printf("恭喜你%s,完成了游戏\n", rparry[RankPerson].name);
printf("走了%d步\n", Step);
PopWindow();
}
}
PushBox();
}
void SavePlayerRecord()
{
FILE* fp = fopen("PlayerRecord.txt", "a");
if (fp == NULL)
{
printf("读取文件失败\n");
return;
}
fprintf(fp, "\t%s\t%d\n", rparry[RankPerson].name, rparry[RankPerson].steps);
fclose(fp);
system("pause");
system("cls");
}
void ReadRecordFile()
{
FILE* fp = fopen("PlayerRecord.txt", "r");
if (fp == NULL)
{
return;
}
for (int i = 0; i < RecordPlayerNums; i++)
{
fscanf(fp, "%s%d", rparry[i].name, &rparry[i].steps);
}
for (int i = 0; i < RecordPlayerNums; i++)
{
if (rparry[i].steps != 0)
{
RankPerson = ++FilePersonNums;
}
}
fclose(fp);
}
void RecordBubbSort()
{
for (int i = 0; i < RankPerson; i++)
{
RpArry Temprparry;
for (int j = 1; j < RankPerson - i; j++)
{
if (rparry[j - 1].steps >= rparry[j].steps)
{
Temprparry = rparry[j];
rparry[j] = rparry[j - 1];
rparry[j - 1] = Temprparry;
}
}
}
}
void DeletePlayerRecord()
{
remove("PlayerRecord.txt");
RankPerson = 0;
printf("清空完毕!\n");
}
void GraphInputName();
void GraphRankPage();
void GraphFirImagesPg()
{
MOUSEMSG m;
initgraph(ROW * 63, COL * 63);
Loadimage();
setbkcolor(BLACK);
cleardevice();
IMAGE picture1;
loadimage(&picture1, "./topbc.jpg", 630, 630);
putimage(0, 0, &picture1);
setfillcolor(GREEN);
fillrectangle(230, 405, 400, 430);
fillrectangle(230, 445, 400, 470);
fillrectangle(230, 485, 400, 510);
settextstyle(85, 0, "黑体");
char Title[] = "推箱子";
char Select1[] = "开始游戏";
char Select2[] = "排行榜";
char Select3[] = "退出游戏";
outtextxy(210, 150, Title);
settextstyle(30, 0, NULL);
setbkmode(TRANSPARENT);
outtextxy(252, 405, Select1);
outtextxy(268, 445, Select2);
outtextxy(255, 485, Select3);
while (1)
{
m = GetMouseMsg();
if (m.x >= 230 && m.x <= 400 && m.y >= 405 && m.y <= 430)
{
setlinecolor(RED);
rectangle(220, 400, 410, 440);
if (m.uMsg == WM_LBUTTONDOWN)
{
GraphInputName();
cleardevice();
while (LEVEL < 6)
{
StartGame();
}
rparry[RankPerson].steps = Step;
SavePlayerRecord();
LEVEL = 0;
Step = 0;
RankPerson++;
ReSetLevel();
cleardevice();
GraphFirImagesPg();
}
}
else if (m.x >= 230 && m.x <= 400 && m.y >= 445 && m.y <= 470)
{
setlinecolor(RED);
rectangle(220, 440, 410, 480);
if (m.uMsg == WM_LBUTTONDOWN)
{
cleardevice();
GraphRankPage();
}
}
else if (m.x >= 230 && m.x <= 400 && m.y >= 485 && m.y <= 510)
{
setlinecolor(RED);
rectangle(220, 480, 410, 520);
if (m.uMsg == WM_LBUTTONDOWN)
{
exit(0);
break;
}
}
else
{
setlinecolor(BLACK);
rectangle(220, 400, 410, 440);
rectangle(220, 440, 410, 480);
rectangle(220, 480, 410, 520);
}
}
}
void GraphInputName()
{
TCHAR InputWindow[] = _T("请输入你的昵称");
InputBox(rparry[RankPerson].name, 10, InputWindow);
}
void PopWindow()
{
HWND hnd = GetHWnd();
int isok = MessageBox(hnd, "恭喜通关", "提示", MB_OK);
}
void GraphDeleteRankPage();
void GraphRankPage()
{
MOUSEMSG m1;
cleardevice();
setbkcolor(BLACK);
cleardevice();
setfillcolor(GREEN);
fillrectangle(440, 90, 580, 140);
fillrectangle(440, 150, 580, 200);
settextstyle(30, 0, "黑体");
setbkmode(TRANSPARENT);
char Delete[10] = { "清空数据" };
char Back[10] = { "返回" };
outtextxy(480, 158, Back);
outtextxy(450, 100, Delete);
outtextxy(60, 100, "排名");
outtextxy(220, 100, "昵称");
outtextxy(350, 100, "步数");
if (RankPerson == 0)
{
settextstyle(50, 0, "黑体");
char TIPS[20] = { "当前记录为空!" };
outtextxy(130, 238, TIPS);
}
RecordBubbSort();
for (int i = 1; i <= RankPerson; i++)
{
char a[5];
sprintf(a, "%d", rparry[i - 1].steps);
char b[5];
sprintf(b, "%d", i);
outtextxy(60, 100 + (50 * i), b);
outtextxy(220, 100 + (50 * i), rparry[i - 1].name);
outtextxy(350, 100 + (50 * i), a);
}
while (1)
{
m1 = GetMouseMsg();
if (m1.x >= 440 && m1.x <= 580 && m1.y >= 85 && m1.y <= 150)
{
rectangle(430, 85, 590, 150);
setlinecolor(RED);
if (m1.uMsg == WM_LBUTTONDOWN)
{
GraphDeleteRankPage();
cleardevice();
GraphRankPage();
}
}
else if (m1.x>=440 && m1.x<=580&&m1.y >=150 && m1.y<200)
{
rectangle(430, 145, 590, 210);
setlinecolor(RED);
if (m1.uMsg == WM_LBUTTONDOWN)
{
cleardevice();
GraphFirImagesPg();
}
}
else
{
setlinecolor(BLACK);
rectangle(430, 85, 590, 150);
rectangle(430, 145, 590, 210);
}
}
}
void GraphDeleteRankPage()
{
HWND hnd = GetHWnd();
int isok = MessageBox(hnd, "确认清空?", "提示", MB_OKCANCEL);
if (isok == IDOK)
{
int tips1 = MessageBox(hnd, "清空成功!", "提示", MB_OK);
DeletePlayerRecord();
}
else if (isok == IDCANCEL)
{
}
}
int main(void)
{
ReadRecordFile();
GraphFirImagesPg();
return 0;
}
|