Speed function doesn't change the turtles position regardless of any parameters I put in

流过昼夜 提交于 2021-02-05 08:08:34

问题


The program I made has two turtles one being the user(player) and the other being player 2 which are run through a function called checkcollision which determines if the turtles intersect thus moving the second turtle to a random position of -250,250 for its x and y coordinates. The problem however is I want the second turtle(non user) to move in a straight line across the screen and I set it to 2 and I also tried setting it to normal and such all not making the turtle move.

import turtle
import random
wn = turtle.Screen()
wn.setup(width = 450, height = 450)
player = turtle.Turtle()
player2 = turtle.Turtle()

def up():

    y  = player.ycor()
    y = y + 5
    player.sety(y)
    if y>=310:
        player.sety(y-15)
    checkcollision(player,player2)

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


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


def right():
    x = player.xcor()
    x = x + 5
    player.setx(x)
    if x>=625:
        player.setx(x-15)
    checkcollision(player,player2)

player.penup()
player.setpos(0,0)
player.showturtle()
player.shape("square")
wn.bgcolor("green")
player2.shape("turtle")
player2.penup()
player2.setpos(300,300)
player2.showturtle()
player2.setheading(-100)
player2.speed(2)


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

turtle.onkeypress(left,"Left")

turtle.onkeypress(right,"Right")

turtle.onkeypress(down, "Down")


def checkcollision(t1,t2):
        if abs(t1.xcor() - t2.xcor()) < 10 and abs(t1.ycor() - t2.ycor()) < 10:
            player2.setpos(random.randint(-250,250), random.randint(-250,250))



checkcollision(player,player2)

回答1:


Your code has multiple problems and I'm surprised it runs at all as presented above. (It should just fall through the bottom of the code, close the turtle window and return to the console.) For example, it doesn't seem to understand it's own coordinate system -- the x coordinates go from -425 to +425 but we're testing if the turtle's x coordinate is <= -625. Below is my rework to address your question and these other issues:

from turtle import Screen, Turtle
from random import randint

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

    if y < 200:
        player.sety(y)
        checkcollision()

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

    if y > -200:
        player.sety(y)
        checkcollision()

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

    if x > -200:
        player.setx(x)
        checkcollision()

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

    if x < 200:
        player.setx(x)
        checkcollision()

def checkcollision():
    if player.distance(player2) < 20:
        player2.setpos(randint(-200, 200), randint(-200, 200))

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

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

player2 = Turtle()
player2.shape('square')
player2.speed('slowest')
player2.color('yellow')
player2.penup()

checkcollision()

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

screen.mainloop()


来源:https://stackoverflow.com/questions/60610664/speed-function-doesnt-change-the-turtles-position-regardless-of-any-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!