Simple class: CopyDot
The class CopyDot
has three data attributes: x, y, z
, they are initialized when an object is created.
It's object has the method mark()
is to mark the original dot's location with a black block,
the object also has a method copy(step, color)
, so a copy of the dot will show at step
blocks away in a specific color.
class CopyDot:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def mark(self):
block(self.x, self.y, self.z, 'box_black')
def copy(self, step, color):
block((self.x + step), self.y, self.z, color)
dot1 = CopyDot(0, 6, -20) // a CopyDot object
dot1.mark()
dot1.copy(4, 'box_red')
dot1.copy(6, 'box_pink')
dot2 = CopyDot(-2, 3, -20) // another CopyDot object
dot2.mark()
dot2.copy(2, 'box_yellow')
dot2.copy(-3, 'box_blue') // a copy at the left side
Challenge yourself to make another class, which objects can make copies of the center dot at different directions(x, y, z
).
===
Objective: calling methods within the class, use self.method
.
Let's try another version of the class, CopyDot2
, so to understand calling method within the class(using self.method
)
class CopyDot2:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
self.mark('box_black')
def mark(self, color):
block(self.x, self.y, self.z, color)
def copy(self, step, color):
self.x += step
self.mark(color)
d3 = CopyDot2(0, 10, -20)
d3.copy(3, 'box_lime')
d3.copy(1, 'box_yellow')
Explanation:
method mark(color)
receives an input parameter color
;
method mark(color)
is called in __init__()
, using self.mark(color)
, so when created and initialized, an object shows itself in CodeCraft world as a black dot;
in method copy(step, color)
, the object variable x
is updated using self.x += step
, so the newly build dot will show up at step
blocks away from the previous dot;
mark(color)
is called again to display new dot;
((is it hard to understand call self.mark() in init()??
))