Player movement and sprites

BlitzMax Forums/BlitzMax Beginners Area/Player movement and sprites

The Forum Archive is locked. These are the original blitzbasic.com forums, kept so their posts can still be read and searched. No new topics, replies or edits can be made here. To join the conversation, head over to the Blitz3D+ forums.

First of all you should consider using Floats over Integers when it comes to coordinates.

Blitzmax is able to draw images on fractions of screencoords.
So drawing from 0,0 | 0.5,0 | 1.0, 0 | ... will give a smoother kind of movement.


Next:
Spend your Types an update-Method - to divide your functionality to the things you will do when you want it - so update in mainloop only updates things, draw only does the things to draw images properly.


so (not tested, just typed in):

Type TBaseObject
	Field x:float, y:float
	

	function CreateBase:TBaseObject(x:float, y:float)
		local obj:TBaseObject = new TBaseObject
		obj.x = x
		obj.y = y
		
		return obj
	End function

	Method Draw() abstract
	Method Update() abstract
End Type

Type TAnimatedBaseObject extends TBaseObject
	field sprite:TImage
	field animPos:int=0
	field animCount:=1
	field animTime:int=0
	field animTimer:int 'every animTimer the animPos will increase

	function CreateAnimated:TAnimatedBaseObject(sprite:TImage, x:float, y:float, animPos:int=0, animCount:int=1, animTimer:int = 100)
		local obj:TAnimatedBaseObject = TAnimatedBaseObject
		obj.x = x
		obj.y = y
		obj.sprite = sprite
		obj.animPos = animPos
		obj.animCount = animCount
		obj.animTimer = animTimer		
		
		return obj
	End function

	Method UpdateAnimation()
		if self.animTime < MilliSecs()
			'increase position but only to the max defined by self.animCount
			'-1 to come to the base blitzmax defines (first stripframe is 0)
			self.animPos = Min(self.animPos+1, self.animCount-1)
			self.animTime = Millisecs() + self.animTimer
		endif
	End Method

	Method Draw() abstract
	Method Update() abstract

End Type


Type TPlayer extends TAnimatedBaseObject
	field name:string = "Player1"

	function Create:TPlayer(name:string="", image:TImage, x:float,y:float)
		local obj:TPlayer = new TPlayer
		obj.name = name
		obj.x = x
		obj.y = y
		'...
		'optional parameters or manual setting of variables like animcount etc.

		return obj
	end function

	Method Update()
		self.UpdateAnimation()
		'check for death
		'check for controllers
		' ...
	End Method

	Method Draw()
		DrawImage(self.sprite, self.x, self.y, self.animPos)
		DrawText(self.name, self.x, self.y-10)
		'... draw weaponeffects (bullets)... items ... other things ;D
	End Method
End Type

local Player1:TPlayer = TPlayer.create("HopeItWorks", 0.0, 50.5)


repeat
	'insert accumulator for update-limitation here
		Player.Update()
	'

	'insert accumulator for draw-limitation here
		cls
		Player.Draw()
	'
until KeyHit(Key_ESC)


Code wont work (no graphics initialisation and so on). also in general you would do the creations not the way i've done it now, I just didn't want to alter it after typing it in ;D


bye MB