Skip to content

Commit 3ddc337

Browse files
authored
Merge pull request #4 from Prouser123/develop
Clean up code
2 parents 68bb8a8 + 80e2560 commit 3ddc337

4 files changed

Lines changed: 82 additions & 114 deletions

File tree

css/game.css

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* Styling needed for a fullscreen canvas and no scrollbars. */
2+
3+
body, html {
4+
width: 100%;
5+
height: 100%;
6+
margin: 0;
7+
padding: 0;
8+
overflow: hidden;
9+
}
10+
11+
#starfield {
12+
width:100%;
13+
height:100%;
14+
z-index: -1;
15+
position: absolute;
16+
left: 0px;
17+
top: 0px;
18+
}
19+
20+
#gamecontainer {
21+
width: 800px;
22+
margin-left: auto;
23+
margin-right: auto;
24+
}
25+
26+
#gamecanvas {
27+
width: 800px;
28+
height: 600px;
29+
}
30+
31+
#info {
32+
width: 800px;
33+
margin-left: auto;
34+
margin-right: auto;
35+
}

index.html

Lines changed: 2 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,7 @@
44
<title>Space Invaders</title>
55
<link rel="stylesheet" type="text/css" href="css/core.css">
66
<link rel="stylesheet" type="text/css" href="css/typeography.css">
7-
<style>
8-
9-
/* Styling needed for a fullscreen canvas and no scrollbars. */
10-
body, html {
11-
width: 100%;
12-
height: 100%;
13-
margin: 0;
14-
padding: 0;
15-
overflow: hidden;
16-
}
17-
18-
#starfield {
19-
width:100%;
20-
height:100%;
21-
z-index: -1;
22-
position: absolute;
23-
left: 0px;
24-
top: 0px;
25-
}
26-
#gamecontainer {
27-
width: 800px;
28-
margin-left: auto;
29-
margin-right: auto;
30-
}
31-
#gamecanvas {
32-
width: 800px;
33-
height: 600px;
34-
}
35-
#info {
36-
width: 800px;
37-
margin-left: auto;
38-
margin-right: auto;
39-
}
40-
</style>
7+
<link rel="stylesheet" type="text/css" href="css/game.css">
418
</head>
429
<body>
4310
<div id="starfield"></div>
@@ -53,59 +20,6 @@
5320

5421
<script src="js/starfield.js"></script>
5522
<script src="js/spaceinvaders.js"></script>
56-
<script>
57-
58-
// Create the starfield.
59-
var container = document.getElementById('starfield');
60-
var starfield = new Starfield();
61-
starfield.initialise(container);
62-
starfield.start();
63-
64-
// Setup the canvas.
65-
var canvas = document.getElementById("gameCanvas");
66-
canvas.width = 800;
67-
canvas.height = 600;
68-
69-
// Create the game.
70-
var game = new Game();
71-
72-
// Initialise it with the game canvas.
73-
game.initialise(canvas);
74-
75-
// Start the game.
76-
game.start();
77-
78-
// Listen for keyboard events.
79-
window.addEventListener("keydown", function keydown(e) {
80-
var keycode = e.which || window.event.keycode;
81-
// Supress further processing of left/right/space/a/d/w/up (37/29/32/65/68/87/38)
82-
if(keycode == 37 || keycode == 39 || keycode == 32 || keycode == 65 || keycode == 68 || keycode == 87 || keycode == 38) {
83-
e.preventDefault();
84-
}
85-
game.keyDown(keycode);
86-
});
87-
window.addEventListener("keyup", function keydown(e) {
88-
var keycode = e.which || window.event.keycode;
89-
game.keyUp(keycode);
90-
});
91-
92-
function toggleMute() {
93-
game.mute();
94-
document.getElementById("muteLink").innerText = game.sounds.mute ? "unmute" : "mute";
95-
}
96-
</script>
97-
<script type="text/javascript">
98-
99-
var _gaq = _gaq || [];
100-
_gaq.push(['_setAccount', 'UA-41728580-1']);
101-
_gaq.push(['_trackPageview']);
102-
103-
(function() {
104-
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
105-
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
106-
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
107-
})();
108-
109-
</script>
23+
<script src="js/setup.js"></script>
11024
</body>
11125
</html>

js/setup.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Create the starfield.
2+
var container = document.getElementById('starfield');
3+
var starfield = new Starfield();
4+
starfield.initialise(container);
5+
starfield.start();
6+
7+
// Setup the canvas.
8+
var canvas = document.getElementById("gameCanvas");
9+
canvas.width = 800;
10+
canvas.height = 600;
11+
12+
// Create the game.
13+
var game = new Game();
14+
15+
// Initialise it with the game canvas.
16+
game.initialise(canvas);
17+
18+
// Start the game.
19+
game.start();
20+
21+
// Listen for keyboard events.
22+
window.addEventListener("keydown", function keydown(e) {
23+
var keycode = e.which || window.event.keycode;
24+
// Supress further processing of left/right/space/a/d/w/up (37/29/32/65/68/87/38)
25+
if(keycode == 37 || keycode == 39 || keycode == 32 || keycode == 65 || keycode == 68 || keycode == 87 || keycode == 38) {
26+
e.preventDefault();
27+
}
28+
game.keyDown(keycode);
29+
});
30+
31+
window.addEventListener("keyup", function keydown(e) {
32+
var keycode = e.which || window.event.keycode;
33+
game.keyUp(keycode);
34+
});
35+
36+
function toggleMute() {
37+
game.mute();
38+
document.getElementById("muteLink").innerText = game.sounds.mute ? "Unmute" : "Mute";
39+
}

js/spaceinvaders.js

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -344,37 +344,17 @@ PlayState.prototype.update = function(game, dt) {
344344
// event for smooth movement, otherwise the ship would move
345345
// more like a text editor caret.
346346

347-
// Left Arrow Key
348-
if(game.pressedKeys[37]) {
347+
// Left Movement Keys : A Key / Left Arrow Key
348+
if(game.pressedKeys[65] || game.pressedKeys[37]) {
349349
this.ship.x -= this.shipSpeed * dt;
350350
}
351351

352-
// Right Arrow Key
353-
if(game.pressedKeys[39]) {
352+
// Right Movement Keys: D Key / Right Arrow Key
353+
if(game.pressedKeys[68] || game.pressedKeys[39]){
354354
this.ship.x += this.shipSpeed * dt;
355355
}
356-
// Space Key
357-
if(game.pressedKeys[32]) {
358-
this.fireRocket();
359-
}
360-
361-
// A Key
362-
if(game.pressedKeys[65]) {
363-
this.ship.x -= this.shipSpeed * dt;
364-
}
365-
366-
// D Key
367-
if(game.pressedKeys[68]) {
368-
this.ship.x += this.shipSpeed * dt;
369-
}
370-
371-
// W Key
372-
if(game.pressedKeys[87]) {
373-
this.fireRocket();
374-
}
375-
376-
// Up Key
377-
if(game.pressedKeys[38]) {
356+
// Fire Keys : Space Key / W Key / Up Key
357+
if(game.pressedKeys[32] || game.pressedKeys[87] || game.pressedKeys[38]) {
378358
this.fireRocket();
379359
}
380360

0 commit comments

Comments
 (0)