Skip to content

Commit b4b6be7

Browse files
authored
代码方面的小优化
1 parent 1d63c21 commit b4b6be7

File tree

10 files changed

+173
-46
lines changed

10 files changed

+173
-46
lines changed

MapReader.exe

5.5 KB
Binary file not shown.

README-EN.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ There are three optional parameter items `save`, `music` and `title`. The method
4545

4646
### V2.4
4747

48+
#### V2.4.3
49+
50+
1. Added a new file `settings.h`, which extracts the problem of option settings separately, and has some minor optimizations and updates to the display interface.
51+
4852
#### V2.4.2
4953

5054
1. Fixed command line issues, see command line parameters for details.
@@ -57,13 +61,13 @@ There are three optional parameter items `save`, `music` and `title`. The method
5761

5862
2. Fixed the issue of read-only corruption in `MapReader`.
5963

60-
3. Add a command line parameter that allows you to modify the title by specifying '-title=' on the command line (provided an archive file is passed in first), such as `main.exe data.mymap -title=FirstMap`. When displaying the title, your title will be attached, in the form of `我的地图(My-Map)FirstMap'. Please note that the title cannot have spaces. (Not actually installed)
64+
3. Add a command line parameter that allows you to modify the title by specifying `-title=` on the command line (provided an archive file is passed in first), such as `main.exe data.mymap -title=FirstMap`. When displaying the title, your title will be attached, in the form of `我的地图(My-Map)FirstMap`. Please note that the title cannot have spaces. (Not actually installed)
6165

6266
#### V2.4.0
6367

64-
1. A new folder 'example' has been added, where you can view examples of the 'mymap' file. We strongly recommend trying to pass the 'maze. mymap' level. This file utilizes some bugs, but I will make it a setting in future technical adjustments. You can wait for it!
68+
1. A new folder `example` has been added, where you can view examples of the `mymap` file. We strongly recommend trying to pass the `maze. mymap` level. This file utilizes some bugs, but I will make it a setting in future technical adjustments. You can wait for it!
6569

66-
2. 'init. h' has been deprecated and its functionality has been decomposed into 'Screen. h' and 'help'_ func.h`。 If there are no issues in the future, I will remove them.
70+
2. `init. h` has been deprecated and its functionality has been decomposed into `Screen. h` and `help_func.h`。 If there are no issues in the future, I will remove them.
6771

6872
### V2.3
6973

@@ -81,11 +85,11 @@ You can play the music now.
8185

8286
1. Add two new achievements (extremely difficult to obtain)!
8387

84-
2. Added a directory for 'README.md'.
88+
2. Added a directory for `README.md`.
8589

8690
#### V2.3.5
8791

88-
Archive updated again! Added language options. In addition, if you do not want to use the default archive location, you can also open the archive file with using 'main.exe'.
92+
Archive updated again! Added language options. In addition, if you do not want to use the default archive location, you can also open the archive file with using `main.exe`.
8993

9094
#### V2.3.4
9195

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ main.exe
4444

4545
### V2.4
4646

47+
#### V2.4.3
48+
49+
1. 新增了一个文件 `settings.h`,将选项设置的问题单独抽出来,有一些小优化和显示界面的更新。
50+
4751
#### V2.4.2
4852

4953
1. 修复了命令行的问题,具体见命令行参数。

