Setting up a game

BlitzMax Forums/BlitzMax Beginners Area/Setting up a game

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

OK I think you need to encapsulate more,
e.g. include the player and enemy within the Game object.

This means you only have to pass them in during setup that should make the code more encapsulated this reduces the complexity a bit as well, so you don't need to add Bullets, Bombs, Explosions to your functions parameters as you go!

Here is an example:
HideMouse()

'Here I have combinged the common elements used in TPlayer and TEnemy into a base object TUnit using Inheritance
' this can simplify the higher level objects and make it easier to maintain their common features
Type TUnit
	Field x:Float
	Field y:Float
	
	Method Draw() ' Removed the variables x and y as stored in the object
		
		DrawRect(x, y, 20, 20)
	EndMethod
	
End Type	

Type TPlayer Extends TUNit

	' Removed the player parameter as this method is within and acts on the player by default
	Method Logic(e:TEnemy, l:TLevel)	
		
		If(y < l.maxY - 20) 
			y = y + 1
		
		Else If (y > l.maxY - 20) 
			y = y		
		EndIf
		
		If(KeyDown(KEY_UP)) y = y - 4
		If(KeyDown(KEY_LEFT)) x = x - 2
		If(KeyDown(KEY_RIGHT)) x = x + 2
	EndMethod
	
EndType

Type TEnemy Extends TUnit
	
	Method Draw()
	
		DrawOval(x, y, 20, 30)
	EndMethod
	
	Method Logic(p:TPlayer, l:TLevel)

		x = x + 1		
	EndMethod	
EndType

Type TLevel
	
	Const maxY:Float = 400
	
	Method DrawLevel()
	
		DrawLine(0, maxY, 640, maxY, True)		
	EndMethod
	
	Method Logic()	
		
	EndMethod
EndType


Type TGame

	' Incorporated player, enemy and level into the game object to reduce coupling
	
	Field player:TPlayer
	Field enemy:TEnemy
	Field level:TLevel
	
	' Added a create function that sets up the player
	Function Create:TGame()
		Local newGame:TGame = New TGame
		newGame.Setup()
		Return newGame
	End Function
	
	Method Setup()
	
		Graphics 800,600
		
		player = New TPlayer
		
		' should really be added to a player Create/Setup Call
		player.x = 20
		player.y = 300

		enemy  = New TEnemy
		level  = New TLevel	
	End Method
	
	Method UpdateDraw()
		Cls()
		player.Draw()
		enemy.Draw()	
		level.DrawLevel()		
		Flip()
	EndMethod
	
	Method UpdateLogic()
		player.Logic( enemy, level)	
		enemy.Logic(player, level)
		level.Logic()
	EndMethod
	
	Method Run()
		While(Not(KeyDown(KEY_ESCAPE)))
			UpdateDraw()
			UpdateLogic()
		Wend
	End Method
	
	
EndType

' Note how the game now simplifies the setup
Local game:TGame = TGame.Create()

game.Run()


Hopefully you can see how much simpler the code looks when you Inherit common features and encapsulate objects within objects that use them!