Skip to content

Commit 5e3673e

Browse files
committed
Initial commit, with README.md and boilerplate code setup.
0 parents  commit 5e3673e

File tree

6 files changed

+228
-0
lines changed

6 files changed

+228
-0
lines changed

README.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# CoderSchool FTW - * Nam, the Monster Hunter *
2+
3+
Created with love by: Charles Lee
4+
5+
This is a simple Canvas-based game that requires a solid understanding of the principles of JavaScript programming to customize.
6+
7+
## Video Walkthrough
8+
9+
Here's a walkthrough of implemented user stories.
10+
11+
```
12+
<img src='http://http://g.recordit.co/cURdPMSHT2.gif' title='Video Walkthrough' width='' alt='Video Walkthrough' />
13+
```
14+
15+
16+
## Required User Stories
17+
18+
- [x] The user can move their character with the arrow keys.
19+
- [x] The user can see a monster.
20+
- [ ] The monster is placed at a random location within the game boundaries.
21+
- [x] The user can catch a monster by moving their character to the same location as a monster.
22+
- [ ] When the user catches a monster, a new monster is placed randomly upon the screen.
23+
- [ ] The user can see the numbers of monsters she has caught. Every time she catches a monster, the number should increment by one.
24+
- [ ] The game ends when the user has caught 20 monsters. The total time elapsed is displayed upon game completion, in seconds.
25+
- [ ] The user can access this game from the internet (e.g. using Netlify).
26+
- [ ] The code has been reviewed by at least one other person, using Pull Requests on GitHub.
27+
28+
## Optional User Stories
29+
30+
- [ ] Custom styling: the developer has replaced the images for the main character, monster, and background with something of her choice.
31+
- [ ] The user cannot move her character off the screen.
32+
- [ ] The user sees a few different types of monster each time a new monster is spawned.
33+
- [ ] The monster moves around on its own each frame, and the user must chase it.
34+
- [ ] The map has "obstacles", such as trees, which block the user from moving through the space occupied by the obstacle.
35+
- [ ] The user can hear sound effects upon movement and upon catching a monster.
36+
- [ ] The user hears background music during the game.
37+
- [ ] The user can see their high score.
38+
39+
40+
The following **additional** features are implemented:
41+
42+
* [x] List anything else that you can get done to improve the page!
43+
44+
## Time Spent and Lessons Learned
45+
46+
Time spent: **X** hours spent in total.
47+
48+
Describe any challenges encountered while building the app.
49+
50+
## License
51+
52+
Copyright [yyyy] [name of copyright owner]
53+
54+
Licensed under the Apache License, Version 2.0 (the "License");
55+
you may not use this file except in compliance with the License.
56+
You may obtain a copy of the License at
57+
58+
http://www.apache.org/licenses/LICENSE-2.0
59+
60+
Unless required by applicable law or agreed to in writing, software
61+
distributed under the License is distributed on an "AS IS" BASIS,
62+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
63+
See the License for the specific language governing permissions and
64+
limitations under the License.

