Skip to content

Commit

Permalink
add pause funtionality
Browse files Browse the repository at this point in the history
  • Loading branch information
drj17 committed May 10, 2017
1 parent 10a39a5 commit 2fb35b7
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 19 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<html>
<head>
<link rel="shortcut icon" href="./favicon.ico" type="image/x-icon">
<link rel="icon" href=".at/favicon.ico" type="image/x-icon">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<meta charset="utf-8">
<title>Cytosis</title>
Expand All @@ -20,6 +19,7 @@ <h1>Welcome to Cytosis!</h1>
<li>Flashing bacteria (including you!) are invulnerable</li>
<li>Avoid bacteria larger than your own</li>
<li>Eat smaller bacteria to increase your size</li>
<li>Press enter to pause/unpause</li>
<li>Press space to restart at any time</li>
</ul>
<button class="game-button" id="start">Start (Space)</button>
Expand Down
35 changes: 26 additions & 9 deletions lib/bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/bundle.js.map

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions lib/cytosis.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ document.addEventListener("DOMContentLoaded", () => {
canvas.height = 700;
const ctx = canvas.getContext("2d");
const game = new Game();
new Viewport(game, ctx);
const viewPort = new Viewport(game, ctx);
const btn = document.getElementById("start");
const modal = document.getElementById("modal");

document.addEventListener('keydown', (e) => {
if(e.keyCode === 32 && !game.playing){
modal.style.display = "none";
game.start();
e.source.removeEventListener('keydown', arguments.callee);
} else if(e.keyCode === 13){
viewPort.paused = !viewPort.paused;
game.paused ? game.unpause() : game.pause();
}
});

Expand Down
19 changes: 17 additions & 2 deletions lib/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ class Game {
this.circles = [];
this.player = new Player();
this.playing = false;
this.generateCircles();
this.paused = false;
this.sizeModifier = 1.0;
this.maxRadius = 40;
this.score = 0;
this.gameOver = false;
this.generateCircles();
this.modal = document.getElementById("end-modal");
this.restartBtn = document.getElementById("restart");
this.restartBtn.addEventListener("click", () => this.restart());
Expand All @@ -20,11 +21,25 @@ class Game {
});
}

unpause(){
this.paused = false;
this.interval = setInterval(() => {
this.circles.push(new Circle(false, (this.player.radius*this.sizeModifier)));
}, 1500);
}

pause(){
this.paused = true;
clearInterval(this.interval);
}

start(){
this.playing = true;
this.player = new Player();
this.interval =
setInterval(() => this.circles.push(new Circle(false, (this.player.radius*this.sizeModifier))), 1500);
setInterval(() => {
this.circles.push(new Circle(false, (this.player.radius*this.sizeModifier)));
}, 1500);
}

restart() {
Expand Down
8 changes: 4 additions & 4 deletions lib/viewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Viewport {
this.game = game;
this.ctx = ctx;
this.start();
this.paused = false;
}

start() {
Expand All @@ -15,14 +16,13 @@ class Viewport {
}

animate(time) {

const dt = time - this.lastTime;

if(!this.paused){
this.game.moveCircles();
this.game.draw(this.ctx);
this.lastTime = time;
requestAnimationFrame(this.animate.bind(this));
}
requestAnimationFrame(this.animate.bind(this));
}

}
Expand Down

0 comments on commit 2fb35b7

Please sign in to comment.