我怎样才能改变矩形的大小
how can i change the size of a rectangle
我用的是Rabbid76的一个功能。有了这个功能,我可以旋转一个矩形。矩形得到 image.I 的大小想要更改矩形的大小,使其小于图像。
我尝试改变左上角和右上角。它没有用。
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()
在下面的代码中:
rect.topleft = (rect.topleft[0] +25 , rect.topleft[1])
rect.topright = (rect.topright[0] -25, rect.topright[1])
topleft[0]
就是top
; topright[0]
和 top
.
一样
然后 top + 25
、top - 25
等于 top + 0
所以什么都没有改变。
也许你想修改 bottomright
.
小提示:逐行调试,注意变量的变化,更容易发现程序执行与预期效果不一致的地方
您所要做的就是缩小输入矩形的大小。使用 pygame.Rect.inflate
with with 负值来减小矩形的大小:
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)
我用的是Rabbid76的一个功能。有了这个功能,我可以旋转一个矩形。矩形得到 image.I 的大小想要更改矩形的大小,使其小于图像。 我尝试改变左上角和右上角。它没有用。
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()
在下面的代码中:
rect.topleft = (rect.topleft[0] +25 , rect.topleft[1])
rect.topright = (rect.topright[0] -25, rect.topright[1])
topleft[0]
就是top
; topright[0]
和 top
.
然后 top + 25
、top - 25
等于 top + 0
所以什么都没有改变。
也许你想修改 bottomright
.
小提示:逐行调试,注意变量的变化,更容易发现程序执行与预期效果不一致的地方
您所要做的就是缩小输入矩形的大小。使用 pygame.Rect.inflate
with with 负值来减小矩形的大小:
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)