无论我输入什么参数,速度函数都不会改变海龟的位置 [英] Speed function doesn't change the turtles position regardless of any parameters I put in

查看:35
本文介绍了无论我输入什么参数,速度函数都不会改变海龟的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作的程序有两个海龟,一个是用户(玩家),另一个是玩家 2,它们通过一个名为 checkcollision 的函数运行,该函数确定海龟是否相交,从而将第二个海龟移动到 -250,250 的随机位置它的 x 和 y 坐标.然而,问题是我希望第二只乌龟(非用户)在屏幕上沿直线移动,我将其设置为 2,我也尝试将其设置为正常,这样都不会使乌龟移动.

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)

推荐答案

您的代码有多个问题,我很惊讶它竟然能按上述方式运行.(它应该只是掉到代码底部,关闭乌龟窗口并返回到控制台.)例如,它似乎不理解它自己的坐标系——x 坐标从 -425 到 +425,但是我们正在测试海龟的 x 坐标是否为 <= -625.以下是我为解决您的问题和其他问题所做的修改:

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

这篇关于无论我输入什么参数,速度函数都不会改变海龟的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