defines/Screen.h

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include<fstream>
88
#include<direct.h>
99
#include "def"
10+
#include "settings.h"
1011
class Egg;
1112
class Map;
1213
class People;
@@ -15,6 +16,7 @@ class Screen{
1516
Egg &egg;
1617
Map &map;
1718
People &people;
19+
Settings sett;
1820
HANDLE handle;
1921
std::string gametitle;
2022
public:
@@ -75,6 +77,14 @@ void Screen::import(){
7577
}
7678
Screen::Screen(Egg &_egg,Map &_m,People &_p,bool isedit,int argc,char ** argv):egg(_egg),map(_m),people(_p),gametitle(defaut_title(isedit)){
7779
handle=GetStdHandle(STD_OUTPUT_HANDLE);
80+
sett=Settings(std::vector<Settings::setting>({
81+
Settings::setting({"","模式","mode"},&data.mode,vector<Settings::setting::option>({
82+
Settings::setting::option({"","正常的模式","normal"}),
83+
Settings::setting::option({"","开发者模式","debugger"}),
84+
Settings::setting::option({"","地狱的模式","infernal"}),
85+
Settings::setting::option({"","清一色模式","all in one color"})
86+
}))
87+
}));
7888
Argument arg(argc,argv);
7989
if(arg.find("title").size()){
8090
gametitle+=arg.find("title");
@@ -125,41 +135,42 @@ Screen::~Screen(){
125135
save();
126136
}
127137
void Screen::setting(){
128-
int choose;
129-
do{
130-
clear();
131-
if(data.lang==Chinese){
132-
//language:Chinese
133-
//语言:中文
134-
std::cout << "模式:";
135-
} else if(data.lang==English){
136-
//language:English
137-
//语言:英文
138-
std::cout << "pattern:";
139-
}
140-
std::cout << mode_content[data.lang][data.mode];
141-
if(data.lang==Chinese){
142-
//language:Chinese
143-
//语言:中文
144-
std::cout << "\n按左右键选择,按q键退出。";
145-
} else if(data.lang==English){
146-
//language:English
147-
//语言:英文
148-
std::cout << "\nPress the left and right keys to select, and press the q key to exit.";
149-
}
150-
choose=getch();
151-
if(choose==224){
152-
choose=getch();
153-
}
154-
switch(choose){
155-
case 77://右键
156-
data.mode=(data.mode+1)%MODE_COUNT;
157-
break;
158-
case 75://左键
159-
data.mode=(data.mode-1+MODE_COUNT)%MODE_COUNT;
160-
break;
161-
}
162-
} while(choose!='q' && choose!='Q');
138+
// int choose;
139+
// do{
140+
// clear();
141+
// if(data.lang==Chinese){
142+
// //language:Chinese
143+
// //语言:中文
144+
// std::cout << "模式:";
145+
// } else if(data.lang==English){
146+
// //language:English
147+
// //语言:英文
148+
// std::cout << "pattern:";
149+
// }
150+
// std::cout << mode_content[data.lang][data.mode];
151+
// if(data.lang==Chinese){
152+
// //language:Chinese
153+
// //语言:中文
154+
// std::cout << "\n按左右键选择,按q键退出。";
155+
// } else if(data.lang==English){
156+
// //language:English
157+
// //语言:英文
158+
// std::cout << "\nPress the left and right keys to select, and press the q key to exit.";
159+
// }
160+
// choose=getch();
161+
// if(choose==224){
162+
// choose=getch();
163+
// }
164+
// switch(choose){
165+
// case 77://右键
166+
// data.mode=(data.mode+1)%MODE_COUNT;
167+
// break;
168+
// case 75://左键
169+
// data.mode=(data.mode-1+MODE_COUNT)%MODE_COUNT;
170+
// break;
171+
// }
172+
// } while(choose!='q' && choose!='Q');
173+
sett.show(data.lang);
163174
}
164175
void Screen::alert(const std::string &title,const std::string &content){
165176
MessageBox(NULL,content.c_str(),title.c_str(),MB_OK);

defines/def

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* 作者:C-cpp-a(Github)
3-
* 最近更新:2023/5/9
3+
* 最近更新:2023/2/3
44
* 功能简述:这是游戏的基本定义部分。负责常量、enum。当然为了游戏的编写方便,这里的显示字符映射使用了std::unordered_map。
55
*/
66
//For some reasons, the English version notes will not be displayed here.
@@ -51,12 +51,14 @@ enum{//键盘操作
5151
DIRECT_ASCII=224,
5252
DIRECT_RIGHT_ASCII=77,
5353
DIRECT_LEFT_ASCII=75,
54+
DIRECT_DOWN_ASCII=80,
55+
DIRECT_UP_ASCII=72,
5456
KEY_UP='W',
5557
KEY_DOWN='A',
5658
KEY_LEFT='S',
5759
KEY_RIGHT='D'
5860
};
59-
std::unordered_map<std::size_t,char> print_c={//对应元素显示成什么样
61+
std::unordered_map<int,char> print_c={//对应元素显示成什么样
6062
{floors,' '},
6163
{rock,'-'},
6264
{door,'@'},

defines/help_func.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class Screen;
1313
namespace{
1414
using namespace std;
15-
void print(const string s);
15+
void print(const string &s);
1616
void gotoxy(short x,short y);
1717
int ach_print(const string s,short starty);
1818
void map_print(const char &ch,Screen &screen);
@@ -65,7 +65,7 @@ namespace{
6565
}
6666
#include "Screen.h"
6767
namespace{
68-
void print(const string s){
68+
void print(const string &s){
6969
for(size_t len=s.length(),i=0U;i<len;i++){
7070
putchar(s[i]);
7171
Sleep(30);

defines/settings.h

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#ifndef SETTINGS_H_H
2+
#define SETTINGS_H_H
3+
#include<string>
4+
#include<vector>
5+
#include<iostream>
6+
#include<initializer_list>
7+
#include<conio.h>
8+
#include "def"
9+
class Settings{
10+
public:
11+
struct setting{
12+
struct option{
13+
//选项
14+
std::string opt_name[LANGUAGE_COUNT];//选项名字
15+
option(std::initializer_list<std::string> init_list){
16+
int j=0;
17+
for(std::initializer_list<std::string>::const_iterator it=init_list.begin();it!=init_list.end() && j<LANGUAGE_COUNT;it++){
18+
opt_name[j++]=*it;
19+
}
20+
}
21+
};
22+
int * thingset;//被更改的数
23+
int nowchoose;//当前选项
24+
std::string settingname[LANGUAGE_COUNT];//设置项名字
25+
std::vector<option> opts;//选项
26+
setting(std::initializer_list<std::string> init_name,int * _thing,const std::vector<option> &_opt):thingset(_thing),nowchoose(0),opts(_opt){
27+
int j=0;
28+
for(std::initializer_list<std::string>::const_iterator it=init_name.begin();it!=init_name.end() && j<LANGUAGE_COUNT;it++){
29+
settingname[j++]=*it;
30+
}
31+
}
32+
void onshow(int _Lang) const{
33+
std::cout << settingname[_Lang] << ":< " << opts[nowchoose].opt_name[_Lang] << " >";
34+
*thingset=nowchoose;
35+
}
36+
void left(){
37+
--nowchoose;
38+
if(nowchoose<0) nowchoose+=opts.size();
39+
*thingset=nowchoose;
40+
}
41+
void right(){
42+
++nowchoose;
43+
if(nowchoose>=opts.size()) nowchoose-=opts.size();
44+
*thingset=nowchoose;
45+
}
46+
};
47+
private:
48+
std::vector<setting> settings;
49+
int nowchoose;//正在选择的选项
50+
public:
51+
Settings(const std::vector<setting> &_set={}):settings(_set),nowchoose(0){}
52+
void screenprint(int _Lang) const{
53+
for(int i=0,siz=settings.size();i<siz;i++){
54+
if(i==nowchoose){
55+
std::cout << "> ";
56+
}
57+
settings[i].onshow(_Lang);
58+
std::cout << '\n';
59+
}
60+
if(_Lang==Chinese){
61+
//language:Chinese
62+
//语言:中文
63+
std::cout << "\n按左右键选择,按q键退出。";
64+
} else if(_Lang==English){
65+
//language:English
66+
//语言:英文
67+
std::cout << "\nPress the left and right keys to select, and press the q key to exit.";
68+
}
69+
}
70+
void show(int _Lang){
71+
while(true){
72+
system("cls");
73+
screenprint(_Lang);
74+
int op=toupper(getch());
75+
switch(op){
76+
case 'Q':
77+
//quit
78+
//退出
79+
return;
80+
case DIRECT_ASCII:
81+
op=toupper(getch());
82+
switch(op){
83+
case DIRECT_LEFT_ASCII:
84+
settings[nowchoose].left();
85+
break;
86+
case DIRECT_RIGHT_ASCII:
87+
settings[nowchoose].right();
88+
break;
89+
case DIRECT_UP_ASCII:
90+
if(nowchoose>0){
91+
--nowchoose;
92+
}
93+
break;
94+
case DIRECT_DOWN_ASCII:
95+
if(nowchoose<settings.size()-1){
96+
++nowchoose;
97+
}
98+
break;
99+
}
100+
}
101+
}
102+
103+
}
104+
};
105+
#endif

include_files.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Map;
77
class People;
88
class Egg;
99
#include "defines/def"
10+
#include "defines/settings.h"
1011
#include "defines/help_func.h"
1112
#include "defines/Map.h"
1213
#include "defines/People.h"

main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
22
* 作者:c-cpp-a(Github)
3-
* 最近更新:2024/1/28
3+
* 最近更新:2024/2/3
44
* 功能简述:这是整个游戏的主体部分,负责运行游戏。
55
*/
66
/*
77
* Author: c-cpp-a(Github)
8-
* Last updated: 2024/1/28
8+
* Last updated: 2024/2/3
99
* Function description: This is the main part of the whole game, responsible for running the game.
1010
*/
1111
//include STL

main.exe

5.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)