KC Coding Camp, Day 4

Let's supercharge KittenBot and put it into Warp Drive Mode.

We will use variables and randomize the values that get assigned to them in order to create a nice screen graphic.

We will start with the operator to pick a random number, and combine it with a method to draw a line.

If you enter the following code, you will get a very neat Warp Drive pattern.

We will start here and will use the Python editor to do even more. Here is the code that is generated for this program.

In order to start generating this, we will also need to create some variables.

It will be important to plan your code and get things set up in order. So start by using the duplicate tool and get everything ready for insertion.

Be sure you have randomized values for x,y,r,g & b as well as the set statements for each one.

While it is not necessary to do it this way if you are coding directly in Scratch, because we will be moving to Python this will be the way to do it to see how everything works. We can change it later if we want.

Finally you can build the code block below.

Modify the second x and y parameter in order to get some more interesting effects. Try several different modifications, including random numbers. Try changing the parameters and observe what happens. Keep some constant and change others.

Next jump into the Python code generator by clicking the Code button. Once your program starts running you can wait for it to complete, but if it is in a Forever Loop you will need to press Ctrl-C to stop it.

You have the ability to issue commands directly to the KittenBot from this command prompt. It will do whatever you tell it to do as long as the commands are defined correctly. Try some of the things that are listed above and observe what is happening.

Try typing just the line that starts with screen.line but instead of variables, enter some numbers. Be careful which numbers you enter or you could go out of bounds.

x 0-127
y 0-159

r 0-255
g 0-255
b 0-255

Now go into Scratch and make this line. The "Flag" block has to be included for Scratch to generate the Python code. You can have multiple flags set.

Now there is much more to do.

Try working with any number of the drawing methods. And remember, you can always use the screen.clear() function on the command prompt to clear your workspace.

Do you notice the difference between the drag and drop method of Scratch and the control you have with Python? There are lots of opportunities to make typing errors so debug your code carefully and take it slowlly.

Now drag some more of the drawing methods onto the Scratch canvas and convert them into Python code:

Selecting draw methods in Scratch
After generating the Python code

You can type in the screen.line draw method one time and use the up-arrow to go back and re-enter what you typed. Use your arrow keys to change up some parameters.

You can modify the code directly in the Python window once you get familiar with  how the parameters work. Practice using some of the other draw methods.