Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 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. const int width = 20;
  12. const int height = 20;
  13. int x, y, fruitX, fruitY, score;
  14. enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN} ;
  15. eDirection dir;
  16.  
  17. void Setup(){
  18. gameOver = false;
  19. dir = STOP;
  20. x = width / 2;
  21. y = height / 2;
  22. fruitX = rand() % width;
  23. fruitY = rand() % height;
  24. score = 0;
  25. }
  26.  
  27. // #####
  28. // # #
  29. // # #
  30. // # #
  31. // #####
  32.  
  33. void Draw(){
  34. // Gora
  35. system("cls"); //system("clear") - lin
  36. for (int i = 0; i < width+1; i++) // +2 - brakujace rogi
  37. cout << "#";
  38. cout << endl;
  39.  
  40. // Bok
  41. for (int i = 0; i < height; i++){
  42. for (int j = 0; j < width; j++){
  43. if (j == 0)
  44. cout << "#";
  45. if (i == y && j == x)
  46. cout << "0";
  47. else if (i == fruitY && j == fruitX)
  48. cout << ".";
  49. else
  50. cout << " ";
  51. // Dol
  52. if (j == width-1)
  53. cout << "#";
  54. }
  55. cout << endl;
  56. }
  57. for (int i = 0; i < width+2; i++)
  58. cout << "#";
  59. cout << endl;
  60. cout << "Wynik: " << score << endl << endl;
  61. cout << "Wcisnij 'x' by zakonczyc" << endl;
  62. }
  63. // w
  64. //a d
  65. // s
  66. void Input(){
  67. //keyboard hit
  68. if (_kbhit()){
  69. switch (_getch()){
  70. case 'a':
  71. dir = LEFT;
  72. break;
  73. case 'd':
  74. dir = RIGHT;
  75. break;
  76. case 'w':
  77. dir = UP;
  78. break;
  79. case 's':
  80. dir = DOWN;
  81. break;
  82. case 'x': // x = koniec
  83. gameOver = true;
  84. break;
  85. }
  86. }
  87. }
  88.  
  89. void Logic(){
  90.  
  91. switch (dir){
  92. case LEFT:
  93. x--;
  94. break;
  95. case RIGHT:
  96. x++;
  97. break;
  98. case UP:
  99. y--;
  100. break;
  101. case DOWN:
  102. y++;
  103. break;
  104. default:
  105. break;
  106. }
  107. //detekcja kolizji
  108. if (x > width || x < 0 || y > height || y < 0)
  109. gameOver = true;
  110. //zjadanie i dodawanie score
  111. if (x == fruitX && y == fruitY){
  112. score += 10;
  113. fruitX = rand() % width;
  114. fruitY = rand() % height;
  115. }
  116. }
  117.  
  118. int main()
  119. {
  120. Setup();
  121. while (!gameOver)
  122. {
  123. Draw();
  124. Input();
  125. Logic();
  126. //sleep(10);
  127. }
  128. while (gameOver)
  129. {
  130. cout << endl << "||| KONIEC GRY |||" << endl;
  131. cout << "||| Wynik: " << score << " |||" << endl << endl;
  132. cout << "Pawel Krakowiak 22.10.2019" << endl;
  133. Sleep(3000);
  134. return 0;
  135. }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement