If the version of your code on github does not run, it is considered non-functioning. If it does not run on your personal computer WITHOUT CHANGES, it is considered non-functioning.
WARNING: Graders will not grade any part of your code that does not execute.
TOTAL: 20 points
forward()
must advance a turtle by independent random valuesIn this lab, you will learn to:
Before we begin writing code for this lab, we need to introduce Python modules. Python calls python files: modules. Modules allow you to include code someone else wrote in your program (and it’s not even cheating). For example, the random
module contains code to generate random numbers. You can include any python source code file with the import
statement. It’s easy to use:
import random
x = random.randrange(1,10)
print(x)
The randrange
function, as called in the example above, generates a random number from 1 to 9. Even though we said 10, the randrange
function goes up to, but does not include, the upper limit value (just like range()
). If you ran the program over and over again you would see that each time you run it python generates a different number. Random numbers are the basis of all kinds of interesting programs we can write, and the randrange
function is just one of many functions available in the random
module.
In this lab we are going to work step by step through the problem of racing turtles. The idea is that we want to create two turtles and have them race across the screen from left to right. The turtle that goes the farthest is the winner.
To start thinking about a solution to the simplest form of the problem, we need a race between two turtles. When you are faced with a problem like this in computer science it is often a good idea to find a solution to a simple problem first and then figure out how to make the solution better.
Here is the sequence of steps that we will need to accomplish:
If you don't already have the code in your main.py
in your ch02 lab, copy and paste the following into the file, overwriting anything that is already there:
import turtle # 1. import modules
import random
# Part A
window = turtle.Screen() # 2. Create a screen
window.bgcolor("lightblue")
michelangelo = turtle.Turtle() # 3. Create two turtles
leonardo = turtle.Turtle()
michelangelo.color("orange")
leonardo.color("blue")
michelangelo.shape("turtle")
leonardo.shape("turtle")
michelangelo.up() # 4. Pick up the pen so we don’t get lines
leonardo.up()
michelangelo.goto(-100, 20)
leonardo.goto(-100, -20
Mac OS/Linux: If you get a message about Tk or Tkinter not being installed or deprecated, you can install the latest version using this guide: https://www.pythonguis.com/installation/install-tkinter-mac/
You can review the documentation for the turtle library, with its list of commands, here. Although you may use anything available to you in the turtle library, you should only need the following:
myturtle.forward(20)
Let's write 2 versions of the race to see the different kinds of behavior (make sure to keep both in your code for grading).
forward
for each turtle object, using a random number between 1-100 as the distance to move.Test your code to make sure it is working. Once you know it is, add a few blank lines and code for:
Make a call to forward()
for each turtle inside a single for
loop.
random.randrange()
to get a value between 1-10 to determine the turtle's movement amount.A new random value should be generated for each separate call to forward.
Reset the turtles to their respective starting positions once you are done with race 2.
Make sure to add:
window.exitonclick()
at the end of part A.
So, which of these programs is better? Which of these programs is most correct?
For the next part of the lab, you are going to need to import 2 additional modules:
pygame
math
POTENTIAL BUG FIX FOR PYGAME MISSING WINDOW
There is an issue with the current version of Pygame that seems to mostly affect Mac OS. If you find the Pygame window isn't opening, but there are no other errors, you can add the following and indent all your code underneath (just like you do for thefor loop
).
while True:
for event in pygame.event.get():
pass
## Your code goes below here with no changes other than making sure it is indented one level ##
We will eventually come to understand all of the above code, but for now, you may need the boilerplate in place just to get the pygame window to display.
In the same file, you will use the pygame.draw.polygon function to draw the following list of regular polygons:
Here is a quick review of how to setup and initialize pygame:
import pygame
pygame.init()
window = pygame.display.set_mode()
In order to complete this part of the lab you will need to look over the documentation for the pygame.draw.polygon function in pygame. Below are some hints about the arguments:
pygame.display.set_mode()
You will need 4 variables for data:
You can generate the list of points for any regular sided polygon using the [following algorithm]:
angle = 360/num_sides
math.cos()
and math.sin()
must be in radians. So we must convert degrees to radians using math.radians()
:radians = math.radians(angle * i)
x
coordinate using radians
and add your xpos
offset:x = xpos + side_length * math.cos(radians)
y
coordinate using radians
and add your ypos
offset:y = ypos + side_length * math.sin(radians)
[x,y]
to the points
listpoints
list. Use the points
list as the argument to the pygame.draw.polygon()
function.
pygame.display.flip()
You have to call the flip()
function to update the screen after drawing each new shapeOtherwise, they will draw so fast we won’t be able to see them
window.fill(<Your choice of background color>)
pygame.display.flip()
make sure you also clear out the
points
list:points = []
Use pygame and the above algorithm to draw each of the following shapes on screen:
ch02/
lab/
main.py
Complete your submission by making a commit and push using either the Source Control tab in VSCode or the command line in the shell:
REMINDER: Submission instructions from Lab 0
Go to your repo on Github to verify your changes were pushed. If your changes are there, that's it! You've completed the work for this lab.