An Hour of Code == An Hour of Fun

During Computer Science Education Week, nearly 75 million people around the globe spent an hour of their time learning how to write computer code. Some wrote a line or two, while some wrote tens or hundreds of lines. They used a variety of programming environments and languages, with some using a tool like Scratch to build a program using pictures as commands, and others writing code in a professional programming language. The point of the week was to make more people knowledgeable of one of the most important skills anyone can have today. Certainly, only a small fraction of those 75 million people are going to end up writing software for a living. Career preparation wasn’t the point of the Hour of Code. However, the people who wrote some code last week now have a much better idea of what makes the tools they use every day on their computers, tablets, and phones function. That insight can help them be safer, more productive, and more informed computer users.

Why don’t we spend our own Hour of Code now? Class is in session. Let’s get coding.

I’m going to teach you how to draw this shape using a programming language called Python.

pythonhouse

It’s a charming little house, isn’t it? Don’t be alarmed. I’m not expecting you to draw this by hand. I’m certain there is no way you could draw something as beautiful and as intricate as that. Leave that to the professionals. Instead, we are going to use Python to tell the computer to draw it for us.

Python is a professional programming language that has been around since the mid 1990s. It was created by a Computer Scientist named Guido van Rossum, who set out to create an easy, intuitive, and open language that had all the power of other languages like C but without the arcane syntax and steep learning curve. In 1999, van Rossum launched his Computer Programming for Everybody initiative, which used his Python language as the tool for teaching people how to code. Today, Python is used to build all sorts of applications, and Python programmers command top dollar (one recent graduate of ours was offered $130,000 per year to write Python software fresh out of school). It still remains one of the easiest languages to learn: so easy, in fact, that we’ll be able to build that quaint little red ranch with it.

You’ll first need to download and install Python. It’s pretty easy to do so. Just go to the Python download site and click the button for version 3.4.2 (or a later version if you’re reading this a few months from now). Just follow the on-screen instructions. When you’re done, Python and its associated tools, such as an editor for entering and running the code you write, will be installed on your machine.

Now that Python is installed on your machine, let’s start coding. To code, you need an editor. The editor that is installed with Python by default is called idle. If you’re on a Windows machine, start idle by opening the Start menu and typing “idle”. If you’re on a mac, open the Launch screen and type “idle”. When you do, you’ll see a screen that looks like this.

pythonshell

 

This is the Python Shell. You can actually type commands in this window, and Python will respond to you. Try it. For example, type 5 + 2, and you’ll see Python respond with 7. The Python Shell is great for typing in a couple of commands, but a drawing as detailed as our red mansion is going to take a lot of lines of code. So, we need to start with a blank slate. To do this, choose Fie >> New File (or File >> New Window, depending on the version you’re using) from the main menu. You’ll see blank white window open in front of you. Its emptiness is a little scary at first, but this is where we’re going to add some code.

What code are we going to add? We’re going to add several instructions to get Python to draw the house. We’ll get some help along the way from a cool feature of the Python language called a turtle. Python uses a modern version of an old programming package called Turtle Graphics that was first introduced in the 1980s to teach people the basics of programming. Python introduces a modern take on Turtle Graphics, and we’ll use it now to create our drawing.

The first step is to declare to Python that we want to use Turtle Graphics. Just add this line to the top of the blank window:

import turtle

That gives us permission to create and command a little drawing “turtle” to sketch our opulent dwelling. Of course, we haven’t created the actual drawing reptile yet. First, let’s create the screen in which the turtle will move and draw. We have to give a name to the screen. Let’s call it scene, as if our turtle were acting in a play. Then, let’s give the scene we just created a background color of SkyBlue. A lot of other colors are available by name, which you can see at this site, but SkyBlue helps create a pretty background for our house.  Here are the lines to type:

scene = turtle.Screen()
scene.bgcolor(“SkyBlue”)

Now let’s create the turtle. Of course, you have to give your turtle a name. I’ll call my turtle “evan”, after my son who likes to draw. Type this line to create a turtle called evan:

evan = turtle.Turtle()

Now, what kind of a turtle would evan be if he didn’t actually look like a turtle? Type this into the window in which you’re writing your code to make evan take the shape of a turtle:

evan.shape(“turtle”)

Python is picky, so make sure you type all your commands exactly as they’re presented here, including quotations marks, parentheses, and capitalization.

