HTML5 keyboard listening principle: capture monsters Game + code
HTML5 games are compiled by capturing monsters and using Canvas and other HTML5 skills. The following describes the implementation steps:
1. Create a game canvas:
. Code
- Var canvas = document. createElement ("canvas ");
- Var ctx = canvas. getContext ("2d ");
- Canvas. width = 512;
- Canvas. height = 480;
- Document. body. appendChild (canvas );
The first thing we need to do is to create a canvas element. I am doing this in JavaScript, instead of using HTML to demonstrate how to do it easily. Once we have the element we get a reference, set its size and it is attached to the body of the document. Front-end framework sharing
2. Images included:
. Code
- Var bgReady = false;
- Var bgImage = new Image ();
- BgImage. onload = function (){
- BgReady = true;
- };
- BgImage. src = "images/background.png ";
We load some images. I think it is as simple as possible to do this, so it is just an image, not a good class or thing wrapped up. Use bgReady to let the canvas know when it is safe to draw it.
3. Game objects:
. Code
- Var hero = {// hero
- Speed: 256, // 256 pixels per second
- X: 0,
- Y: 0
- };
- Var monster = {// monster
- X: 0,
- Y: 0
- };
- Var monstersCaught = 0; // catch several monsters
4. Player input: front-end framework sharing
. Code
- // Process keyboard input
- Var keysDown = {};
- AddEventListener ("keydown", function (e ){
- KeysDown [e. keyCode] = true;
- }, False );
-
- AddEventListener ("keyup", function (e ){
- Delete keysDown [e. keyCode];
- }, False );
5. New games
. Code
- // When a monster is captured, it must be executed
- Var reset = function (){
- Hero. x = canvas. width/2;
- Hero. y = canvas. height/2;
- // Make the monster appear somewhere in the scene (randomization)
- Monster. x = 32 + (Math. random () * (canvas. width-64 ));
- Monster. y = 32 + (Math. random () * (canvas. height-64 ));
- };
6. Update objects:
. Code
- Var update = function (modifier)
- {
- If (38 in keysDown) {// up
- Hero. y-= hero. speed * modifier;
- }
- If (40 in keysDown) {// down
- Hero. y + = hero. speed * modifier;
- }
- If (37 in keysDown) {// left
- Hero. x-= hero. speed * modifier;
- }
- If (39 in keysDown ){//
- Hero. x + = hero. speed * modifier;
- }
- // Are they touching?
- If (
- Hero. x <= (monster. x + 32)
- & Amp; monster. x <= (hero. x + 32)
- & Hero. y <= (monster. y + 32)
- & Amp; monster. y <= (hero. y + 32)
- ){
- ++ MonstersCaught;
- Reset ();
- }
- };
7. Rendering object: frontend Resource Sharing
. Code
- Var render = function (){
- If (bgReady ){
- Ctx. drawImage (bgImage, 0, 0 );
- }
-
- If (heroReady ){
- Ctx. drawImage (heroImage, hero. x, hero. y );
- }
-
- If (monsterReady ){
- Ctx. drawImage (monsterImage, monster. x, monster. y );
- }
-
- // Score
- Ctx. fillStyle = "rgb (250,250,250 )";
- Ctx. font = "24px Helvetica ";
- Ctx. textAlign = "left ";
- Ctx. textBaseline = "top ";
- Ctx. fillText ("gob1_caught:" + monstersCaught, 32, 32 );
- };
8. Main game cycle:
. Code
- Var main = function (){
- Var now = Date. now ();
- Var delta = now-then;
- Update (delta/1000 );
- Render ();
- Then = now;
- };
9. Start the game:
. Code
- Reset ();
- Var then = Date. now ();
- SetInterval (main, 1 );