2014-12-28 72 views
0

我正在爲Flow創建類似的遊戲。如果您熟悉它,則需要用戶通過網格上相同顏色的線將相同的圓圈相匹配。我遇到了一個問題,無論哪裏出現鼠標移動事件,我都希望用戶只能用直線繪製(左,右,上,下)。目前用戶可以在任何地方繪製,我有一個函數可以在鼠標移動事件發生時繪製圓圈,儘管我已經使用了一些if語句來限制用戶可以繪製的位置,但仍然無法使用。也許有更好的方法,而不是使用鼠標移動事件?我不確定,但任何幫助非常感謝!在pygame中用鼠標繪製直線?

下面是一些代碼:

''' *** IN PROGRESS *** ''' 
while not done: 
for event in pygame.event.get(): 
    if event.type == pygame.QUIT: 
     done = True # Closes the game and exits the loop 

    elif event.type == pygame.MOUSEBUTTONDOWN: 
     x, y = event.pos 
     pygame.mouse.set_visible(True) 
     ''' If the user clicks down on the left button the mouse coordinates at that    point are assigned to variables 
     x and y which are used to check the condition of the click_detection function''' 

    elif event.type == pygame.MOUSEBUTTONUP: 
     pygame.mouse.set_visible(True) 
     # Makes the cursor visible to choose a new circle easily 

    elif event.type == pygame.MOUSEMOTION: 

     ''' State is assigned to an array for each of three mouse buttons 
     at state[0] it checks the left mouse button''' 
     state = pygame.mouse.get_pressed() 

     for circle in circle_grid_list: 
      # Checks following conditions for every circle in that Sprite group 

      if ClickDetection.click_detection(circle) == True: 
       ''' Checks whether a circle is being clicked 
       - If so then variable colour is assigned to the colour of the clicked circle 
       - This is used so that the move method from circle_line class can be called using the colour of the clicked circle''' 
       colour = circle.colour 

       # *** WORK IN PROGRESS *** 
       if MovementChecker.check() != False: 
        print("can draw") 
        if state[0] == 1: 
         # Checking the left mouse button state, if 1 then button is being clicked or held down 
         moving_line_mouse.Move() 

       elif MovementChecker.check() == False: 
        # Used to stop the move method for the moving_line object from being called if movement checker is false 
        print("Can't draw") 
+0

我從未聽過任何Python遊戲的名字 –

+0

您能否提供該遊戲代碼的鏈接以便我們看到它? –

+0

檢查您的鼠標是否在水平**或**垂直網格位置之外移動,如果是,則拒絕。按網格元素大小劃分三角形鼠標x和y;其中只有一個可能是1. – usr2564301

回答

0

所以,而不是在思維「這是在我的鼠標位於」,嘗試和思考的「條款這盒子是我的鼠標位於?」。如果是,則爲該框着色。例如,如果每個方格都是100x100像素,並且鼠標位於座標424和651,則可以通過執行int(424/100)int(651/100)來計算網格位置 - 鼠標位於在網格上的位置是4x6(注意,我們從零開始計數,所以左上角的正方形位於位置0x0)。

然後,你可以檢查網格4x6是否已經被填充,檢查它是否填充了不同的顏色,並處理所有的邏輯。

Flow還具有一個功能,它將繪製一條連線,用於連接您輸入的位置和離開的位置。如果您用另一種顏色「穿透」它,它也會刪除預先存在的線的一半。

實現此目的的一種方法是創建一個「方形」對象,用於跟蹤其當前顏色,對來自該方形的引用以及對最終選取的正方形的引用。

通過這種方式,您可以在網格內創建一種「鏈接列表」 - 每個方塊指向它所連接的下一個方塊,因此您知道是否需要繪製垂直條,水平條,或一個角落。如果您切過已設置顏色的方塊,則可以繼續沿着引用鏈查找上一個顏色流過的下一個方塊,並在繼續之前將其設置爲空。

+0

其實真的很聰明!我已經想出瞭如何通過點擊我已經渲染圓的位置的區域來繪製我想要的線(如果它在圓的半徑點擊大約50x50 +/- 40,那麼我知道它有點擊該圓圈並繪製該顏色,你知道連接現有線條的功能是什麼嗎?因爲我現在正在做的是在每個鼠標移動事件中畫一個圓圈,這是不起作用的。 –