| Type TGameObject Field X:Int = 320 Field Y:Int = 420 Field Speed:Int=3 Field Image:TImage Method DrawSelf() DrawImage Image,X,Y End Method Method UpdateState() Abstract End Type |
|
Type TSpaceShip Extends TGameObject Function Create:TSpaceShip(File:String,xstart:Int,ystart:Int) Local Ship:TSpaceShip=New TSpaceShip Ship.X=xstart Ship.Y=ystart Ship.Image=LoadImage(LoadBank(File)) If Ship.Image=Null Print "Not able to load image file. Program aborting" End EndIf Return Ship End Function Method UpdateState() If KeyDown(KEY_LEFT) X :- Speed EndIf If KeyDown(KEY_RIGHT) X :+ Speed EndIf If X<0 Then X=0 If X>620 Then X=620 End Method End Type |
| Type TAlienShip Function Create:TAlienShip(File:String,xstart:Int,ystart:Int) Local Ship:TAlienShip=New TAlienShip Ship.X=xstart Ship.Y=ystart Ship.Image=LoadImage(LoadBank(File)) If Ship.Image=Null Print "Not able to load image file. Program aborting" End EndIf Return Ship End Function Method UpdateState() X :- Speed If X<-ImageWidth(Image) Then X=620 End Method End Type |
| Type TGameObject Field X:Int = 320 Field Y:Int = 420 Field Speed:Int=3 Method DrawSelf() DrawImage Image,X,Y End Method Method UpdateState() Abstract End Type |
| Local ShoppingList:TList=CreateList() ListAddLast ShoppingList,"Milk" ListAddLast ShoppingList,"Eggs" ListAddLast ShoppingList,"Butter" For Local s:String=EachIn ShoppingList Print s Next End |
| Building untitled5 Compiling:untitled5.bmx flat assembler version 1.64 3 passes, 3444 bytes. Linking:untitled5.debug.exe Executing:untitled5.debug.exe Milk Eggs Butter Process complete |
| Local ShoppingList:TList=CreateList() |
| ListAddLast ShoppingList,"Milk" ListAddLast ShoppingList,"Eggs" ListAddLast ShoppingList,"Butter" |
| For s:String=EachIn ShoppingList Print s Next |
| ' ---------------------MAIN LOOP------------------------- Repeat Cls For o:TGameObject=EachIn GameObjectList o.DrawSelf() o.UpdateState() Next Flip Until KeyDown(KEY_ESCAPE) Or AppTerminate() End |
' -----------------SETUP GAME CONDITIONS-------------------- Global GameObjectList:TList=CreateList() Graphics 640,480,0 Local URL:String="http::www.2dgamecreators.com/tutorials/gameprogramming/basic/" Local Player:TSpaceShip = TSpaceShip.Create(URL+"/blobship_1-1.png",320,420) Local Alien:TAlienShip = TAlienShip.Create(URL+"/cartoonufo_1-1.png",320,0) ' ---------------------MAIN LOOP------------------------- Repeat Cls For o:TGameObject=EachIn GameObjectList o.DrawSelf() o.UpdateState() Next Flip Until KeyDown(KEY_ESCAPE) Or AppTerminate() End ' ---------------------TYPES, ATTRIBUTES AND BEHAVIOURS------------------------- Type TGameObject Field X:Int = 320 Field Y:Int = 420 Field Speed:Int=3 Field Image:TImage Method DrawSelf() DrawImage Image,X,Y End Method Method UpdateState() Abstract End Type Type TSpaceShip Extends TGameObject Function Create:TSpaceShip(File:String,xstart:Int,ystart:Int) Local Ship:TSpaceShip=New TSpaceShip Ship.X=xstart Ship.Y=ystart Ship.Image=LoadImage(LoadBank(File)) If Ship.Image=Null Print "Not able to load image file. Program aborting" End EndIf ListAddLast GameObjectList, Ship Return Ship End Function Method UpdateState() If KeyDown(KEY_LEFT) X :- Speed EndIf If KeyDown(KEY_RIGHT) X :+ Speed EndIf If X<0 Then X=0 If X>620 Then X=620 End Method End Type Type TAlienShip Extends TGameObject Function Create:TAlienShip(File:String,xstart:Int,ystart:Int) Local Ship:TAlienShip=New TAlienShip Ship.X=xstart Ship.Y=ystart Ship.Image=LoadImage(LoadBank(File)) If Ship.Image=Null Print "Not able to load image file. Program aborting" End EndIf ListAddLast GameObjectList, Ship Return Ship End Function Method UpdateState() X :- Speed If X<-ImageWidth(Image) Then X=620 End Method End Type |
| Keyword Extends | ||
| Specify user defined Type supertype. |
|
||||
| Create a linked list. | ||||
| Function ListAddLast:TLink( list:TList,value:Object ) | ||
| Add an object to a linked list. |
| Keyword EachIn | ||
| Iterate through an array or collection/list. |
| Keyword For | ||
| Marks the start of a loop that uses an iterator to execute a section of code repeatedly. |
| Keyword Next | ||
| End a For block. |