pythonpython-3.xpygamepygame-surfacepygame2

Pygame: Drag and Draw Rectangle on Mouse Click and Drag


I'm new to Pygame. I wanted to drag and draw the Rectangle when we click and drag left mouse button. The Left Click creates a small new rectangle where ever the cursor is present and Dragging increases rectangle size. Once done, Later, we can increase its size from its borders on dragging.

Is it possible? I'm struck half way through...

Can you please tell mistakes (a lot of mistakes below, probably)?

Thanks!

import pygame

pygame.init()
screen = pygame.display.set_mode((640, 480))
fps = pygame.time.Clock()
rectangle_selection = 0
rectangle_main = pygame.Rect(int(pygame.mouse.get_pos()[0]), int(pygame.mouse.get_pos()[1]),  int(10), int(10))
run = 1
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                rectangle_selection = 1
                pygame.draw.rect(screen, (0,255,0), rectangle_main)
                print("Left")
        elif event.type == pygame.MOUSEMOTION:
            if rectangle_selection:
                rectangle_main.w += event.rel[0]
                rectangle_main.h += event.rel[1]
                rectangle_main.w = max(rectangle_main.w, 10)
                rectangle_main.h = max(rectangle_main.h, 10)
                print("Motion")
        elif event.type == pygame.MOUSEBUTTONUP:
            rectangle_selection = 0
            print("End")
        
    pygame.draw.rect(screen, (0,255,255), rectangle_main)
    pygame.display.flip()
    fps.tick(60)

Solution

  • Figured Out!
    I was able to drag And make rectangle using code below:

    import pygame
    from numpy import array
    
    pygame.init()
    screen = pygame.display.set_mode((640, 480))
    fps = pygame.time.Clock()
    rectangle_selection = 0
    rectangles_storage = []
    
    run = 1
    while run:
        
        for event in pygame.event.get():
            
            if event.type == pygame.QUIT:
                pygame.quit()
    
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    rectangle_selection, rectangle_width, rectangle_height = 1, 1, 1
                    pygame.mouse.set_cursor(pygame.SYSTEM_CURSOR_CROSSHAIR)
                    rectangle_left = int(pygame.mouse.get_pos()[0]) - int((rectangle_width))
                    rectangle_right = int(pygame.mouse.get_pos()[1]) - int((rectangle_height))
                    rectangle_main = pygame.Rect(rectangle_left, rectangle_right, int(rectangle_width), int(rectangle_height))
    
            elif event.type == pygame.MOUSEMOTION:
                if rectangle_selection:
                    if event.buttons[0]:
                        pygame.mouse.set_cursor(pygame.SYSTEM_CURSOR_CROSSHAIR)
                        rectangle_main.w += event.rel[0]
                        rectangle_main.h += event.rel[1]
    
            elif event.type == pygame.MOUSEBUTTONUP:
                rectangle_selection, rectangle_width, rectangle_height = 1, 1, 1
                pygame.draw.rect(screen, (0,255,255), rectangle_main, 2, 3)
                array(rectangles_storage.append(rectangle_main))
                pygame.display.flip()
            
        pygame.display.flip()
        fps.tick(60)