Joachim
Joachim

Reputation: 375

how can i change the size of a rectangle

I use a function of Rabbid76. With this function i can rotate a rectangle. The rectangle get the size of an image.I want to change the size of the rectangle so that is smaller than the image . that i try with changing topleft and topright. It doesent work.

import pygame

pygame.init()
screen = pygame.display.set_mode((400, 400))
font = pygame.font.SysFont(None, 50)
clock = pygame.time.Clock()

#orig_image = font.render("rotated rectangle", True, (255, 0, 0))
angle =0
orig_image = pygame.image.load("Bilder/baum2.png")
rotated_image = pygame.transform.rotate(orig_image, angle)

def draw_rect_angle(surf, rect, pivot, angle):   
    rect.topleft = (rect.topleft[0] +25 , rect.topleft[1])
    rect.topright = (rect.topright[0] -25, rect.topright[1])    
    pts = [rect.topleft, rect.topright, rect.bottomright, rect.bottomleft]
    pts = [(pygame.math.Vector2(p) - pivot).rotate(-angle) + pivot for p in pts]
    pygame.draw.lines(surf, (255, 255, 0), True, pts, 3)

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False    

    screen.fill(0)
    screen_center = screen.get_rect().center
    screen.blit(rotated_image, rotated_image.get_rect(center = screen_center))
    rect = orig_image.get_rect(center = screen_center)
    draw_rect_angle(screen, rect, screen_center, angle)
    pygame.display.flip()

pygame.quit()
exit()

Upvotes: 2

Views: 845

Answers (2)

Rabbid76
Rabbid76

Reputation: 210938

All you have to do is reduce the size of the input rectangle. Use pygame.Rect.inflate with with negative values to reduce the size of the rectangle:

def draw_rect_angle(surf, orig_rect, pivot, angle):

    rect = orig_rect.inflate(-25, -25)

    pts = [rect.topleft, rect.topright, rect.bottomright, rect.bottomleft]
    pts = [(pygame.math.Vector2(p) - pivot).rotate(-angle) + pivot for p in pts]
    pygame.draw.lines(surf, (255, 255, 0), True, pts, 3)

Upvotes: 1

cg-zhou
cg-zhou

Reputation: 563

In the code below:

    rect.topleft = (rect.topleft[0] +25 , rect.topleft[1])
    rect.topright = (rect.topright[0] -25, rect.topright[1])    

The topleft[0] is the top; topright[0] is aslo the top.

Then top + 25, top - 25 equals top + 0 so nothing changed.

Maybe you want to modify bottomright.

A small tip: Debug line by line and pay attention to the changes of variables, you can find the inconsistencies between the program execution and the expected effect more easily

Upvotes: 1

Related Questions