如何在乌龟中进行颜色检测

时间:2019-07-17 08:48:46

标签: python colors turtle-graphics detection

我有一个下面显示的python turtle程序。

当乌龟碰到黑色或碰到线条时,我希望能够停止游戏,但是我在网上找不到任何帮助!!!

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

我没有错误消息,但是我一次又一次失败。请有人帮忙。我很清楚乌龟是一个非常有限的游戏引擎,因此如果无法做到这一点,那么任何人都知道我如何编写自己的模块以补充乌龟中的颜色检测。

1 个答案:

答案 0 :(得分:1)

正如@furas在他的评论中指出的那样,尽管可以在tkinter级别上进行对象检测然后检查颜色,但乌龟中没有颜色检测。下面是一种不同的方法:我们用海龟建造所有墙壁,并使用距离计算以及墙壁的长度来检测碰撞:

df = pd.read_csv("https://vincentarelbundock.github.io/Rdatasets/csv/fpp2/goog200.csv", index_col=0)
df['value'].plot()
plt.ylabel('price')
plt.xlabel('time')
plt.show()

这很复杂,尤其是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() 函数,但是它基本上可以工作。我已经稍微简化了您的原始示例,以删除与问题无关的项目。

enter image description here