2013-05-08 99 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的游戏我做也有一个文本组件,但子进程的方法可能是更好的... ...