Using canvas to implement a maze game
This article mainly introduces the use of canvas to implement maze games. It has certain reference value. Now I share it with you. Friends in need can refer to it.
Preface
(recent design The pattern is a bit overwhelming, and it is a bit boring to face pure js all the time -_-. So write something interesting to spice it up)
Nowcanvas
is not new anymore, but due to daily business It is not commonly used in , so there is not much practice. Today I will share how to implement a simple canvas
maze. This example comes from the second edition of "html5 cheats", and the code has been slightly adjusted.
Because there is a step in the middle when using canvas to obtain image information, must use the server environment. So I first wrote a sample and threw it on the server. You can experience the effect first (use the sense of achievement as the driving force hahaha)
Text
It is not difficult to implement this small game. Let's think about the basic elements of a maze game.
Of course there must be a map first, and then there must be a moving villain. We use cavans to draw these two;
Continue The following is the object movement program. This program mainly includes two aspects:
1. Let the object move according to the instructions we specify;
2. Detect whether the object touches the wall body or export.
Drawing the map of the maze and the moving villain
The main steps to draw the map are:
Get a picture of the map
Use cavans to draw images.
The generation of the maze map can be obtained with the help of an online maze generator from Google.
The same is true for drawing a villain. Just look for a picture of a villain. However, what you need to pay attention to here is that you need to find a square picture, because we will need to do mobile collision detection later. , square shape is easier to judge.
The next step is to write the main function for drawing the maze and the villain
function drawMaze(mazeFile, startingX, startingY) { var imgMaze = new Image() imgMaze.onload = function () { // 画布大小调整 canvas.width = imgMaze.width canvas.height = imgMaze.height // 绘制笑脸 var imgFace = document.getElementById("face") context.drawImage(imgMaze, 0, 0) x = startingX y = startingY context.drawImage(imgFace, x, y) context.stroke() } imgMaze.src = mazeFile }
mazeFile
is the image address of the maze, startingX
and startingY
is the coordinate of the starting point. There are two ways to introduce pictures here. The reason is that I don’t change the pictures of the villain often, so I write them directly on the page. The map of the maze is intended to be variable, so I introduce it in js. If you want to There is no problem if you import it directly using js. The other parts are relatively simple and will not be described again.
Move function
The main principle of movement is:
Accept specified user input (here, the response direction key) and convert it into the corresponding movement instruction. Then periodically check the movement instructions and draw the corresponding target position. To give a simple example:
For example, every time the up direction key is pressed, it is recorded that it should move upward, and then the current movement command is checked every 100 milliseconds, the target location where it should be moved is drawn, and the process is repeated. . The code is also relatively simple:
// 移动函数 function processKey(e) { dx = 0 dy = 0 // 上下左右方向键检测 if (e.keyCode === 38) { dy = -1 } if (e.keyCode === 40) { dy = 1 } if (e.keyCode === 37) { dx = -1 } if (e.keyCode === 39) { dx = 1 } } // 绘制帧 function drawFrame() { if (dx != 0 || dy != 0) { // context.clearRect(x,y,canvas.width,canvas.height) // 绘制移动轨迹 context.beginPath(); context.fillStyle = "rgb(254,244,207)" context.rect(x, y, 15, 15) context.fill() x += dx y += dy // 碰撞检测 if (checkForCollision()) { x -= dx y -= dy dx = 0 dy = 0 } //绘制小人应该移动的地点 var imgFace = document.getElementById('face') context.drawImage(imgFace, x, y) if (canvas.height - y < 17) { // isFirst = false alert('恭喜你通关 游戏结束') return false } // 这里如果重置的话变成非自动移动,也就是每按下一次方向键只前进一步,由于目前体验不好所以先不做重置 // dx = 0 // dy = 0 } setTimeout(drawFrame, 20) }
In the above code, the movement function is relatively simple. The more important function of drawing the frame is the collision detection function, which is explained in detail below.
Collision detection
To detect whether an object collides with a wall, Usually is to save the map information into the memory first, and then detect whether it collides with the wall when moving the object. The current wall collides, but since our map background is a black and white maze, we can use color to detect collision. The specific method is:
Get the coordinate position of the current object, and use canvas
to detect whether the color of this position on the current map is black. If so, it is said to be a wall. The movement should not be performed, the following is the code:
function checkForCollision() { var imageData = context.getImageData(x - 1, y - 1, 15 + 2, 15 + 2) var pixels = imageData.data for (var i = 0, len = pixels.length; i < len; i++) { var red = pixels[i], green = pixels[i + 1] blue = pixels[i + 2] alpha = pixels[i + 3] // 检测是否碰到黑色的墙 if (red === 0 && green === 0 && blue === 0) { return true } } return false }
Here, 15
is the width of the villain, we detect the 1px range on both sides of the villain (corresponding to the # in the code ##getImageData(x - 1, y - 1, 15 2, 15 2)You can think about why it is 2). If it is black, it means that a collision has been detected.
- The collision detection experience is not good at corners;
- There is a trajectory when running in the current situation , how to remove tracks in answer mode?
HTML5 Canvas API to create a simple guessing game
HTML5 Canvas to achieve special effects of fireworks
The above is the detailed content of Using canvas to implement a maze game. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
