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
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()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started