C++小游戏实现【小公主养成记】

1、效果展示


2、代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <iostream>
#include <windows.h>
#include <vector>
#include <array>
#include <iomanip>
#include "time.h"
using namespace std;
 
int main()
{
    /**
     * 1、为父女取名字
     * 2、为女儿录入初始信息(根据星座设置游戏角色基本参数)
     * 3、开始游戏大循环;
     *    1659年开始
     * 4、根据各项参数,判定游戏结果;
     **/
    SetConsoleTitle("小公主养成记-Sian");
    /**玩字名字*/
    string value_player_name;
    /**角色名字*/
    string value_role_name;
    /**生日月份*/
    int value_birth_m;
    /**生日日期*/
    int value_birth_d;
    int value_money = 500;
    /**角色星座*/
    string value_constellation;
    cout << "请输入玩家名字:";
    cin >> value_player_name;
    cout << "请输入小公主名字:";
    cin >> value_role_name;
    bool birth_check = false;
    while(birth_check == false)
    {
        cout << "请输入小公主的出生(MM DD)";
        cin >> value_birth_m >> value_birth_d;
        switch (value_birth_m){
            case 1:case 3:case 5:case 7:case 8:case 10:case 12:{
                if (value_birth_d <= 31)
                    birth_check = true;
            }break;
            case 4:case 6:case 9:case 11:{
                if (value_birth_d <= 30)
                    birth_check = true;
            }break;
            case 2:{
                if (value_birth_d <= 28)
                    birth_check = true;
            } break;
            default:{
                birth_check = false;
            }break;
        }
        if (birth_check == false) cout << "您输入的日期不合法,请重新输入" << endl;
    }
    // 计算星座
    /**星座名称数组*/
    vector <string> value_constellation_vec = {"魔羯座", "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座"};
    /**星座日期数组*/
    double value_constellation_days[] = {1.20, 2.19, 3.21, 4.20, 5.21, 6.22, 7.23, 8.23, 9.23, 10.24, 11.23, 12.22};
    double birthday = value_birth_m + value_birth_d / 100.0;
    int constellation_index = 0;
    for (unsigned int i = 0; i < value_constellation_vec.size(); i++){
        if (birthday < value_constellation_days[i]){
            constellation_index = i;
            break;
        }
    }
    value_constellation = value_constellation_vec[constellation_index];
    /**五项基本信息名称*/
    vector <string> value_role_basic_info_name_vec = {"体力", "智力", "魅力", "道德", "气质"};
    /**五项基本信息值*/
    array<int , 5> value_role_basic_info_value_array;
    switch(constellation_index)
    {
    case 0:    // 魔羯座
        value_role_basic_info_value_array[0] = 56;
        value_role_basic_info_value_array[1] = 21;
        value_role_basic_info_value_array[2] = 16;
        value_role_basic_info_value_array[3] = 25;
        value_role_basic_info_value_array[4] = 23;
        break;
    case 1:    // 水瓶座
        value_role_basic_info_value_array[0] = 43;
        value_role_basic_info_value_array[1] = 43;
        value_role_basic_info_value_array[2] = 20;
        value_role_basic_info_value_array[3] = 23;
        value_role_basic_info_value_array[4] = 17;
        break;
    case 2:    // 双鱼座
        value_role_basic_info_value_array[0] = 41;
        value_role_basic_info_value_array[1] = 20;
        value_role_basic_info_value_array[2] = 29;
        value_role_basic_info_value_array[3] = 23;
        value_role_basic_info_value_array[4] = 32;
        break;
    case 3:    // 白羊座
        value_role_basic_info_value_array[0] = 80;
        value_role_basic_info_value_array[1] = 15;
        value_role_basic_info_value_array[2] = 15;
        value_role_basic_info_value_array[3] = 11;
        value_role_basic_info_value_array[4] = 21;
        break;
    case 4:    // 金牛座
        value_role_basic_info_value_array[0] = 46;
        value_role_basic_info_value_array[1] = 30;
        value_role_basic_info_value_array[2] = 28;
        value_role_basic_info_value_array[3] = 20;
        value_role_basic_info_value_array[4] = 29;
        break;
    case 5:    // 双子座
        value_role_basic_info_value_array[0] = 50;
        value_role_basic_info_value_array[1] = 35;
        value_role_basic_info_value_array[2] = 23;
        value_role_basic_info_value_array[3] = 8;
        value_role_basic_info_value_array[4] = 20;
        break;
    case 6:    // 巨蟹座
        value_role_basic_info_value_array[0] = 40;
        value_role_basic_info_value_array[1] = 31;
        value_role_basic_info_value_array[2] = 33;
        value_role_basic_info_value_array[3] = 17;
        value_role_basic_info_value_array[4] = 33;
        break;
    case 7:    // 狮子座
        value_role_basic_info_value_array[0] = 85;
        value_role_basic_info_value_array[1] = 9;
        value_role_basic_info_value_array[2] = 11;
        value_role_basic_info_value_array[3] = 20;
        value_role_basic_info_value_array[4] = 37;
        break;
    case 8:    // 处女座
        value_role_basic_info_value_array[0] = 35;
        value_role_basic_info_value_array[1] = 28;
        value_role_basic_info_value_array[2] = 36;
        value_role_basic_info_value_array[3] = 18;
        value_role_basic_info_value_array[4] = 40;
        break;
    case 9:    // 天秤座
        value_role_basic_info_value_array[0] = 42;
        value_role_basic_info_value_array[1] = 33;
        value_role_basic_info_value_array[2] = 25;
        value_role_basic_info_value_array[3] = 32;
        value_role_basic_info_value_array[4] = 28;
        break;
    case 10:   // 天蝎座
        value_role_basic_info_value_array[0] = 50;
        value_role_basic_info_value_array[1] = 25;
        value_role_basic_info_value_array[2] = 40;
        value_role_basic_info_value_array[3] = 18;
        value_role_basic_info_value_array[4] = 17;
        break;
    case 11:   // 射手座
        value_role_basic_info_value_array[0] = 57;
        value_role_basic_info_value_array[1] = 31;
        value_role_basic_info_value_array[2] = 15;
        value_role_basic_info_value_array[3] = 19;
        value_role_basic_info_value_array[4] = 20;
        break;
    }
 
    // 角色基本信息输出
    cout << "***角色基本信息***" << endl;
    cout << "姓名:" << value_role_name << endl;
    cout << "生日:1659-" << value_birth_m << "-" << value_birth_d << endl;
    cout << "星座:" << value_constellation <<endl;
    for (unsigned int i = 0; i < value_role_basic_info_name_vec.size(); i++){
        int value = value_role_basic_info_value_array[i];
        cout << left;
        cout << value_role_basic_info_name_vec[i] << ":" << setw(4) << value;
        for (int i = 0; i < 10; i++){
            cout << ((i < value/10) ? "■" : "□");
        }
        cout << endl;
    }
 
    // 主体循环
    for (int year = 1659; year < 1659 + 17; year++){
        for (int month = (year == 1659 ? value_birth_m :1); month <= 12; month ++){
            // 生日
            if (month == value_birth_m && year > 1659){
                char c;
                cout << "今天是小公主" << value_role_name << "的生日,是否赠送礼物(Y/N)?";
                cin >> c;
            }
            // 基本信息打印
            cout << "\n***请选择操作选项:" << endl;
            cout << "1、查看状态\n2、安排行程\n3、亲自谈话\n4、存档\n5、读档\n";
            int choice;
            cout << "请选择:";
            cin >> choice;
            switch(choice){
            case 1:
                cout << "***角色基本信息***" << endl;
                cout << "姓名:" << value_role_name << endl;
                cout << "生日:1659-" << value_birth_m << "-" << value_birth_d << endl;
                cout << "星座:" << value_constellation <<endl;
                for (unsigned int i = 0; i < value_role_basic_info_name_vec.size(); i++){
                    int value = value_role_basic_info_value_array[i];
                    cout << left;
                    cout << value_role_basic_info_name_vec[i] << ":" << setw(4) << value;
                    for (int i = 0; i < 10; i++){
                        cout << ((i < value/10) ? "■" : "□");
                    }
                    cout << endl;
                }
                cout << "金钱:" << value_money << endl;
                cout << year << "年" << month << "月" << endl;
                break;
            case 2: // 安排行程,一个月最多安排三个行程
                cout << "1、学习武艺\n2、上私塾\n3、学习礼法\n4、出城修行\n5、打工赚钱\n";
                int choice;
                cout << "请选择:";
                cin >> choice;
                switch(choice){
                case 1:
                    int temp1, temp2, temp3;
                    value_role_basic_info_value_array[0] += temp1 = rand()%10;
                    value_role_basic_info_value_array[2] += temp2 = rand()%10;
                    value_money -= temp3 = rand()%51;
                    cout << "学习张无忌好榜样!" << endl;
                    cout << "体力+" << temp1 << ", 魅力+" << temp2 << ", 金钱-" << temp3 << endl;
                }
                break;
            default:
                break;
            }
        }
    }
 
    return 0;
}

Leave a Reply