Shanes Curries Blog

Return to Blog directory



How to create a JavaScript game using GenAI for Absolute Beginners

15/02/25 by Mr Shane Currie | Charles Sturt Student

Edited 17/02/25 to include further explanations

This is a technical document requiring technical expertise to understand.

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.

What you need to create a JavaScript game

What you will need to host a JavaScript game online

Building a very simple 2D game

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

Basic structure of an HTML, CSS, and JavaScript document

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.

Common terms

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.

Sytnax

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.

Comments

To include comments in your JavaScript code, include two forward slash characters before the comment as shown below

Canvas

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

Variables

Now lets explain what are variables, a variable is typically a letter or a word that refers to a number or text value. As an example, the word playerHealth is a variable and the number 100 is the value of the players health.

In JavaScript, a variable can start with let,const or var. In this game we are using the LET and CONST variable types. A LET variable is used when values change in the game, such as the payers health. The CONST variable type is used when the value of the variable does not change, such as the file path of the image for the carrot or the apple. Below is an example of a LET variable type.

Local and Global Variables

The difference between a global variable and a local variable is that global variables are declared outside of functions (relax, I will explain functions later) and can be used or modified by any function. Local variables exist only inside a function and do not affect anything outside the function unless they are assigned to a global variable.

It is good practice to pass local and global variables into functions as parameters (again, relax, I will explain parameters later).

In simple terms: If your game includes a kangaroo, a global variable is used to set its starting position on the map. When the kangaroo moves, a function runs using a local variable to determine the new position. If the kangaroo moves to a new area, the global variable must be updated so the game remembers the new position.

Global Conditions/Scope

With Javascript, a variable that is called outside of a function is considered a global condition. The following code includes a variable that contains objects, these objects are the number of apples and carrots the player starts with in the game.

Arrays

An Array can contain multiple values for a single object, such as the location of an apple object within the game, as well as the image used and the size of the image.

Functions

A function is the logic the game uses when an variable is called or an event is triggered. Functions sit inside blocks of code nested within in curly brackets, here is a function that is used to draw the player health and energy stats on canvas, also ground and the inventory icon. This function also includes a conditional statement, I will explain conditional statements further in the next section. A game will typically include multiple functions.

Conditional Statements

Conditional Statements include the logic of a function, conditional statements include the statements IF, ELSE IF & ELSE. In laymans terms lets think of a scenario where you plan to play a game of soccer. IF you kick the ball in the goal, you score a point. ELSE IF you kick the ball out of bounds, ELSE IF you kick the ball to a team member, your team member gains the ball, ELSE IF you kick the ball to the opposition the opposition gains the ball. For anything ELSE, the ball is still in play. Below is an example of a conditional statement included in a function for a jumping kangaroo. This function includes parameters, I will explain parameters in the next section.

Parameters

Parameters can be used to provide pieces of information to a function. To use an example, lets say you are ordering a pizza. Making the pizza is a function but what ingredients will this pizza contain? will it contain salami, beef, olives etc? these ingredients are your parameters for the pizza. (typing this makes me want pizza). To provide an example of parameters used in JavaScript, the following code includes parameters within the curly brackets to draw the kangaroo image at X and Y coordinates. Parameters act as placeholders for values received by a function. Parameters can include global variables, that were first defined outside the function. Global function variables passed into a function as a parameter act as a local variables

Event listeners

Event listeners are used to listen for events, such as the player clicking on the mouse. In the following example if the player clicks on a location within the canvas window the jumpKangaroo function will be triggered

Load Order

The load order is the order that your code is loaded in, computers read code from top to bottom of the document. Variables must be first defined before being used in a function. In the below example, I make sure that the game loads the maingamev1.1.js script first before it loads the inventoryV1.1.js script. The reason I do this, is because the maingamev1.1.js script defines the global variable playerHealth. This variable is called within a function in the inventoryV1.1.js script, so I want the load order define the variable first before using the variable in the function.

Debugging

Debugging will be required when creating javaScript games. Debugging can be accomplished by acquiring error codes from your browser. To acquire these error codes first right click on your web browser to open the developer tab options (in edge they call it the inspect option) within this tab you can find the console output that includes error codes of your game, provide these error codes to GenAI for debugging assistince.

Version control

Version control is VERY important, ALWAYS BACK UP WORKING VERSIONS BEFORE MODIFYING THEM. For example, if you get a concept working in version 1.1 of the game SAVE that version as game-v1.1.js then create a copy of that file and name the copy game-v1.2.js. This way, if yourself or GenAI breaks your game, you can always roll back to the previous working version, TRUST ME, GenAI will break your game many times, and it will frustrate you.

Tip for working with GenAI, section your code!

A tip for working with GenAI, itemise sections of your code with comments. This will make it easier for updating your code when GenAI provides you frameworks, for example.

By sectioning your code using comments, GenAI (or other people assisting you with your code) can simply just prompt you on what section/s to update.

There is much more to learn about JavaScript, but now you know the basics you should be able to formulate prompts for GenAI to build the basic framework for your game. Now this is only the tip of the iceberg, other factors can include introducing object collision, multiple frames for your images (to simulate character movement) Complex mathematical algorithms to simulate gravity, mass, weight etc (dont worry, that what GenAI is for).

Anyway, I want to keep this article short and simple to explain the very basic concepts of creating a JavaScript game. If you have any questions, ask GenAI or myself. and as promised, here is the full code for the basic JavaScript game

If you get a white screen when running the code, its because you have not loaded your images in the assets/images directory.

Click here to demo the JavaScript Game

launchgameV1.4.html file

maingamev1.1js file

inventory.v1.1.js file