Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).ready(function(){
- var canvas = $("#game_canvas");
- var context = canvas.get(0).getContext("2d");
- // Canvas dimensions
- var canvas_width = canvas.width();
- var canvas_height = canvas.height();
- // Game settings.
- var play_game;
- var platform_x;
- var platform_y;
- var platform_outer_radius;
- var platform_inner_radius;
- var asteroids;
- var player;
- var player_original_x;
- var player_original_y;
- var ui = $("#game_ui");
- var ui_intro = $("#game_intro");
- var ui_stats = $("#game_stats");
- var ui_complete = $("#game_complete");
- var ui_play = $("#game_play");
- var ui_reset = $(".game_reset");
- var ui_remaining = $("#game_remaining");
- var ui_score = $(".game_score");
- var Asteroid = function(x, y, radius, mass, friction){
- this.x = x;
- this.y = y;
- this.radius = radius;
- this.mass = mass;
- this.friction = friction;
- this.v_x = 0;
- this.v_y = 0;
- this.player = false;
- };
- // Reset and start the game
- function start_game(){
- ui_score.html("0");
- ui_stats.show();
- // Set up initial game settings
- play_game = false;
- platform_x = canvas_width / 2;
- platform_y = 150;
- platform_outer_radius = 100;
- platform_inner_radius = 75;
- asteroids = new Array();
- var outer_ring = 8; // Asteroids around outer ring
- var ring_count = 3; // Number of rings
- var ring_spacing = (platform_inner_radius / (ring_count - 1)); // Distance between
- // each ring.
- for(var r = 0; r < ring_count; r++){
- var current_ring = 0; // Asteroids around current ring
- var angle = 0; // Angle between each asteroid
- var ring_radius = 0;
- // Is this the innermost ring?
- if(r == ring_count -1){
- current_ring = 1;
- } else {
- current_ring = outer_ring - (r * 3);
- angle = 360 / current_ring;
- ring_radius = platform_inner_radius - (ring_spacing * r);
- };
- for(var a = 0; a < current_ring; a++){
- var x = 0;
- var y = 0;
- // Is this the innermost ring?
- if(r == ring_count - 1){
- x = platform_x;
- y = platform_y;
- } else {
- x = platform_x + (ring_radius * Math.cos((angle * a)
- * (Math.PI / 180)));
- y = platform_y + (ring_radius * Math.sin((angle * a)
- * (Math.PI / 180)));
- };
- var radius = 10;
- var mass = 5;
- var friction = 0.95;
- asteroids.push(new Asteroid(x, y, radius, mass, friction));
- };
- };
- // Start the animation loop
- animate();
- };
- // Initialize game environment
- function init(){
- ui_stats.hide();
- ui_complete.hide();
- ui_play.click(function(e){
- e.preventDefault();
- ui_intro.hide();
- start_game();
- });
- ui_reset.click(function(e){
- e.preventDefault();
- ui_complete.hide();
- start_game();
- });
- };
- // Animation loop that does all the fun stuff
- function animate(){
- // Clear
- context.clearRect(0, 0, canvas_width, canvas_height);
- context.fillStyle = "rgb(100, 100, 100)";
- context.beginPath();
- context.arc(platform_x, platform_y, platform_outer_radius, 0,
- Math.PI * 2, true);
- context.closePath();
- context.fill();
- if(play_game){
- // Run the animation loop again in 33 ms
- setTimeout(animate, 33);
- };
- };
- init();
- });
Add Comment
Please, Sign In to add comment