【问题标题】:Python Turtle collision border collision not workingPython Turtle 碰撞边界碰撞不起作用
【发布时间】:2017-05-22 08:51:44
【问题描述】:

我试图让我的海龟对象在与边界接触时旋转 180 度,但这并没有发生。而不是我想使用 and if 语句说如果 x 和 y cor 高于或低于某个数量,它们将导致对象向后移动。下面是代码。

from turtle import *
import turtle

title("Pokemon: Fire Red")
t = turtle.Turtle()
screen = t.getscreen()

t.penup()


t.left(90)

border = turtle.Turtle()
border.up()
border.setposition(-240, 160)
border.down()
border.pensize(3)
for side in range(2):
    border.forward(480)
    border.right(90)
    border.forward(320)
    border.right(90)

tx, ty = t.pos()
if tx < -240 or tx > 240 or ty < -160 or ty > 160:
    t.hide()




Screen()

showturtle()



def k1():
    t.forward(50)



def k2():
    t.left(90)
    t.forward(50)

    t.right(90)

def k3():
    t.right(90)
    t.forward(50)

    t.left(90)

def k4():
    t.forward(-50)





onkey(k1, "Up")
onkey(k2, "Left")
onkey(k3, "Right")
onkey(k4, "Down")


listen()
mainloop()

任何帮助将不胜感激,谢谢。

【问题讨论】:

标签: python turtle-graphics


【解决方案1】:

你在class turtle下做了函数hide()吗?我已经找过了,但turtle module 没有提供这样的功能。我会在假设您的代码的情况下回答

tx, ty = t.pos()
if tx < -240 or tx > 240 or ty < -160 or ty > 160:
    t.hide()

旨在检查碰撞。在此代码中,您只检查一次与边界的碰撞。每次海龟移动时都应该调用您期望的性能。也就是说,在每个k# 函数下。所以变化会是这样的:

def collision_check (t):
    tx,ty = t.pos()
    if tx < -240 or tx > 240 or ty < -160 or ty > 160:
        t.right(180)
        t.forward(50)
        t.right(180)

def k1():
    t.forward(50)
    collision_check(t)

def k2():
    t.left(90)
    t.forward(50)
    collision_check(t)
    t.right(90)

def k4():
    t.right(180)
    t.forward(50)
    collision.check(t)
    t.right(180)

k3 也是如此。 k4 被调整是因为这个collision_check 函数作用于海龟的方向。

【讨论】:

    【解决方案2】:

    您已为您的海龟定义了一个独特的运动序列,该序列始终偏向 方向,因此我认为通用碰撞检测逻辑不会起作用。我也相信绝对 heading 设置比相对 right()left() 转弯更适合您。因此,我使用标题和 logo 模式重新编写了您的代码,因为它将 up 设置为 0 度:

    from turtle import Turtle, Screen
    
    WIDTH, HEIGHT = 480, 320
    
    def move_turtle(distance, heading):
        ahead = min(50, distance)
    
        if ahead:
            if heading != yertle.heading():
                yertle.setheading(heading)
    
            yertle.forward(ahead)
    
        behind = 50 - ahead
    
        if behind:
            heading -= 180
    
            if heading != yertle.heading():
                yertle.setheading(heading)
    
            yertle.forward(behind)
    
        if yertle.heading() != 0:
            yertle.setheading(0)
    
    def k1():
        screen.onkey(None, "Up")
        move_turtle(HEIGHT/2 - yertle.ycor(), 0)
        screen.onkey(k1, "Up")
    
    def k2():
        screen.onkey(None, "Left")
        move_turtle(yertle.xcor() + WIDTH/2, 270)
        screen.onkey(k2, "Left")
    
    def k3():
        screen.onkey(None, "Right")
        move_turtle(WIDTH/2 - yertle.xcor(), 90)
        screen.onkey(k3, "Right")
    
    def k4():
        screen.onkey(None, "Down")
        move_turtle(HEIGHT/2 + yertle.ycor(), 180)
        screen.onkey(k4, "Down")
    
    screen = Screen()
    screen.mode('logo')  # since 'up' is crucial, make it 0 degrees
    
    yertle = Turtle()
    yertle.penup()
    
    border = Turtle(visible=False)
    border.speed("fastest")  # because I have no patience
    border.pensize(3)
    border.penup()
    border.setposition(-WIDTH/2, -HEIGHT/2)
    border.pendown()
    
    for _ in range(2):
        border.forward(HEIGHT)
        border.right(90)
        border.forward(WIDTH)
        border.right(90)
    
    screen.onkey(k1, "Up")
    screen.onkey(k2, "Left")
    screen.onkey(k3, "Right")
    screen.onkey(k4, "Down")
    
    screen.listen()
    screen.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      • 2018-06-15
      • 1970-01-01
      • 2013-02-18
      相关资源
      最近更新 更多