Draw simple shapes using python turtle graphics objects

Assignment Help Programming Languages
Reference no: EM133116585

ICT112 Programming Fundamentals - University of the Sunshine Coast

Theme: Training Turtles

Learning Objective 1: use variables to store values and update those values;

Learning Objective 2: draw simple shapes using Python 'Turtle Graphics' objects;

Learning Objective 3: use for loops to repeat a block of code a given number of times.

Training a Turtle

One of the fun things about the Python language is that one of its standard libraries is the 'Turtle Graphics' module. Turtles are a great visual way of learning about Python objects and methods, and practicing the problem-solving skills that are crucial to learning programming.

If you are using your own computer and haven't already you'll need to install VS Code and the Python libraries. If you are in the labs, please open VS Code and create a new python file.

Note: you do not need to type in the 'comments' (everything from the '#' character to the end of the line). Python ignores everything after a '#', so you can put comments in your code, but they just for human readers, to explain what is going on, and why the code is written that way.

You should see an output window pop up at this point. Drag it over to the right side of your screen so that it does not overlap your code window.

Then type in the next few lines and watch what alex (the 'turtle') does. and a turtle object that we call 'alex' (short for 'alexander' or 'alexandra'? It is difficult to know the sex of turtles!

Then we give 'alex' a few instructions on how to move, and as he/she moves along, a line is drawn in the imaginary sand.

You can find more explanation of this program and more details about turtles in Chapter 4 ('Python Turtle Graphics') of our online textbook:
Make sure you read that chapter sometime this week, if you are reading that one. Another useful source of reference documentation about turtles is the online Python documentation:

1.1 Some Simple Polygons

Here is a series of simple shapes to draw, to learn how to control the turtle and figure out how the angles work. After you complete each task, add an explanatory comment line above your code to explain what it does (for example: # draws a square). Then start drawing the next shape, by copying and pasting any bits of code that you need to reuse.

Extend the above program so that it draws the whole rectangle, 50 pixels wide and 30 pixels high.

1. Modify the rectangle code so that it draws a square, 100 by 100 pixels.

2. Add some extracode to draw an equilateral triangle, with three sides, each 100 pixels long.

3. Also draw a hexagon that has six sides, each 100 pixels long. Hmm, this hexagon code is getting to be a bit long and repetitive. Imagine how long our program would be if we wanted to draw a shape with 20 sides! Can you use a for loop to make your hexagon code shorter? Hint: for side inrange(6): ... (Make sure that all the code you want to repeat inside the loop is indented by four spaces.)

4. Define a variable: size = 90. Then change your hexagon code so that it uses your sizevariable for the length of each side. Rerun your hexagon code.

5. Change your sizevariable to be 180. Then rerun your hexagon code. Does it look larger?

1.2 Making it colourful
Let's make our shapes picture look a bit more colourful.We can call alex.pencolor("red") to set the colour of the pen to red - note the American spelling of 'color' in the method name.

Okay, now beautify your three shapes as follows:
• Change your square so that it is filled in with a nice light colour. Hint: ask alex to begin_fill() before you start drawing the square, and end_fill() after you finish drawing the square. The end_fill()method will paint the interior of the shape that you have just drawn. You can call alex.fillcolor("color name") to set the fill color any time before you call end_fill().
• Change your triangle so that it is also filled in, but choose a darkeror complementary colour that fits in nicely with the colour of your square.
• Change your hexagon so that it is drawn with dark, bold lines, 5 pixels wide. It should not be filled, just an outline shape. Choose a darkcolour for the lines, so that the hexagon creates a nice border for your picture.
Your final picture should look something like this (but with your colours):

2 Going a bit loopy
For this final part of the lab, we are going to use Python for loops to draw some slightly more interesting and complex pictures.

2.1 From Circles to Spirographs
Now, copy and paste the following example code into your program:

You will find that this draws a circle. But rather slowly, because it takes 360 tiny left turns to get around the whole circle! Try reducing the 360 iterations to just 36, and increase both 1's to 10, and it will draw more quickly, but still look quite close to a circle.
To make a more interesting picture, copy the code for your triangle and paste it inside this circle loop (just after the alex.left(10) line). Make sure that your triangle code is indented the same amount as the preceding lines, so that all the code inside the loop is at the same level. This is how Python knows which lines of code are inside the loop. Then run your program again and see what you see.

2.2 Spiralling Around
Experiment with varying the length of the forward(10) method call at the top of the loop. Also try changing it to forward(10+i).
Then can you change your program so it draws a spiral of triangles like the ones shown here?
Or get creative, and see if you can draw some more interesting spiral shapes!

2.3 The Drunk Pirate
A drunk pirate makes a random turn and then takes 100 steps forward, makes another random turn, takes another 100 steps, turns another random amount, etc. A social science student records the angle of each turn before the next 100 steps are taken. Her experimental data is:
angles = [160, -43, 270, -97, -43, 200, -940, 17, -86].
(Positive angles are counter-clockwise.)
Write a turtle graphics program, which uses your turtle to draw the path taken by our drunken friend. Make his path wide and red.
Hint: to loop through the angles, after you have added the above definition of the list of angles, you can use: for a in angles:
[Extension] A random drunk pirate?
Instead of following the same (historical) path of the same drunk pirate every time your program runs, can we model a drunk pirate who takes a different (and random) path each time? Yes, Python has a ‘random' module that makes it easy to do this.

Can you integrate this idea of 'random angles' into your program, to simulate a real drunk pirate?
Can you make him/her turn right as well as left randomly?

Attachment:- Programming Fundamentals.rar

 

 

Reference no: EM133116585

Questions Cloud

Risk premium and annual risk premium of siri : Suppose you have calculated the average monthly returns of the Fama-French-Carhart (FFC) portfolios over the 1926-2019 period. You have also estimated the facto
Discuss team dynamics for highly effective : Discuss the team dynamics for a highly effective (or ineffective) team of which you were a member.
Discuss relationship between Purchasing - Logistic function : Discuss the relationship between the Purchasing/Contracting function and the Logistics function. How might their goals differ
Common-size statement of financial position oleary : Prepare the common-size, and common-base year for O'leary Industrial Supply's Statement of Financial Position using the spreadsheets below. Round percentages to
Draw simple shapes using python turtle graphics objects : Use variables to store values and update those values and draw simple shapes using Python 'Turtle Graphics' objects - repeat a block of code a given number
Significant long-term impacts : Financial markets have experienced many changes during the last two decades. In you opinion, what types of changes have the most significant long-term impacts o
Calculate the new contribution margin ratio : Calculate the new contribution margin ratio, breakeven point in pesos and operating profit under each of the changes - Unit sales price increases by 15%
Excessive increases in healthcare costs : For this week's discussion, let's do a little survey. Which of the following entities do you judge to be most responsible for excessive increases in healthcare
Pros and cons of federal privacy regulation : What are the pros and cons of a federal privacy regulation and which state policies should be considered as the blueprint for this and why?

Reviews

Write a Review

Programming Languages Questions & Answers

  Design the program so that it automatically counts

Label all output, and design the program so that it automatically counts the respondents.

  Write program which trace execution of RSA cryptosystem

You have to write a program (in the language of your choice) which will trace the execution of the RSA cryptosystem on a simple example

  Write program to calculate student-s quiz average

Write a program that will calculate a student's quiz average. The program should prompt the user for the number of quizzes and then ask the user for each quiz grade.

  Develop an android application

Develop an android application that gathers local weather information and displays it to the user. The application must also demonstrate user input in some form

  Creating form which analyzes poker hand entered by user

To observe behavior of Sub Procedures, draw a Form which analyzes a poker hand entered by user and displays the type of hand.

  Find the error in the given pseudocode

If the following pseudocode were an actual program, why would it not display the output that the programmer expects? Declare String favoriteFood Display "What is the name of your favorite food?" Input favoriteFood Display "Your favorite food is " ..

  Create a system for a simple library

Write a console application to meet the following requirements. Create a system for a simple library. The library has a name and a list of books. Each book has a title, author and an int as the id number.

  Implement an agent that plays the max-connect game

The task in this programming assignment is to implement an agent that plays the Max-Connect4 game using search. Figure 1 shows the first few moves of a game. The game is played on a 6x7 grid, with six rows and seven columns

  Identify all the pure-strategy nash equilibria of game

Identify all of the pure-strategy Nash Equilibria of this game. Identify at least one mixed-strategy Nash Equilibrium, and show that it is a Nash Equilibrium.

  Relational database schema design

Build a searching interface that has term form to browse RNA/DNA sequence/FASTA sequence - create an interface that allows the user to interrogate it

  Calculate the average of a variable quantity of integers

Create a simple command line program to calculate the average of a variable quantity of integers that are passed as an argument when the program is called.

  How to write a constructor

I'm trying to draw a diagram for the following problem statement. I need to create the function in JavaScript and call it in an HTML file.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd