Making with Code

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 #

๐ŸŒ Github Repo: github.com/Making-with-Code/lab_ballpit

๐Ÿ“„ This repository contains the following files:

  • ball.py
  • ballpit.py
  • setup_ballpit_canvas.py
๐Ÿ’ป Enter the Poetry Shell.
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 in BreathingBall() that is overridden from Ball()

๐ŸŽจ 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
๐Ÿ’ป Embrace the chaos of the ball pit! Cause the Ball, WarpBall, and BreathingBall to change color as they move.

๐Ÿ’ป Create your own type of Ball() or a whole new shape - like a Square()!

๐Ÿ‘พ ๐Ÿ’ฌ

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

๐Ÿ’ป Be sure to push any extension changes you make to Github!