Edited 17/02/25 to include further explanations
In todays article, I have created a crash course to explain how to create a JavaScript game for persons who have zero coding experience. By the end of this article, I hope to explain the basic fundamentals in creating your own JavaScript Game with the aid of GenAI.
first we are going to be creating the following
To create a HTML or JavaScript file, simply create a new notepad document then rename the extension from .txt
to .html
for a HTML document or .js
for a JavaScript document . To access these files to input code, simply right click on the file and open it using notepad. Now, I recommend that you download the Notepad++ tool, as this contains line numbers that are very useful for coding
Now we have a basic directory setup we can now start building the game. GenAI can be used to create frameworks. However, in order to use GenAI to its full potential you are going to need to understand the basic structure of a HTML document
In Laymans HTML is the structure of your webpage, CSS is the style, and JavaScript enables user interaction for your website/game. A HTML document includes elements such as HTML, HEAD, STYLE & BODY. Below is a basic template of a HTML document
Now we need to understand some of the basic terminology used with Javascript When prompting GenAI, using these terms will save you a lot of time.
Now I will explain each of these terms in further detail while also providing code examples. At the end of this document, I will provide the code of the simple game in full, so relax you can just copy and paste it. I will also provide a link so you can demo the beta version of this game.
Javascript code includes special characters like {}
(curly brackets) ;
(semicolons) and ()
(curvy brackets). These special characters are important as they organise the structure of your code. Curly brackets are used to group blocks of code together, the semicolon is used to inform that a line of code is finished and curvy brackets can contain parameters.
To include comments in your JavaScript code, include two forward slash characters before the comment as shown below
Note: provided code snipets are excamples, it will be easier for you to understand these concepts first before prompting GenAI.
A Canvus is a HTML element used to provide space for drawing objects. A canvas can be the player screen where the player interacts, to create a canvas, we define the style of the canvas within the STYLE element of the HTML document, such as the colour of the canvas as shown in the following example.
Then we define the canvas width and height within the BODY element of the HTML document, as shown in the example below that sets the height and width of the canvas.
Lastly we define the canvas with the SCRIPT element of the HTML document (or JavaScript document, if you separate your HTML and JavaScript Documents), in this example we are making a 2D game, variables are used here, I will explain what variables are in the next section