Guest User

Untitled

a guest
Jun 21st, 2022
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8. class Game{
  9. public:
  10. Game();
  11. void initMap();
  12. void printMap();
  13. void moveHero(char direction);
  14. void moveMonster();
  15. void updateMap();
  16. void playGame();
  17. void printInfo();
  18. void isDead();
  19. char map[10][10];
  20. private:
  21. int heroX;
  22. int heroY;
  23. int monsterX;
  24. int monsterY;
  25. int heroHP;
  26. int monsterHP;
  27. int heroATK;
  28. int monsterATK;
  29. };
  30.  
  31. Game::Game(){
  32. heroX = 0;
  33. heroY = 0;
  34. heroHP = 100;
  35. heroATK = 20;
  36. monsterX = 9;
  37. monsterY = 9;
  38. monsterHP = 100;
  39. monsterATK = 20;
  40. };
  41.  
  42. void Game::initMap(){
  43. for(int i = 0; i < 10; i++){
  44. for(int j = 0; j < 10; j++){
  45. map[i][j] = '*';
  46. }
  47. }
  48. srand(time(0));
  49. int x = 0;
  50. int y = 0;
  51. for(int i = 0; i < 10; i++){
  52. x = rand() % 10;
  53. y = rand() % 10;
  54. if(map[x][y] == '*'){
  55. map[x][y] = '$';
  56. }
  57. else{
  58. i--;
  59. }
  60. }
  61. map[heroX][heroY] = 'H';
  62. map[monsterX][monsterY] = 'M';
  63. };
  64.  
  65. void Game::printMap(){
  66. for(int i = 0; i < 10; i++){
  67. for(int j = 0; j < 10; j++){
  68. cout << map[i][j] << " ";
  69. }
  70. cout << endl;
  71. }
  72. };
  73.  
  74. void Game::moveHero(char direction){
  75. switch(direction){
  76. case 'w':
  77. if(heroX > 0){
  78. heroX--;
  79. }
  80. break;
  81. case 's':
  82. if(heroX < 9){
  83. heroX++;
  84. }
  85. break;
  86. case 'a':
  87. if(heroY > 0){
  88. heroY--;
  89. }
  90. break;
  91. case 'd':
  92. if(heroY < 9){
  93. heroY++;
  94. }
  95. break;
  96. }
  97. };
  98.  
  99. void Game::moveMonster(){
  100. if(monsterX > heroX){
  101. monsterX--;
  102. }
  103. else if(monsterX < heroX){
  104. monsterX++;
  105. }
  106. if(monsterY > heroY){
  107. monsterY--;
  108. }
  109. else if(monsterY < heroY){
  110. monsterY++;
  111. }
  112. };
  113.  
  114. void Game::updateMap(){
  115. for(int i = 0; i < 10; i++){
  116. for(int j = 0; j < 10; j++){
  117. if(map[i][j] == '$'){
  118. map[i][j] = '*';
  119. monsterHP += 30;
  120. }
  121. }
  122. }
  123. map[heroX][heroY] = 'H';
  124. map[monsterX][monsterY] = 'M';
  125. };
  126.  
  127. void Game::playGame(){
  128. char move;
  129. while(1){
  130. cin >> move;
  131. moveHero(move);
  132. moveMonster();
  133. if(heroX == monsterX && heroY == monsterY){
  134. heroHP -= monsterATK;
  135. monsterHP -= heroATK;
  136. }
  137. updateMap();
  138. printMap();
  139. printInfo();
  140. isDead();
  141. }
  142. };
  143.  
  144. void Game::printInfo(){
  145. cout << "Hero HP: " << heroHP << endl;
  146. cout << "Monster HP: " << monsterHP << endl;
  147. };
  148.  
  149. void Game::isDead(){
  150. if(heroHP <= 0){
  151. cout << "You are dead." << endl;
  152. exit(0);
  153. }
  154. else if(monsterHP <= 0){
  155. cout << "You win!" << endl;
  156. exit(0);
  157. }
  158. };
  159.  
  160. int main(){
  161. Game g;
  162. g.initMap();
  163. g.printMap();
  164. g.playGame();
  165. return 0;
  166. }
  167.  
  168. // g++ rpg.cpp -o rpg
  169.  
Advertisement
Add Comment
Please, Sign In to add comment