有一个while循环使用raw_input关键字不断允许用户输入,然后该输入被传递给一个名为commands()的函数,然后该函数将输入路由到用户指定的命令。在这种情况下,每当我在代码中调用函数移动时,它会使用户按下函数输入并且在用户确实按Enter键之前不会处理数据。
此外,似乎程序将函数运动()中的部分代码返回到commands()函数,因此给出了以下故障:
target leo2
Successful lock on Leo2
launch
Your mobile suit shifts as you begin moving towards Leo2 at coordinates [300, 100, 100]Invalid command
Type 'commands' for a list of valid commands
Distance from suit is: 200
正如您所看到的,在函数的中间,它试图从主模块运行主whileloop,但它然后返回到中间运行函数。为什么会发生这种情况,我该如何解决?
我的代码:
Gundam2.py
import math
class Mobilesuits:
#class global variables/methods here
instances = [] #grid cords here
names=[]
def __init__(self,armor,speed,name,description,cockpit_description,\
radar_range, coordinates):
Mobilesuits.instances.append(self)
Mobilesuits.names.append(name)
self.armor=armor
self.speed=speed
self.name=name
self.description=description
self.cockpit_description=cockpit_description
self.radar_range=radar_range
self.coordinates=coordinates
#Intrinsically links mobile suits objects to their names.
#I want to be able to access each object directly through the name
def update_names(self):
Mobilesuit_names_instances_dictionary={}
for i in range(len(Mobilesuits.instances)):
Mobilesuit_names_instances_dictionary[Mobilesuits.names[i]] =Mobilesuits.instances[i]
return Mobilesuit_names_instances_dictionary
def can_detect(self, other):
for own_coord, other_coord in zip(self.coordinates, other.coordinates):
if abs(own_coord - other_coord) > self.radar_range:
return False
return True
def radar(self):
for other in Mobilesuits.instances:
if other is not self and self.can_detect(other):
print "%s detected at %s" % (other.description, other.coordinates)
def movement(self, target, official_target, currentmobilesuit):
print("Your mobile suit shifts as you begin moving towards %s at coordinates %s" %(target,official_target.coordinates))
distance = (currentmobilesuit.coordinates[0] - official_target.coordinates[0],\
currentmobilesuit.coordinates[1] - official_target.coordinates[1], \
currentmobilesuit.coordinates[2]-official_target.coordinates[2])
calculation=0
for i in distance:
calculation += abs(i)
print("Distance from suit is: %s" % (calculation))
#Make speed calculation based on distance away and speed of suit
#Errors:
#target without a suit gives error
maingundam.py
from Gundam2 import Mobilesuits
import thread
import time
#Main Variable/Object declarations:
Leo1=Mobilesuits(100,100,"Leo1","leo desc","dockpit desc",100,[100,100,100])
Leo2=Mobilesuits(100,100,"Leo2","leo desc","dockpit desc",100,[300,100,100])
Leo3=Mobilesuits(100,100,"Leo3","leo desc","dockpit desc",100,[100,150,100])
currentmobilesuit=Leo1
mobile_suit_names_list=currentmobilesuit.update_names()
#Main Function declarations
def commands(user_input,currentmobilesuit, mobile_suit_names_list):
#radar
if user_input == "radar":
currentmobilesuit.radar()
#Commands overview
elif user_input == "commands":
print("Command list:\nradar\ntarget (object)")
#Target objects
elif user_input.startswith("target"):
try:
global target
target=user_input.split()[1].title()
if mobile_suit_names_list[target]:
global official_target
official_target=mobile_suit_names_list[target]
print("Successful lock on %s" % (target))
except:
print("space object '%s' not detected" %(target))
elif user_input=="launch":
# try:
if official_target:
thread.start_new_thread(currentmobilesuit.movement, (target, official_target, currentmobilesuit))
# except:
# print("No target is selected")
#command not found
else:
print("Invalid command\nType 'commands' for a list of valid commands")
def active_delay(action, delay):
time.sleep(delay)
action()
#Main execution: paused
while True:
mobile_suit_names_list=currentmobilesuit.update_names()
commands(raw_input(),currentmobilesuit,mobile_suit_names_list)
答案 0 :(得分:0)
您的第二个问题是,如果您的用户只是点击返回,则raw_input
的返回值为空字符串""
,这是不由command()
处理。您可以通过添加以下行来解决此问题:
elif user_input == "":
pass
你的第一个问题是raw_input
的运作方式;它始终等待用户输入,然后是返回,并且不设计用于连续控制输入。这很难修复。您可能最好使用专为此类事物设计的库(连续键盘控制),如pygame
。