gpt4 book ai didi

python - Python Coords返回变量

转载 作者:行者123 更新时间:2023-11-28 18:19:49 24 4
gpt4 key购买 nike

有没有办法根据海龟的位置来调用预设的代码行?
我试过使用turtle.pos()xcor/ycor但是我无法让它调用代码。
我试过:

if turtle.pos == (0.00,50.00):
combat()

我是新来的蟒蛇龟。

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

最佳答案

我希望您的代码中有一些问题已经修复:

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

主要的观点是:当海龟撞到某个地方时,如何“触发”一个事件。我对你的代码做了一些修改:
首先,因为你的乌龟总是在一个网格上,所以我以整数的形式分别跟踪 turtle_xturtle_y。这一点很重要,因为可以正确地测试整数是否相等。当海龟处于浮点数位置时,实际上可能无法检查它是否位于特定点。网格中的每个单元格是 -HALF_GRIDHALF_GRID范围内的一个坐标,其位置由 1-1离散地改变。现在你还需要知道每个“事件”在哪里。我通过在文件开头的全局常量列表中存储一些事件的位置来实现这一点。顺便说一下,我还更改了地图绘制方法,以便在这些列表上使用迭代,并对网格绘制的循环使用迭代,因为这样做更清楚(它使代码更易于阅读,因此对我来说更容易调试,而且也更短)。我希望你会同意 map_starting_area现在更容易理解了。由于正在跟踪的坐标现在是整数,因此每当调用 setpos时,都需要将它们“放大”。我通过编写一个helper函数来设置网格中的位置。
简言之,它现在存储了一对整数作为海龟的位置,因此它可以测试是否有任何东西等于那个位置。然后它对海龟做一些数学运算(将每个坐标向上缩放 grid_setpos),将整数坐标映射回网格。
以前,海龟的坐标是浮点数。这意味着它们是“十进制”数字(这已经足够接近于纠正,这并不重要)。但是,由于您在网格上,实际上不需要十进制数。网格上的坐标是离散的(整数)。因此,坐标现在被存储为整数。这意味着可以测试它们是否完全相等。它还使用蜘蛛的坐标列表,并使用一些快速的Python查看当前坐标是否在这个列表中: GRID_UNIT
我已经简化了你处理事件( if (turtle_x, turtle_y) in SPIDER_LOCATIONS:combat()town())的方法,使之能够证明它们是有效的,因为这是这个问题所必需的。你可以对它们中的每一个进行扩展来扩展你的游戏。
我还更改了变量名。在Python中,对于变量和函数通常使用 treasure(),或者可能使用 underscore_case,但据我所知,从未使用过 camelCase(因为这是针对类的),而且几乎从未使用过 CapitalCase
小改动:
导入语句现在都在顶部
从映射逻辑中分离网格逻辑
当你把海龟藏起来/展示给别人看的时候,还有一些虫子
如果您想更广泛地了解这一点,并且有不同的位置、事件的不同属性等等,我建议您开始编写一些类(带有属性,如 Capital_Underscore_Casex,可能还有实例方法,如 y)。这超出了这个问题的范围(用turtle坐标进行相等测试),但在Python类及其使用上有很多 tutorials。我建议你从简单的开始,建立与这个项目的集成。如果你真的被你的类实现困住了,你当然可以总是问另一个问题。
针对您的评论:一般不建议为每个城镇创建单独的变量。当你有很多事情需要表现得一样的时候,列表就可以了。想象一下如果你有50个城镇,都是独立变量。如果你想让每个城镇都知道一个名字/有不同的活动等等,请参考我关于课程的部分。在更好地熟悉了OO编程之后,您需要一个城镇实例列表。如果你在尝试使用类之后真的对它们感到困惑,那么就把一个关于你的问题的具体问题和你目前所拥有的东西一起发布出来

关于python - Python Coords返回变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45920085/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com