
It's nothing great, I made this using BlitzMAX 1.26, and using some sprites for the PureBASIC example "Waponez II". This code contains no elements from the PureBASIC example, and I feel it's somewhat superior.
Anyways, comments are welcome!
Type GameEntity Field X:Float = 0 Field Y:Float = 0 Field Width:Float = 0 Field Height:Float = 0 Field Frame:Float = 0 Field Destructed:Int = False Field Sprites:TImage[ ] Method AddFrame:Int( Url:String ) ' Add one element to Sprites[]. Sprites = Sprites[..(Sprites.Length + 1)] ' Load new image. Sprites[Sprites.Length - 1] = LoadImage( Url ) ' Return new frame number. Return(Sprites.Length - 1) End Method Method Render( ) If (Sprites.Length > 0) ' Update dimension variables for current frame. Width = ImageWidth(Sprites[Int(Frame)]) Height = ImageHeight(Sprites[Int(Frame)]) ' Draw Image DrawImage(Sprites[Int(Frame)], X, Y) EndIf End Method Method Distance:Float( Entity:GameEntity ) Return (Sqr((X - Entity.X) ^ 2 + (Y - Entity.Y) ^ 2)) End Method Method Collided:Int( Entity:GameEntity ) Return(ImagesCollide(Sprites[Frame], X, Y, 0, Entity.Sprites[Entity.Frame], Entity.X, Entity.Y, 0)) End Method Method Update( ) Abstract Method ToString:String( ) Abstract End Type Type GEPlayer Extends GameEntity Const SPEED = 15 Field dX:Float Field dY:Float Field FireTimer:Int = 0 Method New( ) AddFrame("Data/Player_1.png") AddFrame("Data/Player_2.png") AddFrame("Data/Player_3.png") X = (GraphicsWidth( ) / 2) - (ImageWidth(Sprites[Int(Frame)]) / 2) Y = (GraphicsHeight( ) - ImageHeight(Sprites[Int(Frame)])) MoveMouse(X, Y) End Method Method Update( ) Frame = 0 dX = MouseX( ) dY = MouseY( ) If (MouseDown(1) And (FireTimer <= 0)) FireBullet( ) If (dX < X) Frame = 2 If (dX > X) Frame = 1 If (FireTimer > 0) FireTimer :- 1 X = X + ((dX - X) / SPEED) Y = Y + ((dY - Y) / SPEED) If (X < 0) X = 0 If (Y < 0) Y = 0 If ((X + Width) > GraphicsWidth( )) X = (GraphicsWidth( ) - Width) If ((Y + Height) > GraphicsHeight( )) Y = (GraphicsHeight( ) - Height) End Method Method FireBullet( ) Local Entity:GameEntity = New GEBullet Entity.X = X Entity.Y = Y GameEngine.AddEntity(Entity) FireTimer = 10 End Method Method ToString:String( ) Return( "GEPlayer" ) End Method End Type Type GEBullet Extends GameEntity Const SPEED = 15 Method New( ) AddFrame("Data/Bullet_1.png") End Method Method Update( ) Y:- SPEED If (Y < 0) Destructed = True End Method Method ToString:String( ) Return( "GEBullet" ) End Method End Type Type GEEnemy Extends GameEntity Const Speed = 4 Field Armor:Int = 10 Method New( ) AddFrame("Data/Ennemy_3_1.PNG") AddFrame("Data/Ennemy_3_2.PNG") AddFrame("Data/Ennemy_3_3.PNG") AddFrame("Data/Ennemy_3_4.PNG") AddFrame("Data/Ennemy_3_5.PNG") AddFrame("Data/Ennemy_3_6.PNG") End Method Method Update( ) Y :+ SPEED If (Frame < (Sprites.Length - 1)) Frame :+ 0.15 Else Frame = 0 EndIf ' For Entity:GameEntity = EachIn GameEngine.EntityList Select (Entity.ToString( )) Case "GEPlayer" If (Collided(Entity)) DebugLog("Collision With Player") Armor :- 10 EndIf Case "GEBullet" If (Collided(Entity)) DebugLog("Collision With Bullet") Armor :- 5 Entity.Destructed = True EndIf End Select Next If (Armor <= 0) Explo:GameEntity = New GEExplosion Explo.X = X Explo.Y = Y GameEngine.AddEntity( Explo ) Destructed = True EndIf ' If (Y > GraphicsHeight( )) Destructed = True EndIf DrawText("Armor: " + Armor, X - 4, Y - 12) End Method Method ToString:String( ) Return( "GEEnemy" ) End Method End Type Type GEExplosion Extends GameEntity Method New( ) AddFrame("Data/Explosion_1.PNG") AddFrame("Data/Explosion_2.PNG") AddFrame("Data/Explosion_3.PNG") AddFrame("Data/Explosion_4.PNG") AddFrame("Data/Explosion_5.PNG") AddFrame("Data/Explosion_6.PNG") AddFrame("Data/Explosion_7.PNG") AddFrame("Data/Explosion_8.PNG") End Method Method Update( ) If (Frame < (Sprites.Length - 1)) Frame :+ 0.15 Else Destructed = True EndIf End Method Method ToString:String( ) Return( "GEExplosion" ) End Method End Type Type GameEngine Global EntityList:TList = CreateList( ) Field IsRunning:Int = True Field SpawnCounter:Int = 50 Method Init( ) ' AppTitle = "BMWaponez II" Graphics(640, 480, 32) End Method Method Run( ) Init( ) AddEntity( New GEPlayer ) While ((Not AppTerminate( )) And IsRunning) ' Clear screen. Cls( ) ' For Entity:GameEntity = EachIn EntityList If (Entity.Destructed) DeleteEntity( Entity ) Else Entity.Update( ) Entity.Render( ) EndIf Next ' If (SpawnCounter > 0) SpawnCounter :- 1 Else SpawnCounter = 50 Enemy:GameEntity = New GEEnemy Enemy.X = Rand(0, GraphicsWidth( )) AddEntity(Enemy) DebugLog("Spawning Enemy") EndIf ' GCCollect( ) DrawText("Memory Useage: " + GCMemAlloced( ), 5, 5) DrawText("Spawn Counter: " + SpawnCounter, 5, 20) ' Flip( True ) End While End Method Function AddEntity( Entity:GameEntity ) EntityList.AddLast( Entity ) End Function Function DeleteEntity( Entity:GameEntity ) EntityList.Remove( Entity ) Entity = Null End Function End Type Global Engine:GameEngine = New GameEngine Engine.Run( )
The game will never be completed (atleast this version perhaps), it is just for show.
Comments are welcome, Thanks.
:: Watch the game in action ::
Video @ YouTube