How do i make colour detection in turtle

2020-05-06 10:27发布

问题:

I have a python turtle program which is displayed below.

i want to be able to stop game when turtle touches black or touches line but i cant find any help online!!!

import logging
from datetime import datetime
import time
from turtle import *
import winsound
#while True:
#    player1 = input("enter player1 name\n")
#    break
#while True:
#    player2 = input("enter player2 name\n")
#    print("Please click on window titled pacman")
#    break


setup(600, 600)
Screen()
title("Rendering")
horse2 = Turtle()
horse2.shape("triangle")
horse2.color("blue")
#making the map

map1 = Turtle()
map1.color("black")
map1.shape("square")
map1.forward(100)
map1.left(90)
map1.forward(50)
map1.left(180)
map1.forward(50)
map1.right(90)
map1.forward(100)
map1.right(90)
map1.forward(50)
map1.penup()
map1.left(90)
map1.forward(50)
map1.pendown()
map1.forward(50)
map1.right(90)
map1.forward(50)
map1.left(90)
map1.forward(50)
map1.penup()
map1.forward(50)
map1.left(90)
map1.pendown()
map1.forward(50)
map1.forward(50)
map1.left(90)
map1.forward(50)
map1.right(90)
map1.forward(50)
map1.left(90)
map1.forward(50)
map1.penup()
map1.forward(50)
map1.pendown()
map1.right(90)
map1.forward(50)
map1.forward(50)
map1.right(90)
map1.forward(200)
map1.forward(40)
map1.right(90)
map1.forward(400)
map1.right(90)
map1.forward(500)
map1.right(90)
map1.forward(450)
map1.penup()
map1.forward(50)
map1.pendown()
map1.right(90)
map1.forward(500)
map1.right(90)
map1.forward(100)
map1.right(90)
map1.forward(100)
map1.right(90)
map1.forward(50)
map1.left(180)
map1.forward(50)
map1.penup()
map1.forward(50)
map1.forward(50)
map1.pendown()
map1.left(90)
map1.forward(50)
map1.left(180)
map1.forward(100)
map1.left(180)
map1.forward(150)
map1.right(180)
map1.forward(150)
map1.penup()
map1.forward(50)
map1.forward(100)
map1.right(90)
map1.pendown()
map1.forward(150)
map1.right(90)
map1.forward(100)
map1.forward(50)
map1.left(90)
map1.forward(50)
map1.right(180)
map1.penup()
map1.forward(100)
map1.penup()
map1.forward(200)
map1.forward(50)
map1.left(90)
map1.forward(10)
map1.right(90)
map1.pendown()
map1.forward(50)
map1.right(90)
map1.forward(50)
map1.left(90)
map1.forward(50)
map1.right(90)
map1.forward(50)
map1.left(90)
map1.penup()
map1.forward(50)
#making the map
showturtle()
hideturtle()
horse2.penup()
horse2.goto(-250, -100)
title("Pacman")

def k3():
    horse2.right(90)

def k2():
    horse2.left(90)
    if horse2.xcor() > 250:
        print(player2+" wins")
        logging.basicConfig(filename=("bobobwinner.log"), filemode='w', format='%(name)s - %(message)s')
        logging.warning(player2+' won')

def k1():
    horse2.forward(20)

onkey(k1, "w")
onkey(k2, "a")
onkey(k3, "d")
#onkey(k4, "Left")
#onkey(k5, "Down")

listen()
mainloop()

i have no error message however i have failed time after time to succeed. Please can someone help. I am well aware that turtle is a very limited game engine so if this is not possible is anybody aware how i could code my own module to supplement for colour detection in turtle.

回答1:

As @furas notes in his comment, we don't have color detection in turtle though we can get object detection at the tkinter level and then check colors. Below is a different approach: we build all the walls out of turtles and use distance calculations, in conjunction with the wall's length, to detect collision:

from turtle import Screen, Turtle

CURSOR_SIZE = 20

walls = []

