【发布时间】:2017-08-28 13:20:29
【问题描述】:
有没有办法根据海龟的位置调用预设的代码行?
我尝试过使用turtle.pos() 和xcor/ycor,但我无法让它回忆起代码。
我尝试过:
if turtle.pos == (0.00,50.00):
combat()
我是 Python 乌龟的新手。
import turtle
GRID_UNIT = 50
GRID_SIZE = 5 # code below assumes this is an odd number
####################################################################
def Combat():
print('You encounter a raverous Spider! do you wish to fight it?')
Fight = str(input())
if Fight == 'Yes':
#########Battle Code Warrior############
import random
import time
While_Loop = 1
###########Battle 1############
print('============================================')
while While_Loop == 1:
if Foe >= 0:
time.sleep(1)
print('You did', Player_Dmg,'Dmg')
Foe = Foe - Player_Dmg # checks health remaining
print('Your Foe has ',Foe,'Hp left')
print('=============================')
time.sleep(0.5)
Player_Dmg = random.randrange(0,10)
if Foe <= 0:
print('You killed it')
break
else:
time.sleep(1)
if life >= 0:
life = life - Foe_Dmg
print('You took ', Foe_Dmg,'Dmg')
print('You Have', life, 'Hp left')
print('=============================')
time.sleep(0.5)
Foe_Dmg = random.randrange(0, 10)
if life <= 0:
print('You are dead')
else:
print('You die')
life = 0
break
elif Fight == 'No':
print('You Sprint Frantically away from the spider while it chases you down!')
def Map_Starting_Area():
turtle.pu()
turtle.goto(-GRID_SIZE/2 * GRID_UNIT, -GRID_SIZE/2 * GRID_UNIT)
turtle.ht()
turtle.pd()
####### Grid ###########
for _ in range(GRID_SIZE // 2):
turtle.forward(GRID_SIZE * GRID_UNIT)
turtle.left(90)
turtle.forward(GRID_UNIT)
turtle.left(90)
turtle.forward(GRID_SIZE * GRID_UNIT)
turtle.right(90)
turtle.forward(GRID_UNIT)
turtle.right(90)
turtle.forward(GRID_SIZE * GRID_UNIT)
turtle.left(90)
turtle.forward(GRID_UNIT)
turtle.left(90)
turtle.forward(GRID_SIZE * GRID_UNIT)
turtle.left(90)
for _ in range(GRID_SIZE // 2):
turtle.forward(GRID_SIZE * GRID_UNIT)
turtle.left(90)
turtle.forward(GRID_UNIT)
turtle.left(90)
turtle.forward(GRID_SIZE * GRID_UNIT)
turtle.right(90)
turtle.forward(GRID_UNIT)
turtle.right(90)
turtle.forward(GRID_SIZE * GRID_UNIT)
turtle.left(90)
turtle.forward(GRID_UNIT)
turtle.left(90)
turtle.forward(GRID_SIZE * GRID_UNIT)
turtle.pu()
turtle.left(90)
turtle.fd(75)
turtle.left(90)
turtle.fd(25)
###############################################################
# Square = Town
# Circle = Fight
# Arrow = Quest Marker
# Turtle = Weapon Shops
# Triangle = Potion Shop
# Hollow Circle = Chest
turtle.shape('square')
turtle.stamp()
turtle.fd(50)
turtle.right(90)
turtle.fd(50)
turtle.shape('circle')
turtle.stamp()
turtle.back(50)
turtle.left(90)
turtle.fd(50)
turtle.left(90)
turtle.fd(50)
turtle.shape('circle')
turtle.stamp()
turtle.back(50)
turtle.right(90)
turtle.fd(50)
turtle.right(90)
turtle.fd(50)
turtle.shape('circle')
turtle.stamp()
turtle.fd(100)
turtle.shape('square')
turtle.stamp()
turtle.rt(90)
turtle.fd(140)
turtle.rt(90)
turtle.pd()
turtle.circle(10)
turtle.pu()
turtle.fd(150)
turtle.right(90)
turtle.back(10)
###############################################################
turtle.st()
###############################################################
turtle.speed('fastest')
Map_Starting_Area()
turtle.color('orange')
turtle.shape('classic')
print('You Continue with your Journey')
print('You leave the protection of your farm and head into the open land')
print('You Come to a cross road')
while True:
direction = input('Would you like to go North, South, East or West: ').lower()
if direction == 'north' and turtle.ycor() < GRID_UNIT * (GRID_SIZE//2 - 0.5):
turtle.sety(turtle.ycor() + GRID_UNIT)
turtle.update()
elif direction == 'south' and turtle.ycor() > -GRID_UNIT * (GRID_SIZE//2 - 0.5):
turtle.sety(turtle.ycor() - GRID_UNIT)
turtle.update()
elif direction == 'east' and turtle.xcor() < GRID_UNIT * (GRID_SIZE//2 - 0.5):
turtle.setx(turtle.xcor() + GRID_UNIT)
turtle.update()
elif direction == 'west' and turtle.xcor() > -GRID_UNIT * (GRID_SIZE//2 - 0.5):
turtle.setx(turtle.xcor() - GRID_UNIT)
turtle.update()
elif direction == 'quit':
break
if turtle.pos == (0.00,50.00):
combat()
【问题讨论】:
-
还是不行,有些缩进不正确。
-
嗯,我对这个网站上的编码格式不是很好,因为它需要 indetation 而在 shell 上它不需要
-
最直接的问题是,我认为乌龟永远不会接近 (0, 50)。当我运行代码并打印位置时,它的偏移量为 (25, 25),并以 50 步移动。但更重要的是,您不能真正依赖浮点相等。
-
我知道偏移量是 25,每个偏移量是 50,我在一个单独的外壳中用精确的 smae 代码和参数对其进行了测试,这就是我从海龟定位函数中得到的精确坐标