Ejemplo n.º 1
0
def main():
    pygame.init()
    width = 600
    height = 900

    display = pygame.display.set_mode((width, height))
    pygame.display.set_caption("PyDrawer")

    save = Save()
    delete = Delete()
    load = Load()

    color = None
    draw = True
    drawRects = []
    drawing = False
    cs = [(0, 0, 0), (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0),
          (0, 255, 255), (255, 0, 255), (51, 51, 51)]
    cw = 45
    d = 30
    colors = []

    for c in cs:
        col = Color(c)
        colors.append(col)

    while draw:
        display.fill((255, 255, 255))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                draw = False
            if event.type == pygame.MOUSEBUTTONDOWN:
                x, y = pygame.mouse.get_pos()
                if x > cw and x < cw + 4 * cw:
                    if y > 15 * cw and y < 17 * cw:
                        for c in colors:
                            c.isSelected(x, y)

                if y > 15 * cw:
                    save.isClicked(x, y, drawRects)
                    drawRects = load.isClicked(x, y, drawRects)
                    drawRects = delete.isClicked(x, y, drawRects)

                if y < 14 * cw:
                    drawing = True
                    for c in colors:
                        if c.selected:
                            color = c.color
                    tx = x // d
                    ty = y // d
                    check = 0
                    for r in drawRects:
                        if r[0] == tx and r[1] == ty and r[2] == color:
                            check += 1
                    if check == 0:
                        drawRects.append([tx, ty, color])
                else:
                    drawing = False
            if event.type == pygame.MOUSEMOTION and drawing and color:
                x, y = pygame.mouse.get_pos()
                if y < 14 * cw:
                    x = x // d
                    y = y // d

                    for r in range(0, len(drawRects), 1):
                        try:
                            if drawRects[r][0] == x and drawRects[r][1] == y:
                                drawRects.pop(r)
                        except:
                            pass

                    drawRects.append([x, y, color])

                    print(len(drawRects))

            if event.type == pygame.MOUSEBUTTONUP:

                drawing = False
        i = 0
        for y in range(0, 2):
            for x in range(0, 4):
                try:
                    colors[i].draw(display, (x + 1) * cw, (y + 15) * cw, cw)
                    colors[i].update()
                except:
                    pass

                i += 1

        save.draw(display, 7 * cw, 15 * cw)
        load.draw(display, 9 * cw, 15 * cw)
        delete.draw(display, 11 * cw, 15 * cw)
        for r in drawRects:
            pygame.draw.rect(display, r[2], (r[0] * d, r[1] * d, d, d))

        pygame.display.update()

    pygame.quit()
    quit()