def make_wall(turtle, distance):
    turtle.forward(distance / 2)
    clone = turtle.clone()
    clone.shapesize(stretch_len=distance/CURSOR_SIZE)
    clone.showturtle()
    turtle.forward(distance / 2)

    walls.append(clone)

def collision(turtle):
    tx, ty = turtle.position()

    for wall in walls:

        if wall.distance(turtle) < CURSOR_SIZE / 2:
            wall.color('red')
            return True

        wx, wy = wall.position()
        heading = wall.heading()
        _, stretch_len, _ = wall.shapesize()
        half_length = stretch_len * (CURSOR_SIZE + 1) / 2

        if heading in [0, 180]:  # horizontal wall

            if abs(ty - wy) < CURSOR_SIZE / 2 and abs(tx - wx) < half_length:
                wall.color('red')
                return True

        elif heading in [90, 270]:  # vertical wall

            if abs(tx - wx) < CURSOR_SIZE / 2 and abs(ty - wy) < half_length:
                wall.color('red')
                return True

    return False

def k3():
    horse.right(90)

def k2():
    horse.left(90)

def k1():
    screen.onkey(None, "w")

    horse.forward(15)

    if horse.xcor() > 250:
        screen.title("Player wins!")
    elif collision(horse):
        screen.title("Collision!")
    else:
        screen.onkey(k1, "w")

screen = Screen()
screen.setup(600, 600)
screen.title("Rendering")
screen.tracer(False)

mapper = Turtle()
mapper.shape("square")
mapper.hideturtle()
mapper.penup()
mapper.shapesize(stretch_wid=1/CURSOR_SIZE)

# making the map

make_wall(mapper, 100)
mapper.left(90)
make_wall(mapper, 50)
mapper.left(180)
make_wall(mapper, 50)
mapper.right(90)
make_wall(mapper, 100)
mapper.right(90)
make_wall(mapper, 50)

mapper.left(90)
mapper.forward(50)

make_wall(mapper, 50)
mapper.right(90)
make_wall(mapper, 50)
mapper.left(90)
make_wall(mapper, 50)

mapper.forward(50)
mapper.left(90)

make_wall(mapper, 100)
mapper.left(90)
make_wall(mapper, 50)
mapper.right(90)
make_wall(mapper, 50)
mapper.left(90)
make_wall(mapper, 50)

mapper.forward(50)
mapper.right(90)

make_wall(mapper, 100)
mapper.right(90)
make_wall(mapper, 240)
mapper.right(90)
make_wall(mapper, 400)
mapper.right(90)
make_wall(mapper, 500)
mapper.right(90)
make_wall(mapper, 450)

mapper.forward(50)
mapper.right(90)

make_wall(mapper, 500)
mapper.right(90)
make_wall(mapper, 100)
mapper.right(90)
make_wall(mapper, 100)
mapper.right(90)
make_wall(mapper, 50)
mapper.left(180)
make_wall(mapper, 50)

mapper.forward(100)
mapper.left(90)

make_wall(mapper, 50)
mapper.left(180)
make_wall(mapper, 100)
mapper.left(180)
make_wall(mapper, 150)
mapper.right(180)
make_wall(mapper, 150)

mapper.forward(150)
mapper.right(90)

make_wall(mapper, 150)
mapper.right(90)
make_wall(mapper, 150)
mapper.left(90)
make_wall(mapper, 50)

mapper.right(180)
mapper.forward(350)
mapper.left(90)
mapper.forward(10)
mapper.right(90)

make_wall(mapper, 50)
mapper.right(90)
make_wall(mapper, 50)
mapper.left(90)
make_wall(mapper, 50)
mapper.right(90)
make_wall(mapper, 50)

horse = Turtle()
horse.shape("triangle")
horse.color("blue")
horse.penup()
horse.goto(-250, -100)

screen.onkey(k1, "w")
screen.onkey(k2, "a")
screen.onkey(k3, "d")

screen.listen()
screen.tracer(True)
screen.title("Maze")
screen.mainloop()

This is complicated, particularly the collision() function, but it basically works. I've simplified your original example slightly to remove items that have nothing to do with the issue in question.