簡體   English   中英

如何在pygame中畫線並更改顏色?

[英]How do i draw a line and change colors in 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]))

我會做的是

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():”代碼塊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM