Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.80 KB | None | 0 0
  1. /*
  2. Snake Game
  3. by kseny :D
  4. */
  5.  
  6. var screenSize = Global.GetScreenSize();
  7. var gameBoxSize = 300;
  8.  
  9. var lastMove = 0;
  10. var currentMovingDirection = 0;
  11. var snake = [{x : 150, y: 150}, {x : 140, y: 150}, {x : 130, y: 150}];
  12.  
  13. var playerScore = 0;
  14. var highestScore = 0;
  15. var gameOver = false;
  16. var gameOverShowTime = 0;
  17.  
  18. var food = null;
  19.  
  20. function randomPositionInBox(min, max)
  21. {
  22. return Math.round((Math.random() * (max-min) + min) / 10) * 10;
  23. }
  24.  
  25. function generateFood()
  26. {
  27. var good_positions = true;
  28.  
  29. do
  30. {
  31. good_positions = true;
  32. var x = randomPositionInBox(0, gameBoxSize - 10);
  33. var y = randomPositionInBox(0, gameBoxSize - 10);
  34.  
  35. for(var i = 0; i < snake.length; i ++)
  36. {
  37. if(snake[i].x == x && snake[i].y == y)
  38. {
  39. good_positions = false;
  40. break;
  41. }
  42. }
  43. }
  44. while(good_positions == false);
  45.  
  46. food = {x: x, y: y};
  47. }
  48.  
  49. function moveSnake()
  50. {
  51.  
  52. var x_axis = 0;
  53. var y_axis = 0;
  54.  
  55. switch(currentMovingDirection)
  56. {
  57. case 0:
  58. {
  59. x_axis = 10;
  60. break;
  61. }
  62. case 1:
  63. {
  64. x_axis = -10;
  65. break;
  66. }
  67. case 2:
  68. {
  69. y_axis = -10;
  70. break;
  71. }
  72. case 3:
  73. {
  74. y_axis = 10;
  75. break;
  76. }
  77. }
  78.  
  79. if(snake[0].x + x_axis >= gameBoxSize || snake[0].x + x_axis < 0 || snake[0].y + y_axis >= gameBoxSize || snake[0].y + y_axis < 0)
  80. {
  81. gameOver = true;
  82. gameOverShowTime = Global.Realtime() + 5;
  83. return;
  84. }
  85. else
  86. {
  87. for(var i = 1; i < snake.length; i ++)
  88. {
  89. if(snake[i].x == snake[0].x + x_axis && snake[i].y == snake[0].y + y_axis)
  90. {
  91. gameOver = true;
  92. gameOverShowTime = Global.Realtime() + 5;
  93. return;
  94. }
  95. }
  96.  
  97. snake.unshift({x : snake[0].x + x_axis, y : snake[0].y + y_axis });
  98. snake.pop();
  99.  
  100. if(food == null)
  101. {
  102. generateFood();
  103. }
  104. else
  105. {
  106. if(snake[0].x == food.x && snake[0].y == food.y)
  107. {
  108. playerScore ++;
  109.  
  110. if(playerScore > highestScore)
  111. highestScore = playerScore;
  112.  
  113. snake.unshift({x : snake[0].x + x_axis, y : snake[0].y + y_axis });
  114.  
  115. generateFood();
  116. }
  117. }
  118. }
  119. }
  120.  
  121. function onDrawEvent()
  122. {
  123. if(!UI.GetValue("MISC", "JAVASCRIPT", "Script Items", "Play Snake Game") || !UI.IsMenuOpen())
  124. return;
  125.  
  126. Render.GradientRect(screenSize[0]/2 - gameBoxSize/2 - 5, screenSize[1]/2 - gameBoxSize/2 - 34 - 2, gameBoxSize + 10, 4, 1, [217, 157, 86, 255], [223, 174, 97, 255]);
  127. Render.FilledRect(screenSize[0]/2 - gameBoxSize/2 - 5, screenSize[1]/2 - gameBoxSize/2 - 30 - 2, gameBoxSize + 10, 25, [44, 48, 55, 255]);
  128. Render.String(screenSize[0]/2, screenSize[1]/2 - gameBoxSize/2 - 25 - 2, 1, "Snake Game" + " [score: "+ playerScore +"]", [255, 255, 255, 255]);
  129.  
  130.  
  131. Render.FilledRect(screenSize[0]/2 - gameBoxSize/2 - 5, screenSize[1]/2 - gameBoxSize/2 - 5, gameBoxSize + 10, gameBoxSize + 10, [44, 48, 55, 255]);
  132. Render.Rect(screenSize[0]/2 - gameBoxSize/2, screenSize[1]/2 - gameBoxSize/2, gameBoxSize, gameBoxSize, [100, 100, 100, 150]);
  133.  
  134. var snakeIsPaused = UI.IsHotkeyActive("MISC", "JAVASCRIPT", "Script Items", "Pause Snake Game");
  135.  
  136. if(snakeIsPaused)
  137. {
  138. Render.String(screenSize[0]/2, screenSize[1]/2 - 5, 1, "The game is paused", [255, 0, 0, 255]);
  139. }
  140.  
  141. if(gameOver)
  142. {
  143. Render.String(screenSize[0]/2, screenSize[1]/2 - 25, 1, "Game over!", [255, 0, 0, 255]);
  144. Render.String(screenSize[0]/2, screenSize[1]/2 - 10, 1, "Score: " + playerScore, [255, 0, 0, 255]);
  145. Render.String(screenSize[0]/2, screenSize[1]/2 + 5, 1, "Highest score: " + highestScore, [255, 0, 0, 255]);
  146.  
  147. if(Global.Realtime() > gameOverShowTime)
  148. {
  149. playerScore = 0;
  150. gameOver = false;
  151. gameOverShowTime = 0;
  152. currentMovingDirection = 0;
  153. snake = [{x : 150, y: 150}, {x : 140, y: 150}, {x : 130, y: 150}];
  154. }
  155. return;
  156. }
  157.  
  158. if(!snakeIsPaused && !gameOver)
  159. {
  160.  
  161. if(food != null)
  162. {
  163. Render.FilledRect(screenSize[0]/2 - gameBoxSize/2 + food.x, screenSize[1]/2 - gameBoxSize/2 + food.y, 10, 10, [255, 0, 0, 255]);
  164. Render.Rect(screenSize[0]/2 - gameBoxSize/2 + food.x, screenSize[1]/2 - gameBoxSize/2 + food.y, 10, 10, [0, 0, 0, 255]);
  165. }
  166.  
  167. for(var i = 0; i < snake.length; i++)
  168. {
  169. Render.FilledRect(screenSize[0]/2 - gameBoxSize/2 + snake[i].x, screenSize[1]/2 - gameBoxSize/2 + snake[i].y, 10, 10, [17, 102, 9, 255]);
  170. Render.Rect(screenSize[0]/2 - gameBoxSize/2 + snake[i].x, screenSize[1]/2 - gameBoxSize/2 + snake[i].y, 10, 10, [0, 0, 0, 255]);
  171. }
  172. }
  173.  
  174. if(snakeIsPaused)
  175. return;
  176.  
  177. if(Global.IsKeyPressed(0x26) && currentMovingDirection != 3)
  178. {
  179. currentMovingDirection = 2;
  180. }
  181. else if(Global.IsKeyPressed(0x28) && currentMovingDirection != 2)
  182. {
  183. currentMovingDirection = 3;
  184. }
  185. else if(Global.IsKeyPressed(0x27) && currentMovingDirection != 1)
  186. {
  187. currentMovingDirection = 0;
  188. }
  189. else if(Global.IsKeyPressed(0x25) && currentMovingDirection != 0)
  190. {
  191. currentMovingDirection = 1;
  192. }
  193.  
  194. if(Global.Realtime() - lastMove > 0.2)
  195. {
  196. moveSnake();
  197. lastMove = Global.Realtime();
  198. }
  199. }
  200.  
  201. Global.RegisterCallback("Draw", "onDrawEvent");
  202.  
  203. UI.AddCheckbox("Play Snake Game");
  204. UI.AddHotkey("Pause Snake Game");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement