2016-09-17 14 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

まだ色を選択する必要があります。私はrectを押すと、私は色を選択します。この変数はpygame.draw.lineに渡されます。どのようにwhileループを使うのが好きですが、どのように色を選択できますか? – programmer

+0

私はループの前に、あなたがしたコードを使用し、同じ方法で色を選択させると思います。次に、パラメータとしてタプル(color_to_paint_with)をとり、色が選択されたときにこの関数を呼び出す関数のwhileループ中に入れます。 – Akinni

+0

私はこれが役立つことを願っています – Akinni

関連する問題