社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

我需要使用循环编程构造创建一个马赛克,使用python创建16个相同图案和三种或更多不同颜色的瓷砖

Nath • 4 年前 • 2159 次点击  

在我的作业中,我必须使用turtle创建一个形状,然后将该形状转化为一个马赛克,其中16块瓷砖的图案为4*4。我创造了乌龟的形状,但我做的每一次拼图都以错误或没有输出而告终。这是我在这个项目中使用的乌龟形状。我如何使用这个形状创建马赛克?

import turtle
wn = turtle.Screen()
wn.bgcolor('white')
wn.setup(500,500)
bob = turtle.Turtle()
bob.speed(10)
bob.pensize(2)
bob.color('purple')
for i in range(20):
    bob.left(140)
    bob.forward(100)
bob.hideturtle()
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/133553
文章 [ 1 ]  |  最新文章 4 年前
Eric Jin
Reply   •   1 楼
Eric Jin    4 年前

“无输出”可能意味着窗口正在自动关闭。尝试将此添加到末尾以防止:

# ...
bob.hideturtle()
input('press enter to exit')

你可以通过传送到你想要绘制每个形状的地点,多次绘制相同的形状。

def shape():
    for i in range(18):
        bob.left(140)
        bob.forward(100)

# coordinates of each of the shapes
# these are completely arbitrary
# you can change these to whatever (adjust spacing, change where they are, etc)
# you can also write this using ranges if you want
for x in (-100, 0, 100, 200):
    for y in (-150, -50, 50, 150):
        bob.penup()
        bob.setposition(x, y)
        bob.pendown()
        shape()

这将循环通过所有16个点, -100, -150 , -100, -50 , -100, 50 , ..., 200, 150 .

请注意,我将您的形状更改为仅循环18次-这使总旋转为360度的倍数,因此下一个形状不会倾斜。此外,该形状只有18条边,因此绘制额外的2条边将是一种浪费。 This is what would happen if it was left at 20.


输出:

enter image description here