2

学校のプロジェクトで 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]))
4

1 に答える 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()

「if pygame.mouse.get_pressed():」コード ブロックをこれに置き換えます。

于 2016-09-17T14:26:10.073 に答える