Python Forum
Make rectangle snap to other rectangle corners
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Make rectangle snap to other rectangle corners
#1
I would like to make the moveable rectangle r0 snap to the other rectangle corners when I move it near them with the mouse.

That is, if say any corner of my rectangle r0 gets near a corner of r1, then set the position of r0 to that corner.

I can't see how to achieve this.

Maybe create a bigger, unseen version of r0 and if it collides with a corner of r1, set that point as r0.x, r0.y ???


import pygame
from pygame.locals import *
from pygame.rect import *

def rectMM3():
    pygame.init()
    width = 600
    height = 600
    SIZE = (500, 200)

    RED = (255, 0, 0)
    GREEN = (0, 255, 0)
    BLUE = (0, 0, 255)

    YELLOW = (255, 255, 0)
    MAGENTA = (255, 0, 255)
    CYAN = (0, 255, 255)

    BLACK = (0, 0, 0)
    GRAY = (150, 150, 150)
    WHITE = (255, 255, 255)

    # set the font
    font = pygame.font.Font(None, 24)

    screen = pygame.display.set_mode((width, height))

    pts = ('topleft', 'topright', 'bottomleft', 'bottomright')

    def draw_text(text, pos):
        img = font.render(text, True, BLACK)
        screen.blit(img, pos)
    
    running = True
    
    r0 = Rect(50, 60, 200, 80)
    print(f'x={r0.x}, y={r0.y}, w={r0.w}, h={r0.h}')
    r1 = Rect(100, 20, 100, 140)
    print(f'x={r1.x}, y={r1.y}, w={r1.w}, h={r1.h}')
    #r3 = Rect(40, 500, 220, 100)

    moving = False

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

            elif event.type == MOUSEBUTTONDOWN:
                if r0.collidepoint(event.pos):
                    moving = True            

            elif event.type == MOUSEBUTTONUP:
                moving = False

            elif event.type == MOUSEMOTION and moving:
                r0.move_ip(event.rel)

        screen.fill(GRAY)
        pygame.draw.rect(screen, RED, r0)
        pygame.draw.rect(screen, CYAN, r1)
        if moving:
            pygame.draw.rect(screen, BLUE, r0, 4)        
        
        pygame.draw.rect(screen, GREEN, r0, 4)
        pygame.draw.rect(screen, GREEN, r1, 4)
        for pt in pts:
            pos1 = eval('r0.' + pt)
            pos2 = eval('r1.' + pt)
            draw_text(pt, pos1)
            draw_text(pt, pos2)
            # number at the end is the radius
            pygame.draw.circle(screen, RED, pos1, 3)
            pygame.draw.circle(screen, CYAN, pos2, 4)

        pygame.display.flip()  
    pygame.quit()
Reply
#2
(Aug-13-2021, 09:22 AM)Pedroski55 Wrote: Maybe create a bigger, unseen version of r0 and if it collides with a corner of r1, set that point as r0.x, r0.y ???
Yes this is exactly what to do. There is a built in method for these kind of operations for Rects called inflate() and inflate_ip() (inflate in place .ie modify the original instead of creating a new rect)

I would use pygame rects for objects in classes. An original rect for its coordinates. Another inflated rect for its collision detection. You dont need to create a separate object for rect collision.

This is not a drag and drop feature, but a drag example. I think it would illustrate why use classes and will make your world a lot easier if you. It would better organize your code and make it easier for you and us to understand.
https://github.com/Mekire/pygame-samples...ag_text.py

Your code is similar to the non class code. So maybe looking at the comparisons would help in how to use classes.
https://python-forum.io/thread-34609.html

Ugggg. I wrote a longer explanation but it gave a 400 request when i edited the post so i lost it all and now have to leave. sorry.
Recommended Tutorials:
Reply
#3
Thanks!

I copied the code from both links. I will look at it carefully later, until I can understand it!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Surface and rectangle in pygame Fabrizio_fg 6 2,208 May-27-2023, 09:15 AM
Last Post: Fabrizio_fg
  Rotating a rectangle CompleteNewb 19 12,977 Aug-28-2021, 03:23 PM
Last Post: CompleteNewb
  [PyGame] Rectangle keeps teleporting? keyfive 1 3,177 Jun-27-2018, 11:49 PM
Last Post: sonnekrieger7

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020