使用pygame打开弹出游戏而不关闭所有内容? - Python

时间:2013-05-08 03:07:13

标签: python pygame

我是一名业余程序员。我有一个小问题(紧急)。我正在开发基于文本(控制台)的冒险游戏,以获得乐趣。在某个时刻,我想要一个pygame窗口打开。玩家必须尽快点击窗口。响应时间应返回主程序,pygame窗口应关闭。然后主程序将继续运行。

我已经为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()

1 个答案:

答案 0 :(得分: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游戏中运行得很好,它也有一个文本组件,但子进程方式可能更好......