如何在pygame中画线和改变颜色?

2024-04-26 22:59:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为一个学校项目在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]))

Tags: torecteventgetifwithlocationred
1条回答
网友
1楼 · 发布于 2024-04-26 22:59:07

我要做的是

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_按下():“代码块。在

相关问题 更多 >