def selection_sort(list1): turtle.penup() turtle.hideturtle() turtle.sety(200) t1 = turtle.Turtle() t2 = turtle.Turtle() t1.penup() t2.penup() turtle.isvisible() turtle.left(90) t1.left(90) t2.left(90) for i in range(len(list1)): for j in range(len(list1)): if (list1[j] > list1[i]): list1[i], list1[j] = list1[j], list1[i] ty = turtle.ycor() - 20 t1.setx(i * 18 + 2) t1.sety(ty) t2.sety(ty) turtle.sety(ty) t2.setx(j * 18 + 2) turtle.write(list1, font='24') turtle.delay(50) return list1
def draw(x, y, z, vanishx, vanishy): voxel = Voxel((x * 10), (y * 10), z, vanishx, vanishy) if turtle.isvisible(): turtle.hideturtle() def shift(point, towards, depth): return point - (towards * depth) x0 = shift(voxel.x, vanishx, voxel.z) y0 = shift(voxel.y, vanishy, voxel.z) x1 = shift(voxel.x, vanishx, (voxel.z + 1)) y1 = shift(voxel.y, vanishy, (voxel.z + 1)) turtle.penup() turtle.goto(x0, y0) turtle.pencolor("#00FF47") turtle.pendown() turtle.goto((x0 + 10), y0) turtle.goto((x0 + 10), (y0 + 10)) turtle.goto(x0, (y0 + 10)) turtle.goto(x0, y0) turtle.goto(x1, y1) turtle.goto(x1, (y1 + 10)) turtle.goto(x0, (y0 + 10)) turtle.goto(x1, (y1 + 10)) turtle.goto((x1 + 10), (y1 + 10)) turtle.goto((x0 + 10), (y0 + 10)) turtle.goto((x1 + 10), (y1 + 10)) turtle.goto((x1 + 10), y1) turtle.goto((x0 + 10), y0) turtle.goto((x1 + 10), y1) turtle.goto(x1, y1) turtle.penup()
turtle.backward(50) # 向当前画笔相反方向移动distance像素长 turtle.right(100) # 顺时针移动 ° turtle.left(150) # 逆时针移动 ° turtle.pendown() # 移动时绘制图形,缺省时也为绘制 turtle.goto(10, 20) # 将画笔移动到坐标为x,y的位置 turtle.penup() # 移动时不绘制图形,提起笔,用于另起一个地方绘制时用 turtle.circle(50) # 画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆 # 画笔控制命令: turtle.fillcolor('red') # 绘制图形的填充颜色(箭头内部颜色) turtle.color('red', '#72538e') # 同时设置箭头颜色pencolor=color1, fillcolor=color2 turtle.filling() # 返回当前是否在填充状态(无参数) turtle.begin_fill() # 准备开始填充图形(无参数) turtle.end_fill() # 填充完成 turtle.showturtle() # *与hideturtle()函数对应* turtle.hideturtle() # 隐藏箭头显示 # 全局控制命令 turtle.clear() # 清空turtle窗口,但是turtle的位置和状态不会改变 turtle.reset() # 清空窗口,重置turtle状态为起始状态 turtle.undo() # 撤销上一个turtle动作(注:无法撤销reset,clear) turtle.isvisible() # 返回当前turtle是否可见 # turtle.stamp() # 复制当前图 # 三角形 (steps (optional) (做半径为radius的圆的内切正多边形,多边形边数为steps)) turtle.circle(80, steps=30) turtle.circle(20, 80) # 半圆 turtle.done() # python -m pip install --upgrade pip'pip升级命令'
# We could stop here, but we can also have another attempt with a different cell just to try to remove more numbers attempts -= 1 def makeGrid(aInGrid): print('wow we made it') myPen.clear() drawGrid(aInGrid) myPen.getscreen().update() currentGrid = aInGrid print(originalgrid) myPen.clear() drawGrid(grid) myPen.getscreen().update() print("Sudoku Grid Ready") determinant = True while (turtle.isvisible()): lInput = input("[a]nswer, [g]rid?, [e]xit") if lInput == "a": solveGrid(grid) makeGrid(originalgrid) elif lInput == "g": makeGrid(grid) elif lInput == "e": turtle.Screen().bye() print('Exited')
def show3D(self, *show_style): """ Use Python Turtle to display a 3D drawing of the cube. Parameters ---------- *show_style : str or float Up to 5 positional arguments can be passed: `xr`, `yr`, `zc`, `zoom`, `pensize`. If an argument is not given, the last used value is used. They must all be values that can be converted to floats. xr : str or float The angle of rotation about the x axis in degrees. -38 <= `xr` <= 38. yr : str or float The angle of rotation about the y axis in degrees. -45 <= `yr` <= 45. zc : str or float The size difference between close and far points. zoom : str or float How much larger the picture is drawn. Negative inverts drawing. pensize : str or float Thickness of the black lines. Default is abs(zoom) / (5 * size + 5). """ cube = self.cube size = self.size colours = self.colours # Get info if len(show_style) > 5: raise TypeError show_style = list(map(float, show_style)) self.show_style['3D'][:len(show_style)] = show_style xr, yr, zc, zoom, pensize = self.show_style['3D'] if len(show_style) == 4: self.show_style['3D'][4] = abs(zoom) / (5 * size + 5) pensize = self.show_style['3D'][4] if pensize < 0: self.show_style['3D'][4] = 0 pensize = 0 sides = [] if xr > 0: sides.append(0) else: sides.append(5) if yr > 0: sides.append(3) else: sides.append(1) if abs(xr) > abs(yr): sides = sides[::-1] sides.append(2) if abs(xr) > 38: xr = ((xr > 0) * 2 - 1) * 38 self.show_style['3D'][0] = xr if abs(yr) > 45: yr = ((yr > 0) * 2 - 1) * 45 self.show_style['3D'][1] = yr if zc: zc = 1 / zc xr, yr = radians(xr), radians(yr) # Reset screen try: turtle.isvisible() except turtle.Terminator: importlib.reload(turtle) if turtle.isvisible(): turtle.title("Virtual Cube - Made by Fred Lang") turtle.setup(width=0.5, height=1.0, startx=-1) turtle.colormode(255) turtle.hideturtle() turtle.tracer(0, 0) turtle.penup() turtle.clear() turtle.pensize(pensize) if not size: turtle.goto(0, 0) turtle.dot() turtle.update() return # Get co-ordinates coords = {s: [[0] * (size + 1) for _ in range(size + 1)] for s in sides} for s in sides: for y_coord in range(size + 1): for x_coord in range(size + 1): x = x_coord / size * 2 - 1 y = y_coord / size * 2 - 1 if not s: x, y, z = x, 1, -y elif s == 1: x, y, z = -1, -y, -x elif s == 2: x, y, z = x, -y, -1 elif s == 3: x, y, z = 1, -y, x elif s == 4: x, y, z = -x, -y, 1 else: x, y, z = x, -1, y # Rotate about the y axis x, z = x * cos(yr) + z * sin(yr), z * cos(yr) - x * sin(yr) # Rotate about the x axis y, z = y * cos(xr) + z * sin(xr), z * cos(xr) - y * sin(xr) if zc: x = x / (z + zc) * zc y = y / (z + zc) * zc coords[s][y_coord][x_coord] = x * zoom, y * zoom # Draw cube for s in coords: for y in range(size): for x in range(size): turtle.goto(*coords[s][y][x]) turtle.pendown() turtle.fillcolor(colours[cube[s][y][x]]) turtle.begin_fill() turtle.goto(*coords[s][y + 1][x]) turtle.goto(*coords[s][y + 1][x + 1]) turtle.goto(*coords[s][y][x + 1]) turtle.goto(*coords[s][y][x]) turtle.end_fill() turtle.penup() turtle.update() turtle.onscreenclick(lambda x, y: self.show3D(str(-y / 4), str(-x / 4)))
from random import randint t.speed(1000) t.penup() t.goto(-200, 200) t.pendown() t.width(9) t.forward(400) t.right(90) t.forward(400) t.right(90) t.forward(400) t.right(90) t.forward(400) t.right(90) t.hideturtle() t.isvisible() x = 0 y = 0 p = 70 dt = 1 k = 11 ay = -10 Vx = p * random() Vy = p * random() Vx1 = p * random() Vy1 = p * random() Vx2 = p * random() Vy2 = p * random() Vx3 = p * random()
turtle.screensize(800, 600) turtle.speed(5) turtle.forward(100) turtle.right(45) turtle.goto(-100, -200) turtle.up() turtle.goto(-100, 100) turtle.down() turtle.pencolor("red") turtle.pensize(10) turtle.forward(30) turtle.setheading(50) #turtle.reset() #turtle.clear() turtle.circle(50, steps=5) turtle.goto(150, 100) turtle.begin_fill() turtle.fillcolor("blue") turtle.circle(50) turtle.end_fill() turtle.forward(50) turtle.undo() turtle.hideturtle() turtle.isvisible() turtle.done()
#reset函数主要用来直接恢复画布的状态为最原始的默认状态,即进行状态的重置 #turtle.reset() #screensize() 重新设置画布的长度和宽度 turtle.screensize(2000, 2000) #hideturtle画图之后总是存在小箭头,可以隐藏小箭头,showturtle可以展示出小箭头 turtle.fd(100) turtle.hideturtle() import time time.sleep(5) turtle.showturtle() #isvisible函数主要用来检测画布上是否有turtle,false和true print(turtle.isvisible()) #s石头剪刀布的计算机小程序 import random ls = ["剪刀", "石头", "布"] a = random.choice(ls) b = input("请输入你的手势:") print("计算机选择了:", a) if a == "剪刀" and b == "石头": print("恭喜你赢了!") if a == "剪刀" and b == "布": print("很抱歉你输了") if a == "剪刀" and b == "剪刀": print("平局") if a == "石头" and b == "剪刀":
2.2 pen movement ''' t.penup() # alias pu() and up() t.pendown() # alias pd() and down() t.forward(10) # alias fd() t.backward(10) # alias bd() t.right(10) # right(radian) alias rt() for right() t.left(10) # left(radian) alias lt() for left() t.goto(5, 5) # goto(x, y) alias setpos() and setposition() for goto() t.circle(100, 360, 50) # circle(radius, radian=360, step=0) # The center is radius units left of the turtle ''' 2.3 pen controller ''' t.fillcolor('yellow') t.color('red', 'yellow') # pencolor() and fillcolor() t.filling() # Is current state a filling state? t.begin_fill() # begin to fill t.end_fill() # end fill t.hideturtle() # alias ht() make the arrow of the turtle invisible t.showturtle() # alias st() make the arrow of the turtle visible ''' 2.4 global control command ''' t.clear() # clear windows but perverse current state of turtle t.reset() # clear windows and reset turtle state t.undo() # undo last operation t.isvisible() # return # stamp() # copy current graphics t.done() # end turtle
import turtle p=100 while(p>0): if(p==10 or p==30 or p==50 or p==70 or p==90): turtle.color('red') if(p==20 or p==40 or p==60 or p==80 or p==100): turtle.color('yellow') turtle.ht() turtle.st() p=p-1 print turtle.isvisible() turtle.exitonclick()
''' draw calender ''' import turtle as t from python.dayTwo.TurtleFinder import fly, init t.setup(600, 600) t.color("#FFFFFF", "#FC5A5A") t.begin_fill() fly(-100, 50) t.goto(-100, 100) t.goto(100, 100) t.setpos(100, 50) t.setpos(-100, 50) t.end_fill() t.write(arg="MAY", move=False, align="center", font=("Arial", 100, "normal")) print(t.isvisible()) t.done()
#turtle.screensize(canvwidth=400, canvheight=300, bg='blue') ''' 方式二: setup width, height 为整数时,表示像素;若为浮点数时,表示栈屏幕的比例,默认情况下width为0.5,height为0.75 startx,starty 表示左上角顶点的位置,以像素为单位,默认窗口居中 若starx为正,表示从左侧开始计算,若为负,则从右侧边缘开始计算 若starty为正,表示从顶部计算,若为负,表示从下边缘计算 ''' turtle.setup(width=0.5, height=0.75, startx=0, starty=0) # 设置箭头不可见,别名: ht turtle.hideturtle() # 设置箭头可见,别名: st turtle.showturtle() # 获取箭头是否可见 isShow = turtle.isvisible() print(isShow) # 设置画笔的位置为原点,即为turtle.goto(0,0) turtle.setheading(0) turtle.home() # 设置画笔的颜色和填充颜色 turtle.color(color1, color2) ''' # 画笔相关 # 设置画笔宽度;别名: width turtle.pensize(width=10) # 设置画笔的颜色,可使用字符串如"blue"或者RGB值比如(255,2,55) turtle.pencolor('blue') # 画笔移动时,不绘制;别名: pu | up turtle.penup() # 画笔移动时,绘制;别名: pd | down turtle.pendown()
# turtle.fd(200) # turtle.pendown() #pendown()放下画笔,与penup()配对使用 # turtle.circle(50,360) # turtle.pensize(1) # turtle.fd(100) # turtle.color("green") #color()设置画笔颜色 # turtle.pensize(2) # turtle.fd(100) """填充颜色需要begin_fill()和end_fill()合在一起才能实现""" # turtle.begin_fill() # turtle.color("red") # turtle.circle(50,360) """color在circle前,则整个图形均为填充的颜色; 在之后,则图形边框为之前画笔颜色""" # turtle.end_fill() # print(turtle.filling()) """filling()返回填充状态,True已填充,Flase未填充。 其中,放在end_fill之后为未填充,在end_fill前面为填充""" # turtle.fd(200) # turtle.clear() #clear()清空窗口,但不改变当前画笔位置 #reset()清空当前窗口,并重置位置等状态为默认值 # turtle.reset() # turtle.fd(1000) # turtle.screensize(3000,3000) #hideturtle()隐藏画笔的turtle形状,showturtle显示画笔形态 turtle.circle(100, 360) turtle.hideturtle() turtle.showturtle() print(turtle.isvisible()) #isvisible()如果turtle可见返回true,否则返回false turtle.done()