标签: turtle-graphics

python turtle奇怪的光标跳

我正在尝试使用鼠标绘制龟,我在下面的演示代码工作,但有时鼠标移动时光标跳转:

#!/usr/bin/env python
import turtle
import sys

width = 600
height = 300
def gothere(event):
    turtle.penup()
    x = event.x
    y = event.y
    print "gothere (%d,%d)"%(x,y)
    turtle.goto(x,y)
    turtle.pendown()

def movearound(event):
    x = event.x
    y = event.y
    print "movearound (%d,%d)"%(x,y)
    turtle.goto(x,y)

def release(event):
    print "release"
    turtle.penup()

def circle(x,y,r):
    turtle.pendown() 
    turtle.goto(x,y)
    turtle.circle(r)
    turtle.penup()
    return

def reset(event):
    print "reset"
    turtle.clear()

#------------------------------------------------#
sys.setrecursionlimit(90000)
turtle.screensize(canvwidth=width, canvheight=height, bg=None)
turtle.reset()
turtle.speed(0)
turtle.setup(width, height)

canvas = turtle.getcanvas()

canvas.bind("<Button-1>", gothere)
canvas.bind("<B1-Motion>", movearound)
canvas.bind("<ButtonRelease-1>", release)
canvas.bind("<Escape>",reset)

screen = turtle.Screen()
screen.setworldcoordinates(0,height,width,0)
screen.listen() …
Run Code Online (Sandbox Code Playgroud)

python turtle-graphics

7
推荐指数
1
解决办法
397
查看次数

如何在python的海龟图形中旋转文本

我想用python的海龟制作图表(教学目的)。y 轴的标签“值”应该旋转。
Python的turtle有一个在当前位置写入字符串的方法:

from turtle import *
left(90) # does not help
write("values", font=('Arial', 12, 'normal'))
hideturtle()
mainloop()
Run Code Online (Sandbox Code Playgroud)

“价值观”仍然是横向的。

如何用python的turtle旋转文本?

python turtle-graphics python-3.x python-turtle

6
推荐指数
1
解决办法
9643
查看次数

有没有办法可以设置python龟的背景颜色?

我正在尝试设置我的乌龟图形的背景颜色,有没有办法可以设置蟒蛇龟的背景颜色?

python turtle-graphics

6
推荐指数
2
解决办法
5万
查看次数

螺旋描记器 龟 蟒蛇

如何与乌龟玩耍以及如何使用乌龟?

我很难让事情正常工作,如下图所示(忽略颜色)。

图像

from turtle import *
from math import *


def formulaX(R, r, p, t):
    x = (R-r)*cos(t) - (r + p)*cos((R-r)/r*t)

def formulaY(R, r, p, t):
    y = (R-r)*sin(t) - (r + p)*sin((R-r)/r*t)

def t_iterating(R, r, p):
    t = 2*pi
    up()
    goto(formulaX, formulaY)
    down()

    while (True):
        t = t + 0.01
        formulaX(R, r, p, t)
        formulaY(R, r, p, t)


def main():
    R = int(input("The radius of the fixed circle: "))
    r = int(input("The radius of the moving circle: ")) …
Run Code Online (Sandbox Code Playgroud)

python turtle-graphics

6
推荐指数
1
解决办法
1万
查看次数

是否有海龟图形使用的关键事件名称的完整列表?

在使用Python 的 Turtle模块时,我使用了一些关键事件,如官方文档所述:

turtle.onkey(fun, key)

参数:

  • fun – 没有参数或 None 的函数
  • key – 字符串:键(例如“a”)或键符号(例如“空格”)

现在,有趣的是,当你调用1)onkeyrelease()方法,并传递一个未注册的字符串(如一个空的(""),或"+"等)key的参数:

turtle.onkeyrelease(lambda: print("Got key event while listening to none."), "")
Run Code Online (Sandbox Code Playgroud)

不管用户按什么键,程序都会输出“ Got key event ...”,顺便说一下这个问题的问题

不幸的是,我无法在互联网上其他地方的文档中找到有关此行为的更多信息。所以我想知道是否有用于编程关键事件的所有受支持的键名字符串的完整列表?


1)问题中使用的基本设置:

import turtle
turtle.setup(700,500)
turtleWindow = turtle.Screen()
turtleWindow.onkey(lambda: print("You pressed 'a'"), "a")
turtleWindow.listen()
Run Code Online (Sandbox Code Playgroud)

python turtle-graphics

6
推荐指数
1
解决办法
4087
查看次数

在 Skulpt Turtle 中使用图像

