I'm creating a game engine!
Introduction to this great adventure
For a few weeks now I have been working regularly on a project that I think might be interesting to talk about, the creation of my video game engine in JavaScript and HTML5 based on canvas.
You are probably wondering why you chose HTML5 and JavaScript to create video games? The answer is less cool than the question, it is the competition of projects necessary for my school (Zone01 Normandie) and the fact that the languages have everything necessary to carry out this project that led me to choose these technologies.
But actually these are not the languages that I would have chosen as a base and I will surely embark on other adventures of this type with different languages after the finalization of this one.
Architecture
So I got to work designing my video game engine, it will be made up of several classes including at least two main ones: The Game class which will manage the entire game area and the GameObject class allows you to generate the objects in our games and make them interact with each other.
To these classes I will add the CollideBox class which will allow me to manage the collision boxes of all objects.
The Game class has a GameLoop method which will be executed at each frame(image) of the game, a Draw method which will be called during each game loop.
As for the GameObject class, it has a Step method, and a Draw method.
The first executes each round of the game loop and the second each time the Draw method of the GameLoop class is called.
This allows you to theoretically create games by importing the Engine module into a project.
For displaying the sprites I chose to use the canva API which is built-in to HTML5 (built-in means it comes with it by default)
It will allow me to display all the sprites and recut the images in order to create animations which will be extremely useful to me!
After several days I am able to display animations, at a given speed, and detect collisions via my CollideBoxes.
And lots of other nice things that I'll let you see below:
The GameObject class
class GameObject{ constructor(game) { // Initialize the GameObject this.x = 0 this.y = 0 this.sprite_img = {file: undefined, col: 1, row: 1, fw: 1, fh: 1, step: 0, anim_speed: 0, scale: 1} this.loaded = false this.game = game this.kill = false this.collision = new CollideBox() game.gObjects.push(this) }; setSprite(img_path, row=1, col=1, speed=12, scale=1) { var img = new Image(); img.onload = () => { console.log("image loaded") this.sprite_img = {file: img, col: col, row: row, fw: img.width / col, fh: img.height / row, step: 0, anim_speed: speed, scale: scale} this.onSpriteLoaded() }; img.src = img_path } onSpriteLoaded() {} draw(context, frame) { // Draw function of game object if (this.sprite_img.file != undefined) { let column = this.sprite_img.step % this.sprite_img.col; let row = Math.floor(this.sprite_img.step / this.sprite_img.col); // context.clearRect(this.x, this.y, this.sprite_img.fw, this.sprite_img.fh); context.drawImage( this.sprite_img.file, this.sprite_img.fw * column, this.sprite_img.fh * row, this.sprite_img.fw, this.sprite_img.fh, this.x, this.y, this.sprite_img.fw * this.sprite_img.scale, this.sprite_img.fh * this.sprite_img.scale ); if (frame % Math.floor(60 / this.sprite_img.anim_speed) === 0) { // Mise à jour de step seulement à 12 fps if (this.sprite_img.step box.x && this.collision.y box.y ) } onStep() {}; }
The Game class
class Game { constructor(width = 1400, height = 700) { this.gObjects = []; this.toLoad = []; this.timers = []; this.layers = []; this.canvas = document.getElementsByTagName("canvas")[0] this.canvas.width = width this.canvas.height = height this.context = this.canvas.getContext("2d") this.context.globalCompositeOperation = 'source-over'; this.inputs = {}; this.mouse = {x: 0, y: 0} document.addEventListener('keydown', (e) => { this.inputs[e.key] = true; }, false); document.addEventListener('keyup', (e) => { this.inputs[e.key] = false; }, false); document.addEventListener('mousemove', (e) => { this.mouse.x = e.x; this.mouse.y = e.y; }) document.addEventListener('mouseevent', (e) => { switch (e.button) { } }) } draw(frame) { this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); console.log( this.canvas.width, this.canvas.heigh) for(let i = 0; i { clock += 1 for(let i = 0; i <p>There are certainly a lot of optimization or other errors but everything is functional, <br> "Perfect!" will you tell me?<br> That would be way too simple.</p> <h3> The worries </h3> <p>After finishing this and starting to test the waters for creating a game with this engine, I learned some terrible news during a conversation with a colleague.</p> <p>I imagine that you remember that the technology choices made were made to correspond to the requirements of my Zone01 school…<br> Well indeed the languages chosen were good but I was not aware of an instruction which will very seriously handicap the project…<br> We were prohibited from using the Canva library!</p> <p>As a reminder, this is the library that we use to display our images.</p> <h3> What’s next? </h3> <p>As I write this text I am also starting to completely redesign this game engine, without the use of canva.</p> <p>This devlog is finished and you will have the rest of this story soon, don't worry.<br> For the next devlog I will definitely try a new format.</p> <p> Hoping that this content has helped you, entertained you or at least educated you on a few subjects. I wish you a good end of the day and good coding.</p> <h2> DevLogs 1.1: The engine is finished, how does it work? </h2> <h3> Previously </h3> <p>A few months ago I started creating my video game engine, I finished it... Quite a while ago, and with the help of several colleagues from Zone01 we even succeeded to create a game inspired by Super Mario Bros available on my Itch.io page.</p> <p>Deciding the format to apply for this devlog took a lot of time, and I admit I delayed slightly or even completely pushed back the deadline for writing this one.<br> By patiently taking the excuse of my indecision for not working on this subject, I now find myself two months after the planned release date writing in the rest area of the Rouen bus station while my canceled train forces me to wait an extra hour.</p><p> So let's ignore all the details of the architecture, this one having changed very little (apart from the adaptation by avoiding the use of canvases) since the first part of my devlog.<br> We will therefore talk about the project carried out, the way we worked as a team and the problems we encountered.<br> See this as feedback on this project, and I hope that you will be able to draw some lessons from this writing that will help you on one of your projects.</p> <h3> The project </h3> <p>The project was to recreate a Super Mario Bros in JavaScript and starting from scratch, at least in terms of the code.</p> <p>The specifications were simple, we had to have a Mario game with several levels, a way to simply create new ones.<br> Also we had to create a scoreboard and a menu to adjust the options.</p> <p>The difficulties of this project were: </p>
- Horizontal scrolling of elements on the screen
- Optimization of elements not present on the screen
Scrolling because it requires all elements to scroll in the background relative to the player's position.
And optimizing elements that are not displayed on the screen reduces the resources needed to run the game without loss of performance.
After resolving these difficulties we published this game on my itch.io page where you can even go and test it.
This is how this devlog ends, now finished I will be able to write about other projects and/or other subjects.
If you're even a little interested in what I'm telling you, you can go see my different projects (including those in this devlog) on github.
Have a nice rest of the day!
The above is the detailed content of Devlog - I'm creating a game engine!. For more information, please follow other related articles on the PHP Chinese website!

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.

JavaScript does not require installation because it is already built into modern browsers. You just need a text editor and a browser to get started. 1) In the browser environment, run it by embedding the HTML file through tags. 2) In the Node.js environment, after downloading and installing Node.js, run the JavaScript file through the command line.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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

Dreamweaver Mac version
Visual web development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version