Javascript Canvas: Basic Game Logic and Movement

Introduction

If you are interested in writing a game in javascript using the new canvas tag than look no further. In this lesson I will be going over the game loop and what is required to get started with your own. If you have not yet gone through the introduction to the canvas you should click here. It only takes a couple minutes to go through and it will have the required code for this lesson.

The Basic Loop

In order to create a loop we are going to base it off of seconds using the set_interval() function. This function will allow us to call on a function x times per second. This is a good function to use when making a loop based game because it will wait for the function to finish before calling on it again even if the time is exceeded.

1
2
// call on a function every 100 milliseconds.
setInterval(“methodName(), 100);

Drawing to the canvas

For now we are going to simply draw to the canvas using a draw function. You can call on a class.method in the set interval function. In the draw function that we are creating, we are going to have to draw something to the canvas. In the following code we will draw a line from point a to point b.

1
2
3
4
5
6
7
function draw() {
ctx.strokeStyle = “#333333;
ctx.beginPath();
ctx.moveTo(50,50);
ctx.lineTo(60, 50);
ctx.stroke();
}

If the setInterval function has been properly setup, to take the draw function, than you should see a line appear on the canvas. Now lets get a little wild and crazy and give that line some movement.

Layers

The canvas works in layers. If you draw something to it, the item will stay until something has been drawn over it. Because of this the canvas needs to be reset and will need to be reset and have everything drawn back on to it in layers from the background to the foreground.

In a two dimensional game you are going to want to draw the items from the bottom up. The items should be drawn in the following order if you are looking at the ground from a birds eye view.

Draw the dirt
Draw the grass
Draw the trees
Draw the birds

Movement

Create public variables for x position, y position and counter.

1
2
3
var posX = 50;
var posY = 50;
var counter = 0;

Within the draw function we are going to add the following code which will increase the counter and set the position of the line. In the draw method we will need to redraw the background to clear the canvas. Add the following code to the function and place it above everything else within the code block.

1
2
ctx.fillStlye = “#ffffff”;
ctx.fillRect(0,0,800,400);

This will clear the previous items on every draw event. Now we can draw the items onto this layer by adding some logic to the draw function and changing the lineTo using the x and y positions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function draw() {
 
// redraw the background.
ctx.fillStyle = “#ffffff”;
ctx.fillRect(0,0,800,400);
 
// increase the counter
counter++;
 
// reset the counter so the line doesn’t get out of hand.
if(counter == 100) {
counter = 0;
}
 
// Stroke the line.
ctx.strokeStyle = “#333333;
ctx.beginPath();
ctx.moveTo(posX,posY);
ctx.lineTo(posX + counter, posY + counter);
ctx.stroke();
}

You will now have a moving line on the canvas that will reset every 100 updates.

Using Sessions To Handle Errors With PHP

Here is a 10 minute video tutorial that is all about handling errors with sessions. It will teach you how to use three simple functions to set and display error messages for your PHP website or application.

Using sessions to handle errors in php from Caleb Jonasson on Vimeo.

Sorry about the styling error, apparently I cannot type.

Demo

Download Source Files

Introduction to zombies, a java game

Introduction

Zombies is a text based rpg style game that involves a little more strategy since you are trying to rebuild mankind. The idea behind the game so far is to create something that users can play over and over again trying out different options to get a step up in the game.

There are multiple types of zombies,  16 to be exact(possibly more in the future), with varying attributes such as speed, strength and health. The chance of which you encounter these creatures is based on your level. You  can encounter these zombies on chance when collecting resources, finding survivors and hunting zombies. They will also appear when building outside of the confinements of your settlement walls or during the dark when lighting is poor.

As the game continues and your settlement grows bigger(if you even choose to have a settlement) the zombies will become more attracted to you as a food source as the population of survivors lessens. The whole game is based around the idea of building up something that was lost and the zombies are the one thing making that even more of a challenge.

Done

So far in the code the menu systems have been created, the zombies have a chance on entering battle, player stats and town stats are counted, new variables are brought into play, text commands are being inputted, battle systems are partially completed(pending on how much more is needed or will be added), day counters have been created.

On the list…

The game is well under way as far as being created goes, the only thing that is now required is the finishing touches that consist of; research, technology trees, weather, settlement chores and the ability to have survivors do those tasks for you, finishing up the battle system and adding a few fun Easter eggs.

Tags: , ,