I was trying to make an area select feature where you hold left mouse button down and move the mouse and it makes a rectangle and everything inside that rectangle is selected.
import pygame
import time
window = pygame.display.set_mode((1200, 600))
thickest = 10
begin = False
while True:
pygame.event.get()
window.fill((255, 255, 255))
if any(pygame.mouse.get_pressed()) and not begin:
rpos = pygame.mouse.get_pos()
begin = True
if begin:
pos2 = pygame.mouse.get_pos()
pygame.draw.rect(window, (100, 200, 100), (rpos[0], rpos[1], pos2[0] - rpos[0], pos2[1] - rpos[1]), 1)
if not any(pygame.mouse.get_pressed()) and begin:
begin = False
pygame.display.flip()
However, I noticed that a rectangle with negative height isn't drawn at all and one with negative width isn't even a rectangle.
One thing I thought of doing was to offset the position by size and have the size as an absolute value of size. Example in x-axis:
if pos2[0] - rpos[0] < 0:
pygame.draw.rect(window, (100, 200, 100), (rpos[0] - (pos2[0] - rpos[0]),
rpos[1],
abs(pos2[0] - rpos[0]),
pos2[1] - rpos[1]))
But this approach doesn't seem elegant and I was wondering if there is a more pygame-ey way to handle this.