window.onload = init; //load in images var s_bullet = new image("images/bullet.gif"); var s_ship = new image("images/ship.png"); var s_star = new image("images/star.png"); var s_timer = new image("images/timerPlaceholder.png", 10, 10); //10 subimages, with 10 per row. (So just one row in this example.) var soundsFolder = "sounds"; var sounds = [ "shoot" ]; //List of all sound files as strings (do not include file extension) function init() { Game.initialize(20, 20, 500, 500, 60, gameStart); //Start game engine. Create a window at 20,20, width 500 and height 500, 60 frames per second. As soon as loading is done, it'll call gameStart(). } function gameStart() { GameWindow.backgroundColor = {r:0, g:0, b:0}; Game.setUpdateFunction(update); //Let the game engine know we want to link our 'update()' to the game's update step. GameAudio.init(sounds, soundsFolder); GameObject.create(backgroundTemplate, 250, 250); //create the background object! (Big black block) player = GameObject.create(player, 250, 250); //create the player ship at the center of the screen GameObject.create(block, 300, 300); //create an empty block which does nothing for(var i = 0; i < 20; i++) //create a bunch of stars! GameObject.create(star, Math.random() * GameWindow.width, Math.random() * GameWindow.height); } function update() { for(var i = 0; i < Game.activeObjects.length; i++)//loop through all existing objects { if(Game.activeObjects[i].name != "player") //if the current object is not the player... { var obj = Game.activeObjects[i]; //put object reference into a variable obj.transform.position = obj.transform.position.add(player.velocity.stretch(-1)); //move object according to player's velocity vector. /* * We're doing this to create an illusion of movement. By moving everything but the player, * it will appear as if the player is moving, and the camera follows. * Objects can still manipulate their position. */ if(obj.transform.position.distanceTo(player.transform.position) > 1000) //If the object is really far away... obj.destroy(); //just delete it. (If you're expanding this, make sure not to delete important objects!) } } //You can add code here which will be executed every frame. (recommended to do this in individual objects whenever possible) } var block = { start: function() { this.renderer.image = s_timer; }, update: function(deltaTime) { this.renderer.subImage += deltaTime; } } var bullet = { start: function() { this.additionalSpeed = player.velocity.getCopy(); //Bullets copy the initial speed of the shooter. Like throwing an apple out of a moving car; it'll still be moving along with the car until it hits the ground and explodes. Waste of an apple, really. this.renderer.image = s_bullet; this.transform.width = 10; this.transform.height = 30; //it's a bit stretched, but it'll look fun that way. Collision detection will be off, but there are no enemies here anyway so whatever. }, update: function() { this.transform.position = this.transform.position.add( this.transform.forward().stretch(25) ).add(this.additionalSpeed); //Make bullet move forwards! }, onCollision: function(other) { if(other.name != "player") //If i'm colliding with something that's not the player, delete myself. this.destroy(); } } var star = { start: function() { this.ignoreCollisions = true; //Stars are background objects, so we don't want to check for collisions. this.name = "star"; //It's a star, what else is there to say? this.transform.width = 2; //tiny star. this.transform.height = 2; this.color = "white"; //You can change this, but most stars are white so I made these white too. this.renderer.image = s_star; }, update: function() { if(this.transform.position.x < 0 || this.transform.position.x > GameWindow.width || this.transform.position.y < 0 || this.transform.position.y > GameWindow.height) this.reposition(); //If I'm at the border of the screen, or past it, reposition myself! }, reposition: function() { //Put myself at a random spot at a screen border if(Math.random() > 0.5) //Horizontal of vertical? Coin flip it is! { this.transform.position.x = (Math.random() > 0.5? 0 : GameWindow.width); //Another coinflip to decide which side to go to. this.transform.position.y = Math.random() * GameWindow.width; //random point along the border. } else { this.transform.position.y = (Math.random() > 0.5? 0 : GameWindow.height); //Same as above, but on another axis. this.transform.position.x = Math.random() * GameWindow.height; } }, onDestroy: function() { GameObject.create(star, -100, -100); //Stars should never be destroyed. If it is anyway, make a new one. } } var player = //Player ship that flies around and shoots and stuff. { start: function() { this.cooldown = 30; //gun cooldown variable! this.name = "player"; this.renderer.image = s_ship; this.rotSpeed = 2; //how fast can we turn? this.accelleration = 0.2; //Engine power! this.gunOffset = 13; //This will help shoot the bullets from the wings instead of the center. this.velocity = new vector(0, 0); //We start out stationary. }, update: function() { if(GameInput.isHeld(37)) //Left key pressed? Turn left! this.transform.rotation -= this.rotSpeed; if(GameInput.isHeld(39)) //right key pressed? Turn right! this.transform.rotation += this.rotSpeed; if(GameInput.isHeld(38)) //Forward key held? Geronimo! this.velocity = this.velocity.add(this.transform.forward().stretch(this.accelleration)); if(GameInput.isHeld(40)) //Backwards key held? Retreat! this.velocity = this.velocity.add(this.transform.back().stretch(this.accelleration)); this.cooldown--; //let gun cooldown timer run if(GameInput.isHeld(32)) //fire key held? { if(this.cooldown < 0) //cooldown timer expired? { GameAudio.play("shoot"); //Pew pew pew this.gunOffset *= -1; //reverse gun offset to alternate the bullet origin var creationPos = this.transform.position.add(this.transform.forward().stretch(1).add(this.transform.right().stretch(this.gunOffset))); //determine position at which the bullet will be created. var newBullet = GameObject.create(bullet, creationPos.x, creationPos.y); //make a new bullet newBullet.transform.rotation = this.transform.rotation; //rotate the bullet newBullet.transform.rotation += -5 + (Math.random() * 10); //wiggle the bullet for a bit so we're not insanely accurate. Looks cooler. this.cooldown = 5; //set the cooldown to some number to delay the next shot. this.velocity = this.velocity.add(this.transform.back().stretch(this.accelleration * 0.1)); //Knockback in space! } } } } var backgroundTemplate = //Big block which sticks to screen center! { start: function() { this.ignoreCollisions = true; this.name = "background"; this.color = "black"; this.transform.width = 1000; this.transform.height = 1000; }, update: function() { this.transform.position.x = 250; this.transform.position.y = 250; } }