当人们输入像南方这样的欲望位置时,我似乎无法找到一种方法让乌龟转到地图上的特定区域我知道当使用setx和sety线时它会将它发送到我绘制的coords绳索 x起始区域175每个平方是50分开| Y起始区域380每个平方是相隔50。因为我想在学校展示,所以我不使用Pygame
from tkinter import *
import tkinter.messagebox as tm
####################LOUIS'S STORY###################
class Theif_Story():
def Map_Starting_Area():
import turtle
# X axis = 1 -> 5
# Y axis = A -> E
########Compass#######
turtle.speed(99)
turtle.pu()
turtle.rt(90)
turtle.fd(150)
turtle.pd()
turtle.fd(75)
turtle.backward(150)
turtle.fd(75)
turtle.left(90)
turtle.fd(75)
turtle.backward(150)
turtle.pu()
turtle.backward(10)
turtle.write('W')
turtle.fd(10)
turtle.pd()
turtle.fd(150)
turtle.pu()
turtle.fd(10)
turtle.pd()
turtle.write('E')
turtle.pu()
turtle.backward(10)
turtle.backward(75)
turtle.rt(90)
turtle.fd(75)
turtle.pu()
turtle.fd(20)
turtle.pd()
turtle.write('S')
turtle.pu()
turtle.backward(10)
turtle.backward(170)
turtle.pd()
turtle.write('N')
turtle.pu()
turtle.backward(225)
turtle.pd()
####################
#######Grid###########
turtle.left(90)
turtle.ht()
turtle.left(90)
turtle.forward(250)
turtle.right(90)
turtle.forward(250)
turtle.right(90)
turtle.forward(250)
turtle.right(90)
turtle.forward(250)
turtle.backward(50)
turtle.right(90)
turtle.forward(250)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(250)
turtle.backward(250)
turtle.left(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(250)
turtle.backward(250)
turtle.left(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(250)
turtle.backward(250)
turtle.left(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(250)
turtle.backward(250)
turtle.left(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(250)
turtle.backward(250)
turtle.left(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(250)
turtle.backward(250)
turtle.left(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(250)
turtle.backward(250)
turtle.left(90)
turtle.st()
############
# Square = Town
# Circle = Fight
# Arrow = Quest Marker
# Turtle = Weapon Shops
# Triangle = Potion Shop
# hollow circle = chest
turtle.backward(175)
turtle.right(90)
turtle.pu()
turtle.forward(75)
turtle.shape("square")
turtle.stamp()
turtle.shape("classic")
turtle.forward(150)
turtle.left(90)
turtle.fd(150)
turtle.shape("square")
turtle.stamp()
turtle.right(90)
turtle.shape("arrow")
turtle.color("blue")
turtle.stamp()
turtle.left(90)
turtle.color("black")
turtle.shape("classic")
turtle.lt(90)
turtle.fd(100)
turtle.shape("circle")
turtle.stamp()
turtle.shape("classic")
turtle.left(90)
turtle.forward(100)
turtle.shape("circle")
turtle.stamp()
turtle.shape("classic")
turtle.rt(90)
turtle.fd(100)
turtle.rt(90)
turtle.fd(50)
turtle.shape("circle")
turtle.stamp()
turtle.shape("classic")
turtle.rt(90)
turtle.fd(200)
turtle.right(90)
turtle.fd(100)
turtle.rt(90)
turtle.fd(10)
turtle.pd()
turtle.left(90)
turtle.circle(10)
turtle.rt(90)
turtle.pu()
turtle.fd(140)
turtle.color('blue')
turtle.rt(90)
import random
import time
import turtle
Wooden_Chest_Item_List = 'Wooden Short Blade', 'Wooden Axe', 'Wooden Stick', 'Wooden Long Blade', 'Stone Short Blade', 'Stone Axe', 'Stone Wand', 'Stone Long Blade', 'Iron Short Blade', 'Iron Axe', 'Iron Wand', 'Iron Long Blade', 'Diamond Short Blade', 'Diamond Axe', 'Diamond Wand', 'Diamond Long Blade'
Weapon_Chest = random.choice(Wooden_Chest_Item_List)
Potion_Use_Per_Battle = 3
Life = 100
Foe = random.randint(25, 150)
Player_Dmg = 25
Magic_Dmg = 10
Foe_Dmg = 15
Fists = Player_Dmg
Weapon = Fists
Wooden_Short_Blade = Player_Dmg + 3
Wooden_Stick = Magic_Dmg + 5
Wooden_Axe = Player_Dmg + 4
Wooden_Long_Blade = Player_Dmg + 5
Stone_Short_Blade = Player_Dmg + 5
Stone_Axe = Player_Dmg + 6
Stone_Wand = Magic_Dmg + 7
Stone_Long_Blade = Player_Dmg + 7
Iron_Short_Blade = Player_Dmg + 7
Iron_Axe = Player_Dmg + 8
Iron_Wand = Magic_Dmg + 9
Iron_Long_Blade = Player_Dmg + 9
Diamond_Short_Blade = Player_Dmg + 9
Diamond_Axe = Player_Dmg + 10
Diamond_Wand = Magic_Dmg + 11
Diamond_Long_Blade = Player_Dmg + 11
Bupe_Doop = Player_Dmg + Magic_Dmg + 5 ##Louis's Custom Weopon
Small_Health_Potion_Amount = random.randint(0,3)
Medium_Health_Potion_Amount = random.randint(0,3)
Large_Health_Potion_Amount = random.randint(0,3)
print('You Continue with your Journey')
print('You leave the protection of your farm and head into the open land')
print(Map_Starting_Area())
print('You Come to a cross road')
print('Would you like to go South, West or East ')
Direction = str(input())
if Direction == 'South':
turtle.sety(330)
turtle.update()
elif Direction == 'East':
turtle.setx(225)
turtle.update()
elif Direction == 'West':
turtle.setx(125)
turtle.update()
print('Which way do you go')
Direction2 = str(input())
if Direction2 == 'South':
turtle.sety(330) #Stuck on this part since theirs 3 diffrent solution
turtle.update()
elif Direction2 == 'East':
turtle.setx(225)
turtle.update
elif Direction2 == 'West':
turtle.setx(125)
turtle.update()
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("""Spider Appers, You Need to do 10 dmg to kill this enemy!!!""")
print('============================================')
while While_Loop == 1:
if Foe >= 0:
time.sleep(1)
print('You did', Player_Dmg,'Dmg')
Foe = Foe - Player_Dmg
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!')
turtle.backward(100)
elif Direction2 == 'South':
turtle.fd(50)
答案 0 :(得分:1)
我已经提取了足够的下面的代码来创建一个示例,让您可以使用命令North,South,East&amp; amp ;,将乌龟(蓝色圆圈)移动到5 x 5网格周围。 West在控制台:
import turtle
GRID_UNIT = 50
GRID_SIZE = 5 # code below assumes this is an odd number
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.home()
turtle.st()
turtle.speed('fastest')
Map_Starting_Area()
turtle.color('blue')
turtle.shape('circle')
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)
elif direction == 'south' and turtle.ycor() > -GRID_UNIT * (GRID_SIZE//2 - 0.5):
turtle.sety(turtle.ycor() - GRID_UNIT)
elif direction == 'east' and turtle.xcor() < GRID_UNIT * (GRID_SIZE//2 - 0.5):
turtle.setx(turtle.xcor() + GRID_UNIT)
elif direction == 'west' and turtle.xcor() > -GRID_UNIT * (GRID_SIZE//2 - 0.5):
turtle.setx(turtle.xcor() - GRID_UNIT)
elif direction == 'quit':
break
该代码可以防止您的乌龟离开网格。我将网格集中在(0,0)以简化逻辑。希望您可以将其纳入您的程序以控制龟的动作。
通常情况下,我会避免在龟程序中使用while True:
,这应该是基于事件的,但是现在它似乎是最简单的解决方案。