Python 如何使数字在整个运行时的一段时间内缓慢递增

Python 如何使数字在整个运行时的一段时间内缓慢递增,python,Python,我正在尝试制作一个基于文本的游戏,其中用户是太空中的飞行员。我想创建一个运动系统,但不确定如何做。我希望用户能够输入所需的网格坐标,他的车辆将开始改变其网格坐标,以越来越接近他输入的坐标 现在,要做到这一点,我可能需要多线程和时间元素。但我不确定如何使用时间元素。任何建议都非常感谢,我只是想在这里学习。谢谢大家 from Gundam2 import Mobilesuits #Main Variable/Object declarations: Leo1=Mobilesuits(100,10

我正在尝试制作一个基于文本的游戏,其中用户是太空中的飞行员。我想创建一个运动系统,但不确定如何做。我希望用户能够输入所需的网格坐标,他的车辆将开始改变其网格坐标,以越来越接近他输入的坐标

现在,要做到这一点,我可能需要多线程和时间元素。但我不确定如何使用时间元素。任何建议都非常感谢,我只是想在这里学习。谢谢大家

from Gundam2 import Mobilesuits

#Main Variable/Object declarations:

Leo1=Mobilesuits(100,100,"Leo","leo desc","dockpit desc",100,[100,100,100])
Leo2=Mobilesuits(100,100,"Leo","leo desc","dockpit desc",100,[300,100,100])
Leo3=Mobilesuits(100,100,"Leo","leo desc","dockpit desc",100,[100,150,100])
currentmobilesuit=Leo1

#Main Function declarations               
def commands(user_input,currentmobilesuit):
    if user_input == "radar":
        currentmobilesuit.radar()
    elif user_input == "commands":
        print("Command list:\nradar")
    else:
        print("Invalid command\nType 'commands' for a list of valid commands")


#Main execution
while True:
    commands(raw_input(),currentmobilesuit)



class Mobilesuits:
    #class global variables/methods here
    instances = [] #grid cords here
    def __init__(self,armor,speed,name,description,cockpit_description,\
                 radar_range, coordinates):
        Mobilesuits.instances.append(self)
        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



    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)
游戏通常有某种“主循环”;你的在这里:

#Main execution
while True:
    commands(raw_input(),currentmobilesuit)
最简单的方法是在循环中计数:

#Main execution
turn_count = 0
while True:
    commands(raw_input(),currentmobilesuit)
    turn_count += 1
如果您希望所采用的实时时间对计数器产生一定影响,或者成为计数器,则可以从调用
time.time()
time
模块中获取当前时间

还有一些其他想法:

制作一个
游戏
类,并将转向计数器和游戏循环放入其中

命令
功能返回一个数字,该数字是命令执行期间发生的时间单位数;例如,输入无效命令可能需要0次,而修复机器人可能需要5次

#Main execution
turn_count = 0
while True:
    turns_taken = commands(raw_input(),currentmobilesuit)
    turn_count += turns_taken
游戏通常有某种“主循环”;你的在这里:

#Main execution
while True:
    commands(raw_input(),currentmobilesuit)
最简单的方法是在循环中计数:

#Main execution
turn_count = 0
while True:
    commands(raw_input(),currentmobilesuit)
    turn_count += 1
如果您希望所采用的实时时间对计数器产生一定影响,或者成为计数器,则可以从调用
time.time()
time
模块中获取当前时间

还有一些其他想法:

制作一个
游戏
类,并将转向计数器和游戏循环放入其中

命令
功能返回一个数字,该数字是命令执行期间发生的时间单位数;例如,输入无效命令可能需要0次,而修复机器人可能需要5次

#Main execution
turn_count = 0
while True:
    turns_taken = commands(raw_input(),currentmobilesuit)
    turn_count += turns_taken
游戏通常有某种“主循环”;你的在这里:

#Main execution
while True:
    commands(raw_input(),currentmobilesuit)
最简单的方法是在循环中计数:

#Main execution
turn_count = 0
while True:
    commands(raw_input(),currentmobilesuit)
    turn_count += 1
如果您希望所采用的实时时间对计数器产生一定影响,或者成为计数器,则可以从调用
time.time()
time
模块中获取当前时间

还有一些其他想法:

制作一个
游戏
类,并将转向计数器和游戏循环放入其中

命令
功能返回一个数字,该数字是命令执行期间发生的时间单位数;例如,输入无效命令可能需要0次,而修复机器人可能需要5次

#Main execution
turn_count = 0
while True:
    turns_taken = commands(raw_input(),currentmobilesuit)
    turn_count += turns_taken
游戏通常有某种“主循环”;你的在这里:

