Reputation: 117
I am trying to draw a turtle picture that has a forest, mountains, and a cloud floating across the top of the screen.
I cannot import any modules that need to be downloaded, it must be native.
To animate an object on top of a background in python turtle, I need to draw the object, then redraw the background, and then redraw the object in a new position. The problem is, the code that I use to draw the background causes the trees and mountains to be in randomized positions (to look more natural). My setup() functions draw a random background. What I want to know is how do I redraw that exact same background in my main()?
Here is the code that I am using:
# Elan Shetreat-Klein, 4/9/2019, Project 4
# A program to draw a landscape
from shapes import *
import random
import time
def background():
s.bgcolor('#87ceeb')
turtle.pencolor('#402905')
draw_rectangle(-500, -350, 1000, 200, c='#402905')
def tree(x, scale, leaf_color, trunk_color):
y = -350 + 200 * scale
draw_rectangle(x, y, 30 * scale, 200 * scale, c=trunk_color)
draw_tri(x + 15 * scale, y + 25 * scale, 175 * scale, c=leaf_color)
draw_tri(x + 15 * scale, y + 100 * scale, 150 * scale, c=leaf_color)
draw_tri(x + 15 * scale, y + 175 * scale, 100 * scale, c=leaf_color)
def forest():
dark_greens = ['#006400', '#008000', '#556B2F', '#00611C', '#003300', '#004F00']
light_greens = ['#397D02', '#476A34', '#308014', '#3D8B37', '#00CD00', '#0AC92B']
tree_locations = 3 * [x for x in range(-500, 600, 100)]
random.shuffle(tree_locations)
draw_rectangle(-500, -150, 1000, 200, c='#3B5323')
for xPos in tree_locations:
Xvary = random.randrange(-50, 50)
Svary = random.randrange(60, 80) / 100
tree(xPos + Xvary, Svary, dark_greens[random.randrange(0, 5)], '#603311')
for xPos in tree_locations:
Xvary = random.randrange(-40, 40)
Svary = random.randrange(70, 90) / 100
tree(xPos + Xvary, Svary, light_greens[random.randrange(0, 5)], '#734A12')
def mountain_range():
mountX = [x for x in range(-450, 550, 150)]
random.shuffle(mountX)
mountain_colors = ['#6F4242', '#856363', '#8B6969']
for xPos in mountX:
Xvary = random.randrange(-25, 25)
Svary = random.randrange(90, 110) / 100
x = xPos + Xvary
side = 600 * Svary
y = -350 + (sqrt((3 * side ** 2) / 4)) // 2
draw_tri(x, y, side, c=mountain_colors[mountX.index(xPos) % 3])
def create_cloud():
global cloud
cloud = {}
for i in range(20):
x2, y2 = random.randrange(-90, 90), random.randrange(-25, 25)
cloud[x2] = y2
print(cloud)
def draw_cloud(x, y):
turtle.pu()
turtle.goto(x, y)
turtle.dot(100, 'white')
for x2, y2 in cloud.items():
turtle.goto(x2 + x, y2 + y)
size = abs((1 - (turtle.distance(x, y)//100)) * 100)
turtle.dot(size, 'white')
turtle.speed(0)
s = turtle.getscreen()
def setup():
turtle.tracer(0, 0)
s.title("Forest with mountains in the backround")
s.bgcolor('white')
background()
mountain_range()
forest()
def main():
create_cloud()
for xPos in range(-500, 500, 1):
setup()
draw_cloud(xPos, 300)
turtle.update()
time.sleep(0.0001)
main()
turtle.done()
The randomization is fairly simple, it just changes the size and X Position a little bit for each tree and mountain. I already figured out how to keep the same cloud, that's not the problem.
In the code, a new random background is redrawn upon itself, and it starts to slow down. When I used turtle.clear() or turtle.reset(), the screen kept on blinking blue, so I'm not sure what to do about that.
Any help is appreciated!
Upvotes: 0
Views: 1612
Reputation: 41872
The difficulty you're in is due to using a single turtle. This can be done without completely redrawing the entire scene every time with some tricky monitoring of the undo buffer size -- undoing the drawing of just the cloud before redrawing it.
Or, it can be done more simply by allocating a second turtle just for drawing the cloud, and using clear()
and draw_cloud()
on that one turtle between updates, leaving the rest of the scenery intact. Below is your code reworked along these lines with other tweaks as well:
from shapes import *
from random import choice, randrange, shuffle
MOUNTAIN_COLORS = ['#6F4242', '#856363', '#8B6969']
DARK_GREENS = ['#006400', '#008000', '#556B2F', '#00611C', '#003300', '#004F00']
LIGHT_GREENS = ['#397D02', '#476A34', '#308014', '#3D8B37', '#00CD00', '#0AC92B']
def background():
screen.bgcolor('#87ceeb')
default_turtle.pencolor('#402905')
draw_rectangle(-500, -350, 1000, 200, c='#402905')
def tree(x, scale, leaf_color, trunk_color):
y = -350 + 200 * scale
draw_rectangle(x, y, 30 * scale, 200 * scale, c=trunk_color)
draw_tri(x + 15 * scale, y + 25 * scale, 175 * scale, c=leaf_color)
draw_tri(x + 15 * scale, y + 100 * scale, 150 * scale, c=leaf_color)
draw_tri(x + 15 * scale, y + 175 * scale, 100 * scale, c=leaf_color)
def forest():
tree_locations = 3 * [x for x in range(-500, 600, 100)]
shuffle(tree_locations)
draw_rectangle(-500, -150, 1000, 200, c='#3B5323')
for xPos in tree_locations:
Xvary = randrange(-50, 50)
Svary = randrange(60, 80) / 100
tree(xPos + Xvary, Svary, choice(DARK_GREENS), '#603311')
for xPos in tree_locations:
Xvary = randrange(-40, 40)
Svary = randrange(70, 90) / 100
tree(xPos + Xvary, Svary, choice(LIGHT_GREENS), '#734A12')
def mountain_range():
mountX = [x for x in range(-450, 550, 150)]
shuffle(mountX)
for xPos in mountX:
Xvary = randrange(-25, 25)
Svary = randrange(90, 110) / 100
x = xPos + Xvary
side = 600 * Svary
y = -350 + (sqrt((3 * side ** 2) / 4)) // 2
draw_tri(x, y, side, c=choice(MOUNTAIN_COLORS))
def create_cloud():
positions = []
for _ in range(20):
x, y = randrange(-90, 90), randrange(-25, 25)
positions.append((x, y))
return positions
def draw_cloud(x, y):
cloud_turtle.clear()
cloud_turtle.penup()
cloud_turtle.goto(x, y)
cloud_turtle.dot(100, 'white')
for x2, y2 in cloud:
cloud_turtle.goto(x2 + x, y2 + y)
size = abs((1 - (cloud_turtle.distance(x, y) // 100)) * 100)
cloud_turtle.dot(size, 'white')
def setup():
screen.title("Forest with mountains in the background")
background()
mountain_range()
forest()
def animate(xPos=-500):
draw_cloud(xPos, 300)
screen.update()
if xPos < 625: # have draw_cloud() return cloud width & replace this number
screen.ontimer(lambda x=xPos + 1: animate(x), 100)
else:
screen.tracer(True) # stop animation
screen = turtle.Screen()
screen.tracer(False)
default_turtle = turtle.getturtle()
default_turtle.hideturtle()
cloud_turtle = turtle.Turtle()
cloud_turtle.hideturtle()
cloud = create_cloud()
setup()
screen.update()
animate()
screen.exitonclick()
I changed the way your cloud is stored from a dictionary to a list of tuple positions as a dictionary isn't appropriate for this purpose.
Upvotes: 1
Reputation: 5560
There are several options: You could either make the random decisions before drawing and save all those decisions for redrawing or make the output of your random module deterministic by providing a seed. The second is the more hacky solution but requires less changes to your code.
Change the second line to: import random as really_random
Then, in your main loop, right before the call to setup()
add a line like: random = really_random.Random(0)
. You may need this line also before create_cloud() so you have a reference to random.
If you want to change your background every time you execute the program, change the 0 to some other random seed. For example, your main could look like this:
def main():
my_random_seed = really_random.randint(0,10000000)
random = really_random.Random(my_random_seed)
create_cloud()
for xPos in range(-500, 500, 1):
random = really_random.Random(my_random_seed)
setup()
draw_cloud(xPos, 300)
turtle.update()
time.sleep(0.0001)
Upvotes: 0