Seeds
Next class is Seed
, as it explains itself: "I'm a (brown) seed, you can plant more, let the seed grow into a seedling."
class Seed:
def __init__(self, x, z):
self.x = x
self.z = z
self.plant()
def plant(self):
block(self.x, 1, self.z, 'box_brown')
def plantMore(self):
self.x += 5
self.plant()
def grow(self):
for j in range(5):
game.set_timer(j/2, block, self.x, j, self.z, 'box_green')
game.set_timer(3.5, block, self.x-1, 5, self.z, 'box_lime')
game.set_timer(4, block, self.x+1, 5, self.z, 'box_lime')
def __str__ (self):
return "I'm a little seed, you can plant more, and let them grow."
# apply Seed class:
s = Seed(-5, -20)
s.grow()
for i in range(3):
s.plantMore()
s.grow()
r = Seed(0, -15)
for i in range(5):
r.plantMore()
r.grow()
print(s) # I'm a little seed, you can plant more, and let them grow.
s
is a Seed
object, we let it grow()
, we plant 3 more seeds and let them grow; (calling plantMore()
and grow() in a for
loop)
Another object r
is put at the front row, it didn't grow()
, other 5 seeds grow big.
Seeds
class defines a __str__()
method, so we can call print()
on one object s
to show an message about them.
Here is the final result picture, if you run the program yourself, you should see them in motion.