Lesson 5
Download and extract the image files below.
Decomposition
What are the components of the “Gem Catching” game?
How does the player interact with the game?
Can we make this game with our existing knowledge?
What is missing?
What could we add to make the game more interesting?
Functions
A function defines a procedure that we “call” to execute at a different
location in our program. In Python, functions start with the def keyword
to signify the definition of a new function, followed by the function name,
the function arguments, and the code that we want the function to execute.
For example:
def create_gem():
window.create_sprite(Gem)
The Scheduler Class
Pycat includes a Scheduler class to call a function repeatedly at a fixed time interval. For example, if we want to execute our create_gem function every second, we can write:
from pycat.core import Scheduler
Scheduler.update(create_gem, 1)
The Sprite’s delete() method
To delete a sprite from the window, use the delete() method.
if self.is_touching_sprite(cat):
self.delete()
Randomization
You can use the random module to generate random numbers.
from random import randint
randint(1, 5)
The Sprite class also has a method to set a sprite’s position to a random x-y location in the window.
def on_create(self):
self.goto_random_position()