| Type TSpaceShip Field X:Int Field Y:Int End Type |
| Type TSpaceShip Field x:Int Field y:Int End Type |
So in the example above, our Type is called TSpaceShip and it has two attributes called x and y representing the SpaceShip's display location on our graphic screen.
| Type TSpaceShip Field X:Int Field Y:Int End Type |
Note that I have defined the variables X and Y with :Int attached to them. This indicate that we are defining an Integer variable (ie a variable that will hold a number)
Now we can combine what we have learnt from the previous Tutorial with this new concept of Types and start moving our spaceship around the screen.
| Graphics 640,480,0 Type TSpaceShip Field X:Int Field Y:Int End Type SpaceShip:TSpaceship=New TSpaceShip HideMouse() Repeat Cls SpaceShip.X=MouseX() spaceship.Y=420 DrawRect spaceship.X, spaceship.Y,20,20 Flip Until MouseHit(1) End |
compile and run
the program by clicking the "Build and Run" button as per normal. You
should now be able to move the 20x20 white block (representing our Spaceship) along the horizontal
section of the screen:-
![]() |
Let us try to understand what we have done. The instruction SpaceShip:TSpaceShip=New TSpaceShip creates an instance called SpaceShip from the TSpaceShip Type.
You can imagine the TSpaceShip Type as a blueprint from which SpaceShips can be created from (or think of TSpaceShip as a factory to make SpaceShip objects).
| Graphics 640,480,0 Type TSpaceShip Field X:Int Field Y:Int End Type SpaceShip:TSpaceship=New TSpaceShip HideMouse() Repeat Cls SpaceShip.X=MouseX() spaceship.Y=420 DrawRect spaceship.X, spaceship.Y,20,20 Flip Until MouseHit(1) End |
the next instruction, HideMouse() simply hides the normal mouse pointer so that it will not clutter our game. You can try deleting this line and see what happens when you build and run the subsequently changed program.
|
HideMouse() |
| Repeat Cls SpaceShip.X=MouseX() SpaceShip.Y=420 DrawRect SpaceShip.X, SpaceShip.Y,20,20 Flip Until MouseHit(1) End |
We saw in the previous section how Types allow us to create blueprints for our game objects. In the previous section we created attributes of positions in our TSpaceShip type using the Field keyword. Another very powerful feature of Types is the ability to allow behaviours of Types to be programmed as part of the blueprint as well.
Let us demonstrate this capability with a simple example (note the new additions to our previous program):-
| Graphics 640,480,0 Type TSpaceShip Field X:Int Field Y:Int Method UpdateState() If X>500 Then SetColor 255,0,0 Else SetColor 255,255,255 EndIf End Method End Type SpaceShip:TSpaceship=New TSpaceShip HideMouse() Repeat Cls SpaceShip.X=MouseX() spaceship.Y=420 SpaceShip.UpdateState() DrawOval SpaceShip.X, SpaceShip.Y,20,40 Flip Until MouseHit(1) End |
![]() |
| Method UpdateState() If X>500 Then SetColor 255,0,0 Else SetColor 255,255,255 EndIf End Method |
|
Repeat Cls SpaceShip.X=MouseX() spaceship.Y=420 SpaceShip.UpdateState() DrawOval SpaceShip.X, SpaceShip.Y,20,40 Flip Until MouseHit(1) |
| Graphics 640,480,0 Type TSpaceShip Field X:Int Field Y:Int Field Image:TImage Method DrawSelf() DrawImage Image, X,Y End Method Method UpdateState() If X>500 Then SetColor 255,0,0 Else SetColor 255,255,255 EndIf End Method End Type SpaceShip:TSpaceship=New TSpaceShip HideMouse() ImageName:string="http::www.2dgamecreators.com/tutorials/gameprogramming/basic/blobship_1-1.png" SpaceShip.Image:TImage=LoadImage(LoadBank(ImageName)) If SpaceShip.Image=Null Print "Not able to load image file. Program aborting" End EndIf Repeat Cls SpaceShip.X=MouseX() spaceship.Y=420 SpaceShip.UpdateState() SpaceShip.DrawSelf() Flip Until MouseHit(1) End |
![]() |
| Graphics 640,480,0 Type TSpaceShip Field X:Int Field Y:Int Field Image:TImage Method DrawSelf() DrawImage Image, X,Y End Method |
| ImageName:string="http::www.2dgamecreators.com/tutorials/gameprogramming/basic/blobship_1-1.png" SpaceShip.Image:TImage=LoadImage(LoadBank(ImageName)) |
) is:-| ImageName:string="blobship_1-1.png" SpaceShip.Image:TImage=LoadImage(ImageName) |
| If SpaceShip.Image=Null Print "Not able to load image file. Program aborting" End EndIf |
| Repeat Cls SpaceShip.X=MouseX() spaceship.Y=420 SpaceShip.UpdateState() SpaceShip.DrawSelf() Flip Until MouseHit(1) End |
| Rem Our First BlitzMax Game ---------------SETUP GAME CONDITION--------------- EndRem Graphics 640,480,0 HideMouse() SpaceShip:TSpaceship=New TSpaceShip ImageName:string="http::www.2dgamecreators.com/tutorials/gameprogramming/basic/blobship_1-1.png" SpaceShip.Image:TImage=LoadImage(LoadBank(ImageName)) If SpaceShip.Image=Null Print "Not able to load image file. Program aborting" End EndIf ' ---------------------MAIN LOOP------------------------- Repeat Cls SpaceShip.X=MouseX() '<-----User Input spaceship.Y=420 SpaceShip.UpdateState() SpaceShip.DrawSelf() Flip Until MouseHit(1) '<-----Check End Game Here End ' -----------------------TYPES----------------------------- Type TSpaceShip Field X:Int Field Y:Int Field Image:TImage Method DrawSelf() DrawImage Image, X,Y End Method Method UpdateState() If X>500 Then SetColor 255,0,0 Else SetColor 255,255,255 EndIf End Method End Type |
![]() |
| Function HideMouse() | ||
| Make the mouse pointer invisible. |
| Keyword Type | ||
| Begin a user defined Type declaration. |
| Keyword EndType | ||
| End a user defined Type declaration. |
| Keyword Field | ||
| Declare a Field variable. |
| Keyword Method | ||
| Begin a Method declaration. |
| Keyword EndMethod | ||
| End a Method declaration. |
| Keyword Else | ||
| Else provides the ability For an If Then construct to execute a second block of code when the If condition is False. |
| Keyword EndIf | ||
| Marks the End of an If Then block. |
| Function DrawImage( image:TImage,x#,y#,frame=0 ) | ||
| Draw an image to the back buffer. |
| Function LoadImage:TImage( url:Object,flags=-1 ) | ||
| Load an image. |
| Function LoadBank:TBank( url:Object ) | ||
| A bank containing the binary contents of url, or null if url could not be opened. |
| Keyword Rem | ||
| Begin a remark block. |
| Keyword EndRem | ||
| End a remark block. |