海龟.xcor()用于某些海龟对象,而不是其他对象



所以,我对python还很陌生,而且我使用的是turtle。我想做一个基本的游戏,你只需要让角色使用x坐标从一边走到另一边(y坐标并不重要,因为有墙可以阻止你到达你通过目标并仍然获胜的点(,我的代码基本上是,如果你通过目标的xcor((,它会弹出一条消息,说";你赢了";,我得到了适用于墙壁的坐标碰撞系统,但它不适用于目标。

import turtle as Turtle
Window = Turtle.Screen()
Window.title("Game")
Window.bgcolor("grey")
Window.setup(width=800, height=600)
Window.tracer(0)
Instructions = Turtle.Turtle()
Instructions.speed(0)
Instructions.color("black")
Instructions.penup()
Instructions.hideturtle()
Instructions.goto(100,300)
Instructions.write("Get to the Objective, do not touch the walls.", align="center", font=("Courier", 24, "normal"))
Player = Turtle.Turtle()
Player.speed(0)
Player.shape("square")
Player.color("black", "white")
Player.penup()
Player.goto(0, 0)
Player.shapesize(stretch_wid=5, stretch_len=5)
Objective = Turtle.Turtle()
Objective.speed(0)
Objective.shape("square")
Objective.color("black", "lightgreen")
Objective.penup()
Objective.goto(900, 0)
Objective.shapesize(stretch_wid=5, stretch_len=5)
Obstacle = Turtle.Turtle()
Obstacle.speed(0)
Obstacle.shape("square")
Obstacle.color("black", "darkgrey")
Obstacle.penup()
Obstacle.goto(450, -120)
Obstacle.shapesize(stretch_wid=2, stretch_len=50)
Obstacle2 = Turtle.Turtle()
Obstacle2.speed(0)
Obstacle2.shape("square")
Obstacle2.color("black", "darkgrey")
Obstacle2.penup()
Obstacle2.goto(450, 120)
Obstacle2.shapesize(stretch_wid=2, stretch_len=50)
Obstacle3 = Turtle.Turtle()
Obstacle3.speed(0)
Obstacle3.shape("square")
Obstacle3.color("black", "darkgrey")
Obstacle3.penup()
Obstacle3.goto(-80, 0)
Obstacle3.shapesize(stretch_wid=14, stretch_len=2,)
Obstacle4 = Turtle.Turtle()
Obstacle4.speed(0)
Obstacle4.shape("square")
Obstacle4.color("black", "darkgrey")
Obstacle4.penup()
Obstacle4.goto(980, 0)
Obstacle4.shapesize(stretch_wid=14, stretch_len=2,)

def PlayerForward():
if Player.ycor() > -40 and Player.ycor() < 40 and Player.xcor() > -1:
Player.forward(10)
else:
Turtle.setpos(0, -180)
Turtle.write("you touched the wall", font="arial")
Turtle.hideturtle()

def PlayerBackward():
if Player.ycor() > -40 and Player.ycor() < 40 and Player.xcor() > -1:
Player.backward(10)
else:
Turtle.setpos(0, -180)
Turtle.write("you touched the wall", font="arial")
Turtle.hideturtle()

def PlayerRight():
if Player.ycor() > -40 and Player.ycor() < 40 and Player.xcor() > -1:
Player.right(10)
else:
Turtle.setpos(0, -180)
Turtle.write("you touched the wall", font="arial")
Turtle.hideturtle()

def PlayerLeft():
if Player.ycor() > -40 and Player.ycor() < 40 and Player.xcor() > -1:
Player.left(10)
else:
Turtle.setpos(0, -180)
Turtle.write("you touched the wall", font="arial")
Turtle.hideturtle()
#this is the part that doesn't work, I've tried if player.xcor() > 800 ( around where it should touch Objective, haven't got the coordinates down exact as even if you went out of bounds way beyond the coords, it still wont work as this never worked either )
if Player.distance(Objective) < 10:
Turtle.write("You got to the objective, congrats.", font="arial")
Turtle.hideturtle()

Turtle.listen()
Turtle.onkeypress(PlayerForward, "w")
Turtle.onkeypress(PlayerBackward, "s")
Turtle.onkeypress(PlayerRight, "d")
Turtle.onkeypress(PlayerLeft, "a")
while True:
Window.update()

您需要添加代码来检查每次移动时是否已达到目标。

为此,我们创建一个新函数,称之为checkObjective。

def checkObjective():
if Player.distance(Objective) < 10:
Turtle.write("You got to the objective, congrats.", font="arial")
Turtle.hideturtle()

现在,每次移动时都调用它,例如向前移动时。

def PlayerForward():
if Player.ycor() > -40 and Player.ycor() < 40 and Player.xcor() > -1:
Player.forward(10)
checkObjective()
else:
Turtle.setpos(0, -180)
Turtle.write("you touched the wall", font="arial")
Turtle.hideturtle()

最新更新