Python 2.7 and Turtle graphics I have the following code running okay. It\'s pur
ID: 3816084 • Letter: P
Question
Python 2.7 and Turtle graphics
I have the following code running okay. It's purpose is to arrange blocks (or circles in my case) according to a set or locations and orientations. The blocks with the X in the arrangments list are missing.
However we are now being told that if the missing block was meant to be on the bottom of the screen, and there is one above it, the one above it must "fall" down and take it's place. I think I have an idea about how to do it if I make each line of the arrangement list a seperate variable so I can compare each variable to find out which blocks have missing ones below them and then change it's location to lower it. But I am not sure how to do this. Could someone please provide the code needed to do this step?
from turtle import *
## Some example arrangements
arrangement_60 = [['Block B', 'Bottom right', 'Left', 'X'],
['Block D', 'Bottom left', 'Left', 'O'],
['Block C', 'Top right', 'Down', 'O']]
arrangement_91 = [['Block D', 'Bottom right', 'Up', 'X'],
['Block C', 'Bottom left', 'Up', 'X'],
['Block A', 'Top left', 'Up', 'O'],
['Block B', 'Top right', 'Up', 'O']]
arrangement_92 = [['Block D', 'Bottom right', 'Up', 'X'],
['Block B', 'Top right', 'Up', 'O'],
['Block C', 'Bottom left', 'Up', 'O'],
['Block A', 'Top left', 'Up', 'O']]
## Save each line of the arrangement list as a variable somehow
## If one variable has 'X' at line[3] AND says 'Bottom right' at line[1],
# and another variable contains an 'O' at line[3] AND 'Top right' at
## line[1] then change that second variable's line[1] to 'Bottom right'
## Do the same for the left side
## Define the function that uses the block arrangements
def stack_blocks(arrangement):
for line in arrangement:
## Do not draw the block if the arrangement line contains an 'X'
if line[3] == 'X':
line[0] = 'not_drawn'
## Set the center locations for each block
if line[1] == 'Bottom left':
penup()
goto(-125, 125)
if line[1] == 'Bottom right':
penup()
goto(125, 125)
if line[1] == 'Top left':
penup()
goto(-125, 375)
if line[1] == 'Top right':
penup()
goto(125, 375)
## Set the headings for each block
if line[2] == 'Up':
setheading(180)
if line[2] == 'Right':
setheading(90)
if line[2] == 'Down':
setheading(0)
if line[2] == 'Left':
setheading(270)
## Draw each block
if line[0] == 'Block A':
pendown()
color('red')
circle(50)
if line[0] == 'Block B':
pendown()
color('yellow')
circle(50)
if line[0] == 'Block C':
pendown()
color('blue')
circle(50)
if line[0] == 'Block D':
pendown()
color('green')
circle(50)
## Call the function to stack the blocks
stack_blocks(arrangement_92)
hideturtle()
done()
Explanation / Answer
import turtle
class MyTurtleTest(turtle.Turtle):
""""""
def __init__(self):
"""MyTurtleTest Constructor"""
turtle.Turtle.__init__(self, shape="turtle")
def drawCircle(self, x, y, radius=50):
"""
Moves the turtle to the correct position and draws a circle
"""
self.penup()
self.setposition(x, y)
self.pendown()
self.circle(radius)
def drawOlympicSymbol(self):
"""
set of positions to draw the Olympics logo
"""
positions = [(0, 0), (-100, 0), (20,40),
(-60, 60), (-120, 40)]
for position in positions:
self.drawCircle(position[0], position[1])
if __name__ == "__main__":
t = MyTurtleTest()
t.drawOlympicSymbol()
turtle.getscreen()._root.mainloop()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.