python 乌龟从坐标返回变量
python turtle returning varibles from coords
有没有办法根据乌龟的位置调用预设的代码行?
我试过使用 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()
turtle.pos
是函数
import turtle
print(turtle.pos)
turtle.pos == (0.00,50.00) # is always False
turtle.pos() == (0.00, 0.00) # might be what you want
您的代码中存在许多问题,我希望能够解决:
#the imports are now all at the start - this is generally considered the way to go
import turtle
import random
import time
#constant declarations
GRID_UNIT = 50
GRID_SIZE = 5
HALF_GRID = GRID_SIZE // 2
SPIDER_LOCATIONS = [(1, 1),
(-1, 0)]
TREASURE_LOCATIONS = [(1, -2),
(-2, -1)]
TOWN_LOCATIONS = [(-2, -2),
(2, -2),
(-2, 2),
(2, 2)]
#simplified event functions
def combat():
print("You stepped on a spider!")
def treasure():
print("You found treasure!")
def town():
print("You entered a town")
#scaled setpos for use with int grid coords
def grid_setpos(x, y, scale):
turtle.setpos((x - 0.5) * scale, (y - 0.5) * scale)
#draw a grid
def draw_grid(size, unit):
for x in range(-size // 2, size // 2 + 1):
turtle.pu()
turtle.setpos(x * unit, -size // 2 * unit)
turtle.pd()
turtle.setpos(x * unit, size // 2 * unit)
turtle.pu()
turtle.setpos(-size // 2 * unit, x * unit)
turtle.pd()
turtle.setpos(size // 2 * unit, x * unit)
#draw the map, iterating over stored global coordinates
def map_starting_area(size, unit):
draw_grid(size, unit)
# square = town
# circle = fight
# arrow = quest marker
# turtle = weapon shops
# triangle = potion shop
# hollow circle = chest
turtle.shape("circle")
turtle.pu()
for x, y in SPIDER_LOCATIONS:
grid_setpos(x, y, unit)
turtle.stamp()
for x, y in TREASURE_LOCATIONS:
turtle.pu()
grid_setpos(x, y - 0.25, unit)
turtle.pd()
turtle.circle(unit // 4)
turtle.shape("square")
turtle.pu()
for x, y in TOWN_LOCATIONS:
grid_setpos(x, y, unit)
turtle.stamp()
turtle.speed('fastest')
map_starting_area(GRID_SIZE, GRID_UNIT)
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')
turtle.showturtle()
turtle_x, turtle_y = 0, 0
grid_setpos(0, 0, GRID_UNIT)
while True:
direction = input('would you like to go north, south, east or west: ').lower()
if direction == 'north':
if turtle_y < HALF_GRID:
turtle_y += 1
else:
print("You hit a wall")
elif direction == 'south':
if turtle_y > -HALF_GRID:
turtle_y -= 1
else:
print("You hit a wall")
elif direction == 'east':
if turtle_x < HALF_GRID:
turtle_x += 1
else:
print("You hit a wall")
elif direction == 'west':
if turtle_x > -HALF_GRID:
turtle_x -= 1
else:
print("You hit a wall")
elif direction == 'quit':
break
grid_setpos(turtle_x, turtle_y, GRID_UNIT)
if (turtle_x, turtle_y) in SPIDER_LOCATIONS:
combat()
if (turtle_x, turtle_y) in TOWN_LOCATIONS:
town()
if (turtle_x, turtle_y) in TREASURE_LOCATIONS:
treasure()
要解决的要点:如何 "trigger" 海龟撞到特定位置时的事件。我已经通过对您的代码进行一些更改来解决这个问题:
首先,由于您的乌龟将始终在网格上,因此我将 turtle_x
和 turtle_y
与乌龟分开跟踪,作为整数。这很重要,因为整数可以 适当地 测试是否相等。当乌龟位于浮点位置时,实际上可能无法检查它是否位于特定点。网格中的每个单元格都是 -HALF_GRID
到 HALF_GRID
范围内的一个坐标,位置由 1
或 -1
离散更改。现在您还需要知道每个 "event" 在哪里。我通过将许多事件的位置存储在文件开头的全局常量列表中来完成此操作。顺便说一句,我还更改了您的地图绘制方法,以在这些列表上使用迭代,并为网格绘制使用 for 循环,因为这更清晰(它使代码更易于阅读,因此对我来说更容易调试,以及更短)。我希望您同意 map_starting_area
现在更容易理解。由于被跟踪的坐标现在是整数,因此每当调用 setpos
时它们都需要是 'scaled up'。我通过编写辅助函数 grid_setpos
来完成此操作,用于在网格中设置位置。
简而言之,它现在存储了一对 整数 用作海龟的位置,因此它可以测试是否有任何东西等于 then。然后它对海龟进行一些数学运算(将每个坐标放大 GRID_UNIT
)以将整数坐标映射回网格。
之前,乌龟的坐标是浮点数。这意味着它们是 'decimal' 个数字(这足够接近以更正它无关紧要)。但是,正如您在网格上一样,您实际上并不需要十进制数。网格上的坐标是 离散的 (整数)。因此,坐标现在被存储为整数。这意味着可以测试它们是否 完全 相等。它还使用蜘蛛坐标列表,并使用一些快速 Python 查看当前坐标是否在此列表中:if (turtle_x, turtle_y) in SPIDER_LOCATIONS:
.
我已经将您处理事件的方法(combat()
、town()
、treasure()
)简化到可以证明它们正在工作的程度,仅此而已对于这个问题是必要的。您可以扩展它们中的每一个来扩展您的游戏。
我还更改了您的变量名。在 Python 中,通常约定使用 underscore_case
,或者可能 camelCase
用于变量和函数,但从不使用 CapitalCase
(因为这是用于 classes),据我所知,Capital_Underscore_Case
几乎从未被使用过。
小改动:
- 导入语句现在都在顶部
- 从地图逻辑中分离网格逻辑
- 当你是 hiding/showing 乌龟
时的一些其他错误
如果您想对此进行更多概括,并且具有不同的位置、不同的事件属性等,我建议您开始编写一些 classes(具有诸如 x
、 y
,也许还有像 encounter
这样的实例方法。这超出了这个问题的范围(用海龟坐标进行相等性测试),但是 [=77 上有很多 tutorials =] classes 和它们的用途。我建议你从简单开始,然后构建到与这个项目的集成。如果你真的坚持你的 class 实现,你当然可以随时问另一个问题。
回应您的评论:通常不建议为每个城镇等创建单独的变量。列表适用于当您有很多需要表现相同的事物时。想象一下,如果你有 50 个城镇,都在不同的变量中。如果您希望每个城镇跟踪 name/have 不同的事件等,请参阅我关于 classes 的部分。在更好地熟悉 OO 编程之后,您将需要一个 Town 实例列表。如果您在尝试使用它们后真的对 classes 感到困惑,post 一个关于您的问题的具体问题以及您到目前为止所得到的
有没有办法根据乌龟的位置调用预设的代码行?
我试过使用 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()
turtle.pos
是函数
import turtle
print(turtle.pos)
turtle.pos == (0.00,50.00) # is always False
turtle.pos() == (0.00, 0.00) # might be what you want
您的代码中存在许多问题,我希望能够解决:
#the imports are now all at the start - this is generally considered the way to go
import turtle
import random
import time
#constant declarations
GRID_UNIT = 50
GRID_SIZE = 5
HALF_GRID = GRID_SIZE // 2
SPIDER_LOCATIONS = [(1, 1),
(-1, 0)]
TREASURE_LOCATIONS = [(1, -2),
(-2, -1)]
TOWN_LOCATIONS = [(-2, -2),
(2, -2),
(-2, 2),
(2, 2)]
#simplified event functions
def combat():
print("You stepped on a spider!")
def treasure():
print("You found treasure!")
def town():
print("You entered a town")
#scaled setpos for use with int grid coords
def grid_setpos(x, y, scale):
turtle.setpos((x - 0.5) * scale, (y - 0.5) * scale)
#draw a grid
def draw_grid(size, unit):
for x in range(-size // 2, size // 2 + 1):
turtle.pu()
turtle.setpos(x * unit, -size // 2 * unit)
turtle.pd()
turtle.setpos(x * unit, size // 2 * unit)
turtle.pu()
turtle.setpos(-size // 2 * unit, x * unit)
turtle.pd()
turtle.setpos(size // 2 * unit, x * unit)
#draw the map, iterating over stored global coordinates
def map_starting_area(size, unit):
draw_grid(size, unit)
# square = town
# circle = fight
# arrow = quest marker
# turtle = weapon shops
# triangle = potion shop
# hollow circle = chest
turtle.shape("circle")
turtle.pu()
for x, y in SPIDER_LOCATIONS:
grid_setpos(x, y, unit)
turtle.stamp()
for x, y in TREASURE_LOCATIONS:
turtle.pu()
grid_setpos(x, y - 0.25, unit)
turtle.pd()
turtle.circle(unit // 4)
turtle.shape("square")
turtle.pu()
for x, y in TOWN_LOCATIONS:
grid_setpos(x, y, unit)
turtle.stamp()
turtle.speed('fastest')
map_starting_area(GRID_SIZE, GRID_UNIT)
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')
turtle.showturtle()
turtle_x, turtle_y = 0, 0
grid_setpos(0, 0, GRID_UNIT)
while True:
direction = input('would you like to go north, south, east or west: ').lower()
if direction == 'north':
if turtle_y < HALF_GRID:
turtle_y += 1
else:
print("You hit a wall")
elif direction == 'south':
if turtle_y > -HALF_GRID:
turtle_y -= 1
else:
print("You hit a wall")
elif direction == 'east':
if turtle_x < HALF_GRID:
turtle_x += 1
else:
print("You hit a wall")
elif direction == 'west':
if turtle_x > -HALF_GRID:
turtle_x -= 1
else:
print("You hit a wall")
elif direction == 'quit':
break
grid_setpos(turtle_x, turtle_y, GRID_UNIT)
if (turtle_x, turtle_y) in SPIDER_LOCATIONS:
combat()
if (turtle_x, turtle_y) in TOWN_LOCATIONS:
town()
if (turtle_x, turtle_y) in TREASURE_LOCATIONS:
treasure()
要解决的要点:如何 "trigger" 海龟撞到特定位置时的事件。我已经通过对您的代码进行一些更改来解决这个问题:
首先,由于您的乌龟将始终在网格上,因此我将 turtle_x
和 turtle_y
与乌龟分开跟踪,作为整数。这很重要,因为整数可以 适当地 测试是否相等。当乌龟位于浮点位置时,实际上可能无法检查它是否位于特定点。网格中的每个单元格都是 -HALF_GRID
到 HALF_GRID
范围内的一个坐标,位置由 1
或 -1
离散更改。现在您还需要知道每个 "event" 在哪里。我通过将许多事件的位置存储在文件开头的全局常量列表中来完成此操作。顺便说一句,我还更改了您的地图绘制方法,以在这些列表上使用迭代,并为网格绘制使用 for 循环,因为这更清晰(它使代码更易于阅读,因此对我来说更容易调试,以及更短)。我希望您同意 map_starting_area
现在更容易理解。由于被跟踪的坐标现在是整数,因此每当调用 setpos
时它们都需要是 'scaled up'。我通过编写辅助函数 grid_setpos
来完成此操作,用于在网格中设置位置。
简而言之,它现在存储了一对 整数 用作海龟的位置,因此它可以测试是否有任何东西等于 then。然后它对海龟进行一些数学运算(将每个坐标放大 GRID_UNIT
)以将整数坐标映射回网格。
之前,乌龟的坐标是浮点数。这意味着它们是 'decimal' 个数字(这足够接近以更正它无关紧要)。但是,正如您在网格上一样,您实际上并不需要十进制数。网格上的坐标是 离散的 (整数)。因此,坐标现在被存储为整数。这意味着可以测试它们是否 完全 相等。它还使用蜘蛛坐标列表,并使用一些快速 Python 查看当前坐标是否在此列表中:if (turtle_x, turtle_y) in SPIDER_LOCATIONS:
.
我已经将您处理事件的方法(combat()
、town()
、treasure()
)简化到可以证明它们正在工作的程度,仅此而已对于这个问题是必要的。您可以扩展它们中的每一个来扩展您的游戏。
我还更改了您的变量名。在 Python 中,通常约定使用 underscore_case
,或者可能 camelCase
用于变量和函数,但从不使用 CapitalCase
(因为这是用于 classes),据我所知,Capital_Underscore_Case
几乎从未被使用过。
小改动:
- 导入语句现在都在顶部
- 从地图逻辑中分离网格逻辑
- 当你是 hiding/showing 乌龟 时的一些其他错误
如果您想对此进行更多概括,并且具有不同的位置、不同的事件属性等,我建议您开始编写一些 classes(具有诸如 x
、 y
,也许还有像 encounter
这样的实例方法。这超出了这个问题的范围(用海龟坐标进行相等性测试),但是 [=77 上有很多 tutorials =] classes 和它们的用途。我建议你从简单开始,然后构建到与这个项目的集成。如果你真的坚持你的 class 实现,你当然可以随时问另一个问题。
回应您的评论:通常不建议为每个城镇等创建单独的变量。列表适用于当您有很多需要表现相同的事物时。想象一下,如果你有 50 个城镇,都在不同的变量中。如果您希望每个城镇跟踪 name/have 不同的事件等,请参阅我关于 classes 的部分。在更好地熟悉 OO 编程之后,您将需要一个 Town 实例列表。如果您在尝试使用它们后真的对 classes 感到困惑,post 一个关于您的问题的具体问题以及您到目前为止所得到的