我正在尝试绘制一张乌龟图片,其中的森林,山脉和云朵漂浮在屏幕顶部。
我无法导入任何需要下载的模块,它必须是本地的。
要在python turtle中的背景上设置对象动画,我需要绘制对象,然后重新绘制背景,然后在新位置重新绘制对象。问题是,我用来绘制背景的代码使树木和山脉处于随机位置(看起来更自然)。我的setup()函数绘制随机背景。我想知道的是如何在main()中重新绘制完全相同的背景?
这是我正在使用的代码:
# 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()
随机化非常简单,它只是为每棵树和山改变了大小和X位置。我已经想出了如何保持相同的云,这不是问题。
在代码中,新的随机背景重绘在其自身上,并且开始变慢。当我使用turtle.clear()或turtle.reset()时,屏幕一直闪烁蓝色,因此我不确定该怎么办。
感谢您的帮助!
答案 0 :(得分:0)
有几种选择:您可以在绘制之前做出随机决定,然后保存所有这些决定以进行重新绘制,或者可以通过提供种子来确定随机模块的输出是否确定。第二个是更hacky的解决方案,但是需要更少的代码更改。
将第二行更改为:import random as really_random
然后在您的主循环中,在调用setup()
之前,添加如下一行:random = really_random.Random(0)
。您可能在create_cloud()之前也需要此行,因此您可以引用random。
如果您想在每次执行程序时更改背景,请将0更改为其他随机种子。例如,您的主体可能看起来像这样:
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)
答案 1 :(得分:0)
您遇到的困难是由于使用一只乌龟。无需每次都对重绘缓冲区大小进行一些棘手的监视,就不必每次都完全重绘整个整个场景-在重绘之前仅重绘云的绘制即可。
或者,可以更简单地做到这一点,即分配第二只乌龟用于绘制云,并在两次更新之间对该一只乌龟使用clear()
和draw_cloud()
,而其余的场景则保持不变。下面是根据这些代码以及其他调整对代码进行重新处理的方法:
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()
我将您的云存储方式从字典更改为元组位置列表,因为字典不适用于此目的。