2013-07-11 127 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

相关问题