我如何在乌龟中进行颜色检测
How do i make colour detection in turtle
我有一个 python 海龟程序,如下所示。
我想在乌龟触到黑色或触到线时能够停止游戏,但我在网上找不到任何帮助!!!
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()
我没有收到任何错误消息,但是我一次又一次地失败了。请有人帮忙。我很清楚 turtle 是一个非常有限的游戏引擎,所以如果这不可能,有人知道我如何编写自己的模块来补充 turtle 中的颜色检测。
正如@furas 在他的评论中指出的那样,虽然我们可以在 tkinter 级别进行对象检测,然后检查颜色,但我们在 turtle 中没有颜色检测。下面是一种不同的方法:我们用海龟建造所有的墙,并结合墙的长度使用距离计算来检测碰撞:
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()
这个函数,但是基本可以。我稍微简化了您的原始示例,以删除与所讨论问题无关的项目。
我有一个 python 海龟程序,如下所示。
我想在乌龟触到黑色或触到线时能够停止游戏,但我在网上找不到任何帮助!!!
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()
我没有收到任何错误消息,但是我一次又一次地失败了。请有人帮忙。我很清楚 turtle 是一个非常有限的游戏引擎,所以如果这不可能,有人知道我如何编写自己的模块来补充 turtle 中的颜色检测。
正如@furas 在他的评论中指出的那样,虽然我们可以在 tkinter 级别进行对象检测,然后检查颜色,但我们在 turtle 中没有颜色检测。下面是一种不同的方法:我们用海龟建造所有的墙,并结合墙的长度使用距离计算来检测碰撞:
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()
这个函数,但是基本可以。我稍微简化了您的原始示例,以删除与所讨论问题无关的项目。