-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.js
218 lines (192 loc) · 5.86 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
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.getElementById("canvas").appendChild(canvas);
let background = {};
/**
* Setting up our characters.
*
* Note that hero.x represents the X position of our hero.
* hero.y represents the Y position.
* We'll need these values to know the status where to "draw" the hero.
* The same goes for the monsters
*
*/
let hero = { x: canvas.width / 2, y: canvas.height / 2, size: 32 }; // hero position x,y
let monsters = [
{ x: 100, y: 100 }, //monster 1 position x,y
{ x: 200, y: 200 }, //monster 2 position x,y
{ x: 300, y: 300 }, //monster 3 position x,y
];
let startTime = Date.now(); // Record the starting time
const SECONDS_PER_ROUND = 30; // set limit of how long a round SHOULD run
let elapsedTime = 0; // keep track of how long game is run
let keysPressed = {}; //keep track of what key is pressing !!!!!
let score = 0;
//END of declaration
function loadImages() {
background.image = new Image(); //tellinng that we are preparing an image
background.image.onload = function () {
// show the background image
background.ready = true; ///////// to change the check that image is now ready
};
background.image.src = "images/background.png"; //the src of
hero.image = new Image();
hero.image.onload = function () {
// show the hero image
hero.ready = true;
};
hero.image.src = "images/hero.png";
for (let index = 0; index < monsters.length; index++) {
const monster = monsters[index];
monster.image = new Image();
monster.image.onload = function () {
// show the monster image
monster.ready = true;
};
monster.image.src = `images/monster_${index + 1}.png`;
}
}
/**
* Keyboard Listeners
* You can safely ignore this part, for now.
*
* This is just to let JavaScript know when the user has pressed a key.
*/
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"]) {
hero.y -= 5;
}
if (keysPressed["ArrowDown"]) {
hero.y += 5;
}
if (keysPressed["ArrowLeft"]) {
hero.x -= 5;
}
if (keysPressed["ArrowRight"]) {
hero.x += 5;
}
// Check if player and monster collided. Our images
// are 32 pixels big.
// let monsters = [
// { x: 100, y: 100 }, //monster 1 position x,y
// { x: 200, y: 200 }, //monster 2 position x,y
// { x: 300, y: 300 }, //monster 3 position x,y
// ];
//monster conllion
for (let index = 0; index < monsters.length; index++) {
let singleMonster = monsters[index];
if (
hero.x <= singleMonster.x + hero.size &&
singleMonster.x <= hero.x + hero.size &&
hero.y <= singleMonster.y + hero.size &&
singleMonster.y <= hero.y + hero.size
) {
// Pick a new location for the singleMonster.
// Note: Change this to place the singleMonster at a new, random location.
score += 1;
singleMonster.x = Math.floor(Math.random() * canvas.width) - 40;
singleMonster.y = Math.floor(Math.random() * canvas.height) - 40;
}
}
//top and bottom collision detection //left and right collision detection
if (hero.y >= canvas.height - hero.size) {
hero.y = 0;
} else if (hero.y <= 0) {
hero.y = canvas.height - hero.size;
}
if (hero.x >= canvas.width - hero.size) {
hero.x = 0;
} else if (hero.x <= 0) {
hero.x = canvas.width - hero.size;
}
//monster collision detection
// let monsters = [
// { x: 100, y: 100 }, //monster 1 position x,y
// { x: 200, y: 200 }, //monster 2 position x,y
// { x: 300, y: 300 }, //monster 3 position x,y
// ];
};
/**
* This function, render, runs as often as possible.
*/
function render() {
document.getElementById("score").innerHTML = `this is my score: ${score}`;
if (background.ready) {
ctx.drawImage(background.image, 0, 0);
}
if (hero.ready) {
ctx.drawImage(hero.image, hero.x, hero.y);
}
monsters.forEach((monster) => {
if (monster.ready) {
ctx.drawImage(monster.image, monster.x, monster.y);
}
});
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); // keep the browser update IF THERE IS ANIMATION
}
// Cross-browser support for requestAnimationFrame.
// Safely ignore this line. It's mostly here for people with old web browsers.
// enable the function (to keep the browser update IF THERE IS ANIMATION)
var w = window;
requestAnimationFrame =
w.requestAnimationFrame ||
w.webkitRequestAnimationFrame ||
w.msRequestAnimationFrame ||
w.mozRequestAnimationFrame;
// Let's play this game!
loadImages();
setupKeyboardListeners();
main();