New to OO and a bit slow at learning it

BlitzMax Forums/BlitzMax Beginners Area/New to OO and a bit slow at learning it

Pete Carter(Posted 2009) [#1]
have a look at this please. Although it does what i want it to as in it draws a set number of squares and circles to the screen from a list of instances of a class/type im sure ive not got it quite right. help?

Global width:Int = 640
Global height:Int = 480

Graphics width,height

Global cx,cy,csize:Int
Global shapecount:Int = 0
Global Spritelist:TList = New TList
AutoMidHandle(enable)
HideMouse

Type sprite

	Field shape
	Field cx
	Field cy
	Field csize
	Field red,green,blue
	
	
	Method draw(shape,cx,cy,csize,red,green,blue)
		SetColor red,green,blue
		If shape = 1 
			DrawOval(cx,cy,csize,csize)
		EndIf	
		If shape = 2
			DrawRect(cx,cy,csize,csize)
		EndIf
	EndMethod
	
	
	Function Create:sprite(shape,cx,cy,csize,red,green,blue)
	Local s:sprite = New sprite
	s.shape = Rand(1,2)
	s.cx = Rand(-100,640)
	s.cy = Rand(-100,480)
	s.csize = Rand(10,100)
	s.red = Rand(10,255)
	s.green = Rand(10,255)
	s.blue = Rand(10,255)
	Return s
	EndFunction
	 
EndType

While Not KeyDown(KEY_ESCAPE)

	If shapecount < 1000
	Spritelist.addlast(sprite.Create(shape,cx,cy,csize,red,green,blue))
	shapecount = shapecount +1
	EndIf

      Local s:sprite
	For s=EachIn Spritelist
		s.draw(s.shape,s.cx,s.cy,s.csize,s.red,s.green,s.blue)
	Next
Flip
Cls
Wend

End



Htbaa(Posted 2009) [#2]
As your object (Type) has the fields your draw method requires you could do this:

	Method draw()
		SetColor red,green,blue
		If shape = 1 
			DrawOval(cx,cy,csize,csize)
		EndIf	
		If shape = 2
			DrawRect(cx,cy,csize,csize)
		EndIf
	EndMethod


Or this (which is the way I prefer)

	Method draw()
		SetColor Self.red,Self.green,Self.blue
		If Self.shape = 1 
			DrawOval(Self.cx,Self.cy,Self.csize,Self.csize)
		EndIf	
		If Self.shape = 2
			DrawRect(Self.cx,Self.cy,Self.csize,Self.csize)
		EndIf
	EndMethod


Also, I'd use a Select ... End Select block instead of your If ... EndIf part.

By the way, why did you add this? Doesn't look to me like it's being used anywhere...
Global cx,cy,csize:Int



Pete Carter(Posted 2009) [#3]
thanks, the extra line
Global cx,cy,csize:Int

is where it used to procedural and i must have missed it when changing it round.

the shapecount varible can be local missed that too.

I dont learn well from books and ive been just trying basic things myself, its the only way i seam to be able to get my head round stuff like this.

your tips are much appreciated.

Pete


Kistjes(Posted 2009) [#4]
Hi Pete,

Just to show the added value of OOP.
The shape property of the sprite is acually a distinction between two different types of sprite. Therefor it's better (in OOP-terms) to make the sprite-type abstract and extend different types/shapes of sprites.
In the provided code it's quite easy to define a new shapeSprite without rewriting the sprite-type. (try it :)
I hope it helps.
Regards,
Kistjes




Pete Carter(Posted 2009) [#5]
thanks Kistjes thats a good example. I think im almost ready to make a simple game demo.