2013-05-08 96 views
2

我是一名業餘程序員。我有一個小的(和緊急的)問題。我正在研究基於文本(控制檯)的冒險遊戲。在某個時候,我想要一個pygame窗口打開。玩家必須儘快點擊窗口。反應時間應該返回到主程序,pygame窗口應該關閉。主程序將繼續運行。使用pygame打開彈出遊戲而不關閉所有內容? - Python

我已經寫了pygame窗口的腳本,它工作正常。我的主程序也很好。現在我怎麼從主程序調用pygame窗口?

我試過導入pygame腳本,但沒有奏效。

謝謝。

這裏是我的pygame的腳本:

import pygame, sys, time 
from pygame.locals import * 

pygame.init() 

#Set up window 
pygame.event.set_grab(0) 
pygame.mouse.set_visible(1) 
screen = pygame.display.set_mode((300,200)) 
shape = screen.convert_alpha() 
pygame.display.set_caption("Sniper Alert") 

#Colors 
WHITE = (255, 255, 255) 
BLACK = (0,0,0) 
RED = (255, 0, 0) 

#Draw on surface object 
screen.fill(BLACK) 

def alert(): 
    #Create a font 
    font = pygame.font.Font(None,50) 

    #Render the text 
    text = font.render("Sniper Alert", True, RED) 

    #Create a rectangle 
    textRect = text.get_rect() 

    #Center the rectangle 
    textRect.centerx = screen.get_rect().centerx 
    textRect.centery = screen.get_rect().centery 

    #Blit the text 
    screen.blit(text, textRect) 
    pygame.display.update() 

    return press() 

def press(): 
    t0 = time.clock() 
    dt = 0 

    while time.clock() - t0 < 1.5: 
     for event in pygame.event.get(): 
      if event.type == pygame.MOUSEBUTTONDOWN: 
       dt = time.clock()- t0 
       return dt 


#Exit 
pygame.quit() 
sys.exit() 

#Run the game loop 
while True: 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 
+0

緊急給你也許但是,因爲我們沒有得到報酬,可能對我們不那麼迫切:-) – paxdiablo 2013-05-08 03:08:49

+0

ahaha非常真實:) – Jey 2013-05-08 03:09:33

回答

0

有三種方法我能想到的。在這裏,他們是:

解數1:


這種方式可能是最糟糕的解決方案,也是最難實現的,但讓我們興奮的出路。我不會建議使用它,但在某些情況下您可能想要。你可以使用線程模塊。線程是爲這樣的多任務而設計的,並且可以做你想做的事情。您可以創建一個新的線程,像這樣:

import threading 

def DoSomething(a,b,c):   #this function will be called in the background, simultaneously to the main program 
    #do something here 

apple = 1 
banana = 2 
cat = 3 
mythread = threading.thread(target=DoSomething,args=(apple,banana,cat)) #defines a thread 
mythread.start() #tells the thread to start running 

解數2


一個更好的方式來做到這將是剛剛推出它作爲一個不同的程序。您可以使用子進程模塊來執行命令行命令。這將有效地運行該程序,就好像您在終端的新選項卡中執行了一樣(沒有新選項卡)。然後,您可以使程序能夠使用subprocess.comunicate()與您進行通信。我將從這裏連接pygame程序的輸入和輸出到你的主程序。這裏是一個例子:

import subprocess 

input = <insert input>     #when you run the program and it requires something like a raw_input, this will give it input. You could probably avoid needing to send the pygame program input, because all you really want to do is receive an output from it. 

process = subprocess.Popen(["python","<popup filename>"],stdin=subprocess.PIPE,stdout=subprocess.PIPE,shell=False) #this function is how subprocess runs shell/command prompt commands. On a side note, if you simply want to run a shell command without accessing input or output, just use "subprocess.call(<list of words in command (so ls -a would be ['ls','-a'])>)" 
output = process.communicate(input)[0] #this will return any output the program prints, meaning you can communicate with the program and receive information from it 

使用子進程可能是最好的方法。

解數3


最後一個選項,如果你想這一切在一個文件中,不想浪費時間與線程,將只是交替在while循環的兩個方案。基本上,你會運行一個循環來執行這兩個程序的代碼。這是它如何工作的:

while True:   #an infinite loop. it doesn't necessarily have to be infinite. 
    #call methods or run code to maintain text based game 
    #call methods or run code to maintain pygame window 

這工作得很好,我在pygame的遊戲我做也有一個文本組件,但子進程的方法可能是更好的... ...