SHOW:
|
|
- or go back to the newest paste.
1 | // zmienne/argumenty - camelCase | |
2 | // funkcje/metody - snake_case | |
3 | // klasy - PascelCase | |
4 | ||
5 | #include <iostream> | |
6 | #include <vector> | |
7 | #include <string> | |
8 | #include <cstdlib> | |
9 | ||
10 | using namespace std; | |
11 | ||
12 | class Entity | |
13 | { | |
14 | private: | |
15 | ||
16 | string name; | |
17 | int health; | |
18 | int maxHealth; | |
19 | int damage; | |
20 | ||
21 | public: | |
22 | ||
23 | Entity(string entityName) | |
24 | { | |
25 | name = entityName; | |
26 | maxHealth = 10; | |
27 | health = maxHealth; | |
28 | damage = 2; | |
29 | } | |
30 | ||
31 | string get_name() | |
32 | { | |
33 | return name; | |
34 | } | |
35 | ||
36 | int get_health() | |
37 | { | |
38 | return health; | |
39 | } | |
40 | ||
41 | int get_max_health() | |
42 | { | |
43 | return maxHealth; | |
44 | } | |
45 | ||
46 | int get_damage() | |
47 | { | |
48 | return damage; | |
49 | } | |
50 | ||
51 | void lower_health(int value) | |
52 | { | |
53 | health -= value; | |
54 | if (health < 0) | |
55 | { | |
56 | health = 0; | |
57 | } | |
58 | } | |
59 | void add_health(int value) | |
60 | { | |
61 | health += value; | |
62 | if (health > maxHealth) | |
63 | { | |
64 | health = maxHealth; | |
65 | } | |
66 | } | |
67 | }; | |
68 | ||
69 | class Player : public Entity | |
70 | { | |
71 | private: | |
72 | ||
73 | int metal; | |
74 | int oxygen; | |
75 | ||
76 | public: | |
77 | ||
78 | Player(string playerName) : Entity(playerName) | |
79 | { | |
80 | metal = 0; | |
81 | oxygen = 100; | |
82 | } | |
83 | ||
84 | Player() : Entity("John") | |
85 | { | |
86 | ||
87 | } | |
88 | ||
89 | int get_metal() | |
90 | { | |
91 | return metal; | |
92 | } | |
93 | ||
94 | int get_oxygen() | |
95 | { | |
96 | return oxygen; | |
97 | } | |
98 | ||
99 | void display_player_info() | |
100 | { | |
101 | cout << "Stan gracza" << endl; | |
102 | cout << "------------------------------------------------" << endl; | |
103 | cout << "Życie: " << get_health() << "/" << get_max_health() << endl; | |
104 | cout << "Atak: " << get_damage() << endl; | |
105 | cout << "Tlen: " << oxygen << "%" << endl; | |
106 | cout << "Metal: " << metal << "/50" << endl; | |
107 | } | |
108 | ||
109 | void rest() | |
110 | { | |
111 | cout << get_name() << " odpoczywa. Zużycie 2% tlenu." << endl << endl; | |
112 | lower_oxygen(2); | |
113 | add_health(5); | |
114 | display_player_info(); | |
115 | } | |
116 | ||
117 | void lower_oxygen(int value) | |
118 | { | |
119 | oxygen -= value; | |
120 | } | |
121 | ||
122 | void add_oxygen(int value) | |
123 | { | |
124 | if(oxygen + value > 100) | |
125 | { | |
126 | oxygen = 100; | |
127 | } | |
128 | else | |
129 | { | |
130 | oxygen += value; | |
131 | } | |
132 | } | |
133 | ||
134 | void add_metal(int value) | |
135 | { | |
136 | metal += value; | |
137 | } | |
138 | }; | |
139 | ||
140 | class Game | |
141 | { | |
142 | private: | |
143 | ||
144 | Player player; | |
145 | ||
146 | ||
147 | public: | |
148 | ||
149 | Game(string playerName) | |
150 | { | |
151 | player = Player(playerName); | |
152 | cout << player.get_name() << endl; | |
153 | } | |
154 | ||
155 | void start_game() | |
156 | { | |
157 | cout << "------------------------------------------------" << endl; | |
158 | cout << "Rozbiłeś się na księżycu!" << endl; | |
159 | cout << "Twoim zadaniem jest przetrwanie i zdobycie wystarczająco dużo surowców by zbudować statek do domu." << endl; | |
160 | cout << "Uważaj na zapasy swojego tlenu, bez niego nie możesz kontynuować gry." << endl ; | |
161 | cout << "Powodzenia " << player.get_name() << "!" << endl << endl; | |
162 | ||
163 | player.display_player_info(); | |
164 | ||
165 | bool gameRunning = true; | |
166 | int playerChoice; | |
167 | ||
168 | while(gameRunning) | |
169 | { | |
170 | print_main_menu(); | |
171 | cout << "Wpisz numer: "; | |
172 | cin >> playerChoice; | |
173 | ||
174 | switch (playerChoice) | |
175 | { | |
176 | case 1: | |
177 | player.display_player_info(); | |
178 | break; | |
179 | case 2: | |
180 | player.rest(); | |
181 | break; | |
182 | case 3: | |
183 | search_resources(); | |
184 | if(player.get_health() <= 0) | |
185 | { | |
186 | cout << "Gracz przegrywa!" << endl; | |
187 | cout << "Koniec gry! Spróbuj ponownie!" << endl; | |
188 | gameRunning = false; | |
189 | } | |
190 | break; | |
191 | case 4: | |
192 | if(player.get_metal() >= 50) | |
193 | { | |
194 | cout << "Gracz wygrywa!" << endl; | |
195 | cout << "Pomyślnie zbudowałeś statek i wydostałeś się z księżyca!" << endl; | |
196 | cout << "Dzięki za gre!" << endl; | |
197 | } | |
198 | else | |
199 | { | |
200 | cout << "Nie masz wystarczająco dużo metalu by zbudować statek." << endl; | |
201 | } | |
202 | break; | |
203 | case 0: | |
204 | cout << "Koniec gry!" << endl; | |
205 | gameRunning = false; | |
206 | break; | |
207 | } | |
208 | } | |
209 | } | |
210 | ||
211 | void print_main_menu() | |
212 | { | |
213 | cout << endl << "Co chcesz zrobić?" << endl; | |
214 | cout << "------------------------------------------------" << endl; | |
215 | cout << "[1] Sprawdź swój stan." << endl; | |
216 | cout << "[2] Odpocznij." << endl; | |
217 | cout << "[3] Szukaj zasobów." << endl; | |
218 | if (player.get_metal() >= 50) | |
219 | { | |
220 | cout << "[4] Zbuduj statek." << endl; | |
221 | } | |
222 | cout << "[0] Skończ grę." << endl; | |
223 | } | |
224 | ||
225 | void search_resources() | |
226 | { | |
227 | cout << endl << player.get_name() <<" wybiera się na zewnątrz by szukać zasobów. Zużycie 5% tlenu." << endl; | |
228 | player.lower_oxygen(5); | |
229 | cout << "------------------------------------------------" << endl; | |
230 | int encounter = rand() % 3; | |
231 | ||
232 | switch(encounter) | |
233 | { | |
234 | case 0: | |
235 | cout << player.get_name() << " ma szczęście. Natrafił na mały zbiornik tlenu!" << endl; | |
236 | cout << "Zasób tlenu zwiększony o 7%!" << endl << endl; | |
237 | player.add_oxygen(7); | |
238 | player.display_player_info(); | |
239 | break; | |
240 | case 1: | |
241 | cout << player.get_name() << " natrafił na małe złoże metalu!" << endl; | |
242 | cout << "Zasób metalu zwiększony o 5!" << endl << endl; | |
243 | player.add_metal(5); | |
244 | player.display_player_info(); | |
245 | break; | |
246 | case 2: | |
247 | cout << player.get_name() << " napotkał na przeciwnika!" << endl; | |
248 | start_combat(); | |
249 | break; | |
250 | } | |
251 | ||
252 | } | |
253 | ||
254 | void start_combat() | |
255 | { | |
256 | ||
257 | Entity enemy = Entity("Kosmita"); | |
258 | bool combatRunning = true; | |
259 | int choice, playerDamage, enemyDamage; | |
260 | ||
261 | while (combatRunning) | |
262 | { | |
263 | cout << endl << "Co chcesz zrobić?" << endl; | |
264 | cout << "------------------------------------------------" << endl; | |
265 | cout << "Twoje życie: " << player.get_health() << " | Życie przeciwnika: " << enemy.get_health() << endl; | |
266 | cout << "------------------------------------------------" << endl; | |
267 | cout << "[1] Atakuj!" << endl; | |
268 | cout << "[2] Uciekaj!" << endl << endl; | |
269 | ||
270 | cout << "Wpisz numer: "; | |
271 | cin >> choice; | |
272 | ||
273 | switch (choice) | |
274 | { | |
275 | case 1: // Gracz atakuje | |
276 | playerDamage = rand() % player.get_damage() + 1; | |
277 | cout << player.get_name() << " atakuje! Rani przeciwnika o " << playerDamage << " punkty!" << endl; | |
278 | enemy.lower_health(playerDamage); | |
279 | ||
280 | if(enemy.get_health() > 0) | |
281 | { | |
282 | enemyDamage = rand() % enemy.get_damage() + 1; | |
283 | cout << enemy.get_name() << " kontratakuje! Rani bohatera o " << enemyDamage << " punkty!" << endl; | |
284 | player.lower_health(enemyDamage); | |
285 | ||
286 | if(player.get_health() <= 0) | |
287 | { | |
288 | cout << player.get_name() << " przegrywa! Koniec walki!" << endl; | |
289 | combatRunning = false; | |
290 | } | |
291 | } | |
292 | else | |
293 | { | |
294 | cout << enemy.get_name() << " przegrywa! Koniec walki!" << endl; | |
295 | cout << player.get_name() << " znajduje 10 metalu!" << endl; | |
296 | player.add_metal(10); | |
297 | ||
298 | combatRunning = false; | |
299 | } | |
300 | break; | |
301 | ||
302 | case 2: // Gracz ucieka | |
303 | cout << player.get_name() << " ucieka tracąc kolejne 5% tlenu! Koniec walki!" << endl; | |
304 | player.lower_oxygen(5); | |
305 | combatRunning = false; | |
306 | break; | |
307 | } | |
308 | } | |
309 | } | |
310 | ||
311 | ||
312 | }; | |
313 | ||
314 | ||
315 | int main() | |
316 | { | |
317 | cout << "Witaj w grze SpaceRescue!" << endl; | |
318 | string name; | |
319 | cout << "Podaj nazwę swojego bohatera: "; | |
320 | cin >> name; | |
321 | ||
322 | Game game = Game(name); | |
323 | game.start_game(); | |
324 | ||
325 | ||
326 | return 0; | |
327 | } |