标签: python-turtle

如何同时移动多只乌龟?

我有一项任务,要求将两只乌龟放在赛道上(大小相同但赛道不同)。我可以让它们移动,但只有当第一个移动了一半轨道时,第二个才会移动。我不知道如何让乌龟同时移动。这是我的代码:

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)

python turtle-graphics python-turtle

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

如何修复 python 海龟中不一致的帧速率(速度)

我按照本教程制作了一个乒乓球游戏https://youtu.be/C6jJg9Zan7w 我遇到的问题是球(乌龟对象)的速度在不同的计算机上不一样。例如,在教程讲师的计算机上,ball.dx ball.dy的值为2,球速正常,但在我的计算机上速度非常快,所以我必须将其设置为 0.1。我认为这个问题是因为不同的计算机可以输出或多或少的帧。我知道流行的游戏引擎(例如unity-unreal)中有一种方法使用时间而不是帧,因此变量在不同的计算机中是全局的。我遇到的另一个问题是,当我移动球拍时,球的速度会略有变化。不知道这个问题和上面是不是同一个问题

import turtle
import winsound

wn = turtle.Screen()
wn.title('Pong')
wn.bgcolor('black')
wn.setup(width=800, height=600)
wn.tracer(0)

# Paddle A
paddle_a = turtle.Turtle()
paddle_a.speed(0)
paddle_a.shape('square')
paddle_a.color('white')
paddle_a.penup()
paddle_a.goto(-350, 0)
paddle_a.shapesize(5, 1)

# Paddle B
paddle_b = turtle.Turtle()
paddle_b.speed(0)
paddle_b.shape('square')
paddle_b.color('white')
paddle_b.penup()
paddle_b.goto(350, 0)
paddle_b.shapesize(5, 1)

# Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape('square')
ball.color('white')
ball.penup()
ball.dx = 0.15
ball.dy = 0.15

