有没有办法检查乌龟是否碰到了颜色?

时间:2020-06-28 14:32:28

标签: python python-turtle

我正在尝试制作带有边墙和内墙的游戏,但我不知道如何感知乌龟是否接触内壁(外壁会检查乌龟的位置并将其移回)。我的外墙代码是这样的:

import turtle
t = turtle.Turtle()
if t.xcor() >= 425:
        t.setx(424)
    if t.xcor() <= -425:
        t.setx(-424)
    if t.ycor() >= 375:
        t.sety(374)
    if t.ycor() <= -350:
        t.sety(-349)

,我的墙应该像这样: in the middle of the turtle screen

在乌龟屏幕的中央

3 个答案:

答案 0 :(得分:2)

您可以在屏幕上检查海龟的坐标,看看它们是否在墙的坐标处或超出墙的坐标。

答案 1 :(得分:1)

假设您的图形是这样的: enter image description here

方法如下:

if -350 < turtle.xcor() < 350 and -325 < turtle.ycor() < 325: # For A
    if turtle.xcor() >= 350:
        turtle.setx(349)
    if turtle.xcor() <= -350 and (25 < turtle.ycor() < 325 or -25 > turtle.ycor() > -325):
        turtle.setx(-349)
    if turtle.ycor() >= 325:
        turtle.sety(324)
    if turtle.ycor() <= -325:
        turtle.sety(-324)

if -25 < turtle.ycor() < 25 and -425 < turtle.xcor() < -350: # For B
    if turtle.ycor() > 25:
        turtle.sety(24)
    if turtle.ycor() < -25:
        turtle.sety(-24)

答案 2 :(得分:1)

我更喜欢一种方法,该方法最初的开销要比已经提出的答案高,但从长远来看会更简单,因为无需重新进行所有计算就可以更轻松地重新配置墙。

方法是用印章制成墙,即定义一种基本的砖块(乌龟)并通过冲压砖块并跟踪其位置来构造墙。然后,我们可以使用坐标比较碰撞检测来确保我们位于窗口内,但是对于内墙和外墙,我们可以使用乌龟的distance()方法:

from turtle import Screen, Turtle, Vec2D
from random import randrange

BRICK_SIZE = 75
WIDTH, HEIGHT = BRICK_SIZE * 9, BRICK_SIZE * 9
CURSOR_SIZE = 20
EXPLORER_COUNT = 10
EXPLORER_SIZE = BRICK_SIZE / 3.75
CHROME = 14  # window overhead, e.g. borders

def draw_wall(brick):
    wall = []

    brick.goto(-WIDTH/2 + 3 * BRICK_SIZE/2, -HEIGHT/2 + 3 * BRICK_SIZE/2)

    for delta in [Vec2D(1, 0), Vec2D(0, 1), Vec2D(-1, 0), Vec2D(0, -1)]:
        for index in range(6):
            if not (index == 3 and delta == (0, -1)):
                brick.stamp()
                wall.append(brick.position())

            brick.goto(brick.position() + delta * BRICK_SIZE)

    return wall  # a list of brick positions

def collision(t):
    if any(t.distance(brick) < BRICK_SIZE * 2**0.5/2 for brick in wall):
        return True

    x, y = t.position()

    width = screen.window_width()

    if not EXPLORER_SIZE/2 - width/2 < x < width/2 - EXPLORER_SIZE/2:
        return True

    height = screen.window_height()

    if not EXPLORER_SIZE/2 - height/2 < y < height/2 - EXPLORER_SIZE/2:
        return True

    return False

def move():
    for explorer in explorers:
        while True:
            explorer.forward(1)

            if not collision(explorer):
                break

            explorer.undo()
            explorer.setheading(randrange(360))

    screen.update()
    screen.ontimer(move, 10)

screen = Screen()
screen.setup(WIDTH + CHROME, HEIGHT + CHROME)
screen.screensize(100, 100)  # just to accommodate smaller windows
screen.tracer(False)

brick = Turtle()
brick.hideturtle()
brick.shape('square')
brick.shapesize(BRICK_SIZE / CURSOR_SIZE)
brick.color('green')
brick.penup()

wall = draw_wall(brick)

explorers = []

for _ in range(EXPLORER_COUNT):
    explorer = Turtle()
    explorer.shape('turtle')
    explorer.shapesize(BRICK_SIZE / 3.75 / CURSOR_SIZE)
    explorer.color('red', 'pink')
    explorer.setheading(randrange(360))
    explorer.penup()

    explorers.append(explorer)

move()

screen.mainloop()

enter image description here