在 Pygame 中获取旋转图像的旋转矩形 [英] Getting rotated rect of rotated image in Pygame

查看:73
本文介绍了在 Pygame 中获取旋转图像的旋转矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 pygame rect 的问题.

矩形不是我想要的.

我看到我可以用精灵类来做到这一点.但我不想使用精灵.我不明白 Sprite Rect 和 Image Rect 的区别.

这是我的矩形函数:

def getRect(self):返回 self.image.get_rect(center=(self.x,self.y))

图片是旋转后的图片.

我的英语不太好,我已经尽量告诉你了.

解决方案

导入pygamepygame.init()窗口 = pygame.display.set_mode((400, 400))font = pygame.font.SysFont(None, 50)时钟 = pygame.time.Clock()orig_image = font.render(旋转矩形", True, (255, 0, 0))角度 = 30旋转图像 = pygame.transform.rotate(orig_image, 角度)def draw_rect_angle(冲浪,矩形,枢轴,角度):pts = [rect.topleft, rect.topright, rect.bottomright, rect.bottomleft]pts = [(pygame.math.Vector2(p) - pivot).rotate(-angle) + p pts中的pivot]pygame.draw.lines(surf, (255, 255, 0), True, pts, 3)运行 = 真运行时:时钟滴答(60)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误window.fill(0)window_center = window.get_rect().centerwindow.blit(rotated_image,rotated_image.get_rect(center = window_center))rect = orig_image.get_rect(center = window_center)draw_rect_angle(窗口,矩形,window_center,角度)pygame.display.flip()pygame.quit()出口()

I have a problem about pygame rect.

The rect isn't like what I want.

I saw I can do this with sprite class. But I don't want use sprites. I didn't understand differences of Sprite Rect and Image Rect.

I want to get rect like this

But I am getting like this

Here is my rect function:

def getRect(self):
    return self.image.get_rect(center=(self.x,self.y))

Image is rotationed image.

I haven't very well english, I've told you as much as I can.

解决方案

get_rect() returns a pygame.Rect object. A pygame.Rect stores a position and a size. It is always axis aligned and cannot represent a rotated rectangle.

Use pygame.math.Vector2.rotate() to compute the corner points of the rotated rectangle. orig_image is the image before it is roatated:

rect = orig_image.get_rect(center = (self.x, self.y))

pivot = pygame_math.Vector2(self.x, self.y)

p0 = (pygame.math.Vector2(rect.topleft) - pivot).rotate(-angle) + pivot 
p1 = (pygame.math.Vector2(rect.topright) - pivot).rotate(-angle) + pivot 
p2 = (pygame.math.Vector2(rect.bottomright) - pivot).rotate(-angle) + pivot 
p3 = (pygame.math.Vector2(rect.bottomleft) - pivot).rotate(-angle) + pivot 

Use pygame.draw.lines() to draw the rotated rectangle:

pygame.draw.lines(screen, (255, 255, 0), True, [p0, p1, p2, p3], 3)


See also How do I rotate an image around its center using PyGame? and How can you rotate an image around an off center pivot in PyGame.


Minimal example:

import pygame

pygame.init()
window = 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 = 30
rotated_image = pygame.transform.rotate(orig_image, angle)

def draw_rect_angle(surf, rect, pivot, angle):
    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    

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

pygame.quit()
exit()

这篇关于在 Pygame 中获取旋转图像的旋转矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