# Pen
pen = turtle.Turtle()
pen.speed(0)
pen.color('white')
pen.penup()
pen.goto(0, 260)
pen.write("Player A: 0  Player B: 0", align='center', font=('Courier', …
Run Code Online (Sandbox Code Playgroud)

python frame-rate turtle-graphics pong python-turtle

5
推荐指数
1
解决办法
6445
查看次数

Python - 海龟重新创建图像太慢

我很好奇,想知道是否可以让 pyautogui 模块检测图像中每隔几个像素的颜色,并让海龟使用小圆圈在画布中重新创建它们。我最终找到了一种让它工作的方法,而且我喜欢图像的效果!我的代码唯一的问题是需要一段时间才能完成。根据图像的大小,可能需要将近 10-30 分钟才能完成。有什么办法可以优化我的代码吗?我是新手,一直在尝试不同的模块,所以我非常感谢任何建议或帮助!如果你需要我澄清什么,我也很高兴这花了大约 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)

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

5
推荐指数
1
解决办法
185
查看次数

不明白这个 AttributeError: module 'turtle' has no attribute 'Turtle'

#archimedes spiral by rays

import math
import turtle

def spiral(t, a, b):
    diff=5
    number=500
    for i in range(number):
        t.penup()
        t.fd(a+b*i*diff*math.pi/180)
        t.pendown()
        t.lt(90)
        t.fd(10)
        t.bk(10)
        t.rt(90)
        t.penup()
        t.bk(a+b*i*diff*math.pi/180)
        t.lt(diff)


bob=turtle.Turtle()
bob.speed(1000)

spiral(bob,0, 2)
Run Code Online (Sandbox Code Playgroud)

代码给出错误信息如下:

#archimedes spiral by rays

import math
import turtle

def spiral(t, a, b):
    diff=5
    number=500
    for i in range(number):
        t.penup()
        t.fd(a+b*i*diff*math.pi/180)
        t.pendown()
        t.lt(90)
        t.fd(10)
        t.bk(10)
        t.rt(90)
        t.penup()
        t.bk(a+b*i*diff*math.pi/180)
        t.lt(diff)


bob=turtle.Turtle()
bob.speed(1000)

spiral(bob,0, 2)
Run Code Online (Sandbox Code Playgroud)

我不明白错误信息。我怎样才能使代码工作?这段代码在大约 3 到 4 个月前曾经顺利运行。

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

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

尝试导入海龟时收到错误消息(python 3.9,m1 Mac)

当我尝试在 m1 MacBook 上导入python 3图形库turtle时,收到一条错误消息:

 david@Davids-MacBook-Air Python Coding Files % /opt/homebrew/bin/python3 "/Users/david/Desktop/Python Coding Files/hello.py"
Traceback (most recent call last):
  File "/Users/david/Desktop/Python Coding Files/hello.py", line 1, in <module>
    import turtle
  File "/opt/homebrew/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/turtle.py", line 107, in <module>
    import tkinter as TK
  File "/opt/homebrew/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 37, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
Run Code Online (Sandbox Code Playgroud)

tkinter我看到它出于某种原因写入了库。这很奇怪,因为在我的另一台 Mac(基于 Intel 的 Mac)上,具有相同的python 3版本,并且没有安装等库tkinter,在尝试导入时没有收到错误消息turtle …

python tkinter python-3.9 python-turtle apple-m1

4
推荐指数
1
解决办法
3096
查看次数

乌龟和乌龟的区别?

python 2.7版中的turtleTurtle有什么不同?

import turtle
star = turtle.Turtle()
for i in range(50):
    star.forward(50)
    star.right(144)
turtle.done()
Run Code Online (Sandbox Code Playgroud)

python turtle-graphics python-2.7 python-turtle

3
推荐指数
1
解决办法
2091
查看次数

海龟图形中的海龟方向?

如何告诉乌龟面向乌龟图形中的方向?我希望乌龟能够转动并面向一个方向,无论其原始位置如何,我怎样才能实现这一点?

python turtle-graphics python-turtle

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

关于何时应该在函数中使用全局变量的经验法则

当我用函数编码游戏时,我经常对全局变量的哪个变量感到困惑。

我听说全球化变量不是一种很好的做法,所以我尝试通过不全球化任何变量来最小化数量,并且只全球化错误消息告诉我的变量。但是这样做很烦人,而且浪费时间。

有人能告诉我什么时候应该在函数中全局变量的经验法则,

什么时候不需要?

这是我的意思的示例(功能):

import turtle
from random import randint as rd
from time import sleep
delay = 0.1

wn = turtle.Screen()
wn.setup(400,400)
wn.tracer(0)

player = turtle.Turtle('square')
player.penup()
player.goto(0,-170)

rock = turtle.Turtle('circle')
rock.shapesize(0.5,0.5)
rock.penup()
rock.goto(rd(-190,190),200)

rocks = [rock]

pen = turtle.Turtle(visible=False)
pen.penup()
pen.goto(0,150)

def go_left(): # No globalizing here
    if player.xcor() >= -170:
        player.setx(player.xcor()-10)

def go_right(): # No globalizing here
    if player.xcor() <= 170:
        player.setx(player.xcor()+10)

def move_rocks(): # No globalizing here
    for rock in rocks:
        rock.sety(rock.ycor()-rd(0,2))

def loop_rocks():
    global …
Run Code Online (Sandbox Code Playgroud)

python function global-variables turtle-graphics python-turtle

3
推荐指数
1
解决办法
1333
查看次数

如何让乌龟画得更快?

我的代码:

import turtle

screen = turtle.Screen()
bob = turtle.Turtle()
screen.bgcolor("black")
bob.speed(0)


def crazy():
    for i in range(360):
        for colors in ['red', 'yellow', 'green', 'purple', 'orange', 'blue']:
            bob.pencolor(colors)
            bob.forward(i)
            bob.left(124)


crazy()
Run Code Online (Sandbox Code Playgroud)

我想大大加快绘制过程,以便更快地绘制最终图像。关于如何做到这一点有什么想法吗?

顺便说一句,我正在开发一个艺术蒙太奇 python 项目,你可能会猜到这个项目会在其中。感谢所有帮助,谢谢。

python turtle-graphics python-turtle

3
推荐指数
1
解决办法
1995
查看次数

如何在 Python 的海龟图形中创建旋转文本?

我正在使用带有 tkinter 和 tcl 8.6 版本的 Python 3.9,因此根据2010 年 stackoverflow 上的答案,我应该能够绘制旋转文本。实际上我可以在 tkinter 中绘制旋转文本,因此我尝试使用海龟模块创建旋转文本:

import turtle
txt = '      '
tt = turtle.Turtle()
tt.write(txt, angle=-45)
Run Code Online (Sandbox Code Playgroud)

但收到错误消息:TypeError: write() got an unexpected keyword argument 'angle'这意味着海龟模块尚不支持文本旋转功能。

这就提出了一个问题:是否有人知道一个补丁/修复程序可以使turtle模块利用Python 3自3.7.2版本以来提供的文本旋转功能?

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

3
推荐指数
1
解决办法
480
查看次数