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

2024-09-30 04:34:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python还相当陌生,我只是断断续续地修修补补了大约一年,我的最新想法被难倒了,我喜欢做一些简单的愚蠢的游戏,我想我对这个游戏已经有点力不从心了,基本上,这是一个饼干点击器风格的游戏,我有一个功能,我试图使进口的总金额从脚本,然后进入一个循环,它需要的金额加上一倍,你有多少缸(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()

Tags: theimportgame游戏timeactionsleepglobal
1条回答
网友
1楼 · 发布于 2024-09-30 04:34:57

看起来您的Action()方法没有从Vats()方法获取Const值。一种方法是使用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

相关问题 更多 >

    热门问题