Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <conio.h>
  6. #include <windows.h>
  7.  
  8. using namespace std;
  9.  
  10. bool gameOver;
  11. int how_hard;
  12. const int width = 20;
  13. const int height = 20;
  14. int x, y, fruitX, fruitY, score;
  15. enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN} ;
  16. eDirection dir;
  17.  
  18. int Difficulty(int lv_step);
  19. void Setup();
  20. void Draw();
  21. void Input();
  22. void Logic();
  23.  
  24. int main(){
  25. switch(_getch()){
  26. case '1': how_hard = 100; break;
  27. case '2': how_hard = 50; break;
  28. case '3': how_hard = 0; break;
  29. }
  30.  
  31. Setup();
  32. while (!gameOver){
  33. Draw();
  34. Input();
  35. Sleep(how_hard);
  36. Logic();
  37. //sleep(10);
  38. }
  39. while (gameOver){
  40. cout << endl << "||| KONIEC GRY |||" << endl;
  41. cout << "||| Wynik: " << score << " |||" << endl << endl;
  42. cout << "Pawel Krakowiak 22.10.2019" << endl;
  43. Sleep(3000);
  44. return 0;
  45. }
  46. }
  47. void Setup(){
  48. gameOver = false;
  49. dir = STOP;
  50. x = width / 2;
  51. y = height / 2;
  52. fruitX = rand() % width;
  53. fruitY = rand() % height;
  54. score = 0;
  55. }
  56.  
  57. void Draw(){
  58. // Gora
  59. system("cls"); //system("clear") - lin
  60. for (int i = 0; i < width+1; i++) // +2 - brakujace rogi
  61. cout << "#";
  62. cout << endl;
  63.  
  64. // Bok
  65. for (int i = 0; i < height; i++){
  66. for (int j = 0; j < width; j++){
  67. if (j == 0)
  68. cout << "#";
  69. if (i == y && j == x)
  70. cout << "0";
  71. else if (i == fruitY && j == fruitX)
  72. cout << ".";
  73. else
  74. cout << " ";
  75. // Dol
  76. if (j == width-1)
  77. cout << "#";
  78. }
  79. cout << endl;
  80. }
  81. for (int i = 0; i < width+2; i++)
  82. cout << "#";
  83.  
  84. cout << endl;
  85. cout << "Wynik: " << score << endl << endl;
  86. cout << "Wcisnij 'x' by zakonczyc" << endl;
  87.  
  88. // #####
  89. // # #
  90. // # #
  91. // # #
  92. // #####
  93. }
  94.  
  95. void Input(){
  96. //keyboard hit
  97. if (_kbhit()){
  98. switch (_getch()){
  99. case 'a':
  100. dir = LEFT;
  101. break;
  102. case 'd':
  103. dir = RIGHT;
  104. break;
  105. case 'w':
  106. dir = UP;
  107. break;
  108. case 's':
  109. dir = DOWN;
  110. break;
  111. case 'x': // x = koniec
  112. gameOver = true;
  113. break;
  114. }
  115. }
  116. }
  117.  
  118. void Logic(){
  119.  
  120. switch (dir){
  121. case LEFT:
  122. x--;
  123. break;
  124. case RIGHT:
  125. x++;
  126. break;
  127. case UP:
  128. y--;
  129. break;
  130. case DOWN:
  131. y++;
  132. break;
  133. default:
  134. break;
  135. }
  136. //detekcja kolizji
  137. if (x > width || x < 0 || y > height || y < 0)
  138. gameOver = true;
  139. //zjadanie i dodawanie score
  140. if (x == fruitX && y == fruitY){
  141. score += 10;
  142. fruitX = rand() % width;
  143. fruitY = rand() % height;
  144. }
  145. }
  146.  
  147.  
  148. /*
  149. int Difficulty(int lv_step){
  150. cout << "Choose lv [1-3]: ";
  151. switch (_getch()){
  152. case '1':
  153. lv_step=4000;
  154. break;
  155. case '2':
  156. lv_step=10;
  157. break;
  158. case '3':
  159. lv_step=0;
  160. break;
  161. default:
  162. return 0;
  163. break;
  164. system("cls");
  165. return how_hard;
  166. }
  167. }
  168. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement