


Detailed introduction to the sample code of the Super Mario game demo based on HTML5
Function description:
A demo of the Super Mario game based on HTML5. The left and right arrow keys control movement, and the arrow keys control jumping. This game is based on the HTML5 game framework cnGameJS developed by me.
Please use the latest version of the browser to view.
Effect display:
##Code implementation:
The main elements of this game demo are: resource loading, external input, collision detection, animation, game loop and scene. I will analyze and explain step by step.1. Resource loading:
We must first have a game object, representing a level. This object has the following three methods: initialize, update and draw. Their respective functions are initialization, updating all game elements and drawing all game elements. Before the game starts, all image resources must be loaded first, and then the game loop is started and the initialization parameters of the game object are called:##
srcObj={ startSrc:"images/gamestart.png", backgroundSrc:"images/background.png", enemySrc:"images/enemy.png", playerSrc:"images/player.png", stoneSrc:"images/stone.png", stoneSrc2:"images/stone2.png", pillarSrc:"images/pillar.png", bulletSrc:"images/bullet.png" } cnGame.init('gameCanvas',{width:500,height:400}); maryGame={ initialize:(){ }, update:(){ }, draw:(){ } } cnGame.loader.start([srcObj.backgroundSrc,srcObj.playerSrc,srcObj.enemySrc,srcObj.stoneSrc,srcObj.stoneSrc2,srcObj.bulletSrc,srcObj.pillarSrc],maryGame);
2. External input:
Since the keyboard direction keys are required to cause Mary’s movement, we need to detect whether the keyboard direction keys are pressed. We can use cnGameJS’s isPressed (keyName) to detect keyboard input:
(cnGame.input.isPressed("up")){ .jump(); } (cnGame.input.isPressed("right")){ .moveRight(); } (cnGame.input.isPressed("left")){ .moveLeft(); } { .stopMove(); }
In this way, Mary can perform different behaviors through different keyboard inputs. 3. Collision detection:
cnGameJS encapsulates the collision detection of rectangles and rectangles, so we can use it as collision detection between game objects and objects, here The complexity of collision detection is to detect the collision between Mary and enemies or stones, and follow the situation to change Mary's speed and acceleration.
1: Mary collides with the enemy, and determines whether the enemy dies or Mary dies based on the speed of Mary in the Y direction.
2: Mary collides with the stone, causing Mary's Y-direction velocity to be 0 and Y-direction acceleration to 0.
3: Mary leaves the stone: the gravitational acceleration is restored.
4. Animation:The animation here mainly refers to the animation of Mary’s feet when she moves. We can use pictures when Mary stops and animation when she moves. . The so-called animation is actually a frame animation within a large picture. For example, we can first prepare a picture like this:
Then we only need to make it display Mary in a different position each time , to complete the animation: Effect preview
The game loop is a loop started after the resource is loaded. It receives the fps passed in by the user, thus Calculate the loop interval, update frames and draw frames each time, and generate game animation:
(cg.loop&&!cg.loop.stop){ cg.loop.end(); } cg.loop= cg.GameLoop(self.gameObj); cg.loop.start();
6. Scene:
The so-called scene is an object that when Mary moves, Mary remains in the center of the canvas and the background moves in the opposite direction. The scene object not only moves the background, but also moves non-player game objects in the opposite direction according to the player's speed: Effect preview
##.background= cnGame.View({src:srcObj.backgroundSrc,player:.player,imgWidth:2301}); .background.centerPlayer(); .background.insideView(.player,"x");
The above is the detailed content of Detailed introduction to the sample code of the Super Mario game demo based on HTML5. For more information, please follow other related articles on the PHP Chinese website!

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

The core features of HTML5 include semantic tags, multimedia support, offline storage and local storage, and form enhancement. 1. Semantic tags such as, etc. to improve code readability and SEO effect. 2. Simplify multimedia embedding with labels. 3. Offline storage and local storage such as ApplicationCache and LocalStorage support network-free operation and data storage. 4. Form enhancement introduces new input types and verification properties to simplify processing and verification.

H5 provides a variety of new features and functions, greatly enhancing the capabilities of front-end development. 1. Multimedia support: embed media through and elements, no plug-ins are required. 2. Canvas: Use elements to dynamically render 2D graphics and animations. 3. Local storage: implement persistent data storage through localStorage and sessionStorage to improve user experience.

H5 and HTML5 are different concepts: HTML5 is a version of HTML, containing new elements and APIs; H5 is a mobile application development framework based on HTML5. HTML5 parses and renders code through the browser, while H5 applications need to run containers and interact with native code through JavaScript.

Key elements of HTML5 include,,,,,, etc., which are used to build modern web pages. 1. Define the head content, 2. Used to navigate the link, 3. Represent the content of independent articles, 4. Organize the page content, 5. Display the sidebar content, 6. Define the footer, these elements enhance the structure and functionality of the web page.

There is no difference between HTML5 and H5, which is the abbreviation of HTML5. 1.HTML5 is the fifth version of HTML, which enhances the multimedia and interactive functions of web pages. 2.H5 is often used to refer to HTML5-based mobile web pages or applications, and is suitable for various mobile devices.

HTML5 is the latest version of the Hypertext Markup Language, standardized by W3C. HTML5 introduces new semantic tags, multimedia support and form enhancements, improving web structure, user experience and SEO effects. HTML5 introduces new semantic tags, such as, ,, etc., to make the web page structure clearer and the SEO effect better. HTML5 supports multimedia elements and no third-party plug-ins are required, improving user experience and loading speed. HTML5 enhances form functions and introduces new input types such as, etc., which improves user experience and form verification efficiency.

How to write clean and efficient HTML5 code? The answer is to avoid common mistakes by semanticizing tags, structured code, performance optimization and avoiding common mistakes. 1. Use semantic tags such as, etc. to improve code readability and SEO effect. 2. Keep the code structured and readable, using appropriate indentation and comments. 3. Optimize performance by reducing unnecessary tags, using CDN and compressing code. 4. Avoid common mistakes, such as the tag not closed, and ensure the validity of the code.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
