Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在while循环的同时使用shell命令。_Python 3.x_Pygame - Fatal编程技术网

Python 3.x 在while循环的同时使用shell命令。

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

我正在制作一个游戏,用来教人们python,所以我通过分配变量、调用函数等来控制它。screeps.com是一个类似的概念,只是它是Javascript,这就是python。但是我遇到的问题是,我必须使用while循环来刷新屏幕,但是通过shell发出的命令在while循环运行时无法执行。例如,我希望能够输入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()

你为什么不用两条线呢 1) 线程-根据输入定期更新显示
2) 线程-处理输入并更新共享变量线程是正确的方法。确保在顶部导入线程

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,())

我得到的错误是:Traceback(最近一次调用上次):文件“C:\Python34\creates\game.py”,第43行,在threading.start\u new\u thread(threadedConsole,())AttributeError:“module”对象没有属性“start\u new\u thread”是不是正确的术语?不要导入线程,而是导入线程。它们是两个独立的模块,线程是更高级的模块,您不需要。