Member Avatar for computerfreak97
computerfreak97

I am trying to develop a program using pygame that will let you "paint." There is just one problem, when I change the RGB values, all of the pixles change!!! Please HELP!!!

Code:

import pygame, os
pygame.init()
pygame.display.init()
screen = pygame.display.set_mode((830, 620))
draw = 0
selected = 0
points = []
col = [0,0,0]
selectedtxt = 0
currtext1 = "0"
currtext2 = "0"
currtext3 = "0"
first = 0
rgbfont = pygame.font.Font(None, 12)
pygame.display.set_caption('Paint in Python')
while 1:
    screen.fill((255,255,255))
    pygame.draw.polygon(screen,(125,125,125),((0,0),(830,0),(830,20),(30,20),(30,620),(0,620)))
    font = pygame.font.Font(None, 20)
    t = font.render("Clear", 1, (0, 0, 0))
    trect = t.get_rect()
    trect.left, trect.top = 786,3
    screen.blit(t, trect)
    if selected == 1:
        pygame.draw.rect(screen,(255,255,255),(4,24,22,22))#draw selected white box
    pygame.draw.rect(screen,(col[0],col[1],col[2]),(5,25,20,20))#draw brush tool icon
    m1 = pygame.mouse.get_pressed()[0]#get mouse button 1
    mouse_x, mouse_y = pygame.mouse.get_pos()#get mouse x,y
    if mouse_x >= 5 and mouse_x <= 25 and mouse_y >= 25 and mouse_y <= 45 and m1 == 1:
        draw = 1#set it so that you can draw
        selected = 1#set selected to brush
    if mouse_x >= 786 and mouse_x <= 820 and mouse_y >= 3 and mouse_y <= 17 and m1 == 1:
        points = []#reset points
    if draw == 1 and m1 == 1 and mouse_x >= 30 and mouse_y >= 20 and [mouse_x,mouse_y,col] not in points:
        points += [[mouse_x,mouse_y,col]]
    for x in range(0,len(points)):
        pygame.draw.rect(screen,(points[x][2][0],points[x][2][1],points[x][2][2]),(points[x][0],points[x][1],2,2))#draw the pixles
    pygame.draw.rect(screen,(255,255,255),(3,50,20,15))#draw txtbx 1 - R
    pygame.draw.rect(screen,(255,255,255),(3,70,20,15))#draw txtbx 2 - G
    pygame.draw.rect(screen,(255,255,255),(3,90,20,15))#draw txtbx 3 - B
    if (mouse_x >= 3 and mouse_x <= 23 and mouse_y >= 50 and mouse_y <= 65 and m1 == 1 or selectedtxt == 1) and selected == 1:
        pygame.draw.polygon(screen,(255,255,255),((28,53),(28,62),(25,57)))
        selectedtxt = 1
    if (mouse_x >= 3 and mouse_x <= 23 and mouse_y >= 70 and mouse_y <= 85 and m1 == 1 or selectedtxt == 2) and selected == 1:
        pygame.draw.polygon(screen,(255,255,255),((28,73),(28,82),(25,77)))
        selectedtxt = 2
    if (mouse_x >= 3 and mouse_x <= 23 and mouse_y >= 90 and mouse_y <= 105 and m1 == 1 or selectedtxt == 3) and selected == 1:
        pygame.draw.polygon(screen,(255,255,255),((28,93),(28,102),(25,97)))
        selectedtxt = 3
    for event in pygame.event.get():
        #print event.type
        if event.type == pygame.KEYDOWN:
            if event.key >= 48 and event.key <= 57 or event.key == 8:
                if selectedtxt == 1:
                    if event.key == 8:
                        currtext1 = currtext1[0:-1]
                    else:
                        currtext1 += str(event.key-48)
                    if len(currtext1) >= 4:
                        currtext1 = currtext1[0:3]
                    if currtext1 != "":
                        if int(currtext1) > 255:
                            currtext1 = "255"
                    rgbt1 = rgbfont.render(currtext1,1, (0, 0, 0))
                    rgbtrect1 = rgbt1.get_rect()
                    rgbtrect1.left,rgbtrect1.top = 5,54
                if selectedtxt == 2:
                    if event.key == 8:
                        currtext2 = currtext2[0:-1]
                    else:
                        currtext2 += str(event.key-48)
                    if len(currtext2) >= 4:
                        currtext2 = currtext2[0:3]
                    if currtext2 != "":
                        if int(currtext2) > 255:
                            currtext2 = "255"
                    rgbt2 = rgbfont.render(currtext2,1, (0, 0, 0))
                    rgbtrect2 = rgbt2.get_rect()
                    rgbtrect2.left,rgbtrect2.top = 5,74
                if selectedtxt == 3:
                    if event.key == 8:
                        currtext3 = currtext3[0:-1]
                    else:
                        currtext3 += str(event.key-48)
                    if len(currtext3) >= 4:
                        currtext3 = currtext3[0:3]
                    if currtext3 != "":
                        if int(currtext3) > 255:
                            currtext3 = "255"
                    rgbt3 = rgbfont.render(currtext3,1, (0, 0, 0))
                    rgbtrect3 = rgbt3.get_rect()
                    rgbtrect3.left,rgbtrect3.top = 5,94
            if event.key == pygame.K_ESCAPE:
                os._exit(1)
    if first == 0:
        rgbt1 = rgbfont.render(currtext1,1, (0, 0, 0))
        rgbtrect1 = rgbt1.get_rect()
        rgbtrect1.left,rgbtrect1.top = 5,54
        rgbt2 = rgbfont.render(currtext2,1, (0, 0, 0))
        rgbtrect2 = rgbt2.get_rect()
        rgbtrect2.left,rgbtrect2.top = 5,74
        rgbt3 = rgbfont.render(currtext3,1, (0, 0, 0))
        rgbtrect3 = rgbt3.get_rect()
        rgbtrect3.left,rgbtrect3.top = 5,94
        first = 1
    if currtext1 != "":
        screen.blit(rgbt1,rgbtrect1)
        col[0] = int(currtext1)
    if currtext2 != "":
        screen.blit(rgbt2,rgbtrect2)
        col[1] = int(currtext2)
    if currtext3 != "":
        screen.blit(rgbt3,rgbtrect3)
        col[2] = int(currtext3)
    pygame.display.flip()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.