Advertisement

Robot Builder ( Python Project )

 Robot Builder


Creating graphics in Python is easy. Python’s turtle module lets you move a robot “turtle” around the screen, drawing pictures with a pen as it goes. In this project, you’ll program the turtle to build more robots  — or at least pictures of robots!



What happens

When you run the program, Python’s turtle sets off, scuttling around  the screen as it draws a friendly robot. Watch as it assembles the robot piece by piece, using different colors.


How it works

You’ll start by writing a function that draws rectangles. Then you’ll put the rectangles together to build the robot. You can change the size and color of the rectangles by altering the parameters you pass to the function. So you can have long, thin blocks for the legs, square ones for the eyes, and so on.


Robot Builder flowchart

The flowchart shows how the code for this project fits together. First the program sets the background color and how fast the turtle moves. Then it draws the robot one part at a time, starting from its feet and moving up to its head.




1     Create a new file

Open IDLE and create a new file. Save it as “robot_builder.py”.


2     Import the Turtle module

Type this line at the top of your program. The command import turtle as t lets you use functions from the turtle module without having to type “turtle” in full each time. It’s like calling someone whose name is Benjamin “Ben” for short.


3     Create a rectangle function     

Now make the function to draw the blocks that you’re going to use to build your robot. The function has three parameters: the length of the horizontal side; the length of the vertical side; and color. You’ll use a loop that draws one horizontal side and one vertical side each time it runs, and you’ll make it run twice. Put this rectangle function under the code you added in Step 2.



4     Set the background

Next get the turtle ready to start drawing, and set the background color of the window. You need the turtle to start with its pen up so that it doesn’t draw lines until you want it to. It will only begin to draw when it reaches the robot’s feet (Step 5). Type the following code under the code you added in Step 3. 


5     Draw the feet

You need to move the turtle to where you want to start drawing the first foot, and then use your rectangle function to draw it. You’ll need to do the same for the second foot. Type these lines under the code you added in Step 4, then run the program to see your robot’s feet appear.


6     Draw the legs

The next bit of the program makes the turtle move to where it will start drawing the legs. Type these lines under the code you added in Step 5. Now run the code again.


7     Draw the body

Type this code under the code you added in Step 6. Run the program and you should see the body appear.


8     Draw the arms

Each arm is drawn in two parts first the upper arm, from the robot’s shoulder to its elbow; then the lower arm, from the elbow to the wrist. Type this below the code you added in Step 7, then run it to see the arms appear.


9     Draw the neck

Time to give your robot a neck. Type these neck-drawing commands below the code you added in Step 8.


10    Draw the head

Oops—you’ve drawn a headless robot! To give your poor robot a head, type these commands below the code you added in Step 9.


11    Draw the eyes

Let’s add some eyes so that the robot can see where it’s going. To do this, you’ll draw a large white rectangle with two smaller squares inside it (for pupils). You don’t have to write a new function to draw squares, since a square is a rectangle with all its sides the same length. Insert these commands under the code you added in Step 10.


12    Draw the mouth

Now give the robot a mouth. Type these commands under the code you added in Step 11.


13    Hide the turtle

Finally, hide the turtle so it doesn’t look odd sitting on the robot’s face. Type this line after the code you added in Step 12. Run the program to see the whole robot being built.   



 


CODE


import turtle as t
def rectangle(horizontal, vertical, color):
    t.pendown()
    t.pensize(1)
    t.color(color)
    t.begin_fill()
    for counter in range(1, 3):
t.forward(horizontal)
t.right(90)
t.forward(vertical)
t.right(90)
    t.end_fill()
    t.penup()
t.penup()
t.speed('slow')
t.bgcolor('Dodger blue')


# feet
t.goto(–100, –150)
rectangle(50, 20, 'blue')
t.goto(–30, –150)
rectangle(50, 20, 'blue')


# legs
t.goto(–25, –50)
rectangle(15, 100, 'grey')
t.goto(–55, –50)
rectangle(–15, 100, 'grey')


# body
t.goto(–90, 100)
rectangle(100, 150, 'red')


# arms
t.goto(–150, 70)
rectangle(60, 15, 'grey')
t.goto(–150, 110)
rectangle(15, 40, 'grey')
t.goto(10, 70)
rectangle(60, 15, 'grey')
t.goto(55, 110)
rectangle(15, 40, 'grey')

# neck

t.goto(–50, 120)
rectangle(15, 20, 'grey')


# head
t.goto(–85, 170)
rectangle(80, 50, 'red')


# eyes
t.goto(–60, 160)
rectangle(30, 10, 'white')
t.goto(–55, 155)
rectangle(5, 5, 'black')
t.goto(–40, 155)
rectangle(5, 5, 'black')


# mouth
t.goto(–65, 135)
rectangle(40, 5, 'black')


t.hideturtle()

Post a Comment

0 Comments