如何在乌龟中进行颜色检测 [英] How do i make colour detection in turtle
问题描述
我有一个下面显示的python turtle程序。
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.
推荐答案
正如@furas在他的评论中指出的那样,尽管可以在tkinter级别上进行对象检测然后检查颜色,但是我们在turtle中没有颜色检测。下面是一种不同的方法:我们用海龟建造所有墙,并使用距离计算以及墙的长度来检测碰撞:
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()
这很复杂,尤其是 collision()
函数,但是基本上可以使用。我已经稍微简化了您的原始示例,以删除与所讨论的问题无关的项目。
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.
这篇关于如何在乌龟中进行颜色检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!