简体   繁体   中英

Is there a way to check if a turtle is touching a color?

I am tryng to make a game with edge walls and inner walls but I don't know how to sense if the turtle is touching the inner wall(the outer wall checks turtles position and moves him back). My outer wall code is this:

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)

and my walls should look like this: 在龟屏中间

in the center of turtle screen

You could check the coordinates of the turtle on the screen and see if they are at or past the coordinates of the wall.

Assuming your graph is something like this: 在此处输入图像描述

Here is how:

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)

I prefer an approach that initially has higher overhead than the already suggested answer, but in the long run will be simpler as it'll be easier to reconfigure your walls without having to redo all your calculations.

The approach is to make the walls out of stamps , ie define a basic brick that is a turtle and construct your walls by stamping out bricks and keeping track of their positions. We can then use coordinate comparison collision detection to make sure we're inside the window, but as far as the innner and outer walls, we can use turtle's distance() method:

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()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM