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

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

时间:2023-06-08 22:12:31浏览次数:33  
标签:python hideturtle range 实验 import 绘图 circle def 6turtle

实验任务1

test1

实验代码

from turtle import *
def move(x, y):
    penup()
    goto(x, y)
    pendown()
def draw(n, size = 100):
    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)
    hideturtle()
    done()
main()

运行结果

test2

实验代码

from turtle import *
def moveto(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()

运行结果

 

实验任务2

test1

实验代码

from turtle import *
def moveto(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()

运行结果

 test2


实验代码

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

def gen_color():
    return tuple((random() for i in range(3)))
def main():
    setup(800,600)
    radius = 20 # 圆初始半径
    offset = 20 # 同心圆每次位移量
    for i in range(9):
        moveto(0, -radius)
        color(gen_color())
        circle(radius)
        radius += offset
    hideturtle()
    done()
main()

实验结果

 实验任务3

test1

实验代码

from turtle import *
def square(size = 50, rgb = 'pink'):

    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()

实验结果

test2

实验代码

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()

实验结果

 实验任务4

实验代码

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()

实验结果

 实验任务5

test1

实验代码

from turtle import *

right(45)
penup()
goto(-100,-100)
pendown()
begin_fill()
pencolor('black')
fillcolor('black')
circle(100*2**0.5,steps = 4)
end_fill()

left(45)
penup()
goto(0,-100)
pendown()
pensize(2)
pencolor('red')
begin_fill()
fillcolor('red')
circle(100,steps = 4)
end_fill()

hideturtle()
done()

实验结果

 test2

实验结果

from turtle import *

pensize(2)
pencolor('blue')
for i in range(5):
    setheading(90)
    r = 40+20*i
    goto(r,0)
    circle(r,extent = 90)
    goto(0,0)
hideturtle()
done()

运行结果

 实验任务6

from turtle import *
from random import random

setup(800,600)
def gen_color():
    return tuple((random() for i in range(3)))
def rectangle():
    color(gen_color())
    begin_fill()
    for i in range(2):
        forward(5)
        left(90)
        forward(20)
        left(90)
    end_fill()
penup()
goto(-10*5,0)
pendown()
for k in range(20):
    rectangle()
    forward(5)

hideturtle()
done()

实验结果

 

标签:python,hideturtle,range,实验,import,绘图,circle,def,6turtle
From: https://www.cnblogs.com/menghuanhu/p/17467810.html

相关文章

  • Python取整及四舍五入
    向上取整:math.ceil()importmathmath.ceil(-0.9)>>>0math.ceil(0.3)>>>1向下取整:math.floor()、int()、//(整除)math.floor(-0.3)>>>-1int(0.9)>>>03//2#1.5>>>1虚假的四舍五入:round()""&quo......
  • 14dayPythonTask7-类与对象+魔法函数
    目录类与对象1.对象=属性+方法2.self是什么?3.Python的魔法方法4.公有和私有5.继承6.组合7.类、类对象和实例对象8.什么是绑定?9.一些相关的内置函数(BIF)练习题魔法方法1.基本的魔法方法2.算术运算符3.反算术运算符4.增量赋值运算符5.一元运算符6.属性访问7.描......
  • 【python】lambda
    lambdalambda是匿名函数,也就是没有名字的函数。lambda的语法非常简单:下面是一个lambda表达式的简单例子,我们可以把lambda表达式赋值给一个变量,然后通过这个变量来使用它:>>>my_sum=lambdax,y:x+y>>>my_sum(1,2)3lambda默认参数详解语法lambda[parameter_list,p......
  • python常用函数(zip,map,filter,reduce)
    一、zip它是Python的内建函数,(与序列有关的内建函数有:sorted()、reversed()、enumerate()、zip()),其中sorted()和zip()返回一个序列(列表)对象,reversed()、enumerate()返回一个迭代器(类似序列)>>>name=('jack','man','sony','pcky')>>>age=(2001,2003,2005,......
  • python 中输出匹配字符串及其下一行
     001、[root@PC1test05]#lsa.txttest.py[root@PC1test05]#cata.txt##测试数据3333gene9999kkkkgene77778888genegene00006666[root@PC1test05]#cattest.py##测试程序#!/usr/bin/envpython#-*-coding:utf-8-*-in_file=open("a.tx......
  • python - sqlite查询表名 列名
    最近在看到一个之前做的sqlite数据库时发现忘了table名叫什么了,所以找了找发现可以直接用python查询,记录一下importsqlite3conn=sqlite3.connect('test.db')cur=conn.cursor()sql="select*fromsqlite_masterwheretype='table'"cur.execute(sql)print(cur.fetcha......
  • python中调整字符串的宽度,设置填充值
     001、>>>str1="ab"##测试字符串>>>str1'ab'>>>str1.ljust(10)##调整宽度为10,左侧对齐,默认用空格填充'ab'>>>str1.ljust(10,"+")##设置用+号填充'ab......
  • python 中 将列表中的数值转换为字符串
     001、>>>list1=[111,222,333]>>>list1[111,222,333]>>>list1=[str(i)foriinlist1]##将列表中数值转换为字符串>>>list1['111','222','333'] ......
  • 【python】多文件编程
    多文件编程在Python中,可以将代码拆分成多个文件进行编程,这样有助于组织和维护大型程序。以下是一些常用的方法:模块导入:将代码划分成几个逻辑上相似的文件,然后使用import语句导入需要使用的模块。包:将相关的模块放到同一个文件夹内,并加上一个名为__init__.py的空文件,这个文件会......
  • python opencv addWeighted
    pythonopencvaddWeighted importcv2#Loadtheimageimg=cv2.imread('20230222100736979.jpg')#Adjustthebrightnessbrightness=50adjusted=cv2.addWeighted(img,1,img,0,brightness)#Displaytheoriginalandadjustedimagescv2.i......