Make a new kind of object with class, set up each one in __init__, and store its values with self.
Game engines are full of classes: one Enemy class, a hundred enemies made from it, each with its own position and health.
A dictionary needed star["speed"]. A class lets you make your own kind of object: class Star describes what every star stores, and Star(100) makes one. Its values read like a turtle's: star.speed.
Make your prediction first. Help opens once you've locked it in.
class Pet:
def __init__(self, name, legs):
self.name = name
self.legs = legs
a = Pet("Rex", 4)
b = Pet("Tweety", 2)
print(a.name, b.legs)
class Pet: describes a new kind of object. __init__ (two underscores each side) runs every time one is made, and self.name = name stores the name in that pet. What does it print?
The same game, now with a Star class. Star(-150 + i * 100) makes a star at that x position, and __init__ sets up its turtle, speed and points. In tick, compare star.speed with last lesson's star["speed"].
from turtle import *
import random
speed(0)
penup()
goto(0, -180)
left(90)
score = 0
pen = Turtle()
pen.hideturtle()
pen.penup()
pen.goto(-220, 200)
class Star:
def __init__(self, x):
self.turtle = Turtle()
self.turtle.shape("circle")
self.turtle.color("gold")
self.turtle.penup()
self.turtle.goto(x, 200)
self.turtle.setheading(270)
self.speed = random.randint(3, 7)
self.points = 1
stars = []
for i in range(4):
stars.append(Star(-150 + i * 100))
def show_score():
pen.clear()
pen.write("Score: " + str(score), False, "left", ("Arial", 16, "normal"))
def go_left():
goto(xcor() - 25, ycor())
def go_right():
goto(xcor() + 25, ycor())
def tick():
global score
for star in stars:
star.turtle.forward(star.speed)
if star.turtle.distance(xcor(), ycor()) < 25:
score += star.points
star.turtle.goto(random.randint(-200, 200), 200)
if star.turtle.ycor() < -220:
star.turtle.goto(random.randint(-200, 200), 200)
show_score()
ontimer(tick, 50)
onkey(go_left, "Left")
onkey(go_right, "Right")
listen()
show_score()
tick()
mainloop()
from turtle import *
import random
speed(0)
penup()
goto(0, -180)
left(90)
score = 0
pen = Turtle()
pen.hideturtle()
pen.penup()
pen.goto(-220, 200)
class Star:
def __init__(self, x):
self.turtle = Turtle()
self.turtle.shape("circle")
self.turtle.color("gold")
self.turtle.penup()
self.turtle.goto(x, 200)
self.turtle.setheading(270)
self.speed = random.randint(3, 7)
self.points = 1
stars = []
for i in range(4):
stars.append(Star(-150 + i * 100))
def show_score():
pen.clear()
pen.write("Score: " + str(score), False, "left", ("Arial", 16, "normal"))
def go_left():
goto(xcor() - 25, ycor())
def go_right():
goto(xcor() + 25, ycor())
def tick():
global score
for star in stars:
star.turtle.forward(star.speed)
if star.turtle.distance(xcor(), ycor()) < 25:
score += star.points
star.turtle.goto(random.randint(-200, 200), 200)
if star.turtle.ycor() < -220:
star.turtle.goto(random.randint(-200, 200), 200)
show_score()
ontimer(tick, 50)
onkey(go_left, "Left")
onkey(go_right, "Right")
listen()
show_score()
tick()
mainloop()
What each part of the class does.
The word every line inside the class uses.
A value that was never stored in the star.
Match each line with what it does.
This stops with AttributeError: 'Star' object has no attribute 'speed'. Click the line that causes it.
Give __init__ a colour parameter, and pass a colour in where the stars are made.
Add a colour parameter after x: def init(self, x, colour):
Use it with self.turtle.color(colour), and pass a colour where the stars are made: Star(-150 + i * 100, "orange").
Give each star a random 1 to 3 points.
self.points = 1 gives every star 1 point. Use random.randint(1, 3).
Give __init__ a colour parameter and use it.
Run your program. This ticks when your code does all of the above.
Give each star a random 1 to 3 points.
Run your program. This ticks when your code does all of the above.
Add a Rock class: a grey square that falls, with its own speed. Make a list of two rocks, move them in tick like the stars, and take 3 points off when one touches you.
Add a Rock class and a list of rocks that cost 3 points when they touch you.
Run your program. This ticks when your code does all of the above.
The stars and rocks still need the game to move them and check them one line at a time. Next lesson gives the class its own methods, so a star can move itself: star.move().
Students rebuild the stars game with a Star class, set up each star in __init__, store its values with self, and add a second class for rocks. About 55 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
Read the unit primer first. class Star: describes a new kind of object. __init__ (two underscores each side; say "init") runs every time Star(...) makes one, and self is the object being made. self.speed = 5 stores a value in that object; a plain speed = 5 only makes a variable that disappears when __init__ ends, which is the bug task (AttributeError later, when the game asks for s.speed). Values read as star.speed, like a turtle's own commands. No inheritance, getters or class diagrams in this course.
self: why values need it to stay with each star. Ask them to point to where each value is stored, and whether it has self. in front.
Stretch: Give Star a size parameter and use shape() and points so bigger stars are worth more.
Support: Give students the Rock class already written, and ask them to make the list of rocks and move them in tick.
Next lesson adds methods, so a star moves itself (star.move()) and adding a new kind of falling thing is one class. Every game engine is built from classes like these.