简体   繁体   English

如何在Python中有效检查和更改龟坐标?

[英]How do I check and change turtle coordinates efficiently in Python?

I am quite new to Python and I have started a Sokoban game. 我对Python很陌生,已经开始了推箱子游戏。 I have been testing this code for checking coords to ensure the box/player moves back when walking into a wall. 我一直在测试此代码以检查坐标,以确保进入墙壁时盒子/播放器能够向后移动。 I tried making a little loop and the function, but I keep getting errors. 我试着做一些循环和函数,但是我不断出错。

import turtle
wn=turtle.Screen()
a=turtle.Turtle()
b=turtle.Turtle()


def checking(x,y):
    if x.xcor()==y.xcor() and x.ycor()==y.ycor():
        return True
    else:
        return False


if checking(a,b)==True:
    a.xcor()=a.xcor()+50

Syntax error- Cannot assign to function call The a in the last line is highlighted. 语法错误-无法分配给函数调用最后一行中的a被突出显示。

The checking function works as this code worked perfectly. 检查功能可以正常工作,因为此代码可以完美运行。

import turtle
wn=turtle.Screen()
a=turtle.Turtle()
b=turtle.Turtle()


def checking(x,y):
    if x.xcor()==y.xcor() and x.ycor()==y.ycor():
        return True
    else:
        return False


if checking(a,b)==True:
    wn.bgcolor("blue")

I would appreciate if anyone knew a way to fix the code. 如果有人知道修复代码的方法,我将不胜感激。 Thank you! 谢谢!

This line is a problem: 这行是个问题:

a.xcor()=a.xcor()+50

as xcor() is used to access a coordinate, not set one. 因为xcor()用于访问坐标,而不是设置坐标。 You want setx() : 您需要setx()

from turtle import Screen, Turtle

wn = Screen()

a = Turtle()
b = Turtle()

def checking(x, y):
    return x.xcor() == y.xcor() and x.ycor() == y.ycor()
    # or better yet: return x.position() == y.position()

if checking(a, b):
    a.setx(a.xcor() + 50)

Here's your next issue -- the checking() function won't work in the long term. 这是您的下一个问题checking()函数无法长期使用。 Turtles crawl a floating point plane and they often don't return to the exact position they left, eg (0, 0) vs (0, 0.001) . 乌龟爬行浮点平面,它们通常不会返回到它们原来的确切位置,例如(0, 0) vs (0, 0.001) To deal with this, we'll need a less exact comparison: 为了解决这个问题,我们需要一个不太精确的比较:

def checking(a, b):
    return abs(a.xcor() - b.xcor()) < 1 > abs(a.ycor() - b.ycor())

or better yet: 或更好:

def checking(a, b):
    return a.distance(b) < 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python龟图形中更改龟的Hitbox大小? - How do I change the Hitbox size of a turtle in python turtle graphics? 如何在 turtlesim 中获取特定海龟的坐标(通过 python 脚本)? - How do I get the coordinates of a specific turtle in turtlesim (by python script)? 如何检查笛卡尔坐标是否有效构成矩形? - How do I check if cartesian coordinates make up a rectangle efficiently? 如何使用乌龟在python上更改onclick的背景颜色? - How do I change the background color onclick on python using turtle? 如何使用Python 2.7.9中的“ Turtle”修改画布坐标的设置? - How do I modify the settings of the coordinates of the canvas using “Turtle” in Python 2.7.9? Python:如何将 pymouse 坐标转换为海龟坐标 - Python:How to convert pymouse coordinates to turtle coordinates 如何调用函数在按键上打印Turtle的坐标? - How do I call a function to print a Turtle's coordinates on keypress? 如何使用Python从列表中填写海龟坐标? - How can I fill in turtle coordinates from a list using Python? 如何获得python乌龟中一组坐标处的颜色? - How can I get the color that is at a set of coordinates in python turtle? 我如何改变乌龟的颜色,而不是笔? - How do I change the color of the turtle, not the pen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM