' -----------------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() SetColor 255,255,255 DrawImage Image,X,Y End Method Method UpdateState() Abstract End Type Type TSpaceShip Extends TGameObject Field MissileDelay:Int 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 KeyDown(KEY_SPACE) FireMissile() EndIf MissileDelay :- 1 If X<0 Then X=0 If X>620 Then X=620 End Method Method FireMissile() If MissileDelay<0 Local FileName:String="D:\Program Files\BlitzMax\samples\firepaint\bullet.png" Local Missile:TMissile=TMissile.Create(FileName, X+23,Y) MissileDelay=10 End If End Method End Type Type TAlienShip Extends TGameObject Field Explosion:Int 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 CheckCollision() If explosion>0 explosion :- 1 End Method Method CheckCollision() For Local g:TGameObject=EachIn GameObjectList If g<>Self Then If ImagesCollide(self.Image,X,Y,0,g.Image,g.X,g.Y,0) ListRemove(GameObjectList,g) Self.Explosion :+ 10 EndIf EndIf Next End Method Method DrawSelf() If explosion>0 SetColor 255-explosion,0,0 Else SetColor 255,255,255 EndIf DrawImage Image,X,Y End Method End Type Type TMissile Extends TGameObject Function Create:TMissile(File:String,xstart:Int,ystart:Int) Local Ship:TMissile=New TMissile 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() Y :- Speed If Y<0 Then ListRemove(GameObjectList,Self) End Method End Type |
![]() |
| Method UpdateState() X :- Speed If X<-ImageWidth(Image) Then X=620 CheckCollision() If explosion>0 explosion :- 1 End Method |
Method CheckCollision() For Local g:TGameObject=EachIn GameObjectList If g<>Self Then If ImagesCollide(self.Image,X,Y,0,g.Image,g.X,g.Y,0) ListRemove(GameObjectList,g) Self.Explosion :+ 10 EndIf EndIf Next End Method |
| Method DrawSelf() If explosion>0 SetColor 255-explosion,0,0 Else SetColor 255,255,255 EndIf DrawImage Image,X,Y End Method |
| Function ImagesCollide(image1:TImage,x1,y1,frame1,image2:TImage,x2,y2,frame2) | ||
| Tests if two images collide |