Ball Pit #
In this lab, we’ll get re-aquainted with CS concepts such as classes and decomposition with the Turtle library.
[0] Set up #
๐ป Let's start by cloning the repository
in your cs10\unit00_networking
folder. Be sure to change yourgithubusername
to your actual Github username.
cd ~/desktop/making_with_code/cs10/unit00_networking
git clone https://github.com/the-isf-academy/lab_ballpit_yourgithubusername
cd lab_ballpit_yourgithubusername
๐ค Remember,
tab
autocompletes files and folders in the Terminal
๐ This repository contains the following files:
ball.py
ballpit.py
setup_ballpit_canvas.py
poetry shell
[1] Exploring Ball Pit #
๐ป
Run ballpit.py
to see the Ballpit animation.
python ballpit.py
In the Ballpit, notice each ball has a different size, stays the same size the whole time, and bounces off the walls.
๐ป
Let’s delve into the code. Start by looking at ballpit.py
code .
๐ป Change the number of Balls that are in the Ballpit.
๐ป
Now, let’s focus on ball.py
and the method set_color()
. Change set_color()
so the color is a random shade of green. Or, feel free to change it to any other color!
๐ง You may need to look around the file for an example of how to use the Random library
[2] Inheritance #
What if we wanted to add different types of Balls to the Ballpit? For example, what if we wanted some of the Balls to change size as they moved?
We can use inheritance!
BreathingBall #
BreathingBall()
is a child class of Ball()
that grows and shrinks as it moves. It looks like it’s breathing in and out.
It inherits all of the methods from Ball()
and overrides the update()
method.
๐ป
Copy and paste the code below into the bottom of ball.py
class BreathingBall(Ball):
""" BreathingBall extends the Ball class.
A BreathingBall is the same as a Ball,
but it grows and shrinks as it moves.
"""
def __init__(self):
"""This constructor calls the constructor of the parent class,
and adds a counter variable.
"""
super().__init__()
self.step = random.randint(0,120)
def update(self):
"""Calls the parent method and also changes the size of the ball
"""
super().update()
self.step += 1
new_radius = math.sin(self.step/20)*(3/2)+1.5
self.set_size(new_radius)
๐ป
Add the BreathingBall()
to the Ballpit.
๐ง What lines of code will you need to add to
ballpit.py
?
Override a Method #
Currently, it’s pretty difficult to notice the regular Ball()
from the BreathingBall()
. Let’s change that by overriding set_color()
in BreathingBall()
๐ป
In ball.py
under the BreathingBall()
, override the class set_color()
.
๐ง Remember,
update()
is another function inBreathingBall()
that is overridden fromBall()
๐จ Feel free to customize your BallPit however you’d like! For example, bgcolor('red')
changes the background color of the screen to the color red.
[3] Deliverables #
โกโจOnce you’ve successfully completed the lab, fill out this Google form.
๐ป Push your work to Github:
- git status
- git add -A
- git status
- git commit -m “describe your code and your process here”
be sure to customize this message, do not copy and paste this line
- git push
[4] Extension: Increase the Chaos #
If time allows, ramp up the chaos even further!
๐ป
Add in a WarpBall()
. It warps around to the opposite side of the screen.
class WarpBall(Ball):
""" WarpBall extends the Ball class.
A WarpBall is the same as a Ball,
but instead of bouncing off the wall,
it reappears on the other side of the screen.
"""
def __init__(self):
super().__init__()
def update(self):
"""Checks whether the ball has hit a wall.
If the ball has hit a wall, it "warps" the ball
onto the opposite side of the screen
"""
if self.x > 1:
self.x = 0
if self.y > 1:
self.y = 0
๐ป
Create your own type of Ball()
or a whole new shape - like a Square()
!
๐พ ๐ฌ๐ป Be sure to push any extension changes you make to Github!If you want to use an inherited method while also changing some behavior, you can do that like this:
def update(self): super().update() # this calls the inherited behavior self.turtle.right(5) # any new code will be run afterwards