Search code examples
pythongraphicspygamedraw

Saving history of drawing in Pygame in order to implement Ctrl Z


I am trying to remember previous drawings to implement Ctrl-Z (Undo) and (Redo). For this I am using a stack (deque) from collections and what I also did was to create an Object called DrawEntity, which has an attribute of a list of rectangles (pygame class) because I don't want to save every pixel that I draw, but only one 'brushstroke' and not the components that make him (little ractangles make a brushstroke) however, I don't know how to insert to the main DrawEntity list which will save those entities in the queue for each brushstroke and not for each rectangle. This is what I have so far:

main.py:

import pygame as pg
from collections import deque
from DrawEntity import DrawEntity

DrawEnt = deque()

def draw(window):
    d_Ent = DrawEntity()
    mouseX, mouseY = pg.mouse.get_pos()
    click = pg.mouse.get_pressed()
    if click[0]:
        rect = pg.draw.rect(window, (0, 0, 0), pg.Rect(mouseX, mouseY, 3, 3), 3)
        d_Ent.add(rect)

    if d_Ent.entity:
        print("new Entity")
        print(d_Ent)
        DrawEnt.append(d_Ent)



def main():

    running = True
    window = pg.display.set_mode((640, 480))
    window.fill((255, 255, 255))


    while running:
        clock.tick(3200)

        for event in pg.event.get():
            if event.type == pg.QUIT:
                running = False

            if event.type == pg.KEYDOWN:

                if event.key == pg.K_z and pg.key.get_mods() & pg.KMOD_LCTRL:

                    print(len(DrawEnt))

            draw(window)

        pg.display.flip()
    #end main loop
    pg.quit()

if __name__ == '__main__':

    pg.init()
    clock = pg.time.Clock()
    main()

And DrawEntity.py :

class DrawEntity:
def __init__(self):
    self.entity = []

def add(self, toAdd):
    self.entity.append(toAdd)

def remove(self):
    self.entity = []

def __str__(self):
    return ' '.join(map(str, self.entity))

Basically, I want to enter a while loop and stay there WHILE I keep clicking, in order to gather all the rectangles into the list and then append it into the main entity list, however this does not enter a while loop and each time I draw it appends each new entity with a single rectangle and not a series of rectangles. (If I try to use while loop the game crashes)
So:

-1 While clicking gather all the rectangles I've drawn so far

1.1 If I stopped clicking then append the new d_Ent with a list of rectangles to the main entity list (DrawEnt on line 5 in main.py)

  1. continue with the program
  2. If I click again, go to 1

Solution

  • Every time whe the mouse button is clicked, then you have to create a new DrawEntity object:

    if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
        DrawEnt.append(DrawEntity())
    

    In draw you have to append a Rect to the last DrawEntity in the deque (DrawEnt):

    def draw(window):
        if len(DrawEnt) > 0:
            d_Ent = DrawEnt[-1]
    
            mouseX, mouseY = pg.mouse.get_pos()
            click = pg.mouse.get_pressed()
            if click[0]:
                rect = pg.draw.rect(window, (0, 0, 0), pg.Rect(mouseX, mouseY, 3, 3), 3)
                d_Ent.add(rect)
    

    On ctrl-z remove the last element from DrawEnt (pop), clear the display and draw all remaining elements again:

    if event.type == pg.KEYDOWN:
        if event.key == pg.K_z and pg.key.get_mods() & pg.KMOD_LCTRL:
            if len(DrawEnt) > 0:
                DrawEnt.pop()
                window.fill((255, 255, 255))
                for entity in DrawEnt:
                    for r in entity.entity:
                        pg.draw.rect(window, (0, 0, 0), r, 3)
    

    See the example:

    class DrawEntity:
        def __init__(self):
            self.entity = []
    
        def add(self, toAdd):
            self.entity.append(toAdd)
    
        def remove(self):
            self.entity = []
    
        def __str__(self):
            return ' '.join(map(str, self.entity))
    
    DrawEnt = deque()
    
    def draw(window):
        if len(DrawEnt) > 0:
            d_Ent = DrawEnt[-1]
    
            mouseX, mouseY = pg.mouse.get_pos()
            click = pg.mouse.get_pressed()
            if click[0]:
                rect = pg.draw.rect(window, (0, 0, 0), pg.Rect(mouseX, mouseY, 3, 3), 3)
                d_Ent.add(rect)
    
    def main():
    
        running = True
        window = pg.display.set_mode((640, 480))
        window.fill((255, 255, 255))
    
    
        while running:
            clock.tick(3200)
    
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    running = False
    
                if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
                    d_Ent = DrawEntity()
                    DrawEnt.append(DrawEntity())
    
                if event.type == pg.KEYDOWN:
    
                    if event.key == pg.K_z and pg.key.get_mods() & pg.KMOD_LCTRL:
    
                        if len(DrawEnt) > 0:
                            DrawEnt.pop()
                            window.fill((255, 255, 255))
                            for entity in DrawEnt:
                                for r in entity.entity:
                                    pg.draw.rect(window, (0, 0, 0), r, 3)
    
                draw(window)
    
            pg.display.flip()
        #end main loop
        pg.quit()
    
    if __name__ == '__main__':
    
        pg.init()
        clock = pg.time.Clock()
        main()