-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
84 lines (74 loc) · 1.89 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var actualWidth = ctx.canvas.width;
var actualHeight = ctx.canvas.height;
var gameSpeed = 0;
var stepsSpentCollided = 0;
startgame();
function startgame() {
var ctx = c.getContext("2d");
ctx.clearRect(0, 0, c.width, c.height);
var ONE_FRAME_TIME = 1000 / 60 ;
gameSetup();
var mainloop = function() {
updateGame();
drawGame();
};
setInterval( mainloop, ONE_FRAME_TIME );
}
function gameSetup() {
player
GameController.blockers.push(
new CeilBlocker(0),
new Blocker(150),
new Blocker(300),
new CeilBlocker(450),
new CeilBlocker(600));
}
function updateGame() {
GameController.update();
}
function drawGame() {
ctx.clearRect(0, 0, c.width, c.height);
drawHUD();
player.draw();
GameController.draw();
}
function drawHUD() {
ctx.rect(0,0,actualWidth,actualHeight);
ctx.stroke();
ctx.fillStyle = "#000000";
ctx.font = "24px Helvetica";
ctx.fillText("Speed Up - <",actualWidth-350,200);
ctx.fillText("Slow Down - >",actualWidth-350,230);
ctx.fillText("Change Position - space bar",actualWidth-350,260);
ctx.fillStyle = "#990000";
ctx.fillText("Steps spent colliding " + stepsSpentCollided, actualWidth-350,290);
ctx.fillStyle = "#000000";
ctx.fillRect(0,0,actualWidth,actualHeight/2);
}
window.addEventListener("keyup", dealWithKeyboardUp, false);
window.addEventListener("keydown", dealWithKeyboardDown, false);
function dealWithKeyboardUp(e) {
if (e.keyCode == "32") {
// Flip gravity
player.reverse();
}
}
function dealWithKeyboardDown(e) {
if (e.keyCode == "37") {
gameSpeed++;
}
if (e.keyCode == "39") {
gameSpeed--;
if (gameSpeed < 0) {
gameSpeed = 0;
}
}
}
function collides(a, b) {
return a.x < b.x + b.width &&
a.x + a.width > b.x &&
a.y < b.y + b.height &&
a.y + a.height > b.y;
}