SHARE
TWEET

Snake - by Kieran Baines

a guest Feb 27th, 2020 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2. <head>
  3. <meta charset="utf-8" />
  4. <title>Snake</title>
  5. <style type="text/css">
  6. body {text-align:center;}
  7. canvas { border:5px dotted #ccc; }
  8. h1 { font-size:50px; text-align: center; margin: 0; padding-bottom: 25px;}
  9. </style>
  10. <script type="text/javascript">
  11. function play_game()
  12. {
  13. var level = 100; // Game level, by decreasing will speed up
  14. var rect_w = 45; // Width
  15. var rect_h = 30; // Height
  16. var inc_score = 50; // Score
  17. var snake_color = "#385726"; // Snake Color
  18. var ctx; // Canvas attributes
  19. var tn = []; // temp directions storage
  20. var x_dir = [-1, 0, 1, 0]; // position adjusments
  21. var y_dir = [0, -1, 0, 1]; // position adjusments
  22. var queue = [];
  23. var frog = 1; // defalut food
  24. var map = [];
  25. var MR = Math.random;
  26. var X = 5 + (MR() * (rect_w - 10))|0; // Calculate positions
  27. var Y = 5 + (MR() * (rect_h - 10))|0; // Calculate positions
  28. var direction = MR() * 3 | 0;
  29. var interval = 0;
  30. var score = 0;
  31. var sum = 0, easy = 0;
  32. var i, dir;
  33. // getting play area
  34. var c = document.getElementById('playArea');
  35. ctx = c.getContext('2d');
  36. // Map positions
  37. for (i = 0; i < rect_w; i++)
  38. {
  39. map[i] = [];
  40. }
  41. // random placement of snake food
  42. function rand_frog()
  43. {
  44. var x, y;
  45. do
  46. {
  47. x = MR() * rect_w|0;
  48. y = MR() * rect_h|0;
  49. }
  50. while (map[x][y]);
  51. map[x][y] = 1;
  52. ctx.fillStyle = snake_color;
  53. ctx.strokeRect(x * 10+1, y * 10+1, 8, 8);
  54. }
  55. // Default somewhere placement
  56. rand_frog();
  57. function set_game_speed()
  58. {
  59. if (easy)
  60. {
  61. X = (X+rect_w)%rect_w;
  62. Y = (Y+rect_h)%rect_h;
  63. }
  64. --inc_score;
  65. if (tn.length)
  66. {
  67. dir = tn.pop();
  68. if ((dir % 2) !== (direction % 2))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top