Amazing-Python-Scripts

Форк
0
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
	
9
	var SPAWN_TIME = 2000;
10
	var difficultyMultiplier = 1;
11
	var nextEnemyTime = SPAWN_TIME;
12
	
13
	// The ship
14
	var ship;
15
	
16
	// The score model that we add points to when an enemy is killed
17
	var scoreModel;
18
	
19
	// The time that has passed since the last enemy was spawned
20
	var enemyTime = 0;
21
	
22
	// If we're in the game over state
23
	var _gameOver = false;
24
	var _running = false;
25
	
26
	// An array of enemy type variations that can be selected from
27
	// when spawning a new one
28
	var _enemyTypes;
29
	
30
	var _enemiesKilled = _enemiesEscaped = 0;
31
	
32
	// All the possible enemy types in order of difficulty
33
	var _allEnemyTypes = [
34
		{
35
			health: 1
36
		},
37
		{
38
			health: 2
39
		},
40
		{
41
			health: 3
42
		},
43
		{
44
			health: 4
45
		},
46
		{
47
			health: 5
48
		}
49
	];
50
	
51
	var _allShipPowerups = [
52
		/*{
53
			burstLength: 1,
54
			roundDelay: 90
55
		},*/
56
		{
57
			burstLength: 2,
58
			roundDelay: 90
59
		},
60
		{
61
			burstLength: 3,
62
			roundDelay: 90
63
		},
64
		{
65
			burstLength: 5,
66
			roundDelay: 90
67
		},
68
		{
69
			burstLength: 5,
70
			roundDelay: 60
71
		}
72
	];
73
	
74
	var initialize = function(assets, start){
75
		// Add the instruction screen
76
		assets.add(new Instructions({
77
			message: 'Press [Enter] to start!',
78
			bounds: {
79
				top: 0,
80
				right: game.width,
81
				bottom: game.height,
82
				left: 0
83
			},
84
			start: start
85
		}));
86
	}
87
	
88
	// setupGameAssets is passed an array of game assets. Add
89
	// to this array to automatically update and draw them
90
	// each frame.
91
	var setupGameAssets = function(assets){
92
		_running = true;
93
		
94
		enemyTime = 0;
95
		
96
		// Set the initial possible enemy type including the
97
		// first enemy only
98
		_enemyTypes = [_allEnemyTypes[0]];
99
		_enemiesKilled = _enemiesEscaped = 0;
100
		difficultyMultiplier = 1;
101
		
102
		scoreModel = new ScoreModel({
103
			
104
			// When increaseDifficulty is called add the next difficulty enemy
105
			// to the enemy types pool. This runs through each enemy index as
106
			// difficultyLevel increments one each callback. If we run out of
107
			// enemies to add then the last (most difficult) enemy will be
108
			// added again to increase the likelyhood of it being selected.
109
			increaseDifficulty: function(difficultyLevel){
110
				if(_allEnemyTypes.length > difficultyLevel){
111
					_enemyTypes.push(_allEnemyTypes[_allEnemyTypes.length-1, difficultyLevel]);
112
				}
113
				if(difficultyMultiplier > 0.6){
114
					difficultyMultiplier = Math.max(0.6, difficultyMultiplier - 0.03);
115
				}
116
			},
117
			
118
			// When increasePowerup() is called we apply the upgrade for that
119
			// lever to the ship for as long as there are upgrades to apply.
120
			increasePowerup: function(powerupLevel){
121
				if(_allShipPowerups.length > powerupLevel){
122
					ship.powerup(_allShipPowerups[powerupLevel]);
123
				}
124
			}
125
		});
126
		
127
		var bounds = {
128
			top: 0,
129
			right: game.width,
130
			bottom: game.height,
131
			left: 0
132
		};
133
		
134
		assets.add(new Background({
135
			width: game.width,
136
			height: game.height
137
		}));
138
		
139
		// assets.add(new FrameTimer({
140
		// 	bounds: bounds
141
		// }));
142
		
143
		assets.add(new ScoreBoard({
144
			bounds: bounds,
145
			scoreModel: scoreModel
146
		}));
147
		
148
		ship = assets.add(new Ship({
149
			x: game.width/2,
150
			y: game.height-135,
151
			bounds: bounds
152
		}));
153
		ship.powerup(_allShipPowerups[0]);
154
	}
155
	
156
	// Update anything in addition to registered assets
