'-----------------SETUP GAME CONDITIONS-------------------- Global GameObjectList:TList=CreateList() Graphics 640,480,0 Global URL:String="http::www.2dgamecreators.com/tutorials/gameprogramming/basic/" Global ExpImage:Timage=LoadAnimImage(LoadBank(URL+"exp1.png"),64,64,0,16) Global AlienImage:TImage=LoadImage(LoadBank(URL+"cartoonufo_1-1.png")) Global PlayerImage:TImage=LoadImage(LoadBank(URL+"blobship_1-1.png")) Global MissileImage:TImage=LoadImage(BlitzMaxPath()+"\samples\firepaint\bullet.png") TSpaceShip.Create(PlayerImage,320,420) TAlienShip.Create(AlienImage,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 Field Frame:Int=0 Field XScale:Float=1.0 Field YScale:Float=1.0 Method DrawSelf() SetColor 255,255,255 SetScale XScale,YScale DrawImage Image,X,Y,Frame End Method Method UpdateState() Abstract End Type Type TSpaceShip Extends TGameObject Field MissileDelay:Int Function Create:TSpaceShip(Image:TImage,xstart:Int,ystart:Int) Local Ship:TSpaceShip=New TSpaceShip CreateObject(Ship,Image,xstart,ystart) Return Ship End Function Method UpdateState() If KeyDown(KEY_LEFT) X :- Speed EndIf If KeyDown(KEY_RIGHT) X :+ Speed EndIf If KeyDown(KEY_SPACE) FireMissile() EndIf MissileDelay :- 1 If KeyDown(KEY_A) DebugStop EndIf If X<0 Then X=0 If X>620 Then X=620 End Method Method FireMissile() If MissileDelay<0 TMissile.Create(MissileImage, X+23,Y) MissileDelay=10 End If End Method End Type Type TAlienShip Extends TGameObject Field Explosion:Int=0 Function Create:TAlienShip(Image:TImage,xstart:Int,ystart:Int) Local Ship:TAlienShip=New TAlienShip CreateObject(Ship,Image,xstart,ystart) Return Ship End Function Method UpdateState() X :- Speed If X<-ImageWidth(Image) Then X=620 CheckCollision() If explosion>0 explosion :- 1 End Method Method CheckCollision() For Local g:TMissile=EachIn GameObjectList CollideImage(g.Image,g.X,g.Y,0,0,1,g) Next Local p:Object[]= CollideImage(Image,X,Y,0,1,0) For Local k:TMissile=EachIn p Explosion :+ k.Damage ListRemove(GameObjectList,k) Next If Explosion > 15 ListRemove(GameObjectList,Self) TExplosion.Create(ExpImage,X,Y) EndIf ResetCollisions() End Method Method DrawSelf() If explosion>0 SetColor 255-explosion,0,0 Else SetColor 255,255,255 EndIf SetScale XScale, YScale DrawImage Image,X,Y End Method End Type Type TMissile Extends TGameObject Field Damage:Int=10 Function Create:TMissile(Image:TImage,xstart:Int,ystart:Int) Local Ship:TMissile=New TMissile CreateObject(Ship,Image,xstart,ystart) Return Ship End Function Method UpdateState() Y :- Speed If Y<0 Then ListRemove(GameObjectList,Self) End Method End Type Type TExplosion Extends TGameObject Field AnimDelay:Int=10 Function Create:TExplosion(Image:TImage,xstart:Int,ystart:Int) Local obj:TExplosion=New TExplosion CreateObject(Obj,Image,xstart,ystart,2) Return obj End Function Method UpdateState() If AnimDelay<0 Then Frame :+ 1 AnimDelay=10 If Frame>15 Then TAlienShip.Create(AlienImage,X,Y) ListRemove(GameObjectList,Self) Return EndIf EndIf AnimDelay :- 1 End Method End Type Function CreateObject(Obj:TGameObject, Image:TImage,xstart:Int,ystart:Int,scale:Float=1.0) Obj.X=xstart Obj.Y=ystart Obj.XScale=scale Obj.YScale=Scale Obj.Image=Image If Obj.Image=Null Print "Not able to load image file. Program aborting" End EndIf ListAddLast GameObjectList, Obj End Function