OOP help
BlitzMax Forums/BlitzMax Beginners Area/OOP help
| ||
| Could somebody please finish off this bit of code for me? I'm trying to do things properly now - ie in OOP, and I'm not quite sure how to proceed. It's just a parallex star field with stars of 2 speeds and the background stars vary in brightness. Many thanks, Steve
Rem
Scrolling Starfield
End Rem
Framework BRL.Basic
Import BRL.glmax2d
Import BRL.pngloader
Strict
'----------------------------------------------------------
' global variables
Const X_RES = 800, Y_RES = 600 : Int
Global star_list:Tlist = CreateList()
'----------------------------------------------------------
' define types
Type star
Field x : Int
Field y : Int
Field speed : Int
Method init_star()
Local loop : Int
For loop = 1 To 75
Astar:star = New star
ListAddLast star_list, star
Astar.speed = 1
If loop <= 50 Astar.speed = 0
Astar.x = Rand(10,X_RES-10) ; Astar.y = Rand(10,Y_RES-10)
next
End method
Method update_star()
If Astar.speed = 1
Astar.y :+ 2
else
Astar.y :+ 1
endif
If Astar.y > Y_RES Astar.y = Astar.y - Y_RES
End method
Method draw_star()
SetBlend(SOLIDBLEND) ; SetColor(255,255,255)
For Astar:star = EachIn star_list
If Astar.speed = 1
SetColor(255,255,255)
Plot(Astar.x,Astar.y)
Else
Local star_bright = Rand(1,100)
If star_bright < 11
SetColor(255,255,255)
else
SetColor(128,128,128)
endif
Plot(Astar.x,Astar.y)
EndIf
next
End method
End Type
'----------------------------------------------------------
Graphics(X_RES,Y_RES,32,0)
HideMouse()
Repeat
Cls
Until KeyHit(KEY_ESCAPE)
|
| ||
| Er, what's the problem with it? What's giving you trouble in finishing this? Just asking someone to finish it isn't very useful. I'd be glad to help if I could just figure out what to help with. |
| ||
| Er, I'm stuck - simple as that! As far as I can work out most of the code and methods are there and I described what I want it to do (Astar is not recognized for a start). Basically if I can get this working I can would understand OOP much better. |
| ||
| I thought before you could call a method you needed and instance of the type. I might be wrong but should your init_star be a function? Also the instance name isn't needed within the method This works but whether it's OOP I don't know...
Framework BRL.Basic
Import BRL.glmax2d
Import BRL.pngloader
Strict
'----------------------------------------------------------
' global variables
Const X_RES = 800, Y_RES = 600 : Int
Global star_list:Tlist = CreateList()
'----------------------------------------------------------
' define types
Type star
Field x : Int
Field y : Int
Field speed : Int
Method update_star()
If speed = 1
y :+ 2
else
y :+ 1
endif
If y > Y_RES y = y - Y_RES
End method
Method draw_star()
SetBlend(SOLIDBLEND) ; SetColor(255,255,255)
If speed = 1
SetColor(255,255,255)
Plot(x,y)
Else
Local star_bright = Rand(1,100)
If star_bright < 11
SetColor(255,255,255)
else
SetColor(128,128,128)
endif
Plot(x,y)
EndIf
End method
function init_star()
Local loop : Int
For loop = 1 To 75
Local Astar:star = New star
ListAddLast star_list,astar
Astar.speed = 1
If loop <= 50 Astar.speed = 0
Astar.x = Rand(10,X_RES-10) ; Astar.y = Rand(10,Y_RES-10)
next
End function
End Type
'----------------------------------------------------------
Graphics(X_RES,Y_RES,32,0)
HideMouse()
star.init_star()
Repeat
Cls
For Local astar:star = EachIn star_list
astar.update_star
astar.draw_star
Next
flip
Until KeyHit(KEY_ESCAPE)
|
| ||
| Beat by tonyg but a other techniques used here. For example, using the New() method which gets called automatically when you do: s:star=New star Rem
Scrolling Starfield
End Rem
Framework BRL.Basic
Import BRL.glmax2d
Import BRL.pngloader
Strict
'----------------------------------------------------------
' global variables
Const X_RES = 800, Y_RES = 600 : Int
Global star_list:Tlist = CreateList()
'----------------------------------------------------------
' define types
Type star
Field x,y,speed : Int
Method New()
ListAddLast star_list, Self
x = Rand(10,X_RES-10)
y = Rand(10,Y_RES-10)
speed=1
End Method
Function update_stars()
For Local s:star=EachIn star_list
s.y:+s.speed
If s.y>Y_RES s.y=s.y-Y_RES
Next
End Function
Function draw_stars()
SetBlend SOLIDBLEND ; SetColor 255,255,255
For Local s:star = EachIn star_list
If s.speed = 1
SetColor 255,255,255
Else
If Rand(1,100)<50
SetColor 255,255,255
Else
SetColor 128,128,128
EndIf
EndIf
Plot s.x,s.y
Next
End Function
End Type
'----------------------------------------------------------
Global s:star
Graphics X_RES,Y_RES,32,0
HideMouse
' create 75 stars
For Local n=1 To 75
s=New star
If n <= 50 s.speed = 2
Next
' main loop
Repeat
Cls
s.update_stars
s.draw_stars
FlushMem
Flip
Until KeyHit(KEY_ESCAPE)
End |
| ||
| Thanks Tony - but a bit worried by your statement "This works but whether it's OOP I don't know..." I would like to know weither this is good OOP code or not - I don't want redundant code or incorrect code because of the problems in a larger program - I'd like a good example so I can get back to coding in this new style. I'm not comfortable with OOP at all so any thoughts would be appreciated. Thanks JB. |
| ||
| I think they both use good OO methods (although I do like that 'new' method). From the tutorials and doc the suggestion is that function code should refer to actions against the type while methods should refer to the instances of the type. Both these programs produce a desired result where the 'control' of the instances (objects) is within the type itself. I understand what you're looking for but I think there's still going to be some personal preference to coding. |
| ||
| I would like to know weither this is good OOP code or not - I don't want redundant code or incorrect code because of the problems in a larger program - I'd like a good example so I can get back to coding in this new style. It's mostly fine. If you want to be really pedantic you could argue that the init_star() function (and yes, as it's used here it should be a function) should either not have the responsibility for createing more than one star, or should be 'attached' to the collector instead (Seperation of Concerns). I'm not comfortable with OOP at all so any thoughts would be appreciated. Get a good book. I realise there isn't one for BlitzMAX, but Thinking in Java (3rd Edition) is a really good place to start (even tho' it's in Java, obviously).I suppose jb's version is more semantically correct, but at this point it's not really something you should be losing sleep over. The fact that you seem to have grasped the point of objects is an acheivement all in itself. |
| ||
| Thanks FlameDuck I guess I understand enough to proceed now and it was helpful to see a couple of examples. |
| ||
| Ok, it's ok, but, the methods/functions should probably renamed to update, draw, and init. This is what makes OOP actually useful syntactically. Keep in mind that it doesn't matter if methods have the same names as methods in other types. In fact it is encouraged if you can manage to extend a base class with methods used by all the child classes, allowing you to refer to any child class as a base class. |
| ||
| Sounds like a good idea - and using the same 3 method/function names init (or create) update and draw for every type, the program almost writes its self! And of course the example Astar.update() seems neater than Astar.update_star(). |