Amazing-Python-Scripts
321 строка · 7.0 Кб
1// IDEAS:
2// Floaty glowing spots in the foreground kinda like the chemical
3// brothers video with the glowing face.. Could be some nice depth
4// effects. Depth would be good to investigate with the game anyway
5// to give hte effect of motion, paralax etc.
6
7(function(){8
9var SPAWN_TIME = 2000;10var difficultyMultiplier = 1;11var nextEnemyTime = SPAWN_TIME;12
13// The ship14var ship;15
16// The score model that we add points to when an enemy is killed17var scoreModel;18
19// The time that has passed since the last enemy was spawned20var enemyTime = 0;21
22// If we're in the game over state23var _gameOver = false;24var _running = false;25
26// An array of enemy type variations that can be selected from27// when spawning a new one28var _enemyTypes;29
30var _enemiesKilled = _enemiesEscaped = 0;31
32// All the possible enemy types in order of difficulty33var _allEnemyTypes = [34{35health: 136},37{38health: 239},40{41health: 342},43{44health: 445},46{47health: 548}49];50
51var _allShipPowerups = [52/*{53burstLength: 1,
54roundDelay: 90
55},*/
56{57burstLength: 2,58roundDelay: 9059},60{61burstLength: 3,62roundDelay: 9063},64{65burstLength: 5,66roundDelay: 9067},68{69burstLength: 5,70roundDelay: 6071}72];73
74var initialize = function(assets, start){75// Add the instruction screen76assets.add(new Instructions({77message: 'Press [Enter] to start!',78bounds: {79top: 0,80right: game.width,81bottom: game.height,82left: 083},84start: start85}));86}87
88// setupGameAssets is passed an array of game assets. Add89// to this array to automatically update and draw them90// each frame.91var setupGameAssets = function(assets){92_running = true;93
94enemyTime = 0;95
96// Set the initial possible enemy type including the97// first enemy only98_enemyTypes = [_allEnemyTypes[0]];99_enemiesKilled = _enemiesEscaped = 0;100difficultyMultiplier = 1;101
102scoreModel = new ScoreModel({103
104// When increaseDifficulty is called add the next difficulty enemy105// to the enemy types pool. This runs through each enemy index as106// difficultyLevel increments one each callback. If we run out of107// enemies to add then the last (most difficult) enemy will be108// added again to increase the likelyhood of it being selected.109increaseDifficulty: function(difficultyLevel){110if(_allEnemyTypes.length > difficultyLevel){111_enemyTypes.push(_allEnemyTypes[_allEnemyTypes.length-1, difficultyLevel]);112}113if(difficultyMultiplier > 0.6){114difficultyMultiplier = Math.max(0.6, difficultyMultiplier - 0.03);115}116},117
118// When increasePowerup() is called we apply the upgrade for that119// lever to the ship for as long as there are upgrades to apply.120increasePowerup: function(powerupLevel){121if(_allShipPowerups.length > powerupLevel){122ship.powerup(_allShipPowerups[powerupLevel]);123}124}125});126
127var bounds = {128top: 0,129right: game.width,130bottom: game.height,131left: 0132};133
134assets.add(new Background({135width: game.width,136height: game.height137}));138
139// assets.add(new FrameTimer({140// bounds: bounds141// }));142
143assets.add(new ScoreBoard({144bounds: bounds,145scoreModel: scoreModel146}));147
148ship = assets.add(new Ship({149x: game.width/2,150y: game.height-135,151bounds: bounds152}));153ship.powerup(_allShipPowerups[0]);154}155
156// Update anything in addition to registered assets157var update = function(frameTime){158if(_gameOver){159if(Input.restart()){160_gameOver = false;161game.run();162}163return;164}165
166// Spawn enemies as time passes167enemyTime += frameTime;168if(enemyTime > (nextEnemyTime)){169enemyTime = 0;170
171// Calculate the number of ms until we spawn another enemy172nextEnemyTime = SPAWN_TIME * difficultyMultiplier;173
174// Pick a random enemy from the pool, this pool gets more difficult175// enemies added over time as more enemies are killed.176var options = extend({177x: (Math.random() * (game.width-40)) + 20,178y: -20,179bounds: {180top: -20,181right: game.width,182bottom: game.height+20,183left: 0184},185escaped: function(){186scoreModel.resetMultiplier();187++_enemiesEscaped;188}189}, _enemyTypes[Math.floor(Math.random() * (_enemyTypes.length))]);190
191game.assets.add(new Enemy(options));192}193
194var bullets = Bullet.instances;195var enemies = Enemy.instances;196
197// Run through each enemy to check for both bullet and ship collisions.198// Doing this in the same loop saves on CPU time.199for(var i=enemies.length-1; i>-1; --i){200var enemy = enemies[i];201var destroyed = false;202
203// Check if any bullets hit them204for(var r=bullets.length-1; r>-1; --r){205var bullet = bullets[r];206
207if(bullet.hits(enemy)){208bullet.destroy();209if(enemy.damage({210damage: 1,211angle: bullet.angle,212speed: bullet.speed * 0.8,213x: bullet.x,214y: bullet.y215})){216// If an enemy is destroyed then we don't need to check collisions217// against the rest of the bullets.218destroyed = true;219scoreModel.add(10);220++_enemiesKilled;221break;222}223}224}225
226// This enemy has been killed then don't check it for ship collisions227if(destroyed) continue;228
229// Check if they hit the ship230if(ship.hits(enemy)){231
232// Kill all the enemies233killAllEnemies();234
235// Apply the damage to the ship, if it's run out of health as a result then it's game over!236if(ship.damage(1)){237gameOver();238return;239}240}241}242};243
244// Kill all enemies245var killAllEnemies = function(){246var enemies = Enemy.instances;247for(var i=enemies.length-1; i>-1; --i){248var enemy = enemies[i];249enemy.destroy({250explode: true,251x: enemy.x,252y: enemy.y,253speed: 5,254angle: 0,255angleVariation: 6.28256});257scoreModel.add(10);258++_enemiesKilled;259}260difficultyMultiplier = Math.min(1, Math.max(0.6, difficultyMultiplier + 0.1));261}262
263var gameOver = function(){264_gameOver = true;265
266// Destroy the ship267ship.destroy();268
269// Save the score270scoreModel.save();271
272// Show Game Over273game.assets.add(new GameOver({274bounds: {275top: 0,276right: game.width,277bottom: game.height,278left: 0279},280scoreModel: scoreModel,281bulletsFired: ship.bulletsFired,282enemiesKilled: _enemiesKilled,283enemiesescaped: _enemiesEscaped284}));285};286
287// Draw anything in addition to registered assets288var draw = function(ctx){};289
290// If the pause button is pressed then pause the game loop291document.addEventListener('keydown', function(e){292if(!_gameOver && _running && e.keyCode == 80){293if(game.paused){294game.assets.remove(instructions);295game.pause(false);296}else{297instructions = new Instructions({298state: 'paused',299bounds: {300top: 0,301right: game.width,302bottom: game.height,303left: 0304}305});306game.assets.add(instructions);307game.pause(true);308}309}310});311
312// Start the game loop313var game = window.game = new GameLoop({314canvas: $('#canvas'),315initialize: initialize,316setupGameAssets: setupGameAssets,317update: update,318draw: draw319});320game.run();321})();