首页 > 编程语言 >实验6 turtle绘图与python库应用编程体验

实验6 turtle绘图与python库应用编程体验

时间:2023-06-11 17:13:20浏览次数:56  
标签:turtle moveto python range 绘图 import main def

task1_1

代码:

from turtle import *

def move(x, y):
    '''画笔移动到坐标(x,y)处'''
    penup()
    goto(x, y)
    pendown()

def draw(n, size = 100):
    '''绘制边长为size的正n变形'''
    for i in range(n):
        fd(size)
        left(360/n)

def main():
    pensize(2)
    pencolor('red')

    move(-200, 0)
    draw(3)

    move(0, 0)
    draw(4)

    move(200, 0)
    draw(5)

运行截图:

 

task1_2

代码:

from turtle import *

def moveto(x, y):
    '''把画笔移动至坐标(x, y)处'''
    penup()
    goto(x, y)
    pendown()

def main():
    pensize(2)
    pencolor('blue')

    moveto(-150, 0)
    circle(50)

    moveto(0, 0)
    circle(50, steps=4)

    moveto(150, 0)
    circle(50, steps=5)

    moveto(300, 0)
    circle(50, steps=6)

    hideturtle()
    done()

main()

运行截图:

 

task2_1

代码:

'''以(0,0)为圆心,绘制9个同心圆'''

from turtle import *

def moveto(x, y):
    '''把画笔移至坐标(x,y)处'''
    penup()
    goto(x, y)
    pendown()

def main():
    setup(800, 600)

    radius = 20  # 圆初始半径
    offset = 20  # 同心圆每次位移量

    for i in range(9):
        moveto(0, -radius)
        circle(radius)
        radius += offset

    hideturtle()
    done()

main()

运行截图:

 

task2_2

代码:

'''以(0, 0)为圆心,绘制9个彩色同心圆,色彩随机生成'''

from turtle import *
from random import random

def moveto(x, y):
    '''把画笔移至坐标(x,y)处'''
    penup()
    goto(x, y)
    pendown()

def gen_color():
    '''生成一个以rgb三元组表示的颜色值并返回'''
    return tuple((random()for i in range(3)))

def main():
    setup(800, 600)

    radius = 180  # 圆初始半径
    offset = 20  # 同心圆每次位移量

    for i in range(8):
        moveto(0, -radius)
        color(gen_color())

        begin_fill()
        circle(radius)
        end_fill()

        radius -= offset

    hideturtle()
    done()

main()

运行截图

 

 

task3_1

代码:

from turtle import *

def square(size = 50, rgb = 'orange'):
    '''绘制正方形
    :para: size:int 指定边长 (如未指定参数,使用默认值)
    :para: c: str 指定画笔颜色值 (如未指定参数,使用默认值)
    '''

    pencolor(rgb)
    for i in range(4):
        fd(size)
        left(90)

def main():
    setup(800, 600)
    speed(0)
    n = 10
    for i in range(n):
        square(80)
        left(360/n)

    hideturtle()
    done()
main()

运行截图:

 

task3_2

代码:

from turtle import *
setup(800, 600)
pencolor('pink')

n = 10
for i in range(10):
# 绘制一片花瓣
    for j in range(2):
        circle(80, 90)
        left(90)
    right(360/n)

hideturtle()
done()

运行截图:

task4

代码:

from turtle import *
setup(800, 600)
bgcolor('black')
pencolor('white')
speed(0)

angle = 0
size = 2

n = 5 # 螺旋n边形
count = 50 # 循环次数
for i in range(count):
    fd(size)
    angle += 360/n
    seth(angle)
    size += 5

hideturtle()
done()

运行截图;

 

task5_1

代码:

from turtle import *
def move(x, y):
    penup()
    goto(x, y)
    pendown()

def main(c, n, l):
    color(c)
    begin_fill()
    for i in range(n):
        fd(l)
        left(360 / n)
    end_fill()


move(-100, -100)
main('black', 4, 200)

move(-100, 0)
right(45)
main('red', 4, 100 * (2 ** 0.5))