game.js

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
Code modified from:
3+
http://www.lostdecadegames.com/how-to-make-a-simple-html5-canvas-game/
4+
using graphics purchased from vectorstock.com
5+
*/
6+
7+
/* Initialization.
8+
Here, we create and add our "canvas" to the page.
9+
We also load all of our images.
10+
*/
11+
12+
13+
let canvas;
14+
let ctx;
15+
16+
canvas = document.createElement("canvas");
17+
ctx = canvas.getContext("2d");
18+
canvas.width = 512;
19+
canvas.height = 480;
20+
document.body.appendChild(canvas);
21+
22+
let bgReady, heroReady, monsterReady;
23+
let bgImage, heroImage, monsterImage;
24+
25+
function loadImages() {
26+
bgImage = new Image();
27+
bgImage.onload = function () {
28+
// show the background image
29+
bgReady = true;
30+
};
31+
bgImage.src = "images/background.png";
32+
heroImage = new Image();
33+
heroImage.onload = function () {
34+
// show the hero image
35+
heroReady = true;
36+
};
37+
heroImage.src = "images/hero.png";
38+
39+
monsterImage = new Image();
40+
monsterImage.onload = function () {
41+
// show the monster image
42+
monsterReady = true;
43+
};
44+
monsterImage.src = "images/monster.png";
45+
}
46+
47+
/**
48+
* Setting up our characters.
49+
*
50+
* Note that heroX represents the X position of our hero.
51+
* heroY represents the Y position.
52+
* We'll need these values to know where to "draw" the hero.
53+
*
54+
* The same applies to the monster.
55+
*/
56+
57+
let heroX = canvas.width / 2;
58+
let heroY = canvas.height / 2;
59+
60+
let monsterX = 100;
61+
let monsterY = 100;
62+
63+
/**
64+
* Keyboard Listeners
65+
* You can safely ignore this part, for now.
66+
*
67+
* This is just to let JavaScript know when the user has pressed a key.
68+
*/
69+
let keysDown = {};
70+
function setupKeyboardListeners() {
71+
// Check for keys pressed where key represents the keycode captured
72+
// For now, do not worry too much about what's happening here.
73+
addEventListener("keydown", function (key) {
74+
keysDown[key.keyCode] = true;
75+
}, false);
76+
77+
addEventListener("keyup", function (key) {
78+
delete keysDown[key.keyCode];
79+
}, false);
80+
}
81+
82+
83+
/**
84+
* Update game objects - change player position based on key pressed
85+
* and check to see if the monster has been caught!
86+
*
87+
* If you change the value of 5, the player will move at a different rate.
88+
*/
89+
let update = function () {
90+
if (38 in keysDown) { // Player is holding up key
91+
heroY -= 5;
92+
}
93+
if (40 in keysDown) { // Player is holding down key
94+
heroY += 5;
95+
}
96+
if (37 in keysDown) { // Player is holding left key
97+
heroX -= 5;
98+
}
99+
if (39 in keysDown) { // Player is holding right key
100+
heroX += 5;
101+
}
102+
103+
// Check if player and monster collided. Our images
104+
// are about 32 pixels big.
105+
if (
106+
heroX <= (monsterX + 32)
107+
&& monsterX <= (heroX + 32)
108+
&& heroY <= (monsterY + 32)
109+
&& monsterY <= (heroY + 32)
110+
) {
111+
// Pick a new location for the monster.
112+
// Note: Change this to place the monster at a new, random location.
113+
monsterX = monsterX + 50;
114+
monsterY = monsterY + 70;
115+
}
116+
};
117+
118+
/**
119+
* This function, render, runs as often as possible.
120+
*/
121+
var render = function () {
122+
if (bgReady) {
123+
ctx.drawImage(bgImage, 0, 0);
124+
}
125+
if (heroReady) {
126+
ctx.drawImage(heroImage, heroX, heroY);
127+
}
128+
if (monsterReady) {
129+
ctx.drawImage(monsterImage, monsterX, monsterY);
130+
}
131+
};
132+
133+
/**
134+
* The main game loop. Most every game will have two distinct parts:
135+
* update (updates the state of the game, in this case our hero and monster)
136+
* render (based on the state of our game, draw the right things)
137+
*/
138+
var main = function () {
139+
update();
140+
render();
141+
// Request to do this again ASAP. This is a special method
142+
// for web browsers.
143+
requestAnimationFrame(main);
144+
};
145+
146+
// Cross-browser support for requestAnimationFrame.
147+
// Safely ignore this line. It's mostly here for people with old web browsers.
148+
var w = window;
149+
requestAnimationFrame = w.requestAnimationFrame || w.webkitRequestAnimationFrame || w.msRequestAnimationFrame || w.mozRequestAnimationFrame;
150+
151+
// Let's play this game!
152+
loadImages();
153+
setupKeyboardListeners();
154+
main();

images/background.png

7.98 KB
Loading

images/hero.png

1.07 KB
Loading

images/monster.png

1.08 KB
Loading

index.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Best Game Ever!</title>
6+
</head>
7+
<body>
8+
<script src="game.js"></script>
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)