June 29, 2015 3:41 pm

Creating a game with JavaScript and Processing.js part 1

The game that we are going to make is a simple 2D fighting game. You have a certain number of lives and the enemy has a certain number of lives and your goal is to avoid being hit by him or his stars while shooting him with your own stars until his lives deplete. Demo and download will be included with last part.

Creating a game with JavaScript and Processing.js part 1

We are going to build upon the article that I published here in PHPGang some time ago which was about creating a simple animation with Processing.js.

Creating a game with JavaScript and Processing.js part 2

Creating a game with JavaScript and Processing.js part 3

We only create the basics but you can edit it in any way you want – by adding levels, weapons placed on a random place on a random time that give the player different bonuses – like shooting many stars in different directions, setting a time limit for round completion and so on.

Processing.js concepts used:

Rect(xPos,yPos,width,height) –> creates a rectangle starting at x,y coordinates with a certain width and height

Text(“text”,xPos,yPos) –> draws the text in the first argument starting at certain x,y coordinates

mouseX and mouseY –> global variables that contain the current x/y coordinates of the mouse

fill(red,green,blue) -> sets the fill color for future shapes and texts

textSize(font-size) -> sets the font-size for future text

stroke(red,green,blue) -> sets the rgb color values of the strokes in shapes and text

draw() function -> similar to setInterval() but with already predefined interval. Code inside will be executed around 30 times per second.

keyPressed -> global variable. True if a key has been pressed by the user

keyCode -> global variable. Contains the special key that was pressed. For example – LEFT, RIGHT, DOWN, UP

loadImage(“IMG_PATH”) –> loads an image for future use

image(imageResource,xPos,yPos, width, height) -> displays an image starting at the specified x,y coordinates with a particular width and height.

You can use loadImage(“IMG_PATH”) as imageResource

mousePressed() -> executes when the mouse has been pressed

Processing.js uses canvas so we define a script tag with type attribute set to “application/processing” and define the target (the name of the canvas where the game will run)

Of course, we also have to create a canvas element with the id that we set in the target:

We define the object that will initialize and keep the game running

We define the variables that our game will be using within our main game object:

Afterwards, we will be adding properties and methods to it that dictate the game’s logic.

Author Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter

Tutorial Categories: