-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathgame.js
159 lines (139 loc) · 4.09 KB
/
game.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Code modified from:
http://www.lostdecadegames.com/how-to-make-a-simple-html5-canvas-game/
using graphics purchased from vectorstock.com
*/
/* Initialization.
Here, we create and add our "canvas" to the page.
We also load all of our images.
*/
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
canvas.width = 512;
canvas.height = 480;
document.body.appendChild(canvas);
let bgReady, heroReady, monsterReady;
let bgImage, heroImage, monsterImage;
let startTime = Date.now();
const SECONDS_PER_ROUND = 30;
let elapsedTime = 0;
function loadImages() {
bgImage = new Image();
bgImage.onload = function () {
// show the background image
bgReady = true;
};
bgImage.src = "images/background.png";
heroImage = new Image();
heroImage.onload = function () {
// show the hero image
heroReady = true;
};
heroImage.src = "images/hero.png";
monsterImage = new Image();
monsterImage.onload = function () {
// show the monster image
monsterReady = true;
};
monsterImage.src = "images/monster.png";
}
/**
* Setting up our characters.
*
* Note that heroX represents the X position of our hero.
* heroY represents the Y position.
* We'll need these values to know where to "draw" the hero.
*
* The same applies to the monster.
*/
let heroX = canvas.width / 2;
let heroY = canvas.height / 2;
let monsterX = 100;
let monsterY = 100;
/**
* Keyboard Listeners
* You can safely ignore this part, for now.
*
* This is just to let JavaScript know when the user has pressed a key.
*/
let keysPressed = {};
function setupKeyboardListeners() {
// Check for keys pressed where key represents the keycode captured
// For now, do not worry too much about what's happening here.
document.addEventListener("keydown", function (e) {
keysPressed[e.key] = true;
}, false);
document.addEventListener("keyup", function (e) {
keysPressed[e.key] = false;
}, false);
}
/**
* Update game objects - change player position based on key pressed
* and check to see if the monster has been caught!
*
* If you change the value of 5, the player will move at a different rate.
*/
let update = function () {
// Update the time.
elapsedTime = Math.floor((Date.now() - startTime) / 1000);
if (keysPressed["ArrowUp"]) {
heroY -= 5;
}
if (keysPressed["ArrowDown"]) {
heroY += 5;
}
if (keysPressed["ArrowLeft"]) {
heroX -= 5;
}
if (keysPressed["ArrowRight"]) {
heroX += 5;
}
// Check if player and monster collided. Our images
// are 32 pixels big.
if (
heroX <= (monsterX + 32)
&& monsterX <= (heroX + 32)
&& heroY <= (monsterY + 32)
&& monsterY <= (heroY + 32)
) {
// Pick a new location for the monster.
// Note: Change this to place the monster at a new, random location.
monsterX = monsterX + 50;
monsterY = monsterY + 70;
}
};
/**
* This function, render, runs as often as possible.
*/
function render() {
if (bgReady) {
ctx.drawImage(bgImage, 0, 0);
}
if (heroReady) {
ctx.drawImage(heroImage, heroX, heroY);
}
if (monsterReady) {
ctx.drawImage(monsterImage, monsterX, monsterY);
}
ctx.fillText(`Seconds Remaining: ${SECONDS_PER_ROUND - elapsedTime}`, 20, 100);
};
/**
* The main game loop. Most every game will have two distinct parts:
* update (updates the state of the game, in this case our hero and monster)
* render (based on the state of our game, draw the right things)
*/
function main() {
update();
render();
// Request to do this again ASAP. This is a special method
// for web browsers.
requestAnimationFrame(main);
};
// Cross-browser support for requestAnimationFrame.
// Safely ignore this line. It's mostly here for people with old web browsers.
var w = window;
requestAnimationFrame = w.requestAnimationFrame || w.webkitRequestAnimationFrame || w.msRequestAnimationFrame || w.mozRequestAnimationFrame;
// Let's play this game!
loadImages();
setupKeyboardListeners();
main();