使用Skulpt时,如何访问 Web 服务器上的 gif 图像?我正在使用海龟图形,并且我想将海龟的形状设置为保存在网络服务器上的图片,但我不知道如何执行此操作。

我知道要通过常规 python shell 执行此操作,我只需说 screen.addshape,然后是turtle.shape,但这不适用于 Skulpt。

我该怎么做才能在 Skulpt 中将海龟的形状更改为 gif?

谢谢你的帮助!

javascript turtle-graphics skulpt

6
推荐指数
0
解决办法
305
查看次数

如何获得乌龟的位置?

如何在python中找到乌龟的坐标?

例如,如果海龟位于(200, 300),我将如何检索该位置?

python turtle-graphics python-turtle

6
推荐指数
1
解决办法
2万
查看次数

Pythonturtle.Terminator错误

当我使用海龟模块用这个简单的函数画一个圆时:

def draw_shape(self):
    canvas = Screen()
    t = Turtle()
    t.circle(self.r)
    canvas.exitonclick()
Run Code Online (Sandbox Code Playgroud)

当我第一次调用此函数时,它会打开一个新窗口并绘制一个圆圈,我单击它退出,当我尝试再次从控制台中的菜单调用此函数时,我收到错误:

Original exception was:
Traceback (most recent call last):
  File "main.py", line 136, in <module>
    main()
  File "main.py", line 132, in main
    OPTIONS[user_input][1](shapes)
  File "main.py", line 48, in handle_sixth_menu_option
    t = Turtle()
  File "/usr/lib/python3.6/turtle.py", line 3816, in __init__
    visible=visible)
  File "/usr/lib/python3.6/turtle.py", line 2557, in __init__
    self._update()
  File "/usr/lib/python3.6/turtle.py", line 2660, in _update
    self._update_data()
  File "/usr/lib/python3.6/turtle.py", line 2646, in _update_data
    self.screen._incrementudc()
  File "/usr/lib/python3.6/turtle.py", line 1292, in _incrementudc
    raise Terminator
turtle.Terminator
Run Code Online (Sandbox Code Playgroud)

python turtle-graphics python-3.6 python-turtle

6
推荐指数
1
解决办法
2万
查看次数

Python Turtle 在包含框中写入值

我希望能够创建一些通过子类化来显示值的海龟turtle.Turtle

这些海龟应该将它们的值显示为以它们自己的形状居中的文本。我还希望能够准确地定位海龟,因此相对于给定的字体大小设置/确定它们的宽度和高度很重要。

到目前为止,这是我的尝试:

在此处输入图片说明

我认为这个答案是相关的:如何知道 python 中海龟图形上特定文本的像素大小?但它已经很旧了,它在文本周围绘制的边界框使用 python 3.8 不在正确的位置。

import turtle

FONT_SIZE = 32

class Tile(turtle.Turtle):
    def __init__(self):
        super().__init__(shape="square")
        self.penup()
    
    def show_value(self, val):
        self.write(val, font=("Arial", FONT_SIZE, "bold"), align="center")


screen = turtle.Screen()
vals = [5, 7, 8, 2]
for i in range(len(vals)):
    tile = Tile()
    tile_size = (FONT_SIZE / 20)
    tile.shapesize(tile_size)
    tile.fillcolor("red" if i % 2 == 0 else "blue")
    tile.setx(i * FONT_SIZE)
    tile.show_value(vals[i])
turtle.done()
Run Code Online (Sandbox Code Playgroud)

python turtle-graphics

6
推荐指数
1
解决办法
1006
查看次数

如何防止乌龟向相反方向移动

代码:

import turtle
import random
import time

s = turtle.getscreen()
turtle.screensize(canvwidth=400, canvheight=400)
t = turtle.Turtle()
t.pensize(0)
t.shape('square')
t.color("black")
t.speed(0)
t.penup()

def moveu(num):
    t.setheading(num)
    t.forward(20)
    

s.onkey(lambda : moveu(90), 'w')
s.onkey(lambda : moveu(270), 's')
s.onkey(lambda : moveu(180), 'a')
s.onkey(lambda : moveu(0), 'd')
    
s.listen()
Run Code Online (Sandbox Code Playgroud)

我还没有完成这个项目,但我遇到了一些问题。我想在turtle模块中创建一个游戏。但我不知道如何防止块向后移动。我见过其他人用过t.direction什么的。但我已经尝试过了,但它并没有真正奏效,也许我只是愚蠢而我做错了什么。如何防止正方形向相反方向移动?

python turtle-graphics python-3.x python-turtle

6
推荐指数
1
解决办法
142
查看次数