2013-07-11 36 views
4

我想知道是否有一種方法,在Python中,雖然我的games.screen.mainloop()中的圖形塊正在運行,如果我可以執行某些操作,例如通過控制檯的raw_input()獲取用戶輸入。有沒有辦法在運行pygame時,我也可以運行控制檯?

+0

你的意思是不停止循環? – Serial

+0

如果您使用的是Linux,請在行末 – Dan

+0

處運行帶有&符號(&)的命令,而不停止games.screen.mainloop() – emufossum13

回答

0

事情是這樣的,如果你做了類似raw_input的東西,它會停止程序,直到輸入輸入,這樣將停止程序每個循環輸入,但你可以做的事情,如print,但他們會打印每個循環

如果你想利用投入使用InputBox Module這將使一個小的輸入框彈出在環路

屏幕這就是,如果你想從你可以嘗試線程在控制檯做到這一點,其即時通訊不熟悉但你可以檢查出來Multi-threading Tutorial

這裏是一個問題,這可能會幫助你

Pygame writing to terminal

祝您好運! :)

4

是的,看看下面的例子:

import pygame 
import threading 
import Queue 

pygame.init() 
screen = pygame.display.set_mode((300, 300)) 
quit_game = False 

commands = Queue.Queue() 

pos = 10, 10 

m = {'w': (0, -10), 
    'a': (-10, 0), 
    's': (0, 10), 
    'd': (10, 0)} 

class Input(threading.Thread): 
    def run(self): 
    while not quit_game: 
     command = raw_input() 
     commands.put(command) 

i = Input() 
i.start() 

old_pos = [] 

while not quit_game: 
    try: 
    command = commands.get(False) 
    except Queue.Empty: 
    command = None 

    if command in m: 
    old_pos.append(pos) 
    pos = map(sum, zip(pos, m[command])) 

    if pygame.event.get(pygame.QUIT): 
    print "press enter to exit" 
    quit_game = True 

    pygame.event.poll() 

    screen.fill((0, 0, 0)) 
    for p in old_pos: 
     pygame.draw.circle(screen, (50, 0, 0), p, 10, 2) 
    pygame.draw.circle(screen, (200, 0, 0), pos, 10, 2) 
    pygame.display.flip() 

i.join() 

它創建了一個小紅圈。你可以用左右進入一個小號d移動它瓦特,到控制檯。

enter image description here

相關問題