如何在程序中同时移动两只乌龟?

时间:2020-03-14 22:14:30

标签: python python-3.x turtle-graphics

当我运行该程序时,从某种意义上来说,player2乌龟会按预期从窗口弹回,并且当它与玩家乌龟(用户)碰撞时,它的位置会设置为随机位置并停止前进。但是,随着player2乌龟(非用户)移动并且玩家乌龟同时移动,在整个过程中,两只乌龟都会冻结并出现毛刺。

import turtle
import random
wn = turtle.Screen()
wn.setup(width = 450, height = 450)
wn.bgcolor("green")
player = turtle.Turtle()
player2 = turtle.Turtle()
player.shape("square")
player2.shape("turtle")
player.penup()
player2.penup()
player.setpos(0,0)
player.showturtle()
player2.showturtle()
player2.setpos(150,150)
#the x and y distance that the player2 turtle moves in the main loop
dx = 5
dy = 5


def up():
    y = player.ycor()
    y = y + 5
    player.sety(y)

    if y>=310:
        player.sety(y-15)

def down():
    y = player.ycor()
    y = y - 5
    player.sety(y)
    if y<-310:
        player.sety(y+15)


def left():
    x = player.xcor()
    x = x - 5
    player.setx(x)
    if x<=-625:
        player.setx(x+15)


def right():
    x = player.xcor()
    x = x + 15
    player.setx(x)

    if x>=625:
        player.setx(x-15)



def checkcollision(t1,t2):
        if abs(t1.xcor() - t2.xcor()) < 10 and abs(t1.ycor() - t2.ycor()) < 10:
            pos = player.xcor()
            player2.fd(0)
            player2.setpos(random.randint(-250,250), random.randint(-250,250))
            if (pos) != player2.xcor():
                while True:
                    player2.fd(0)


#main loop
while True:

    wn.listen()
    wn.onkeypress(up,"Up")

    wn.onkeypress(left,"Left")

    wn.onkeypress(right,"Right")

    wn.onkeypress(down, "Down")

    x2 = player2.xcor()
    y2 = player2.ycor()
    player2.setx(x2 + dx)
    player2.sety(y2 + dy)

    head = player2.heading()

    checkcollision(player,player2)
    if y2>=300:
        player2.sety(300)
        dy *= -1
        player2.sety(y2 + dy)


    if y2<=-310:
        player2.sety(-310)
        dy *=-1
        player2.sety(y2 + dy)


    if x2<=-625:
        player2.setx(-625)
        dx *=-1
        player2.setx(x2 + dx)



    if x2>=625:
       player2.setx(625)
       dx  *=-1
       player2.setx(x2 + dx)

1 个答案:

答案 0 :(得分:1)

我会再试一次,但是您在回答关于同一程序的上一个问题时忽略了advice and code I provided

首先,您的代码似乎无法理解它自己的坐标系-您将x坐标定义为-225到+225,但是您正在测试乌龟的x坐标是否> = 625以及类似的坐标不一致

您的checkcollision()函数中有一个无限循环:

while True:
    player2.fd(0)

没有办法。 Turtle有一种distance()方法,因此您无需这样做:

abs(t1.xcor() - t2.xcor()) < 10 and abs(t1.ycor() - t2.ycor())

在像乌龟这样的事件驱动世界中,不应存在while True:形式的主循环-而是使用计时器事件。循环执行onkeypress()listen()也没有意义,它们只需要执行一次即可。

checkcollision()函数可以更改player2的位置,因此此命令序列值得怀疑:

y2 = player2.ycor()
...
checkcollision(player,player2)
if y2>=300:

由于y2可能不再代表player2调用后checkcollision()的y位置。

以下是我的重做,以解决您的问题,上述问题和其他问题:

from turtle import Screen, Turtle
from random import randint

def up():
    y = player.ycor() + 5

    if y >= 200:
        y -= 15

    player.sety(y)

def down():
    y = player.ycor() - 5

    if y < -200:
        y += 15

    player.sety(y)

def left():
    x = player.xcor() - 5

    if x <= -200:
        x += 15

    player.setx(x)

def right():
    x = player.xcor() + 5

    if x >= 200:
        x -= 15

    player.setx(x)

def checkcollision(t1, t2):
    while t1.distance(t2) < 10:
        t2.setpos(randint(-100, 100), randint(-100, 100))

# the x and y distance that the player2 turtle moves in the main loop
dx = 5
dy = 5

def move():
    global dx, dy

    checkcollision(player, player2)

    x2, y2 = player2.position()
    player2.setposition(x2 + dx, y2 + dy)

    if y2 <= -200 or y2 >= 200:
        dy *= -1
        player2.sety(y2 + dy)

    if x2 <= -200 or x2 >= 200:
        dx *= -1
        player2.setx(x2 + dx)

    screen.ontimer(move, 100)

screen = Screen()
screen.setup(width=450, height=450)
screen.bgcolor('green')

player = Turtle()
player.shape('square')
player.penup()

player2 = Turtle()
player2.shape('turtle')
player2.penup()
player2.setpos(150, 150)

screen.onkeypress(up, 'Up')
screen.onkeypress(left, 'Left')
screen.onkeypress(right, 'Right')
screen.onkeypress(down, 'Down')
screen.listen()

move()

screen.mainloop()