Object Oriented Games Programming in VBScript

How to Creates VBScript Classes for Use in Web Page Applications

© Mark Alexander Bain

Sep 26, 2009
Object Oriented Games Programming in VBScript, Mark Alexander Bain
VBScript is a powerful scripting language and supports object oriented languages. It is, therefore, an excellent learning tool for anyone new to OOP or games programming

There is just one drawback to using VBScript and that's that it can only be used in the Microsoft Internet Explorer web browser. This is, of course, a limiting factor for a programmer wanting to create web based applications such as games.

It means that they will not be able to create a game that runs in every other web browser (such as the increasingly popular Firefox). However, there is a major advantage to anyone learning object oriented programming. A class created with VBScript looks very much like a textbook defined class. It is very easy for the programmer (learner or otherwise) to design a class and implement it in VBScript.

Object Oriented Design

When a programmer comes to design a class they will do three things:

  • name the class
  • define the properties
  • define the methods

And this translates directly into VBScript

A VBScript Class

A programmer starts and ends with the VBScript class statement:

Class myClass
' Properties
' Methods
End Class

It's then a just a matter of creating the variables and subroutines that will make up the class.

Adding Properties to VBScript Class

VBScript class properties are defined within the class and can be one of two types:

  • Public – available to the user of the class
  • Private – only available to methods within the class

For example:

<body><img src="background.jpg"></body>
<script language="vbscript">
Class vbAlien
Public image

Here a background (as seen in figure 1 at the bottom of this article) and a single, public property have been defined

Adding Methods to a VBScript Class

VBScript class methods can also be public (available to the programmer) or private (not available to the programmer). However, there are other, built in methods that the programmer can use. For example, the class_initialize method runs automatically when the class is instantiated into an object:

Private Sub Class_Initialize
Set image = document.createElement("img")
image.src = "alien.png"
image.style.position = "absolute"
End Sub

The enables the programmer to set any parameters to their initial states and, as in this case, to load any images for a game. And then any custom methods can be considered. In this case they are the methods that the programmer will use to move their alien (shown in figure 2) around their games screen:

Public Sub show (left, top)
moveto left,top
document.body.appendChild(image)
End Sub
Public Sub moveto (left, top)
image.style.top = top
image.style.left = left
End Sub
Public Sub movedown
image.style.top = replace(image.style.top,"px","") + 1
End Sub
End Class

Obviously this only provides very limited motion, and does not take account of any user interaction with the alien, but it does give enough functionality for the programmer to animate the alien

Animating Objects in a VBScript Game

The first step in animating an object is to create it:

Dim alien1: Set alien1 = new vbAlien
Dim alien2: Set alien2 = new vbAlien

And then to place in on the screen:

alien1.show 300,200
alien2.show 250,200

The animation itself is carried out by means of a simple loop:

Sub animate_aliens
alien1.movedown
alien2.movedown
setTimeout "animate_aliens" , 10, "vbscript"
End Sub

In this example the loop repeats once every 10 milliseconds, and so the final step is to start the loop running:

animate_aliens
</script>

If this is saved as a .html file then the animation will be seen to run (as shown in figure 3). This is a simple start, but does shows how effectively a VBScript class can be used in games programming or, for the new programmer, to understand the structure and use of that class.


The copyright of the article Object Oriented Games Programming in VBScript in Windows Programming is owned by Mark Alexander Bain. Permission to republish Object Oriented Games Programming in VBScript in print or online must be granted by the author in writing.


Object Oriented Games Programming in VBScript, Mark Alexander Bain
Figure 1: A Games Background, Mark Alexander Bain
Figure 2: An Alien for a Game, Mark Alexander Bain
Figure 3: A Class Based Game, Mark Alexander Bain
 


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo