为了测量杆的旋转速度,我需要制作一个表盘,其中有大量交替排列的深色/透明部分,排列成圆圈。旋转转盘会中断光电传感器上的光,然后我只需要测量光脉冲的频率。Python 海龟图形似乎是绘制这个表盘的好主意。
我需要将此图像绘制得非常大,以避免分段边缘出现阶梯效应 - 我需要平滑的边缘。但是,如果我执行turtle.setup(x, y),其中x或y大于屏幕,则画布将被截断以适合屏幕。如何避免这种情况?
我的代码包含在最后。请参阅此处的屏幕截图,其中 x = y = 1420 的画布被截断
编辑:只是为了清楚起见 - getscreen() / getcanvas() 最后捕获此截断的画布图像并将其按原样(截断)保存到 EPS 文件中。这就是让我烦恼的事情。我需要在高分辨率图像文件中捕获整个圆。
我在 Ubuntu 13.04 上使用 python-2.7.4
这是代码:
#!/usr/bin/python
# set this to 1 to troubleshoot
debug = 0
import turtle
import math
# image file with the result
fname="dial.eps"
# number of lines
n = 100
# external radius
r2 = 700
# length of each line
l = round(r2 / 10)
r1 = r2 - l
# pen …
Run Code Online (Sandbox Code Playgroud) 只是想知道,是否可以用半透明墨水绘制/填充乌龟?
就像是:
turtle.setfillopacity(50) # Would set it to 50% transparency
Run Code Online (Sandbox Code Playgroud)
运行Python 2.7
我有这个学校作业:
\n\n不用乌龟circle
函数堆雪人
雪人应该在蓝色背景上,并且应该用白色填充。
\n\n雪人的轮廓应该是黑色的。
\n\n雪人\xe2\x80\x99s 的身体应该由 3 个实心圆圈组成。
\n\n每个圆圈的轮廓应为 3 像素宽。
\n\n底部圆的半径应为 100 像素。
\n\n中间圆的半径应为 70 像素。
\n\n顶部圆圈的半径应为 40 像素。
\n\n每个圆圈应位于其下方圆圈上方的中心(底部圆圈除外,它可以位于任何位置)。
\n\n圆圈之间不应有间隙。
\n\n给雪人一个嘴巴、眼睛和鼻子(帽子是可选的)。
\n\n确保每只手都有两个棍臂和至少两个手指。
\n\n到目前为止,我创建了这个,但在继续之前我似乎无法正确获取圆圈。\n而且,不知道如何在圆圈中着色或为眼睛制作点。请帮助我,第一次编码。
\n\nimport turtle # allows us to use turtle library\nwn = turtle.Screen() # allows us to create a graphics window\nwn.bgcolor("blue") # sets gtaphics windows background color to blue\nimport math # allows us to use math …
Run Code Online (Sandbox Code Playgroud) 我想使用 python 的海龟来有效地模拟乐高机器人。乐高机器人能够对机器人所在位置的颜色进行采样。考虑到这一点,我想创建一个背景迷宫,并让机器人在迷宫中找到出路。我希望对 10 年级的编程学生使用这个。
到目前为止,我可以使用 tkinter 创建一个简单的画布,并在该画布上创建一个彩色矩形。我可以将乌龟放在画布上并让它们共存。乌龟可以放置在彩色矩形上。
现在我只需要能够以某种方式对颜色进行采样。这可以通过获取海龟的位置然后对该坐标进行采样来完成。但我现在陷入困境。
到目前为止,这是我的代码:
from tkinter import Tk, Canvas, Frame, BOTH
import turtle
top = Tk()
C = Canvas(top, height=500, width=600)
Doug = turtle.RawTurtle(C)
rectangle = C.create_rectangle(30, 10, 120, 80, outline="#fb0", fill="#fb0")
Doug.fd(50)
Doug.rt(90)
Doug.fd(50)
C.pack(fill=BOTH, expand=1)
top.mainloop()
Run Code Online (Sandbox Code Playgroud)
我确实注意到,当我运行代码时,海龟的“轨迹”位于矩形后面,这表明存在分层问题。
我正在尝试编写一些代码来启动全屏屏幕(无需单击最大化按钮即可填充整个屏幕)
def __init__(self, states = 2):
self.window = turtle.Screen()
#self.window.screensize(1920,1080)
self.window.title('States')
self.turtles = []
Run Code Online (Sandbox Code Playgroud)
self.window.screensize 最大分辨率 1920x1080,但仅限于小屏幕内。要达到全屏宽度和高度,您必须将其最大化。有办法解决这个问题吗?
当我运行一个海龟图形单元并绘制一些东西时,它没问题,但是如果我关闭窗口并再次运行该单元,我会收到一个名为 Terminator 的奇怪错误,我必须重新启动内核才能再次运行该单元. 如果我尝试连续运行 2 个不同的海龟图形单元(这 2 个单元绘制不同的内容并且编码良好),也会发生这种情况。如果我运行其中一个,然后我重新启动内核,然后运行另一个,不会发生错误,但是必须一直重新启动内核并不好,让我感到不安。这只会发生在我的新 macbook 上,我的 PC Windows 一切都很好,我可以连续重复运行海龟图形单元,唯一需要做的就是关闭当前的海龟窗口来运行另一个。
import turtle
window = turtle.Screen()
t = turtle.Turtle()
t.forward(50)
turtle.mainloop()
Run Code Online (Sandbox Code Playgroud)
如果我运行此代码一次就可以了。但是如果我关闭乌龟窗口并再次运行它而不重新启动内核,我会收到此错误:
--------------------------------------------------------------------------
Terminator Traceback (most recent call last)
<ipython-input-2-48bfc10d8dfd> in <module>()
2
3 window = turtle.Screen()
----> 4 t = turtle.Turtle()
5
6 t.forward(50)
/Users/marti/anaconda/lib/python3.5/turtle.py in __init__(self, shape, undobuffersize, visible)
3814 shape=shape,
3815 undobuffersize=undobuffersize,
-> 3816 visible=visible)
3817
3818 Pen = Turtle
/Users/marti/anaconda/lib/python3.5/turtle.py in __init__(self, canvas, shape, undobuffersize, visible)
2555 self._undobuffersize = undobuffersize
2556 self.undobuffer = Tbuffer(undobuffersize) …
Run Code Online (Sandbox Code Playgroud) turtle-graphics python-3.x ipython-notebook anaconda osx-elcapitan
我有一项任务,要求将两只乌龟放在赛道上(大小相同但赛道不同)。我可以让它们移动,但只有当第一个移动了一半轨道时,第二个才会移动。我不知道如何让乌龟同时移动。这是我的代码:
import turtle
import random
import time
wn = turtle.Screen()
wn.bgcolor("lightgreen")
t = turtle.Turtle()
t.shape('turtle')
t.color('red')
t2 = turtle.Turtle()
t2.shape('turtle')
t2.color('blue')
#user input function
p = float(input('please insert the perimeter:'))
#set the track
def drawTrack(p,r):
shortside = (p/2.0)/(r+1)
longside = r*shortside
turtle.setup((shortside*2)+60, longside +40)
t.penup()
t2.penup()
t.setposition(-shortside-10, -longside/2)
t2.setposition(10, -longside/2)
for i in range (2):
#first track
t.speed(1)
t.pendown()
t.forward(shortside)
t.left(90)
t.forward(longside)
t.left(90)
#second track
t2.speed(1)
t2.pendown()
t2.forward(shortside)
t2.left(90)
t2.forward(longside)
t2.left(90)
drawTrack(p,2)
wn.exitonclick()
Run Code Online (Sandbox Code Playgroud) 我是编程新手,正在阅读一本名为《如何像计算机科学家一样思考》(How To Think Like A Computer Scientist) 的书。第四章讲函数。
在本章末尾,有一个练习要求我使用 Python 的乌龟模块绘制以下图案。
我正在检查这张图片并决定将它分成两部分:1)中间的线和 2)像螺旋一样相互重叠的正方形。
我使用以下代码绘制了第一部分:
import turtle
wn = turtle.Screen() # Set up the window
wn.bgcolor("lightgreen")
alex = turtle.Turtle() # Create Alex
alex.color("blue")
alex.pensize(3)
for i in range(20): # Here I start drawing the lines
alex.forward(100)
alex.backward(100)
alex.left(360/20) # Fit 20 lines in the 360 degree circle
wn.mainloop()
Run Code Online (Sandbox Code Playgroud)
当我运行它时,它绘制了这个:
然后,我创建了 draw_square 函数并设法绘制了第一个正方形:
import turtle
def draw_square(turtle, size):
for i in range(4):
turtle.forward(size)
turtle.left(90)
wn = turtle.Screen() # Set up the window …
Run Code Online (Sandbox Code Playgroud) 我正在用python制作一个简单的面向对象的弹丸模拟程序。我只是在使用 Turtle 和 Math 模块。问题是当我尝试简单地移动我的弹丸(作为积分一些方程之前的测试)时,它不会移动。
import turtle
from turtle import Turtle
import math
window = turtle.Screen()
window.title("Projectile")
window.bgcolor("black")
window.setup(width=800, height=800)
window.tracer(0)
def drawLine():
line = turtle.Turtle()
line.penup()
line.pencolor("white")
line.pensize(10)
line.goto(-400, -300)
line.pendown()
line.forward(800)
line.penup()
line.ht()
class Projectile:
def __init__(self, x, y, color, shape, a, b):
self.turtle = turtle.Turtle()
self.turtle.goto(x, y)
self.turtle.color(color)
self.turtle.shape(shape)
self.turtle.shapesize(a, b)
def launch(self, x, y):
self.turtle.penup()
self.turtle.setx(self.turtle.xcor() + x)
self.turtle.sety(self.turtle.ycor() + y)
running = True
while running:
window.update()
drawLine()
projectileOne = Projectile(-290, -290, "red", "circle", 1, 1) …
Run Code Online (Sandbox Code Playgroud) 我很好奇,想知道是否可以让 pyautogui 模块检测图像中每隔几个像素的颜色,并让海龟使用小圆圈在画布中重新创建它们。我最终找到了一种让它工作的方法,而且我喜欢图像的效果!我的代码唯一的问题是需要一段时间才能完成。根据图像的大小,可能需要将近 10-30 分钟才能完成。有什么办法可以优化我的代码吗?我是新手,一直在尝试不同的模块,所以我非常感谢任何建议或帮助!如果你需要我澄清什么,我也很高兴
import time
import turtle
import pyautogui
time.sleep(2)
minimum = pyautogui.position()
print(minimum)
time.sleep(2)
max_x, max_y = pyautogui.position()
print(max_x, max_y)#I use the first point as the top left corner, and the second as the bottom right.
wn = turtle.Screen()
t = turtle.Turtle()
wn.colormode(255)#Allows RGB colors
t.pensize(2)
t.speed(0)
wn.setup(width = 1.0, height = 1.0)
wn.bgcolor("black")
x, y = minimum
min_x, min_y = minimum
def turtlemove(x, y):
t.pu()
t.goto(x - 965, 565 - y)#Compared coordinates in pyautogui with positions …
Run Code Online (Sandbox Code Playgroud) turtle-graphics ×10
python ×9
python-3.x ×4
anaconda ×1
geometry ×1
pyautogui ×1
python-2.7 ×1
tkinter ×1