标签: python-turtle

检测 Python 海龟游戏中的碰撞

我正在尝试制作一个红海龟追逐蓝海龟的Python游戏。当红海龟抓住蓝海龟时,我希望它在屏幕上显示“碰撞”,但它不起作用。当它碰撞时,什么也没有发生,并且给我一个错误“Turtle”对象不可调用”。

from turtle import Turtle, Screen

playGround = Screen()

playGround.screensize(250, 250)
playGround.title("Turtle Keys")

run = Turtle("turtle")
run.speed("fastest")
run.color("blue")
run.penup()
run.setposition(250, 250)

follow = Turtle("turtle")
follow.speed("fastest")
follow.color("red")
follow.penup()
follow.setposition(-250, -250)

def k1():
    run.forward(45)

def k2():
    run.left(45)

def k3():
    run.right(45)

def k4():
    run.backward(45)

def quitThis():
    playGround.bye()

def follow_runner():
    follow.setheading(follow.towards(run))
    follow.forward(8)
    playGround.ontimer(follow_runner, 10)

playGround.onkey(k1, "Up")  # the up arrow key
playGround.onkey(k2, "Left")  # the left arrow key
playGround.onkey(k3, "Right")  # you get it!
playGround.onkey(k4, "Down")

playGround.listen()

follow_runner()

def is_collided_with(self, run):
    return self.rect.colliderect(run.rect)

runner …
Run Code Online (Sandbox Code Playgroud)

python collision-detection turtle-graphics python-3.x python-turtle

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

海龟删除屏幕上的文字并重写

在我的代码中,在任何函数下,我都会:

t = turtle.Turtle()
t.write(name, font=("Arial", 11, "normal"), align="center")
Run Code Online (Sandbox Code Playgroud)

但是当我改变屏幕时,我想删除这个文本,然后在其他地方重写它。我知道清除整个屏幕的“简单方法”。但有没有办法只删除文字呢?

我还尝试在文本上绘制白色方块,但这不起作用。

有人尝试过不同的方法吗?

python turtle-graphics python-turtle

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

如何在 Turtle 中绘制此形状?

在此处输入图片说明

我今天在一个学习小组遇到了一个挑战,要在 python 海龟库中绘制这个形状。

我想不出一种表达几何解的方法来找到我需要的转弯角度和线的大小。

你能告诉我如何单独绘制第一个多边形吗?我已经知道如何制作图案了。

我在五年级。所以请给我一个我能理解的解决方案。

python turtle-graphics python-turtle

2
推荐指数
2
解决办法
1136
查看次数

turtle.tracer() 是做什么的?

我目前正在学习turtlePython中的模块。所以我在这个链接中阅读了tracer()的函数,但我还没有理解第n次常规屏幕更新实际上意味着什么。例如,如果我设置screen.tracer(0)screen.tracer(2, 100)他们实际做什么?

python turtle-graphics python-turtle

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

如何在 Python 中将函数放入列表中?

我想在Python中使用switch语句,但由于python中没有switch语句,我想使用函数列表。但是,当我定义列表时,函数就会被执行。

这是我的代码:

shapes = [drawSquare(), drawRectangle(), myTurtle.circle(10), drawTriangle(), drawStar()]
Run Code Online (Sandbox Code Playgroud)

这是使用海龟的输出:

输出

如何在 python 不运行函数的情况下定义函数列表?

python python-turtle

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

有没有另一种方法可以分别选择蓝色和红色?

它的编码从去1000一个个像红色,蓝色,红色,蓝色半径的圆。我用 if else 做到了,但我相信我可以用更高级的方式来做到这一点,比如随机、列表或其他任何东西。你能帮忙吗?

import turtle
screen = turtle.Screen()
turtle = turtle.Turtle('turtle')
turtle.pensize(3)
turtle.pencolor('red')
def circle(x, y, r):
    if r <= 0:
        return
    turtle.penup()
    turtle.goto(0, -r)
    turtle.pendown()
    turtle.circle(r)
    if (turtle.pencolor() == 'red'):
        turtle.pencolor('blue')
    else:
        turtle.pencolor('red')
    circle(0, 0, r-10)

circle(0, 0, 100)
screen.exitonclick()
Run Code Online (Sandbox Code Playgroud)

python python-turtle

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

使用乌龟图形绘制虚线

from turtle import Turtle, Screen
tt_turtle_obj = Turtle()

for _ in range(15):
    tt_turtle_obj.forward(10)
    tt_turtle_obj.color("white")
    tt_turtle_obj.forward(10)
    tt_turtle_obj.color("black")

screen = Screen()
screen.exitonclick()
Run Code Online (Sandbox Code Playgroud)

我用这段代码做了同样的事情。还有其他办法吗?

python line turtle-graphics python-turtle

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

如何使用 Python Turtle 制作线性渐变?

我目前正在尝试复制此图像: https://i.stack.imgur.com/fymWE.jpg 我正在尝试在背景中制作该渐变,但我不知道如何做到这一点,并且基本上没有任何内容互联网。编辑:如果有帮助的话,我有两端的 RGB 颜色。顶部为 rgb(154,0,254),底部为 rgb(221,122,80)。

python linear-gradients turtle-graphics python-turtle

-1
推荐指数
1
解决办法
8675
查看次数