问题:读取存档后只有dt(地图)2维数组的不显示
注:+键存档(进入世界后)
不了解具体情况可以试试存储一个世界,然后读档
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
int dt[150][500];
int pmx1, pmx2, pmy1, pmy2, mx, my; // 屏幕xy和角色xy
int dmgd; // 地面高度
string bb[12]; int bbs[12] = {0}; char id[12]; // 背包与物品ID
int zzx, zzy; // 游戏指针坐标(zzx,zzy)
char jw[20]; int zz_jw; // 键位和键位指针
int tick = 10; // 游戏刻(10tick/s)
int kscd_zz = 0; // 主菜单指针
void bc() {
system("cls");
cout << "要等一下存档有点慢,\n看到有存档文件就可以关掉程序了";
Sleep(10000);
FILE* fp = fopen("sj.json", "w");
if (fp == NULL) {
cout << "无法打开文件进行写入" << endl;
return;
}
for (int i = 0; i < 150; i++) {
for (int j = 0; j < 500; j++) {
fprintf(fp, "%d ", dt[i][j]);
}
fprintf(fp, "\n");
}
fprintf(fp, "%d %d %d %d\n", pmx1, pmx2, mx, my);
for (int i = 0; i <= 10; i++) {
fprintf(fp, "%d\n", bbs[i]);
}
fclose(fp);
}
void jw_ui() {
string sc_pm; // 单缓冲存储
sc_pm += "按键:\n上"; sc_pm += jw[1]; if (zz_jw == 1) { sc_pm += "√\n"; } else { sc_pm += "\n"; };
sc_pm += "下"; sc_pm += jw[2]; if (zz_jw == 2) { sc_pm += "√\n"; } else { sc_pm += "\n"; };
sc_pm += "左"; sc_pm += jw[3]; if (zz_jw == 3) { sc_pm += "√\n"; } else { sc_pm += "\n"; };
sc_pm += "右"; sc_pm += jw[4]; if (zz_jw == 4) { sc_pm += "√\n"; } else { sc_pm += "\n"; };
sc_pm += "背包"; sc_pm += jw[5]; if (zz_jw == 5) { sc_pm += "√\n"; } else { sc_pm += "\n"; };
sc_pm += "摧毁"; sc_pm += jw[6]; if (zz_jw == 6) { sc_pm += "√\n"; } else { sc_pm += "\n"; };
sc_pm += "放置"; sc_pm += jw[7]; if (zz_jw == 7) { sc_pm += "√\n"; } else { sc_pm += "\n"; };
sc_pm += "放置确定"; sc_pm += jw[8]; if (zz_jw == 8) { sc_pm += "√\n"; } else { sc_pm += "\n"; };
sc_pm += "切换指针模式"; sc_pm += jw[9]; if (zz_jw == 9) { sc_pm += "√\n"; } else { sc_pm += "\n"; };
sc_pm += "改按键"; sc_pm += jw[10]; if (zz_jw == 10) { sc_pm += "√\n"; } else { sc_pm += "\n"; };
sc_pm += "按键查看"; sc_pm += jw[11]; if (zz_jw == 11) { sc_pm += "√\n"; } else { sc_pm += "\n"; };
system("cls");
cout << sc_pm; // 输出缓冲
}
void dthz_1() {
string sc_pm; // 单缓冲存储
for (int i = pmy1; i < pmy2; i++) {
for (int j = pmx1; j < pmx2; j++) {
if (i == my && j == mx) { sc_pm += "@"; }
else if (dt[i][j] == 0) { sc_pm += " "; }
else if (dt[i][j] == 1) { sc_pm += id[1]; }
else if (dt[i][j] == 2) { sc_pm += id[2]; }
else if (dt[i][j] == 3) { sc_pm += id[3]; }
else if (dt[i][j] == 4) { sc_pm += id[4]; }
else if (dt[i][j] == 5) { sc_pm += id[5]; }
else if (dt[i][j] == 6) { sc_pm += id[6]; }
else if (dt[i][j] == 7) { sc_pm += id[7]; }
else if (dt[i][j] == 8) { sc_pm += id[8]; }
else if (dt[i][j] == 9) { sc_pm += id[9]; }
else if (dt[i][j] == 10) { sc_pm += id[10]; }
}
sc_pm += "\n";
}
system("cls");
cout << sc_pm; // 输出缓冲
cout << "屏幕最左x:" << pmx1 << "\n屏幕最右x:" << pmx2 << "\n屏幕最上" << 150 - pmy1 << "\n屏幕最下" << 150 - pmy2 << "\n我的坐标" << "(" << mx << "," << 150 - my << ")";
}
void dthz_2() {
string sc_pm; // 单缓冲存储
for (int i = pmy1; i < pmy2; i++) {
for (int j = pmx1; j < pmx2; j++) {
if (i == zzy && j == zzx) { sc_pm += "."; }
else if (i == my && j == mx) { sc_pm += "@"; }
else if (dt[i][j] == 0) { sc_pm += " "; }
else if (dt[i][j] == 1) { sc_pm += id[1]; }
else if (dt[i][j] == 2) { sc_pm += id[2]; }
else if (dt[i][j] == 3) { sc_pm += id[3]; }
else if (dt[i][j] == 4) { sc_pm += id[4]; }
else if (dt[i][j] == 5) { sc_pm += id[5]; }
else if (dt[i][j] == 6) { sc_pm += id[6]; }
else if (dt[i][j] == 7) { sc_pm += id[7]; }
else if (dt[i][j] == 8) { sc_pm += id[8]; }
else if (dt[i][j] == 9) { sc_pm += id[9]; }
else if (dt[i][j] == 10) { sc_pm += id[10]; }
}
sc_pm += "\n";
}
system("cls");
cout << sc_pm; // 输出缓冲
cout << "屏幕最左x:" << pmx1 << "\n屏幕最右x:" << pmx2 << "\n屏幕最上" << 150 - pmy1 << "\n屏幕最下" << 150 - pmy2 << "\n我的坐标" << "(" << mx << "," << 150 - my << ")";
}
void zl() {
if (dt[my + 2][mx] == 0 && dt[my + 1][mx] == 0) {
my++;
if (pmy2 - my <= 3) {
pmy1++;
pmy2 = pmy1 + 9;
}
dthz_1();
}
}
void aj_jc() {
if (kbhit()) {
char ch1 = getch();
if (ch1 == jw[12]) {
bc();
return;
}
if (ch1 == jw[3]) {
mx--;
if ((mx - pmx1) < 10) {
pmx1--;
pmx2 = pmx1 + 31;
}
if (mx == 0 || dt[my][mx] != 0) {
mx++;
}
else {
if (pmx1 == 0) {
pmx1++;
pmx2 = pmx1 + 31;
}
dthz_1();
}
}
if (ch1 == jw[4]) {
mx++;
if ((pmx2 - mx) < 10) {
pmx1++;
pmx2 = pmx1 + 31;
}
if (mx == 500 || dt[my][mx] != 0) {
mx--;
}
else {
if (pmx2 == 501) {
pmx1--;
pmx2 = pmx1 + 31;
}
dthz_1();
}
}
if (ch1 == jw[1]) {
if (my != 1) {
if (my - pmy1 == 3) {
pmy1--;
pmy2 = pmy1 + 9;
}
if (dt[my + 1][mx] != 0 && dt[my - 1][mx] == 0) {
my--;
dthz_1();
}
}
}
if (ch1 == jw[2]) {
if (my != 149) {
if (pmy2 - my == 3) {
pmy1++;
pmy2 = pmy1 + 9;
}
if (dt[my + 1][mx] == 0) {
my++;
dthz_1();
}
}
}
if (ch1 == jw[5]) {
system("cls");
cout << "背包:\n";
for (int i = 1; i <= 10; i++) {
cout << bb[i] << "有:" << bbs[i] << "个\n";
}
while (1) {
if (kbhit()) {
char ch2 = getch();
if (ch2 != jw[5] && ch2 != jw[11]) {
dthz_1();
break;
}
}
}
}
if (ch1 == jw[9]) {
zzx = mx; zzy = my;
dthz_2();
while (1) {
if (kbhit()) {
char ch2 = getch();
if (ch2 == jw[9]) {
dthz_1();
break;
}
if (ch2 == jw[3]) {
if (zzx - 1 != mx && zzx != 1) {
zzx--;
dthz_2();
}
}
if (ch2 == jw[4]) {
if (zzx + 1 != mx && zzx != 500) {
zzx++;
dthz_2();
}
}
if (ch2 == jw[1]) {
if (zzy - 1 != my && zzy != 0) {
zzy--;
dthz_2();
}
}
if (ch2 == jw[2]) {
if (zzy + 1 != my && zzy != 149) {
zzy++;
dthz_2();
}
}
if (ch2 == jw[6]) {
if (zzx != mx || zzy != my) {
if (dt[zzy][zzx] != 0) {
bbs[dt[zzy][zzx]] += 1;
dt[zzy][zzx] = 0;
dthz_2();
}
}
}
if (ch2 == jw[7]) {
if (zzx != mx || zzy != my) {
if (dt[zzy][zzx] == 0) {
int bbzz = 1;
system("cls");
cout << "背包:\n";
for (int i = 1; i <= 10; i++) {
cout << bb[i] << "有:" << bbs[i] << "个";
if (i == bbzz) {
cout << "√\n";
}
else {
cout << "\n";
}
}
while (1) {
if (kbhit()) {
char ch2 = getch();
if (ch2 == jw[4] || ch2 == jw[2]) {
bbzz++;
if (bbzz > 10) {
bbzz = 1;
}
system("cls");
cout << "背包:\n";
for (int i = 1; i <= 10; i++) {
cout << bb[i] << "有:" << bbs[i] << "个";
if (i == bbzz) {
cout << "√\n";
}
else {
cout << "\n";
}
}
}
if (ch2 == jw[1] || ch2 == jw[3]) {
bbzz--;
if (bbzz < 1) {
bbzz = 10;
}
system("cls");
cout << "背包:\n";
for (int i = 1; i <= 10; i++) {
cout << bb[i] << "有:" << bbs[i] << "个";
if (i == bbzz) {
cout << "√\n";
}
else {
cout << "\n";
}
}
}
if (ch2 == jw[8]) {
if (bbs[bbzz] - 1 >= 0) {
bbs[bbzz]--;
dt[zzy][zzx] = bbzz;
dthz_2();
break;
}
}
if (ch2 == jw[7]) {
dthz_2();
break;
}
}
}
}
}
}
}
Sleep(1000 / tick);
}
}
if (ch1 == jw[11]) {
system("cls");
cout << "按键说明:\n正常模式:" << jw[1] << "上 " << jw[2] << "下 " << jw[3] << "左 " << jw[4] << "右 " << jw[9] << "切换指针模式 " << jw[11] << "按键说明 " << jw[5] << "背包 " << jw[10] << "修改键位\n\n指针模式:" << jw[1] << "上 " << jw[2] << "下 " << jw[3] << "左 " << jw[4] << "右 " << jw[9] << "切换正常模式 " << jw[6] << "挖掘 " << jw[7] << "防置(空格确认)(用wsad选择)\n按" << jw[11] << "即可退出哦~~~";
while (1) {
if (kbhit()) {
char ch2 = getch();
if (ch2 == jw[11]) {
dthz_1();
break;
}
}
}
}
if (ch1 == jw[10]) {
zz_jw = 1;
jw_ui();
while (1) {
if (kbhit()) {
char ch2 = getch();
if (ch2 == jw[3] || ch2 == jw[1]) {
if (zz_jw != 1) {
zz_jw--;
jw_ui();
}
}
if (ch2 == jw[4] || ch2 == jw[2]) {
if (zz_jw != 11) {
zz_jw++;
jw_ui();
}
}
if (ch2 == jw[8]) {
system("cls");
cout << "改成?";
while (1) {
if (kbhit()) {
char ch3 = getch();
jw[zz_jw] = ch3;
jw_ui();
break;
}
}
}
if (ch2 == jw[10]) {
dthz_1();
break;
}
}
}
}
}
}
/*开始菜单*/
void kscd_hz() {
system("cls");
cout << "1.新游戏"; if (kscd_zz == 0) { cout << "√"; } cout << endl;
cout << "2.我要读档!!!"; if (kscd_zz == 1) { cout << "√"; } cout << endl;
cout << "空格确定" << endl;
}
/*梦开始的地方*/
int main() {
/*窗口设置*/
system("mode con cols=36 lines=20");
system("color 3F");
cout << "按w,s来选择吧!" << endl; kscd_hz();
while (1) {
if (kbhit()) {
char kscd_ch;
kscd_ch = getch();
if (kscd_ch == 'w') {
kscd_zz--;
if (kscd_zz == -1) {
kscd_zz++;
}
kscd_hz();
}
if (kscd_ch == 's') {
kscd_zz++;
if (kscd_zz == 2) {
kscd_zz--;
}
kscd_hz();
}
if (kscd_ch == ' ') {
break;
}
}
}
id[1] = '^'; // ^
id[2] = '#'; // #
id[3] = '$'; // $
id[4] = '%'; // %
id[5] = '*'; // *
id[6] = ' '; //
id[7] = ' '; //
id[8] = ' '; //
id[9] = ' '; //
id[10] = ' '; //
bb[1] = "泥土";
bb[2] = "石头";
bb[3] = "原木";
bb[4] = "树叶";
bb[5] = "树苗";
bb[6] = "未知";
bb[7] = "未知";
bb[8] = "未知";
bb[9] = "未知";
bb[10] = "未知";
jw[1] = 'w';
jw[2] = 's';
jw[3] = 'a';
jw[4] = 'd';
jw[5] = 'b';
jw[6] = 'j';
jw[7] = 'k';
jw[8] = ' ';
jw[9] = '~';
jw[10] = '?';
jw[11] = 'p';
jw[12] = '+';
if (kscd_zz == 0) {
cout << "设置*屏幕最左x(0~469):";
cin >> pmx1; pmx2 = pmx1 + 31;
while (pmx1 <= -1 || pmx2 >= 501) {
cout << "重输入(0~469):";
cin >> pmx1;
pmx2 = pmx1 + 31;
}
cout << "设置*地面高度(145~5):";
cin >> dmgd;
while (dmgd < 5 || dmgd > 145) {
cout << "设置*地面高度(145~5):";
cin >> dmgd;
}
dmgd = 150 - dmgd;
mx = pmx1 + 16;
my = dmgd - 1;
pmy1 = my - 4;
pmy2 = pmy1 + 9;
for (int i = 0; i < 150; i++) { // 生成地形与树苗
for (int j = 0; j < 500; j++) {
if (i == dmgd) {
dt[i][j] = 1;
}
else if (i > dmgd) {
dt[i][j] = 2;
}
else {
int sj_tree;
sj_tree = rand() % 10;
if (sj_tree == 0) {
dt[i][j] = 5;
}
else {
dt[i][j] = 0;
}
}
}
}
for (int i = 1; i < 150; i++) { // 移除浮空树苗
for (int j = 0; j < 500; j++) {
if (dt[i][j] == 5) {
if (dt[i + 1][j] != 1) {
dt[i][j] = 0;
}
}
}
}
for (int i = 3; i < 150; i++) { // 树苗拓展生成树
for (int j = 2; j < 498; j++) {
if (dt[i][j] == 5) {
dt[i][j] = 3; dt[i - 1][j] = 3; dt[i - 2][j] = 3;
dt[i - 1][j + 1] = 4; dt[i - 1][j - 1] = 4; dt[i - 1][j + 2] = 4; dt[i - 1][j - 2] = 4;
dt[i - 2][j + 1] = 4; dt[i - 2][j - 1] = 4; dt[i - 2][j + 2] = 4; dt[i - 2][j - 2] = 4;
dt[i - 3][j + 1] = 4; dt[i - 3][j - 1] = 4; dt[i - 3][j] = 4;
j += 3; // 防止连续生成树,但又不破坏树苗
}
}
}
}
else if (kscd_zz == 1) {
FILE* fp = fopen("sj.json", "r");
if (fp == NULL) {
cout << "无法打开文件进行读取" << endl;
return 1;
}
for (int i = 0; i < 150; i++) {
for (int j = 0; j < 500; j++) {
fscanf(fp, "%d", &dt[i][j]);
}
}
fscanf(fp, "%d %d %d %d", &pmx1, &pmx2, &mx, &my);
for (int i = 0; i <= 10; i++) {
fscanf(fp, "%d", &bbs[i]);
}
fclose(fp);
}
dthz_1();
cout << "\n@是你哦~\n如果你是新手得话,记得按p查看操作哦~";
while (1) {
aj_jc(); // 检测按键
zl(); // 游戏重力检测
Sleep(1000 / tick); // 游戏刻
}
return 0;
}