【问题标题】:How do i draw a line and change colors in pygame?我如何在pygame中画一条线并改变颜色?
【发布时间】:2016-09-17 13:51:38
【问题描述】:

我正在尝试在 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]))

【问题讨论】:

    标签: python python-2.7 pygame pygame-surface


    【解决方案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():”代码块。

    【讨论】:

    • 好吧,我还是要选择颜色。当我按下一个矩形时,我选择一种颜色。然后将此变量传递给 pygame.draw.line。我喜欢你使用 while 循环的方式,但我如何选择颜色。
    • 我想在循环之前,我会使用您所做的代码并让它以相同的方式选择颜色。然后我会把这个while循环放在一个以元组(color_to_paint_with)作为参数的函数中,并在选择颜色时调用这个函数。
    • 我希望这会有所帮助
    • 你的意思是函数drawline(color_to_paint_with, Mouse_location,displacement_val)
    • 是的。我会在我说“//调用drawloophere”的地方调用它
    猜你喜欢
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 2015-02-04
    • 2019-07-24
    • 2012-01-23
    • 2018-08-25
    相关资源
    最近更新 更多