2016-02-18 66 views
0

我對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() 

回答

0

看起來你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