Students bring the Star class from Objects with turtles into microStudio, where each star keeps self.x and self.y and draws itself, fix a method that forgets self, give each star its own colour, make 8 stars, and add a class of their own.
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
Predict and Run 10 min · Investigate 15 min · Modify 10 min · Make 15 min
Read the unit primer first. This lesson leans on Objects with turtles (classes, self, methods, a list of objects); the only new idea is that a star keeps its own x and y and paints itself in draw(), since there are no turtles. random works in microStudio's Python as usual. print() writes in microStudio's console, under the game. The bug task is the forgotten self from Objects with turtles, in a method this time.
Moving from turtles to drawing by position: a star is now just numbers, painted each frame. Compare the two versions side by side.
| What you will see | A question to ask | The fix |
|---|---|---|
| cannot access local variable 'y'. | Whose y is it: a new variable, or this star's? | Write self.y. |
| NameError: name 'random' is not defined. | Is import random at the top of the file? | Add import random as the first line. |
| The new objects are made but never seen. | Are they drawn in draw(), after screen.clear? | Add a loop in draw() that calls each one's draw(). |
| All the stars have the same colour. | Is the colour picked in __init__ (once per star) or in draw()? | Pick it in __init__ and store it in self.colour. |
One possible solution for each task. Other correct programs are fine.
import random
class Star:
def __init__(self, x):
self.x = x
self.y = 100
self.speed = random.randint(5, 15) / 10
self.colour = random.choice(["rgb(255,210,0)", "rgb(255,120,160)", "rgb(120,220,255)"])
def move(self):
self.y -= self.speed
if self.y < -100:
self.y = 100
self.x = random.randint(-90, 90)
def draw(self):
screen.fillRound(self.x, self.y, 8, 8, self.colour)
def init():
global stars
stars = []
for i in range(4):
stars.append(Star(-60 + i * 40))
def update():
for star in stars:
star.move()
def draw():
screen.clear("rgb(10,10,40)")
for star in stars:
star.draw()
import random
class Star:
def __init__(self, x):
self.x = x
self.y = 100
self.speed = random.randint(5, 15) / 10
def move(self):
self.y -= self.speed
if self.y < -100:
self.y = 100
self.x = random.randint(-90, 90)
def draw(self):
screen.fillRound(self.x, self.y, 8, 8, "rgb(255,210,0)")
def init():
global stars
stars = []
for i in range(8):
stars.append(Star(-84 + i * 24))
def update():
for star in stars:
star.move()
def draw():
screen.clear("rgb(10,10,40)")
for star in stars:
star.draw()
import random
class Star:
def __init__(self, x):
self.x = x
self.y = 100
self.speed = random.randint(5, 15) / 10
def move(self):
self.y -= self.speed
if self.y < -100:
self.y = 100
self.x = random.randint(-90, 90)
def draw(self):
screen.fillRound(self.x, self.y, 8, 8, "rgb(255,210,0)")
class Cloud:
def __init__(self, y):
self.x = -120
self.y = y
def move(self):
self.x += 0.3
if self.x > 120:
self.x = -120
def draw(self):
screen.fillRound(self.x, self.y, 30, 12, "rgb(200,200,220)")
def init():
global stars, clouds
stars = []
for i in range(4):
stars.append(Star(-60 + i * 40))
clouds = [Cloud(60), Cloud(20)]
def update():
for star in stars:
star.move()
for cloud in clouds:
cloud.move()
def draw():
screen.clear("rgb(10,10,40)")
for cloud in clouds:
cloud.draw()
for star in stars:
star.draw()
Stretch: Give stars a size as well as a colour, or make some stars twinkle by changing their size in move().
Support: Give students the Cloud class's __init__ and ask them only for move() and draw().
Why does each star have self.x and self.y?
So that each star remembers where it is; draw() paints it there and move() changes it, for that star only.
Next lesson: catching stars with the paddle and keeping score, which is the turtle course's Catch the turtle in a game engine.