在 python (pygame) 中存储鼠标坐标值

Storing mouse coordinate values in python (pygame)

我的问题是,在 python 中,我正在构建一个类似于绘画的程序,因为用户可以单击并移动 his/her 鼠标(这会改变矩形的大小),然后再次单击以设置矩形的尺寸

这是我的代码:

            import pygame 

            pygame.init() 

            #colours
            white = (255,255,255) 
            black = (0,0,0) 
            blue = (0,0,255)
            red = (255,0,0)
            green = (0,255,0)
            #################### 

            #window settings
            gameDisplay = pygame.display.set_mode((800,600))  
            pygame.display.set_caption('INTERFACE')
            ##########################

            #pre-defined variables
            gameExit = False 
            colour = black 
            x = 0 
            y = 0
            count = 0 

            ######################################### 

            #frames per second initialzing 
            clock = pygame.time.Clock()
            ###############################3

            while not gameExit:  
                for event in pygame.event.get(): 
                    if event.type == pygame.QUIT: 
                        gameExit = True   

                #retrieves coordinates of the mouse
                m_x,m_y = pygame.mouse.get_pos()    

                #Background colour
                gameDisplay.fill(white)  

                print count   

                if event.type == pygame.MOUSEBUTTONDOWN and count == 0: #If count is zero and user clicks mouse then make count equal to one
                    count = 1 

                if count == 1: #if count is equal to one make a black retangle that has size properties following the mouse coordinates
                    pygame.draw.rect(gameDisplay, black, [x,y,m_x,m_y]) 


                if event.type == pygame.MOUSEBUTTONUP:  #if the user releases the mouse button make count equal to 2
                    count = 2  

                while count == 2: #while count equals to 2 make another red rectangle with same properties as the black one then if user clicks again make count = 0
                 pygame.draw.rect(gameDisplay, red, [x,y,m_x,m_y]) 
                 if count == 2 and pygame.MOUSEBUTTONDOWN: 
                        count = 0
                pygame.display.update() 
                #frames per second actual value
                clock.tick(15)

            pygame.quit() 
            quit()

所以到目前为止,我能够让用户单击并使用鼠标来更改矩形的大小用户再次单击以将矩形设置到位的部分让我感到困惑我是不确定如何实际存储不断变化的鼠标坐标,因为每当我分配变量时,它们都会随着鼠标移动而变化,我想如何存储最近与矩形制作相关联的最新坐标。

当你将一个列表赋给一个变量时,它指向同一个列表,所以当列表改变时,变量也会改变。你想要做的是这样分配:

new_list = old_list[:] 

new_list = list(old_list)

例如:

之前:

>>> ol = []
>>> nl = ol
>>> ol.append(90)
>>> ol
[90]
>>> nl
[90]
>>> 

现在:

>>> old_list = []
>>> new_list = old_list[:]
>>> old_list.append(90)
>>> old_list
[90]
>>> new_list
[]