157
	var update = function(frameTime){
158
		if(_gameOver){
159
			if(Input.restart()){
160
				_gameOver = false;
161
				game.run();
162
			}
163
			return;
164
		}
165
		
166
		// Spawn enemies as time passes
167
		enemyTime += frameTime;
168
		if(enemyTime > (nextEnemyTime)){
169
			enemyTime = 0;
170
			
171
			// Calculate the number of ms until we spawn another enemy
172
			nextEnemyTime = SPAWN_TIME * difficultyMultiplier;
173
			
174
			// Pick a random enemy from the pool, this pool gets more difficult
175
			// enemies added over time as more enemies are killed.
176
			var options = extend({
177
				x: (Math.random() * (game.width-40)) + 20,
178
				y: -20,
179
				bounds: {
180
					top: -20,
181
					right: game.width,
182
					bottom: game.height+20,
183
					left: 0
184
				},
185
				escaped: function(){
186
					scoreModel.resetMultiplier();
187
					++_enemiesEscaped;
188
				}
189
			}, _enemyTypes[Math.floor(Math.random() * (_enemyTypes.length))]);
190
			
191
			game.assets.add(new Enemy(options));
192
		}
193
		
194
		var bullets = Bullet.instances;
195
		var 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.
199
		for(var i=enemies.length-1; i>-1; --i){
200
			var enemy = enemies[i];
201
			var destroyed = false;
202
			
203
			// Check if any bullets hit them
204
			for(var r=bullets.length-1; r>-1; --r){
205
				var bullet = bullets[r];
206
				
207
				if(bullet.hits(enemy)){
208
					bullet.destroy();
209
					if(enemy.damage({
210
						damage: 1,
211
						angle: bullet.angle,
212
						speed: bullet.speed * 0.8,
213
						x: bullet.x,
214
						y: bullet.y
215
					})){
216
						// If an enemy is destroyed then we don't need to check collisions
217
						// against the rest of the bullets.
218
						destroyed = true;
219
						scoreModel.add(10);
220
						++_enemiesKilled;
221
						break;
222
					}
223
				}
224
			}
225
			
226
			// This enemy has been killed then don't check it for ship collisions
227
			if(destroyed) continue;
228
			
229
			// Check if they hit the ship
230
			if(ship.hits(enemy)){
231
				
232
				// Kill all the enemies
233
				killAllEnemies();
234
				
235
				// Apply the damage to the ship, if it's run out of health as a result then it's game over!
236
				if(ship.damage(1)){
237
					gameOver();
238
					return;
239
				}
240
			}
241
		}
242
	};
243
	
244
	// Kill all enemies
245
	var killAllEnemies = function(){
246
		var enemies = Enemy.instances;
247
		for(var i=enemies.length-1; i>-1; --i){
248
			var enemy = enemies[i];
249
			enemy.destroy({
250
				explode: true,
251
				x: enemy.x,
252
				y: enemy.y,
253
				speed: 5,
254
				angle: 0,
255
				angleVariation: 6.28
256
			});
257
			scoreModel.add(10);
258
			++_enemiesKilled;
259
		}
260
		difficultyMultiplier = Math.min(1, Math.max(0.6, difficultyMultiplier + 0.1));
261
	}
262
	
263
	var gameOver = function(){
264
		_gameOver = true;
265
		
266
		// Destroy the ship
267
		ship.destroy();
268
		
269
		// Save the score
270
		scoreModel.save();
271
		
272
		// Show Game Over
273
		game.assets.add(new GameOver({
274
			bounds: {
275
				top: 0,
276
				right: game.width,
277
				bottom: game.height,
278
				left: 0
279
			},
280
			scoreModel: scoreModel,
281
			bulletsFired: ship.bulletsFired,
282
			enemiesKilled: _enemiesKilled,
283
			enemiesescaped: _enemiesEscaped
284
		}));
285
	};
286
	
287
	// Draw anything in addition to registered assets
288
	var draw = function(ctx){};
289
	
290
	// If the pause button is pressed then pause the game loop
291
	document.addEventListener('keydown', function(e){
292
		if(!_gameOver && _running && e.keyCode == 80){
293
			if(game.paused){
294
				game.assets.remove(instructions);
295
				game.pause(false);
296
			}else{
297
				instructions = new Instructions({
298
					state: 'paused',
299
					bounds: {
300
						top: 0,
301
						right: game.width,
302
						bottom: game.height,
303
						left: 0
304
					}
305
				});
306
				game.assets.add(instructions);
307
				game.pause(true);
308
			}
309
		}
310
	});
311
	
312
	// Start the game loop
313
	var game = window.game = new GameLoop({
314
		canvas: $('#canvas'),
315
		initialize: initialize,
316
		setupGameAssets: setupGameAssets,
317
		update: update,
318
		draw: draw
319
	});
320
	game.run();
321
})();

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.