多个循环从函数导入和导出

时间:2016-02-18 00:20:37

标签: python

我对Python很陌生,我只有大约一年的时间来修补它,我已经被我的最新想法所困扰,我喜欢制作简单的愚蠢游戏而且我认为我已经离开这个了一点,基本上它是一个cookie点击式游戏,我有一个功能,我试图从脚本导入总量然后进入一个循环,它需要的金额增加了你有多少桶(bvats和bvatl只是为了我以后可以做一个商店并更改变量以使它更有效地生成const)然后将该值导出到脚本和在循环之前睡一会儿。第二个功能是你做列出命令的动作或你最终去商店的地方,它会不断被提示,你也可以看到你所拥有的海岸总量,我的问题是循环停止产生(或者第二个功能停止显示一个接一个的海岸不断增加的数量,所以如果有人愿意帮助它将非常感激。谢谢!

# coding: utf-8
# coding: utf-8
#Imports and vaules:
import time
import threading 
import thread
from threading import Thread
game = "game"
over = "over"
#const is money
Const = 0 
#vat stuff
bvats = 1
bvatl = 4
#Game start
def Vats():
    global bvatl
    global bvats
    global Const
    while bvats >= 1:
        Const = Const + 1 * int(bvats)
        return Const
        time.sleep(int(bvatl))

def Action():
    while game != over:
        action = raw_input("Yes Boss? Did you need something? ")
        if action == "List":
            print "List: Displays this message, try it in the shop it will list the items available"            
            time.sleep(0.5)
            print "Shop: Goes to the shop"
            time.sleep(0.5)
            print "Const: Displays how much Const™ you have in the bank"
            time.sleep(0.5)
        elif action == "Const":
            print "You have " + str(Const) + " Const™ in the bank" 

t1 = Thread(target = Vats())
t2 = Thread(target = Action())
t1.start()
t2.start()

1 个答案:

答案 0 :(得分:0)

您的Action()方法看起来没有从Const方法获取Vats()值。一种方法是使用Queue在两种方法之间传递Const值。我从Python Cookbook在线程之间进行通信一节中提到了这个想法(注意:我添加了一些额外的行,所以" Ctrl-C"我可以退出程序测试了它):

# coding: utf-8
# coding: utf-8
#Imports and vaules:
import time
import threading 
import thread
from threading import Thread
from Queue import Queue
game = "game"
over = "over"
#const is money
Const = 0 
#vat stuff
bvats = 1
bvatl = 4
#Game start
def Vats(out_q):
    global bvatl
    global bvats
    global Const
    while bvats >= 1:
        Const = Const + 1 * int(bvats)
        #return Const
        time.sleep(int(bvatl))
        out_q.put(Const)

def Action(in_q):
    while game != over:
        Const = in_q.get()
        action = raw_input("Yes Boss? Did you need something? ")
        if action == "List":
            print "List: Displays this message, try it in the shop it will list the items available"            
            time.sleep(0.5)
            print "Shop: Goes to the shop"
            time.sleep(0.5)
            print "Const: Displays how much Const™ you have in the bank"
            time.sleep(0.5)
        elif action == "Const":
            print "You have " + str(Const) + " Const™ in the bank" 

q = Queue()
t1 = Thread(target = Vats, args=(q,))
t2 = Thread(target = Action, args=(q,))
t1.setDaemon(True)
t2.setDaemon(True)
t1.start()
t2.start()
while True:
    pass