在while循环的同时使用shell命令。

时间:2015-03-20 02:11:46

标签: python-3.x pygame

我正在制作一个用于教人python的游戏,因此我通过分配变量,调用函数等来控制它.screeps.com是一个类似的概念,除了它的Javascript,这是Python。我遇到的问题是,我必须使用while循环刷新屏幕,但是在while循环运行时不能执行通过shell发出的命令。例如,我希望能够输入x + = 5,并让玩家向右移动。所以我需要的是一种在执行shell命令的同时刷新屏幕的方法。另外,我使用的是Python 3.4。提前致谢。

    import pygame

pygame.init()

display_width = 800
display_height = 600

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('My game')

black = (0,0,0)
white = (255,255,255)

clock = pygame.time.Clock()
crashed = False
pImg = pygame.image.load('player.png')


def car(x,y):
    gameDisplay.blit(pImg, (x,y))

x =  (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
car_speed = 0

while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True   
    gameDisplay.fill(black)
    car(x,y)

    pygame.display.update()
    clock.tick(60)

pygame.quit()
quit()

2 个答案:

答案 0 :(得分:0)

为什么不使用两个线程 1)线程 - 根据输入定期更新显示 2)线程 - 处理输入并更新共享变量

答案 1 :(得分:0)

线程是执行此操作的正确方法。确保在顶部导入线程。

import thread

consoling = False

def threadedConsole():
    global consoling
    consoling = True
    exec(raw_input(">>> "))
    consoling = False

while True:
    #Game loop
    if not consoling:
        thread.start_new_thread(threadedConsole,())