2017-05-22 81 views
0

我试图让我的龟对象在接触到边框时变成180度,但是这不会发生。而不是我想要使用的,如果声明说如果x和y的偏差高于或低于一定数量,他们将导致对象回退。代码如下。Python龟碰撞边界碰撞不起作用

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

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

+0

从(HTTP [在Python乌龟游戏检测碰撞]可能重复: //stackoverflow.com/questions/43461566/detecting-collision-in-python-turtle-game?rq=1) – Mike

回答

0

您是否在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。因为这个collision_check函数在龟的方向上工作,所以调整k4。

0

你已经为你的龟是总是在方向偏向这样的,我不相信一般的碰撞检测逻辑将工作独特的运动序列。我也相信,绝对标题设置将为您的工作比相对right()left()转。因此,我返工使用标题和标志模式你的代码,因为它设置高达为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()