Guest User

Untitled

a guest
Jan 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. $(document).ready(function(){
  2. var canvas = $("#game_canvas");
  3. var context = canvas.get(0).getContext("2d");
  4.  
  5. // Canvas dimensions
  6. var canvas_width = canvas.width();
  7. var canvas_height = canvas.height();
  8.  
  9. // Game settings.
  10. var play_game;
  11. var platform_x;
  12. var platform_y;
  13. var platform_outer_radius;
  14. var platform_inner_radius;
  15. var asteroids;
  16. var player;
  17. var player_original_x;
  18. var player_original_y;
  19. var ui = $("#game_ui");
  20. var ui_intro = $("#game_intro");
  21. var ui_stats = $("#game_stats");
  22. var ui_complete = $("#game_complete");
  23. var ui_play = $("#game_play");
  24. var ui_reset = $(".game_reset");
  25. var ui_remaining = $("#game_remaining");
  26. var ui_score = $(".game_score");
  27.  
  28. var Asteroid = function(x, y, radius, mass, friction){
  29. this.x = x;
  30. this.y = y;
  31. this.radius = radius;
  32. this.mass = mass;
  33. this.friction = friction;
  34. this.v_x = 0;
  35. this.v_y = 0;
  36.  
  37. this.player = false;
  38. };
  39.  
  40. // Reset and start the game
  41. function start_game(){
  42. ui_score.html("0");
  43. ui_stats.show();
  44.  
  45. // Set up initial game settings
  46. play_game = false;
  47. platform_x = canvas_width / 2;
  48. platform_y = 150;
  49. platform_outer_radius = 100;
  50. platform_inner_radius = 75;
  51. asteroids = new Array();
  52.  
  53. var outer_ring = 8; // Asteroids around outer ring
  54. var ring_count = 3; // Number of rings
  55. var ring_spacing = (platform_inner_radius / (ring_count - 1)); // Distance between
  56. // each ring.
  57. for(var r = 0; r < ring_count; r++){
  58. var current_ring = 0; // Asteroids around current ring
  59. var angle = 0; // Angle between each asteroid
  60. var ring_radius = 0;
  61.  
  62. // Is this the innermost ring?
  63. if(r == ring_count -1){
  64. current_ring = 1;
  65. } else {
  66. current_ring = outer_ring - (r * 3);
  67. angle = 360 / current_ring;
  68. ring_radius = platform_inner_radius - (ring_spacing * r);
  69. };
  70.  
  71. for(var a = 0; a < current_ring; a++){
  72. var x = 0;
  73. var y = 0;
  74.  
  75. // Is this the innermost ring?
  76. if(r == ring_count - 1){
  77. x = platform_x;
  78. y = platform_y;
  79. } else {
  80. x = platform_x + (ring_radius * Math.cos((angle * a)
  81. * (Math.PI / 180)));
  82. y = platform_y + (ring_radius * Math.sin((angle * a)
  83. * (Math.PI / 180)));
  84. };
  85.  
  86. var radius = 10;
  87. var mass = 5;
  88. var friction = 0.95;
  89. asteroids.push(new Asteroid(x, y, radius, mass, friction));
  90. };
  91. };
  92.  
  93. // Start the animation loop
  94. animate();
  95. };
  96.  
  97. // Initialize game environment
  98. function init(){
  99. ui_stats.hide();
  100. ui_complete.hide();
  101.  
  102. ui_play.click(function(e){
  103. e.preventDefault();
  104. ui_intro.hide();
  105. start_game();
  106. });
  107.  
  108. ui_reset.click(function(e){
  109. e.preventDefault();
  110. ui_complete.hide();
  111. start_game();
  112. });
  113. };
  114.  
  115. // Animation loop that does all the fun stuff
  116. function animate(){
  117. // Clear
  118. context.clearRect(0, 0, canvas_width, canvas_height);
  119.  
  120. context.fillStyle = "rgb(100, 100, 100)";
  121. context.beginPath();
  122. context.arc(platform_x, platform_y, platform_outer_radius, 0,
  123. Math.PI * 2, true);
  124. context.closePath();
  125. context.fill();
  126.  
  127. if(play_game){
  128. // Run the animation loop again in 33 ms
  129. setTimeout(animate, 33);
  130. };
  131. };
  132.  
  133. init();
  134. });
Add Comment
Please, Sign In to add comment