1

I am working on a paint project and it's almost complete but I want it to behave like the ms paint , in which whenever we draw an object (rectangle ,line) it shows it on the screen while we are moving our mouse . I implemented it for only one object i.e the window shows which object I am drawing but when I start drawing another object it wipes the previously drawn one

import pygame
pygame.init()       
run = True      
screen =   pygame.display.set_mode((400,400))     
clock = pygame.time.Clock()     
screen.fill((255,255,255))      
down = None     
while run == True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
           run = False
    elif event.type == pygame.MOUSEMOTION:
        motion = event.pos
        screen.fill((255,255,255))
        if down:
            pygame.draw.line(screen,(0,0,0),(down[0],down[1]),\
                (motion[0],motion[1]))

    elif event.type == pygame.MOUSEBUTTONDOWN:
        down = event.pos

    elif event.type == pygame.MOUSEBUTTONUP:
        up = event.pos
        pygame.draw.line(screen,(0,0,0),(down[0],down[1]),(up[0],up[1]))
        down = None

pygame.display.update()
clock.tick(60)

I tried saving the data of the all previously draw objects in an array and then calling a function make(self,arr) to draw it again on the screen but it's very slow. How can I draw the objects like i showed in my program and still have other objects stay on the screen ?

  • This is commonly accomplished by saving the composited scene with the newly-completed shape into an off-screen texture. On future frames, instead of clearing the screen to a solid colour, you blit this saved texture onto the screen instead, to "clear" it to the last cached state. I'm not familiar enough with the PyGame API to share the exact steps as an Answer, though. – DMGregory Jul 07 '20 at 17:02
  • Thank you so so much , i knew that the solution would be something like that but I couldn't implement it , i was just trying to solve it and I just did after an hour. What I did to solve it is , created an object cpy = screen.copy() to copy the surface when the mouse button is released , then I blitted it just after the screen.fill(white) line under mousemotion and it worked flawlessly thank you – user9797560 Jul 07 '20 at 21:52
  • Great work! Can you share your solution as an Answer below? – DMGregory Jul 07 '20 at 21:56
  • Yes sure I am on my way of doing that ! Thanks again – user9797560 Jul 07 '20 at 22:03

1 Answers1

0
import pygame
pygame.init()
screen = pygame.display.set_mode((400,400))
run = True
white = (255,255,255)
black = (0,0,0)
screen.fill(white)
down = None
cpy = None
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.MOUSEMOTION:
            motion = event.pos
            screen.fill(white)
            if cpy:
                screen.blit(cpy,(0,0))
            if down:
                pygame.draw.line(screen,black,(down[0],down[1]),\
                    (motion[0],motion[1]))

        elif event.type == pygame.MOUSEBUTTONDOWN:
            down = event.pos
        elif event.type == pygame.MOUSEBUTTONUP:
            up = event.pos
            down = None
            cpy = screen.copy()
    pygame.display.update()