当我尝试从Python文档中turtle
运行第一段示例代码时:
from turtle import *
color('red', 'yellow')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
end_fill()
done()
Run Code Online (Sandbox Code Playgroud)
我得到一个NameError
:
NameError:未定义名称"颜色"
调整import
并手动指定模块也不起作用:
import turtle
turtle.color('red', 'yellow')
turtle.begin_fill()
while True:
turtle.forward(200)
turtle.left(170)
if abs(turtle.pos()) < 1:
break
turtle.end_fill()
turtle.done()
Run Code Online (Sandbox Code Playgroud)
我正在使用Python v3.2.3,它清楚地包含turtle.color
,根据文档.Python也tkinter
支持安装,因为import tkinter
也可以.
完整的痕迹是:
Traceback (most recent call last):
File "<path name that contains no spaces>/turtle.py", line 1, in <module>
from turtle import *
File "<path name …
Run Code Online (Sandbox Code Playgroud) 目前,我已经定义了许多形状Turtle
使用begin_poly
和end_poly
再register_shape
.我希望能够将所有这些值放入列表中,只需按一下按钮,循环显示列表,从而改变Turtle
形状.我很难实现这一目标,Itertools
并且想知道如何实现这一目标.
编辑:我最终将它工作,我将所有值附加到列表中然后使用计数器来选择要转到的索引.
我想用python的乌龟编写一个程序来创建一个带有关卡的树.下面是一些I/O,所以你看看它是做什么的.
我的程序适用于第一种情况,但对于第二种情况打印太多.该计划的规定如下:
必须是递归的
只能使用以下龟功能:
turtle.forward(100) <-- turtle goes forward 100 steps
turtle.right(90) <-- turtle turns right 90 degrees
turtle.penup() <-- turtle lifts its pen up off of the paper
turtle.forward(100) <-- turtle goes forward 100 steps
turtle.pendown() <-- turtle puts its pen down on the paper
turtle.pencolor("red") <-- turtle uses red pen
turtle.circle(100) <-- turtle draws circle of radius 100
turtle.pencolor("blue") <-- turtle changes to blue pen (most other common colors work too!)
turtle.forward(50) <-- turtle moves forward 50 steps …
Run Code Online (Sandbox Code Playgroud)我有两组代码,第一部分是乌龟图形窗口,第二部分是Tkinter窗口.我应该把这两个部分放在一个窗口?
我的第一部分代码
from turtle import *
def move(thing, distance):
thing.circle(250, distance)
def main():
rocket = Turtle()
ISS = Turtle()
bgpic('space.gif')
register_shape("ISSicon.gif")
ISS.shape("ISSicon.gif")
rocket.speed(10)
ISS.speed(10)
counter = 1
title("ISS")
screensize(750, 750)
ISS.hideturtle()
rocket.hideturtle()
ISS.penup()
ISS.left(90)
ISS.fd(250)
ISS.left(90)
ISS.showturtle()
ISS.pendown()
rocket.penup()
rocket.fd(250)
rocket.left(90)
rocket.showturtle()
rocket.pendown()
rocket.fillcolor("white")
while counter == 1:
move(ISS, 3)
move(rocket, 4)
main()
Run Code Online (Sandbox Code Playgroud)
第二部分
from Tkinter import *
control=Tk()
control.title("Control")
control.geometry("200x550+100+50")
cline0=Label(text="").pack()
cline1=Label(text="Speed (km/s)").pack()
control.mainloop()
Run Code Online (Sandbox Code Playgroud)
非常感谢 ;)
我正在使用python turtle进行一个项目,我需要乌龟来绘制字符.但是,当我尝试使用rgb值foe颜色时,我不断收到错误消息.输入是
turtle.color((151,2,1))
Run Code Online (Sandbox Code Playgroud)
然后是一系列动作,但是当我运行该程序时,我收到此消息
File "C:/Users/Larry/Desktop/tests.py", line 5, in center
turtle.color((151,2,1))
File "<string>", line 1, in color
File "C:\Python33\lib\turtle.py", line 2208, in color
pcolor = self._colorstr(pcolor)
File "C:\Python33\lib\turtle.py", line 2688, in _colorstr
return self.screen._colorstr(args)
File "C:\Python33\lib\turtle.py", line 1158, in _colorstr
raise TurtleGraphicsError("bad color sequence: %s" % str(color))
turtle.TurtleGraphicsError: bad color sequence: (151, 2, 1)
Run Code Online (Sandbox Code Playgroud)
这是什么意思,我该如何解决?
我正试图通过Python龟获得鼠标位置.一切正常,除了我不能让乌龟跳到鼠标点击的位置.
import turtle
def startmap(): #the next methods pertain to drawing the map
screen.bgcolor("#101010")
screen.title("Welcome, Commadore.")
screen.setup(1000,600,1,-1)
screen.setworldcoordinates(0,600,1000,0)
drawcontinents() #draws a bunch of stuff, works as it should but not really important to the question
turtle.pu()
turtle.onclick(turtle.goto)
print(turtle.xcor(),turtle.ycor())
screen.listen()
Run Code Online (Sandbox Code Playgroud)
据我所知,"turtle.onclick(turtle.goto)"这一行应该将乌龟发送到我点击鼠标的地方,但事实并非如此.打印线是一个测试,但它只返回我最后发送的位置,名义上(0,650)虽然这没有重大意义.
我尝试查找教程和pydoc,但到目前为止我还没能成功写出来.
我感谢您的帮助.谢谢.
编辑:我需要乌龟去点击位置(完成),但我还需要它来打印坐标.
我正在尝试编写一个绘制Spirograph的Python龟程序,并且我不断收到此错误:
Traceback (most recent call last):
File "C:\Users\matt\Downloads\spirograph.py", line 36, in <module>
main()
File "C:\Users\matt\Downloads\spirograph.py", line 16, in main
spirograph(R,r,p,x,y)
File "C:\Users\matt\Downloads\spirograph.py", line 27, in spirograph
spirograph(p-1, x,y)
TypeError: spirograph() missing 2 required positional arguments: 'x' and 'y'
>>>
Run Code Online (Sandbox Code Playgroud)
这是代码:
from turtle import *
from math import *
def main():
p= int(input("enter p"))
R=100
r=4
t=2*pi
x= (R-r)*cos(t)-(r+p)*cos((R-r)/r*t)
y= (R-r)*sin(t)-(r+p)*sin((R-r)/r*t)
spirograph(R,r,p,x,y)
def spirograph(R,r,p,x,y):
R=100
r=4
t=2*pi
x= (R-r)*cos(t)-(r+p)*cos((R-r)/r*t)
y= (R-r)*sin(t)-(r+p)*sin((R-r)/r*t)
while p<100 and p>10:
goto(x,y)
spirograph(p-1, x,y)
if p<10 or …
Run Code Online (Sandbox Code Playgroud) 当我打电话t.pencolor('83, 58, 27')
(乌龟被导入为t)时我得到了TurtleGraphicsError: bad color string: 83, 58, 27
尽管我(我认为)改变了我的颜色模式.
t.colormode(255)
t.pencolor('83, 58, 27')
Run Code Online (Sandbox Code Playgroud)
我在OS 10.9上运行python 2.7
下面的代码完美无缺,然而,PyCharm抱怨语法错误 forward(100)
#!/usr/bin/python
from turtle import *
forward(100)
done()
Run Code Online (Sandbox Code Playgroud)
既然turtle
是stanrd库我不认为我需要做额外的配置,对吗?
所以我一直在用 Python 开发一些游戏(战舰、井字游戏等),本周的项目是 Snake。我有一个基本的设置;蛇可以移动并吃掉食物,但我还没有在碰撞检测中编程或离开边缘。问题是响应时间。如果您运行下面的代码,您会看到蛇对按键做出响应,但不会响应几次——我将它们称为帧——在按下之后。我不太明白 listen() 方法是如何工作的;我是否正确使用它?如果没有,我应该如何使用它,如果是,我该如何解决延迟?我知道 Pygame,但是 a) 我找不到一个易于安装的 64 位版本的 python 3.4(这个http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame不容易安装,whl 文件到底是什么?)和 b)无论如何我都想挑战自己。任何帮助,将不胜感激。
import random
import turtle
import time
class Square:
def __init__(self, x, y):
self.x = x
self.y = y
def drawself(self, turtle):
# draw a black box at its coordinates, leaving a small gap between cubes
turtle.goto(self.x - 9, self.y - 9)
turtle.begin_fill()
for i in range(4):
turtle.forward(18)
turtle.left(90)
turtle.end_fill()
class Food:
def __init__(self, x, y):
self.x = x
self.y = y
self.state = …
Run Code Online (Sandbox Code Playgroud) turtle-graphics ×10
python ×9
python-3.x ×2
list ×1
pycharm ×1
python-2.7 ×1
python-3.2 ×1
tkinter ×1
tree ×1
windows ×1