-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove animation-frame, use requestAnimationFrame, add a template
- Loading branch information
Even Alander
committed
Oct 1, 2020
1 parent
6adc4da
commit 01f197e
Showing
6 changed files
with
95 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title><%= htmlWebpackPlugin.options.title %></title> | ||
<%= htmlWebpackPlugin.tags.headTags %> | ||
</head> | ||
<body> | ||
<canvas id="snake"></canvas> | ||
<%= htmlWebpackPlugin.tags.bodyTags %> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,87 @@ | ||
"use strict"; | ||
|
||
import { | ||
IDLE, | ||
ENDED, | ||
} from './constants'; | ||
import { IDLE, ENDED } from "./constants"; | ||
|
||
import AnimationFrame from 'animation-frame'; | ||
import canvas from './html.js'; | ||
import Snake from './snake'; | ||
import canvas from "./html.js"; | ||
import Snake from "./snake"; | ||
|
||
var animationFrame = new AnimationFrame(); | ||
var gridWidth = 30; | ||
var context = canvas.getContext('2d'); | ||
var s = new Snake(context, canvas.width, canvas.height, gridWidth); | ||
var time; | ||
var highScore = 0; | ||
var textMarginLeft = 20; | ||
var textHeight = 50; | ||
var textWidth = canvas.width - textMarginLeft * 2; | ||
var render = () => { | ||
var now = new Date().getTime(); | ||
var dt = now - (time || now); | ||
time = now; | ||
const gridWidth = 30; | ||
const context = canvas.getContext("2d"); | ||
const s = new Snake(context, canvas.width, canvas.height, gridWidth); | ||
const textMarginLeft = 20; | ||
const textHeight = 50; | ||
const textWidth = canvas.width - textMarginLeft * 2; | ||
|
||
context.fillStyle = '#eee'; | ||
context.fillRect(0,0,canvas.width,canvas.height); | ||
let highScore = 0; | ||
let time; | ||
|
||
s.update(dt); | ||
s.render(); | ||
const render = () => { | ||
const now = new Date().getTime(); | ||
const dt = now - (time || now); | ||
time = now; | ||
|
||
context.fillStyle = "#999"; | ||
context.font = "32px sans-serif"; | ||
switch (s.getState()) { | ||
case IDLE: | ||
context.fillText("Press return/enter to start a new game", textMarginLeft, textHeight, textWidth); | ||
break; | ||
case ENDED: | ||
context.fillText("GAME OVER!", textMarginLeft, textHeight, textWidth); | ||
context.fillText("Press return/enter to start a new game", textMarginLeft, textHeight * 2, textWidth); | ||
if (s.getScore() > 0 && s.getScore() >= highScore) { | ||
highScore = s.getScore(); | ||
context.fillText( | ||
"Congratulations, new high score: " + highScore, | ||
textMarginLeft, | ||
textHeight * 3, // eslint-disable-line | ||
textWidth); | ||
} else { | ||
context.fillText( | ||
"You scored " + s.getScore() + " points. Your high score is " + highScore, | ||
textMarginLeft, | ||
textHeight * 3, // eslint-disable-line | ||
textWidth); | ||
} | ||
break; | ||
default: | ||
context.fillText(s.getScore(), textMarginLeft, textHeight, textWidth); | ||
animationFrame.request(render); | ||
break; | ||
} | ||
context.fillStyle = "#eee"; | ||
context.fillRect(0, 0, canvas.width, canvas.height); | ||
|
||
s.update(dt); | ||
s.render(); | ||
|
||
context.fillStyle = "#999"; | ||
context.font = "32px sans-serif"; | ||
switch (s.getState()) { | ||
case IDLE: | ||
context.fillText( | ||
"Press return/enter to start a new game", | ||
textMarginLeft, | ||
textHeight, | ||
textWidth | ||
); | ||
break; | ||
|
||
case ENDED: | ||
context.fillText("GAME OVER!", textMarginLeft, textHeight, textWidth); | ||
context.fillText( | ||
"Press return/enter to start a new game", | ||
textMarginLeft, | ||
textHeight * 2, | ||
textWidth | ||
); | ||
if (s.getScore() > 0 && s.getScore() >= highScore) { | ||
highScore = s.getScore(); | ||
context.fillText( | ||
"Congratulations, new high score: " + highScore, | ||
textMarginLeft, | ||
textHeight * 3, // eslint-disable-line | ||
textWidth | ||
); | ||
} else { | ||
context.fillText( | ||
"You scored " + | ||
s.getScore() + | ||
" points. Your high score is " + | ||
highScore, | ||
textMarginLeft, | ||
textHeight * 3, // eslint-disable-line | ||
textWidth | ||
); | ||
} | ||
break; | ||
|
||
default: | ||
context.fillText(s.getScore(), textMarginLeft, textHeight, textWidth); | ||
requestAnimationFrame(render); | ||
break; | ||
} | ||
}; | ||
|
||
animationFrame.request(render); | ||
requestAnimationFrame(render); | ||
|
||
import controls from './controls'; | ||
import controls from "./controls"; | ||
|
||
controls.on('move', (dir) => { s.setDir(dir); }); | ||
controls.on('new_game', () => { | ||
s.newGame(); | ||
animationFrame.request(render); | ||
controls.on("move", (dir) => { | ||
s.setDir(dir); | ||
}); | ||
controls.on("new_game", () => { | ||
s.newGame(); | ||
requestAnimationFrame(render); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters