Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Factor out game-over.js, main.js, score.js, and powerups.js from game…

…07-weapons.js.
  • Loading branch information...
commit 1027e5e5caee97980b3dd7a61436c95d44b37d57 1 parent a1c5d62
@wtaysom authored
View
20 game01/game-over.js
@@ -0,0 +1,20 @@
+player.lives = 3;
+var gameOver = false;
+
+function drawGameOver() {
+ c.fillStyle = 'red';
+ c.font = "bold 60pt Arial";
+ var baseX = 120;
+ var baseY = 140;
+ c.fillText("GAME", baseX, baseY);
+ c.fillText("OVER", baseX, baseY + 100);
+}
+
+function restart() {
+ if (gameOver) {
+ player.lives = 3;
+ gameOver = false;
+ }
+}
+
+key('w,a,s,d', restart);
View
5 game01/game.html
@@ -28,6 +28,9 @@
<script src="player.js"></script>
<script src="bullets.js"></script>
<script src="bad-guys.js"></script>
- <script src="game07-weapons.js"></script>
+ <script src="powerups.js"></script>
+ <script src="game-over.js"></script>
+ <script src="score.js"></script>
+ <script src="main.js"></script>
</body>
</html>
View
152 game01/game07-weapons.js
@@ -1,152 +0,0 @@
-function removeIfHasRunOffTheBottom(piece, remove) {
- if (sides(piece).top > canvas.height) {
- remove(piece);
- }
-}
-
-/** Powerups **/
-
-var powerups = [];
-var maxPowerups = 3;
-var maxPowerupCoolDown = 600;
-var powerupCoolDown = 20;
-
-function addPowerup() {
- var size = randomBetween(10, 20);
- powerups.push({
- x: randomX(),
- y: -30,
- width: size,
- height: size,
- color: '#881'
- });
-}
-
-function removePowerup(powerup) {
- var i = powerups.indexOf(powerup);
- if (i !== -1) {
- powerups.splice(i, 1);
- }
-}
-
-function maybeAddPowerup() {
- if (powerups.length >= maxPowerups) {
- return;
- }
-
- if (randomBetween(0, powerupCoolDown) < 10) {
- powerupCoolDown = maxPowerupCoolDown;
- addPowerup();
- } else {
- --powerupCoolDown;
- }
-}
-
-function updatePowerups() {
- for (var i = 0; i < powerups.length; ++i) {
- var powerup = powerups[i];
- powerup.y += 2;
-
- if (intersects(powerup, player)) {
- removePowerup(powerup);
- powerupBullets();
- }
- }
-}
-
-function powerupBullets() {
- if (maxBulletCoolDown > 5) {
- maxBulletCoolDown *= 0.8;
- }
-}
-
-function maybeRemovePowerup() {
- for (var i = 0; i < powerups.length; ++i) {
- var powerup = powerups[i];
- removeIfHasRunOffTheBottom(powerup, removePowerup);
- }
-}
-
-/** Score **/
-
-player.score = 0;
-
-function score(points) {
- player.score += points;
-}
-
-function drawScore() {
- var div = document.getElementById('score');
- div.innerHTML = player.score;
-}
-
-/** Game Over **/
-
-player.lives = 3;
-var gameOver = false;
-
-function drawGameOver() {
- c.fillStyle = 'red';
- c.font = "bold 60pt Arial";
- var baseX = 120;
- var baseY = 140;
- c.fillText("GAME", baseX, baseY);
- c.fillText("OVER", baseX, baseY + 100);
-}
-
-function restart() {
- if (gameOver) {
- player.lives = 3;
- gameOver = false;
- }
-}
-
-key('w,a,s,d', restart);
-
-/** Update **/
-
-function update() {
- move(player);
-
- maybeAddBadGuys();
- updateBadGuys();
- maybeRemoveBadGuys();
-
- maybeAddBullet();
- updateBullets();
- maybeRemoveBullets();
-
- maybeAddPowerup();
- updatePowerups();
- maybeRemovePowerup();
-
- bound(player, boundingBox);
-}
-
-function drawPowerups() {
- for (var i = 0; i < powerups.length; ++i) {
- var powerup = powerups[i];
- fillPiece(powerup);
- }
-}
-
-/** Redraw **/
-
-function redraw() {
- c.clearRect(0, 0, canvas.width, canvas.height);
- if (gameOver) {
- drawGameOver();
- } else {
- drawPowerups();
- drawBadGuys();
- drawBullets();
- drawPlayer();
- drawScore();
- }
-}
-
-/** State **/
-
-random.seed = 2074710737;
-
-state('random heldKeys player bullets bulletCoolDown maxBulletCoolDown badGuys marchDirection powerups maxPowerupCoolDown powerupCoolDown gameOver');
View
36 game01/main.js
@@ -0,0 +1,36 @@
+function update() {
+ move(player);
+
+ maybeAddBadGuys();
+ updateBadGuys();
+ maybeRemoveBadGuys();
+
+ maybeAddBullet();
+ updateBullets();
+ maybeRemoveBullets();
+
+ maybeAddPowerup();
+ updatePowerups();
+ maybeRemovePowerup();
+
+ bound(player, boundingBox);
+}
+
+function redraw() {
+ c.clearRect(0, 0, canvas.width, canvas.height);
+ if (gameOver) {
+ drawGameOver();
+ } else {
+ drawPowerups();
+ drawBadGuys();
+ drawBullets();
+ drawPlayer();
+ drawScore();
+ }
+}
+
+/** State **/
+
+random.seed = 2074710737;
+
+state('random heldKeys player bullets bulletCoolDown maxBulletCoolDown badGuys marchDirection powerups maxPowerupCoolDown powerupCoolDown gameOver');
View
67 game01/powerups.js
@@ -0,0 +1,67 @@
+var powerups = [];
+var maxPowerups = 3;
+var maxPowerupCoolDown = 600;
+var powerupCoolDown = 20;
+
+function addPowerup() {
+ var size = randomBetween(10, 20);
+ powerups.push({
+ x: randomX(),
+ y: -30,
+ width: size,
+ height: size,
+ color: '#881'
+ });
+}
+
+function removePowerup(powerup) {
+ var i = powerups.indexOf(powerup);
+ if (i !== -1) {
+ powerups.splice(i, 1);
+ }
+}
+
+function maybeAddPowerup() {
+ if (powerups.length >= maxPowerups) {
+ return;
+ }
+
+ if (randomBetween(0, powerupCoolDown) < 10) {
+ powerupCoolDown = maxPowerupCoolDown;
+ addPowerup();
+ } else {
+ --powerupCoolDown;
+ }
+}
+
+function updatePowerups() {
+ for (var i = 0; i < powerups.length; ++i) {
+ var powerup = powerups[i];
+ powerup.y += 2;
+
+ if (intersects(powerup, player)) {
+ removePowerup(powerup);
+ powerupBullets();
+ }
+ }
+}
+
+function powerupBullets() {
+ if (maxBulletCoolDown > 5) {
+ maxBulletCoolDown *= 0.8;
+ }
+}
+
+function maybeRemovePowerup() {
+ for (var i = 0; i < powerups.length; ++i) {
+ var powerup = powerups[i];
+ removeIfHasRunOffTheBottom(powerup, removePowerup);
+ }
+}
+
+function drawPowerups() {
+ for (var i = 0; i < powerups.length; ++i) {
+ var powerup = powerups[i];
+ fillPiece(powerup);
+ }
+}
View
10 game01/score.js
@@ -0,0 +1,10 @@
+player.score = 0;
+
+function score(points) {
+ player.score += points;
+}
+
+function drawScore() {
+ var div = document.getElementById('score');
+ div.innerHTML = player.score;
+}
View
17 game01/util.js
@@ -70,17 +70,8 @@ function fillPiece(p) {
c.fillRect(x, y, p.width, p.height);
}
-/** Game **/
-
-var player = {
- x: canvas.width * 0.6,
- y: canvas.height * 0.8,
- width: 30,
- height: 40,
- color: '#C21',
- speed: 300
-}
-
-function drawPlayer() {
- fillPiece(player);
+function removeIfHasRunOffTheBottom(piece, remove) {
+ if (sides(piece).top > canvas.height) {
+ remove(piece);
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.