为什么我不能使用Python命令行实时更新Pygame窗口

时间:2014-10-31 15:34:41

标签: python python-2.7 pygame

我正在尝试使用Pygame库在Python中创建一个程序。我想在窗口处于活动状态时从python命令行给出pygame窗口信息。一切似乎都在工作,但它并没有将Shapes绘制到屏幕上,当我尝试绘制它时崩溃了pygame窗口。我似乎无法找到问题或原因,我之前遇到过这个问题。代码在下面提供,并且在我学习时很乱。是否有可能绕过这一点,或者我是否必须完全重新创建代码。

多线程是一种选择,但我不知道如何实现它以适合我的代码。

    import pygame
    import sys

    Black = 0,0,0
    White = 255,255,255
    Blue = 0,0,255
    Red = 255,0,0
    Green = 0,255,0
    Commands = ["Pygame Start", "Pygame End", "Help", "Exit", "Clear Screen", "DrawMode", "Display Update"]
    Types = ["Player", "Enemy"]
    PlayerRect = [0,0,0,0]
    DrawPlayer = False
    pygame.init()
    def Console():
        print "Commands: ", "\n", Commands[0],"\n", Commands[1],"\n", Commands[2], "\n", Commands[3], "\n", Commands[4], "\n", Commands[5], "\n", Commands[6]
        Input = raw_input("// ")

        if Input == Commands[0]:
            Window(True)

        if Input == Commands[1]:
            Window(False)

        if Input == Commands[2]:
            Help()

        if Input == Commands[3]:
            sys.exit()

        if Input == Commands[4]:
            pygame.draw.rect(screen,Blue,NewPlayerRect)

        if Input == Commands[5]:
            DrawMode()

        if Input == Commands[6]:
            DisplayUpdate()

    def Window(Active):
        if(Active == True):
            global screen
            screen = pygame.display.set_mode((640,480))



        elif(Active == False):
            pygame.display.quit()

    def Help():
        print "What Command Do You Need Explained?\n"
        KindOfHelp = raw_input("// ")

        if KindOfHelp == Commands[0]:
            print("\nTurns on the Pygame Module, which opens a pygame window")

        if KindOfHelp == Commands[1]:
            print("\nTurns off the Pygame Module, which closes a pygame window")

        if KindOfHelp == Commands[2]:
            print("\nOpens the Help Screen")

    def CLS():
        print "\n" * 100

    def DisplayUpdate():
        pygame.display.update()

    def DrawMode():
        print "Things you are able to  Create are a: ","\n", Types[0], "\n", Types[1]
        Type = raw_input("What Do you Want to Create? ")

        if Type == Types[0]:
            PlayerPosX = int(raw_input("Where Should The Player Spawn On The X Axis? "))
            PlayerPosY = int(raw_input("Where Should The Player Spawn On The Y Axis? "))
            PlayerSizeX = int(raw_input("What Should The Player X Size Be?"))
            PlayerSizeY = int(raw_input("What Should The Player Y Size Be?"))
            PlayerRect[0] = PlayerPosX
            PlayerRect[1] = PlayerPosY
            PlayerRect[2] = PlayerSizeX
            PlayerRect[3] = PlayerSizeY
            DrawPlayer = True



    while True:
        X = PlayerRect[0]
        Y = PlayerRect[1]
        XSize = PlayerRect[2]
        YSize = PlayerRect[3]
        NewPlayerRect = (X,Y,XSize,YSize)

        print(X,Y,XSize,YSize)
        Console()

0 个答案:

没有答案