Most of the house in our picture is red. So, we need to tell evan the turtle to pick red as his color. All turtles can have two colors, one to draw outlines and the other to fill in shapes. We want evan to outline shapes in red. So, type this command to give evan a red pen:

evan.color(“Red”)

Now, evan is an eager turtle, ready to draw. However, we need him to move somewhere. Our evan’s home base is smack dab in the middle of the screen. The coordinates for that point in the middle of the screen – in other words, its address – is (0,0). The first number represents how far to the left or the right of home evan happens to be, and the second number represents how far above or below home evan is. In terms of the first number, left of home is negative, and right of home is positive. In terms of above and below, which is what the second number tells us, the value will be positive for any point above home, and it will be negative for any number below home. As we said, evan is now at home – point (0,0). We will want him to move to the bottom left corner of the house, but we don’t want him to draw as he moves. We just want him to move there. So, we have to tell evan to pick up his pen. Here’s how:

evan.penup()

It’s fun bossing evan around. That’s one of the great things about programming. You’re the boss, you’re in control, and what you say should happen will happen. It’s a great way to be bossy without getting into trouble.

We’re almost ready to start drawing the main part of the house, but we need to have a plan first. The plan will help us figure out how to tell evan to move and what colors he should use in the different sections. To come up with the plan, it will be very helpful to use some graph paper. You can print out some graph paper at this site. Figure out where the exact middle of the graph paper is, and mark it with a dot. That’s evan’s home, the point (0,0). Now, sketch out the home as shown in the figure below, and label all the corners of the various parts of the sketch with their respective location. I’m assuming that each little square is 10 units wide and 10 units tall, which is how I’ve come up with the coordinates of each point. For example, the location of the bottom left corner of the house is (-100, 0), because it is 10 squares – 100 units – to the left of home, and it isn’t above or below home at all. The roof is located at (0, 100), because it is 10 squares – 100 units – above home but isn’t to the left or right of it. Sketch out the home, label the locations of each point, and you’ll then have a really great plan for telling evan how to draw. Here’s what your drawing should look like:

house

Labeling the locations of all the points will make it really clear how to order evan around as we draw the various parts of the house. For each part, we’ll tell evan to pick his pen up (which we already did), move to the starting point, set his fill color to what we want it to be, tell him to put his pen down, indicate to him that we want him to begin drawing and filling a section, and then tell him to move to each of the various points that outline that section. At the end, we need to tell him to stop drawing and filling. Here are the various commands we will use:

evan.penup() – this tells evan to pick his pen up

evan.goto(-100, 0) – this tells evan to move to a particular point (-100, 0) in this case. The command “goto” simply means “go to a particular location.

evan.fillcolor(“Red”) – this tells evan to set his fill color to a particular one. In this case, evan will set the color he uses to fill in shapes to Red.

evan.pendown() – this tells evan to put his pen down on the screen so that, once he’s told to “goto” or move to another point, he’ll leave a pen trail behind him.

evan.begin_fill() – this tells evan to get ready to draw and paint as he moves around the screen.

evan.end_fill() – this tells evan to relax, because he’s done drawing and painting that particular part of the house.

Those are all the commands we’ll need to draw the various parts of the house. Let’s start with the main part of the house. We’ll move evan from (-100, 0) to (-100, 50) to (100, 50) to (100, 0) and back to (-100, 0) to form the red rectangle that makes up the bulk of the house. Here is the code, including the evan.penup() command we typed earlier:

evan.penup()
evan.goto(-100,0)
evan.fillcolor(“Red”)
evan.pendown()
evan.begin_fill()
evan.goto(-100,50)
evan.goto(100,50)
evan.goto(100,0)
evan.goto(-100,0)
evan.end_fill()

You can try running your code up to this point by choosing Run >> Run Module from Python’s main menu. You’ll see the blue background with a solid red rectangle drawn on it. If you don’t, check your code against the complete listing below very carefully to make sure you haven’t mistyped anything.

import turtle
scene = turtle.Screen()
scene.bgcolor(“SkyBlue”)
evan = turtle.Turtle()
evan.shape(“turtle”)
evan.color(“Red”)
evan.penup()
evan.goto(-100,0)
evan.fillcolor(“Red”)
evan.pendown()
evan.begin_fill()
evan.goto(-100,50)
evan.goto(100,50)
evan.goto(100,0)
evan.goto(-100,0)
evan.end_fill()

