Bit Reference
The byubit
library helps you learn to program by drawing pictures and solving
puzzles. Bit works in a “world” of squares:
You can think of Bit as a robot, in the shape of a triangle. Bit has a position and a direction. In the picture above, Bit is at the bottom left, facing right.
Installing byubit
The most reliable way to install byubit
is through PyCharm. Open the
File->Settings
(Windows) or File->Preferences
(MacOS) menu and select
Project
. You should see a your Conda environment and a list of packages
installed in that environment:
Click the +
and then type in byubit
. Then click Install Package
.
Importing Bit
To use Bit, you always need to import it using the following:
from byubit import Bit
Creating a new world
You can create a new world with this function:
Bit.new_world(columns, rows)
— creates a new world that has the given number of columns and rows and returns this world
For example:
bit = Bit.new_world(5, 3)
This creates a new world, like the one shown above, that is 5 columns wide and 3
rows high. The world is stored in a variable called bit
.
Moving Bit
You can move and turn Bit using the following functions:
bit.move()
— move forward one spacebit.right()
— turn right (without moving)bit.left()
— turn left (without moving)
An example that moves Bit a few spaces and turns:
bit.move()
bit.left()
bit.move()
bit.right()
bit.move()
Painting colors
You can use the following functions to change the colors of squares:
bit.paint(color)
— paint the color of the current square; valid colors are ‘red’, ‘green’, and ‘blue’bit.get_color()
— returns the color of the current squarebit.erase()
— erases the color of the current square
An example that paints the current square red:
bit.paint('red')
An example that gets the color of the current square and stores it in a variable:
current_color = bit.get_color()
Checking if a space is clear
Bit can’t move if the square in front of it is black, or if it reaches the end of its world. You can use the following methods to check squares near Bit:
bit.front_clear()
— checks if the square in front of Bit is clear (not black, not the end of the world)bit.right_clear()
— checks if the square to the right of Bit is clearbit.left_clear()
— checks if the square to the left of Bit is clear
An example that moves Bit until it reaches the end of the world:
while bit.front_clear():
bit.move()
Drawing the world
You won’t see anything that Bit is doing until you draw the world:
bit.draw()
— draws the world
Once you draw the world, you can close the window that appears, and then the next command will be executed.
Saving and loading the world
You can use the following functions to save and load worlds:
bit.save(filename)
— saves the current world to a fileBit.load(filename)
— loads the world from a file
Notice that the first one uses a lowercase bit
because it is working with an
existing world, whereas the second one uses an uppercase Bit
because it is
loading a new world.
For example, to save the current world to a file called my-world.txt
:
bit.save('my-world.txt')
To load a world from a file called cool-world.txt
:
Bit.load('cool-word.txt')
Because worlds are just files, you can load them from a directory:
Bit.load('worlds/lab1-world.txt')
Comparing worlds
We will often give you a solution world in a file. To check whether you have reached the right solution you can use:
bit.compare(bit)
— compares the current world to the world provided
Notice that this function takes a variable that is a bit. To use it you can do the following:
expected_world = Bit.load('solution.txt')
bit.compare(expected_world)
Notice how we stored the expected world in a variable called expected_world
.
Then we give this variable to the compare()
function as an argument.
You can do this in one step with:
bit.compare(Bit.load('solution.txt'))
Cheat Sheat
from byubit import Bit
- Worlds
Bit.new_world(columns, rows)
— creates a new world that has the given number of columns and rows and returns this worldbit.save(filename)
— saves the current world to a fileBit.load(filename)
— loads the world from a file
- Moving
bit.move()
— move forward one spacebit.right()
— turn right (without moving)bit.left()
— turn left (without moving)
- Painting
bit.paint(color)
— paint the color of the current square; valid colors are ‘red’, ‘green’, and ‘blue’bit.get_color()
— returns the color of the current squarebit.erase()
— erases the color of the current square
- Checking
bit.front_clear()
— checks if the square in front of Bit is clear (not black, not the end of the world)bit.right_clear()
— checks if the square to the right of Bit is clearbit.left_clear()
— checks if the square to the left of Bit is clear
- Comparing
bit.compare(bit)
— compares the current world to the world provided, e.g.bit.compare(Bit.load('solution.txt'))