Shapes at http://onestepback.org/articles/poly/
Posted: Thu Jan 08, 2009 11:06 am
Hi,
Someone here suggested to write a Cobra version of the program at http://onestepback.org/articles/poly/. The program has not yet appeared and I can’t find the link here so I have started a new thread about this subject.
The problem is that the print command is actually a println. How do I do a print without ‘\n’?
I also append the code I written so far.
I have used Cobra 0.8 since I could not download the recent version correctly
(so var + pro are combined in a single staemets).
It is also possibble to a second Cobra version with "all extra included".
Regards
Csaba
Someone here suggested to write a Cobra version of the program at http://onestepback.org/articles/poly/. The program has not yet appeared and I can’t find the link here so I have started a new thread about this subject.
The problem is that the print command is actually a println. How do I do a print without ‘\n’?
I also append the code I written so far.
I have used Cobra 0.8 since I could not download the recent version correctly
(so var + pro are combined in a single staemets).
It is also possibble to a second Cobra version with "all extra included".
Regards
Csaba
- Code: Select all
"""
Shapes.cobra demo for http://onestepback.org/articles/poly/ by CsabaU@Hotmail.com
Minimal version (but the code can be even tighter!)
"""
class Shape
var _x as float
var _y as float
pro x from var # defines a property -`from var` is a shortcut syntax to cover the variable _x.
pro y from var
def init(initX as float, initY as float)
_x = initX
_y = initY
def moveTo(newX as float, newY as float)
_x = newX
_y = newY
def relativeMove(deltaX as float, deltaY as float)
_x +=deltaX
_y +=deltaY
def draw
print 'Draw [.getType.name] at:( [_x] , [_y] ). ' # with meta info
class Rectangle
inherits Shape
var _width as float
var _height as float
pro width from var
pro height from var
def init(initX as float, initY as float, initWidth as float, initHeight as float)
base.init(initX, initY)
_width = initWidth
_height = initHeight
def draw
base.draw
print ' With height: [_height], width: [_width]'
class Circle
inherits Shape
var _radius as float
pro radius from var
def init(initX as float, initY as float, initRadius as float )
base.init(initX, initY)
_radius = initRadius
def draw
base.draw
print ' With radius: [_radius]'
class Program
def main is shared
shapes = [Rectangle(10f, 20f, 5f, 6f), Circle(15f, 25f, 8f)] # a list with hard coded shapes
# using shapes polymorphically
for aShape in shapes
aShape.draw
aShape.relativeMove(100f, 100f)
aShape.draw
# access a rectangle specific function
aRectangle= Rectangle(0f, 0f, 15f, 15f)
aRectangle.width = 30f
aRectangle.draw