试图用pygame在屏幕上画画,但任何方法都不成功。

问题描述 投票:0回答:1

这是我的一段代码,接受文字输入,并将像素绘制到屏幕上,这是命令格式!像素32,32 255,255,255。

##Command Format !Pixel X,Y R,G,B
def screencontrol():
##now we open the PyGame window
(width, height) = (256, 256) #more window setup
screen = pygame.display.set_mode((width, height)) #more window setup
pygame.display.flip() #more window setup
global message
#global splitmsg
while True:
    commandmessage = message
    pygame.init()
    pygame.event.pump() ##keeps pygame window refreshed / not crash
    if "!pixel" == commandmessage.lower().split(" ", 1)[0]: #Split message and check for pixel command
        print("Received a draw pixel request!") #debug output

        coordsforpix = commandmessage.lower().split(" ", -1)[1]
        print("Extracting draw... ")
        plotpixx = coordsforpix.split(",", 2)[0]
        print("X = " + plotpixx)
        plotpixy =coordsforpix.split(",", 2)[1]
        print("Y = " + plotpixy)
        plotpixrgb = commandmessage.lower().split(" ", -1)[2]
        print("RGB = " + plotpixrgb)
        plotpixr = plotpixrgb.split(",", 3)[0]
        plotpixg =plotpixrgb.split(",", 3)[1]
        plotpixb =plotpixrgb.split(",", 3)[2]
        print("R G B = " + plotpixr + plotpixg + plotpixb)
        print("Done extracting")
        pygame.draw.circle(screen, tuple(map(int, plotpixrgb.split(",", -1))), (int(plotpixx), int(plotpixy)),  1)#plot point
        commandmessage = ""
        message = ""
        pass
    else:
        pass

为了让它正常工作,我已经拔了两天的头发,其他所有的东西都能正常工作,调试的时候输出的东西都是应该的,但是就是不能绘制任何像素......先谢谢了!

python pygame pixel
1个回答
0
投票

pygame.display.flip() 将完整的显示Surface更新到屏幕上,并且必须在应用程序循环结束时进行,但是...。pygame.init() 初始化所有导入的 pygame 模块,并且只需要做一次。它必须是第1条pygame指令。

def screencontrol():

    pygame.init() # init pygame

    ##now we open the PyGame window
    (width, height) = (256, 256) #more window setup
    screen = pygame.display.set_mode((width, height)) # create window

    global message
    #global splitmsg
    while True:
        commandmessage = message
        pygame.event.pump() ##k

        if "!pixel" == commandmessage.lower().split(" ", 1)[0]:
            # [...]

        pygame.display.flip() # update display at the end of the loop
© www.soinside.com 2019 - 2024. All rights reserved.