Вы можете просто изменить ширину и высоту прямоугольника (атрибуты w
и h
).Когда мышь перемещается (событие MOUSEMOTION
добавляется в очередь) и если выбран прямоугольник, добавьте относительное движение event.rel
к ширине и высоте.
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
rect = pg.Rect(100, 100, 161, 100)
rect_selected = False
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.MOUSEBUTTONDOWN:
# Set rect_selected to True when the user clicks on the rect.
if rect.collidepoint(event.pos):
rect_selected = True
elif event.type == pg.MOUSEBUTTONUP:
rect_selected = False
elif event.type == pg.MOUSEMOTION:
if rect_selected:
# Simply adjust the width and the height of the screen
# by subtracting the relative mouse movement.
rect.w += event.rel[0]
rect.h += event.rel[1]
# 10*10 px is the minimal size, so that the rect can
# still be clicked.
rect.w = max(rect.w, 10)
rect.h = max(rect.h, 10)
screen.fill((30, 30, 30))
pg.draw.rect(screen, (0, 100, 250), rect)
pg.display.flip()
clock.tick(30)
Если вы хотите сделать это с несколькими ритами, вы можете просто назначить прямоугольник, на который нажали, переменной (в данном случае selected_rect
), а затем масштабировать его.
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
rect1 = pg.Rect(100, 100, 161, 100)
rect2 = pg.Rect(300, 200, 161, 100)
selected_rect = None
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.MOUSEBUTTONDOWN:
# Set selected_rect to the colliding rect.
for rect in (rect1, rect2):
if rect.collidepoint(event.pos):
selected_rect = rect
elif event.type == pg.MOUSEBUTTONUP:
selected_rect = None # De-select.
elif event.type == pg.MOUSEMOTION:
if selected_rect is not None: # Scale if a rect is selected.
selected_rect.w += event.rel[0]
selected_rect.h += event.rel[1]
selected_rect.w = max(selected_rect.w, 10)
selected_rect.h = max(selected_rect.h, 10)
screen.fill((30, 30, 30))
pg.draw.rect(screen, (0, 100, 250), rect1)
pg.draw.rect(screen, (0, 200, 120), rect2)
pg.display.flip()
clock.tick(30)