2016-09-17 118 views
2

我想在pygame中爲一個學校項目製作繪圖程序。在這個模塊中,我打算讓用戶按下鼠標並在表面畫一條線。如果某人按下矩形,則該人選的顏色就是繪製的線的顏色。出於某種原因,該變量可以改變,但即使我按下鼠標,沒有畫線。 下面的代碼:如何在pygame中繪製一條線並更改顏色?

def painting_module(): 
    running = True 
    while running: 
     #clock for timingn processes 
    Clock.tick(FPS) 
    # Process input (event) 
    for event in pygame.event.get(): 
     Mouse_location = pygame.mouse.get_pos() 
     click = pygame.mouse.get_pressed() 
     displacement_val = pygame.mouse.get_rel() 
     color_to_paint_with = (0,0,0) 
     if event.type == pygame.QUIT: 
      running = False 
     if click[0] == 1: 
      # Intended to select colors if pressed, draw a line if the mouse is not on the rect 
      if red_rect.collidepoint(Mouse_location): 
       color_to_paint_with = RED 
       print "Red" 
       print color_to_paint_with 
      if green_rect.collidepoint(Mouse_location): 
       color_to_paint_with = GREEN 
       print "red" 
       print color_to_paint_with 
      if blue_rect.collidepoint(Mouse_location): 
       color_to_paint_with = BLUE 
       print "red" 
       print color_to_paint_with 
      if gray_rect.collidepoint(Mouse_location): 
       color_to_paint_with = GRAY 
       print "red" 
       print color_to_paint_with 
      if eraser_ivory_rect.collidepoint(Mouse_location): 
       color_to_paint_with = IVORY 
       print "red" 
       print color_to_paint_with 
        #draws the line 
      pygame.draw.line(Screen, color_to_paint_with, Mouse_location, (Mouse_location[0] + displacement_val[0], Mouse_location[1] + displacement_val[1])) 

回答

0

我會做的是

painting_module() 

def painting_module(): 
running = True 
while running: 
    #clock for timingn processes 
Clock.tick(FPS) 
# Process input (event) 
for event in pygame.event.get(): 
    Mouse_location = pygame.mouse.get_pos() 
    click = pygame.mouse.get_pressed() 
    displacement_val = pygame.mouse.get_rel() 
    color_to_paint_with = (0,0,0) 
    if event.type == pygame.QUIT: 
     running = False 
    if click[0] == 1: 
     # Intended to select colors if pressed, draw a line if the mouse is not on the rect 
     if red_rect.collidepoint(Mouse_location): 
      color_to_paint_with = RED 
      print "Red" 
      print color_to_paint_with 
      // Call drawloop here 
     if green_rect.collidepoint(Mouse_location): 
      color_to_paint_with = GREEN 
      print "red" 
      print color_to_paint_with 
     if blue_rect.collidepoint(Mouse_location): 
      color_to_paint_with = BLUE 
      print "red" 
      print color_to_paint_with 
     if gray_rect.collidepoint(Mouse_location): 
      color_to_paint_with = GRAY 
      print "red" 
      print color_to_paint_with 
     if eraser_ivory_rect.collidepoint(Mouse_location): 
      color_to_paint_with = IVORY 
      print "red" 
      print color_to_paint_with 

def drawline(color_to_paint_with, Mouse_location, displacement_val): 
    for event in pygame.event.get(): 
     while pygame.mouse.get_pressed()[0] == 1: 
      pygame.draw.line(Screen, color_to_paint_with, Mouse_location, (Mouse_location[0] + displacement_val[0], Mouse_location[1] + displacement_val[1])) 
      pygame.display.flip() 

我將取代 「如果pygame.mouse.get_pressed():」 有了這個代碼塊。

+0

嗯,我還是要選擇顏色。當我按下矩形時,我選擇一種顏色。這個變量然後傳遞給pygame.draw.line。我喜歡你如何使用while循環,但我如何選擇顏色。 – programmer

+0

我想在循環之前,我會使用你所做的代碼並讓它以相同的方式選擇它的顏色。然後,我將這個while循環放在一個函數中,該函數將元組(color_to_paint_with)作爲參數,並在選擇顏色時調用此函數。 – Akinni

+0

我希望這有助於 – Akinni

相關問題