Advertisement
Guest User

Game source code for example

a guest
Apr 24th, 2020
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.22 KB | None | 0 0
  1.  
  2.  
  3.  
  4. Snake game
  5.  
  6.  
  7. <!DOCTYPE html>
  8. <html>
  9. <head>
  10. <title></title>
  11. <style>
  12. html, body {
  13. height: 100%;
  14. margin: 0;
  15. }
  16.  
  17. body {
  18. background: black;
  19. display: flex;
  20. align-items: center;
  21. justify-content: center;
  22. }
  23. canvas {
  24. border: 1px solid white;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <canvas width="400" height="400" id="game"></canvas>
  30. <script>
  31. var canvas = document.getElementById('game');
  32. var context = canvas.getContext('2d');
  33.  
  34. var grid = 16;
  35. var count = 0;
  36.  
  37. var snake = {
  38. x: 160,
  39. y: 160,
  40.  
  41. // snake velocity. moves one grid length every frame in either the x or y direction
  42. dx: grid,
  43. dy: 0,
  44.  
  45. // keep track of all grids the snake body occupies
  46. cells: [],
  47.  
  48. // length of the snake. grows when eating an apple
  49. maxCells: 4
  50. };
  51. var apple = {
  52. x: 320,
  53. y: 320
  54. };
  55.  
  56. // get random whole numbers in a specific range
  57. // @see https://stackoverflow.com/a/1527820/2124254
  58. function getRandomInt(min, max) {
  59. return Math.floor(Math.random() * (max - min)) + min;
  60. }
  61.  
  62. // game loop
  63. function loop() {
  64. requestAnimationFrame(loop);
  65.  
  66. // slow game loop to 15 fps instead of 60 (60/15 = 4)
  67. if (++count < 4) {
  68. return;
  69. }
  70.  
  71. count = 0;
  72. context.clearRect(0,0,canvas.width,canvas.height);
  73.  
  74. // move snake by it's velocity
  75. snake.x += snake.dx;
  76. snake.y += snake.dy;
  77.  
  78. // wrap snake position horizontally on edge of screen
  79. if (snake.x < 0) {
  80. snake.x = canvas.width - grid;
  81. }
  82. else if (snake.x >= canvas.width) {
  83. snake.x = 0;
  84. }
  85.  
  86. // wrap snake position vertically on edge of screen
  87. if (snake.y < 0) {
  88. snake.y = canvas.height - grid;
  89. }
  90. else if (snake.y >= canvas.height) {
  91. snake.y = 0;
  92. }
  93.  
  94. // keep track of where snake has been. front of the array is always the head
  95. snake.cells.unshift({x: snake.x, y: snake.y});
  96.  
  97. // remove cells as we move away from them
  98. if (snake.cells.length > snake.maxCells) {
  99. snake.cells.pop();
  100. }
  101.  
  102. // draw apple
  103. context.fillStyle = 'red';
  104. context.fillRect(apple.x, apple.y, grid-1, grid-1);
  105.  
  106. // draw snake one cell at a time
  107. context.fillStyle = 'green';
  108. snake.cells.forEach(function(cell, index) {
  109.  
  110. // drawing 1 px smaller than the grid creates a grid effect in the snake body so you can see how long it is
  111. context.fillRect(cell.x, cell.y, grid-1, grid-1);
  112.  
  113. // snake ate apple
  114. if (cell.x === apple.x && cell.y === apple.y) {
  115. snake.maxCells++;
  116.  
  117. // canvas is 400x400 which is 25x25 grids
  118. apple.x = getRandomInt(0, 25) * grid;
  119. apple.y = getRandomInt(0, 25) * grid;
  120. }
  121.  
  122. // check collision with all cells after this one (modified bubble sort)
  123. for (var i = index + 1; i < snake.cells.length; i++) {
  124.  
  125. // snake occupies same space as a body part. reset game
  126. if (cell.x === snake.cells[i].x && cell.y === snake.cells[i].y) {
  127. snake.x = 160;
  128. snake.y = 160;
  129. snake.cells = [];
  130. snake.maxCells = 4;
  131. snake.dx = grid;
  132. snake.dy = 0;
  133.  
  134. apple.x = getRandomInt(0, 25) * grid;
  135. apple.y = getRandomInt(0, 25) * grid;
  136. }
  137. }
  138. });
  139. }
  140.  
  141. // listen to keyboard events to move the snake
  142. document.addEventListener('keydown', function(e) {
  143. // prevent snake from backtracking on itself by checking that it's
  144. // not already moving on the same axis (pressing left while moving
  145. // left won't do anything, and pressing right while moving left
  146. // shouldn't let you collide with your own body)
  147.  
  148. // left arrow key
  149. if (e.which === 37 && snake.dx === 0) {
  150. snake.dx = -grid;
  151. snake.dy = 0;
  152. }
  153. // up arrow key
  154. else if (e.which === 38 && snake.dy === 0) {
  155. snake.dy = -grid;
  156. snake.dx = 0;
  157. }
  158. // right arrow key
  159. else if (e.which === 39 && snake.dx === 0) {
  160. snake.dx = grid;
  161. snake.dy = 0;
  162. }
  163. // down arrow key
  164. else if (e.which === 40 && snake.dy === 0) {
  165. snake.dy = grid;
  166. snake.dx = 0;
  167. }
  168. });
  169.  
  170. // start the game
  171. requestAnimationFrame(loop);
  172. </script>
  173. </body>
  174. </html>
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187. Calculator
  188.  
  189.  
  190.  
  191.  
  192. <html>
  193. <head>
  194. <script>
  195. //function that display value
  196. function dis(val)
  197. {
  198. document.getElementById("result").value+=val
  199. }
  200.  
  201. //function that evaluates the digit and return result
  202. function solve()
  203. {
  204. let x = document.getElementById("result").value
  205. let y = eval(x)
  206. document.getElementById("result").value = y
  207. }
  208.  
  209. //function that clear the display
  210. function clr()
  211. {
  212. document.getElementById("result").value = ""
  213. }
  214. </script>
  215. <!-- for styling -->
  216. <style>
  217. .title{
  218. margin-bottom: 10px;
  219. text-align:center;
  220. width: 210px;
  221. color:green;
  222. border: solid black 2px;
  223. }
  224.  
  225. input[type="button"]
  226. {
  227. background-color:green;
  228. color: black;
  229. border: solid black 2px;
  230. width:100%
  231. }
  232.  
  233. input[type="text"]
  234. {
  235. background-color:white;
  236. border: solid black 2px;
  237. width:100%
  238. }
  239. </style>
  240. </head>
  241. <!-- create table -->
  242. <body>
  243. <div class = title >SecurePr0x Calculator</div>
  244. <table border="1">
  245. <tr>
  246. <td colspan="3"><input type="text" id="result"/></td>
  247. <!-- clr() function will call clr to clear all value -->
  248. <td><input type="button" value="c" onclick="clr()"/> </td>
  249. </tr>
  250. <tr>
  251. <!-- create button and assign value to each button -->
  252. <!-- dis("1") will call function dis to display value -->
  253. <td><input type="button" value="1" onclick="dis('1')"/> </td>
  254. <td><input type="button" value="2" onclick="dis('2')"/> </td>
  255. <td><input type="button" value="3" onclick="dis('3')"/> </td>
  256. <td><input type="button" value="/" onclick="dis('/')"/> </td>
  257. </tr>
  258. <tr>
  259. <td><input type="button" value="4" onclick="dis('4')"/> </td>
  260. <td><input type="button" value="5" onclick="dis('5')"/> </td>
  261. <td><input type="button" value="6" onclick="dis('6')"/> </td>
  262. <td><input type="button" value="-" onclick="dis('-')"/> </td>
  263. </tr>
  264. <tr>
  265. <td><input type="button" value="7" onclick="dis('7')"/> </td>
  266. <td><input type="button" value="8" onclick="dis('8')"/> </td>
  267. <td><input type="button" value="9" onclick="dis('9')"/> </td>
  268. <td><input type="button" value="+" onclick="dis('+')"/> </td>
  269. </tr>
  270. <tr>
  271. <td><input type="button" value="." onclick="dis('.')"/> </td>
  272. <td><input type="button" value="0" onclick="dis('0')"/> </td>
  273. <!-- solve function call function solve to evaluate value -->
  274. <td><input type="button" value="=" onclick="solve()"/> </td>
  275. <td><input type="button" value="*" onclick="dis('*')"/> </td>
  276. </tr>
  277. </table>
  278. </body>
  279. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement