2015-04-16 48 views
1

我的问题是,在python中,我正在构建一个类似于paint的程序,用户有能力点击并移动他/她的鼠标(改变矩形的大小相应),然后再次单击以设置矩形在Python中存储鼠标坐标值(pygame)

这里的尺寸是我的代码:

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

那么我迄今为止的是,我能够得到用户点击并使用鼠标来改变矩形的大小用户再次单击以将矩形设置到位的部分使我感到困惑我不确定如何实际存储ever c悬挂鼠标坐标,因为每当我指定变量时,它们随着鼠标移动而变化,我如何存储最后一个与矩形制作关联的坐标。

+0

所以我到目前为止是我能够让用户点击并使用鼠标来更改矩形的大小用户再次单击以将矩形设置就位的部分使我感到困惑我不确定在如何以实际存储不断变化的鼠标坐标,因为每当我指定变量随着鼠标移动而变化时,我如何存储最后一个与矩形制作关联的坐标 – user3145648

+0

当您将列表分配给变量时,它指向相同的列表,所以当列表发生变化时,变量会发生变化。你想要做的就是像这样赋值:'new_list = old_list [:]'或'new_list = list(old_list)' –

回答

0

当您将一个列表分配给一个变量时,它指向相同的列表,所以当列表发生变化时,变量会发生变化。你想要做的就是分配是这样的:

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 
[]