#Main execution
while True:
    commands(raw_input(),currentmobilesuit)
最简单的方法是在循环中计数:

#Main execution
turn_count = 0
while True:
    commands(raw_input(),currentmobilesuit)
    turn_count += 1
如果您希望所采用的实时时间对计数器产生一定影响,或者成为计数器,则可以从调用
time.time()
time
模块中获取当前时间

还有一些其他想法:

制作一个
游戏
类,并将转向计数器和游戏循环放入其中

命令
功能返回一个数字,该数字是命令执行期间发生的时间单位数;例如,输入无效命令可能需要0次,而修复机器人可能需要5次

#Main execution
turn_count = 0
while True:
    turns_taken = commands(raw_input(),currentmobilesuit)
    turn_count += turns_taken

您可以使用非阻塞I/O。这将帮助您避免线程的复杂性。以下是使用stdin的非阻塞读取实现的示例代码:

#!/usr/bin/python
import sys
import select
call_count = 0
#Main Function declarations
def commands(user_input):
   global call_count
   if len(user_input) > 0:
      print('call count: ' + str(call_count) + ' user entered: ' + user_input)

def raw_input_no_block():
   global call_count
   call_count = call_count + 1
   input_avail = select.select([sys.stdin], [], [], 0.1)[0] #wait for 0.1 seconds
   if input_avail:
      return sys.stdin.readline()
   else:
      return ''

#Main execution
while True:
    commands(raw_input_no_block())

您可以使用非阻塞I/O。这将帮助您避免线程的复杂性。以下是使用stdin的非阻塞读取实现的示例代码:

#!/usr/bin/python
import sys
import select
call_count = 0
#Main Function declarations
def commands(user_input):
   global call_count
   if len(user_input) > 0:
      print('call count: ' + str(call_count) + ' user entered: ' + user_input)

def raw_input_no_block():
   global call_count
   call_count = call_count + 1
   input_avail = select.select([sys.stdin], [], [], 0.1)[0] #wait for 0.1 seconds
   if input_avail:
      return sys.stdin.readline()
   else:
      return ''

#Main execution
while True:
    commands(raw_input_no_block())

您可以使用非阻塞I/O。这将帮助您避免线程的复杂性。以下是使用stdin的非阻塞读取实现的示例代码:

#!/usr/bin/python
import sys
import select
call_count = 0
#Main Function declarations
def commands(user_input):
   global call_count
   if len(user_input) > 0:
      print('call count: ' + str(call_count) + ' user entered: ' + user_input)

def raw_input_no_block():
   global call_count
   call_count = call_count + 1
   input_avail = select.select([sys.stdin], [], [], 0.1)[0] #wait for 0.1 seconds
   if input_avail:
      return sys.stdin.readline()
   else:
      return ''

#Main execution
while True:
    commands(raw_input_no_block())

您可以使用非阻塞I/O。这将帮助您避免线程的复杂性。以下是使用stdin的非阻塞读取实现的示例代码:

#!/usr/bin/python
import sys
import select
call_count = 0
#Main Function declarations
def commands(user_input):
   global call_count
   if len(user_input) > 0:
      print('call count: ' + str(call_count) + ' user entered: ' + user_input)

def raw_input_no_block():
   global call_count
   call_count = call_count + 1
   input_avail = select.select([sys.stdin], [], [], 0.1)[0] #wait for 0.1 seconds
   if input_avail:
      return sys.stdin.readline()
   else:
      return ''

#Main execution
while True:
    commands(raw_input_no_block())

我将从以下问题和答案开始:。我将从以下问题和答案开始:。我将从以下问题和答案开始:。主循环是此处的关键,因为您希望确保大量代码可以访问这些值。游戏循环通常计算“自上次更新开始以来经过的实时时间”,然后将该值传递给所有活动对象中的更新函数,以便它们可以根据时间(移动等)进行操作,而无需单独计算时间增量主循环是关键,因为您希望确保很多代码都可以访问这些值。游戏循环通常计算“自上次更新开始以来经过的实时时间”,然后将该值传递给所有活动对象中的更新函数,以便它们可以根据时间(移动等)进行操作,而无需单独计算时间增量主循环是关键,因为您希望确保很多代码都可以访问这些值。游戏循环通常计算“自上次更新开始以来经过的实时时间”,然后将该值传递给所有活动对象中的更新函数,以便它们可以根据时间(移动等)进行操作,而无需单独计算时间增量主循环是关键,因为您希望确保很多代码都可以访问这些值。游戏循环通常会计算“自上次更新开始以来经过的实时时间”,然后将该值传递给所有活动对象中的更新函数,以便它们可以根据时间(移动等)进行操作,而无需单独计算时间增量