hideturtle()
done()

运行截图:

task5_2

代码:

from turtle import *
color('blue')
pensize(2)
r = 20
for i in range(5):
    r += 20
    forward(r)
    left(90)
    circle(r, 90)
    left(90)
    forward(r)
    left(90)
    forward(40)
    goto(0, 0)
hideturtle()
done()

运行截图:

 

task6

代码:

color('yellow')
begin_fill()
left(135)
fd(200)
right(90)
fd(200)
left(90)
circle(200, 270)
end_fill()

moveto(80, 70)
dot(40, 'black')
moveto(72, 76)
dot(10, 'white')

hideturtle()
done()

运行截图:

 

标签:turtle,moveto,python,range,绘图,import,main,def
From: https://www.cnblogs.com/Hj-soo320/p/17473193.html

相关文章

  • Python中的logging模块
    官方文档基本用法下面的代码展示了logging最基本的用法。#-*-coding:utf-8-*-importloggingimportsys#获取logger实例,如果参数为空则返回rootloggerlogger=logging.getLogger("AppName")#指定logger输出格式formatter=logging.Formatter('%(ascti......
  • Centos 7.4+ 通过anaconda 安装Python3.10
    做记录,在centos里安装3.10版本时,老是报错ssl。或者一些其他问题,做个记录吧。大概用了2天才弄好,主业不是运维所以不太了解在https://www.anaconda.com/官网下载安装,此处自己根据系统、根据版本,自己安装下载地址:https://www.anaconda.com/download#downloads安装好后condai......
  • 实验6 turtle绘图与python库应用编程体验
    实验任务1task1_1.py程序源码:1fromturtleimport*23defmove(x,y):#画笔移动到坐标(x,y)处4penup()5goto(x,y)6pendown()78defdraw(n,size=100):#绘制边长为size的正n变形9foriinrange(n):10forward(size)11......
  • python: Decorators
      #装饰器defprintpy(func):definner_func():func()print("hellopython!GeovinDu")returninner_func#@装饰器@printpydefprinthello():print("helloworld!")#调用printhello()'''De......
  • Python modbus_tk 库源码分析
    modbus_tk源代码分析前言modbus_tcp协议是工业项目中常见的一种基于TCP/IP协议的设备数据交互协议。作为TCP/IP协议的上层协议,modbus_tcp协议涉及到两个概念:client和server。但更标准的叫法应该是master和slave。Slave:TCP/IP协议中的server方Master:TCP/IP协......
  • 实验六 turtle绘图与python库应用编程体验
    1fromturtleimport*234defmove(x,y):5penup()6goto(x,y)7pendown()8910defdraw(n,size=100):11foriinrange(n):12fd(size)13left(360/n)141516defmain():17pensize(2)18pen......
  • python: generator
    a=10defaddgoled():globalaa+=1dd=[{'name':'zhang','age':10},{'name':'tu','age':13}]d={'name':'zhang','age':10}defadddict():globald#......
  • 实验6 turtle绘图与python库应用编程体验
    task1_11fromturtleimport*234defmove(x,y):5penup()6goto(x,y)7pendown()8910defdraw(n,size=100):11foriinrange(n):12fd(size)13left(360/n)141516defmain():17pensize(2)18......
  • Linux下安装python3.7.9
    操作系统与原有python[pythondemo@localhost~]$cat/etc/centos-releaseCentOSLinuxrelease7.4.1708(Core)[pythondemo@localhost~]$pythonPython2.7.5(default,Aug42017,00:39:18)[GCC4.8.520150623(RedHat4.8.5-16)]onlinux2Type"help",......
  • python第一次cli程序的坑
    错误一:appacheerror记录到错误:AH01215:(13)Permissiondenied:execof'/var/www/cgi-bin/cli_test.py'failed1.对应程序加上执行权限2.selinux=disable3.指定安全上下文 chcon-R-thttpd_sys_content_t/var/www/cgi-bin这三个方法可以试一下错误二:malformedheade......