如何获取鼠标在pygame中单击的位置的RGB值以及如何更改颜色? (Python 3.3.2)

时间:2014-01-19 03:08:23

标签: python pygame paint

所以我使用pygame制作绘画程序。我已经加载了色谱,我应该点击鼠标。除此之外,我不太确定如何使用.get_at找出颜色,我不确定如何改变颜色。

到目前为止我的代码是:

color = []
color = ((0,0,0))
running = True
while running:
    mb = mouse.get_pressed()
    mx,my = mouse.get_pos()
    if mb[0] == 1 and palette.collidepoint(mx,my):
        color = screen.get_at((mx,my))
        color.append()

每次点击我的调色板,我的颜色

2 个答案:

答案 0 :(得分:1)

import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640,480])
mousepressed = False
color = (0,0,0,255)
while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            mousepressed = True
        if event.type == pygame.MOUSEBUTTONUP:
            mousepressed = False
    if mousepressed:
        screen.set_at(pygame.mouse.get_pos(),color)
    pygame.display.flip()

抱歉,匆忙,希望这有帮助!

答案 1 :(得分:0)

mouseState = mouse.get_pressed()

mx, my = mouse.get_pos()
if mouseState[0]:
    color = screen.get_at((mx, my))
    # color is a Color object
    # Do some color manipulation by using Color.r, Color.g, Color.b, and Color.a
    # For example Color.r = 25
    # And at last...
    screen.set_at((mx, my), color)

这就是你想要的吗?阅读文档以了解Color.r等的值是什么。

http://www.pygame.org/docs/ref/color.html