Remember that you have to type this exactly as shown here or it’s likely your code won’t work. Python is picky!

Once you’ve gotten the red base of the house to draw, let’s “raise the roof” by changing evan’s fill color to brown and tracing the roof shape from (-100, 50) to (0, 100) to (100, 50) and back to (-100, 50). Here is the code. Remember to pick evan’s pen up before moving to the starting point for the roof and to change his fill color to Brown.

evan.goto(-100,50)
evan.fillcolor(“Brown”)
evan.pendown()
evan.begin_fill()
evan.goto(0,100)
evan.goto(100,50)
evan.goto(-100,50)
evan.end_fill()

Run your program to make sure that the roof looks right.

Now let’s ad the orange door. The outline of the door stretches from (-40, 0) to (-40, 30) to (-20, 30) to (-20, 0) and back to (-40, 0). Tell evan to pick up his pen, move to the starting point for the door, change his fill color to orange, put his pen down, and then draw the door. Here is the code.

evan.penup()
evan.goto(-40,0)
evan.fillcolor(“Orange”)
evan.pendown()
evan.begin_fill()
evan.goto(-40,30)
evan.goto(-20,30)
evan.goto(-20,0)
evan.goto(-40,0)
evan.end_fill()

Last but not least, let’s draw the window. The window is white and stretches from (20, 20) to (20, 40) to (50, 40) to (50, 20) and back to (20, 20). Tell evan to pick his pen up, move to (20, 20), change his fill color to White, and then draw the window.

evan.penup()
evan.goto(20,20)
evan.fillcolor(“White”)
evan.pendown()
evan.begin_fill()
evan.goto(20,40)
evan.goto(50,40)
evan.goto(50,20)
evan.goto(20,20)
evan.end_fill()

Finally, so that it doesn’t look like our house has a turtle growing out of it, tell evan to hide:

evan.hideturtle()

And that’s it. If you run your code, you should see that magnificent red ranch-style mansion floating over a sky blue background. You can certainly add things to this, such as a grass field, trees and bushes, clouds, and a chimney. It’s your house, and evan is at your command.

In case you missed something, the full code for this appears at the end of this article.

Clearly, this is just an example. Now that you know the important commands and how to sketch out your design on graph paper, you can draw all sorts of things using Python and its Turtle Graphics.

This is real computer programming. It’s fun. It’s creative. And it’s what so many of today’s and tomorrow’s jobs require. If you like this kind of thing, I would encourage you to keep playing with it. Programming will take you places: great places that are even better than the house you just drew.

*******

As promised, here is the code in its entirety:

import turtle
scene = turtle.Screen()
scene.bgcolor(“SkyBlue”)
evan = turtle.Turtle()
evan.shape(“turtle”)
evan.color(“Red”)
evan.penup()
evan.goto(-100,0)
evan.fillcolor(“Red”)
evan.pendown()
evan.begin_fill()
evan.goto(-100,50)
evan.goto(100,50)
evan.goto(100,0)
evan.goto(-100,0)
evan.end_fill()
evan.penup()
evan.goto(-100,50)
evan.fillcolor(“Brown”)
evan.pendown()
evan.begin_fill()
evan.goto(0,100)
evan.goto(100,50)
evan.goto(-100,50)
evan.end_fill()
evan.penup()
evan.goto(-40,0)
evan.fillcolor(“Orange”)
evan.pendown()
evan.begin_fill()
evan.goto(-40,30)
evan.goto(-20,30)
evan.goto(-20,0)
evan.goto(-40,0)
evan.end_fill()
evan.penup()
evan.goto(20,20)
evan.fillcolor(“White”)
evan.pendown()
evan.begin_fill()
evan.goto(20,40)
evan.goto(50,40)
evan.goto(50,20)
evan.goto(20,20)
evan.end_fill()
evan.hideturtle()

About Ray Klump

Professor and chair of Mathematics and Computer Science Director, Master of Science in Information Security Lewis University http://online.lewisu.edu/ms-information-security.asp, http://online.lewisu.edu/resource/engineering-technology/articles.asp, http://cs.lewisu.edu. You can find him on Google+.

Leave a Reply

Your email address will not be published. Required fields are marked *