Skip to main content
 首页 » 编程设计

python之sge-pygame 旋转父子 Sprite

2024年10月01日8zfyouxi

我一直在研究并试图解决这个问题很长一段时间。我正在尝试创建一个父/子附加系统,以便当父移动时,附加到它的子移动/旋转/缩放相同。我遇到的主要问题是轮换。

这是我目前为 children 使用的代码:

def _rotate(self, origin, point, angle): 
  ox, oy = origin 
  px, py = point 
 
  qx = ox + math.cos(angle) * (px - ox) - math.sin(angle) * (py - oy) 
  qy = oy + math.sin(angle) * (px - ox) + math.cos(angle) * (py - oy) 
 
  return qx, qy 

_rotate 代码将 2D 点旋转我在搜索问题答案时找到的原点周围的角度。我使用的点是每个盒子/ Sprite 的中心。

旋转父对象时,我使用 sge-pygame image_rotation,然后继续进行子对象定位。之后我需要根据新的位置和新的角度计算出 child 的旋转来旋转 child 。

发生的事情的图像:

初始

和

旋转 10 度

父级是大矩形,盒子是子级。

我遇到的第二个问题是弄清楚子项的旋转角度是多少,以使其与父项保持一致。我通过 stackflow 发现 math.atan2 的使用应该用于此但是我似乎无法弄清楚如何使用它。

如有任何帮助,我们将不胜感激。

谢谢

请您参考如下方法:

您首先将父项和子项旋转相同的量(在此示例中,假设应控制父项而子项不应该)。然后你只需将 child 移动到 parent 的枢轴点(在本例中为它的中心),并在 parent 面对的方向上额外偏移(偏移当然是可选的)。

from math import cos, sin, radians 
import pygame 
pygame.init() 
 
SIZE = WIDTH, HEIGHT = 720, 480 
BACKGROUND_COLOR = pygame.Color('black') 
FPS = 60 
 
screen = pygame.display.set_mode(SIZE) 
clock = pygame.time.Clock() 
 
 
def update_child_rotation(parent, child, degrees, offset): 
    # Rotate the parent and child with the same angle. 
    parent.rotate(degrees) 
    child.rotate(degrees) 
 
    # Calculate the direction the parent is pointing. If you remember the unit circle then you'll 
    # know that cos(angle) represent the x value and sin(angle) the y value. At angle = 0 the direction 
    # is to the right (which is what your sprite should be pointing, otherwise you'll have to add an angle 
    # offset), and it rotates counterclockwise. The minus in '-sin' is because pygame uses positive y as downwards. 
    angle_in_radians = radians(parent.angle) 
    direction = pygame.math.Vector2(cos(angle_in_radians), -sin(angle_in_radians)) 
 
    # Update the child's position to the parent's position but with an added offset in the direction the parent 
    # is pointing. 
    child.position = parent.position + direction * offset 
 
 
class Entity(pygame.sprite.Sprite): 
 
    def __init__(self, position, size): 
        super().__init__() 
 
        self.original_image = pygame.Surface(size) 
        self.original_image.fill((255, 0, 0)) 
        self.original_image.set_colorkey(BACKGROUND_COLOR) 
 
        self.image = self.original_image 
        self.rect = self.image.get_rect(center=position) 
 
        self.angle = 0 
        self.position = pygame.math.Vector2(position) 
        self.velocity = pygame.math.Vector2(0, 0) 
 
    def rotate(self, degrees): 
        self.angle = (self.angle + degrees) % 360 
        self.image = pygame.transform.rotate(self.original_image, self.angle) 
        self.rect = self.image.get_rect(center=self.position) 
 
    def update(self, dt): 
        self.position += self.velocity 
        self.rect.center = self.position 
 
 
def main(): 
    parent = Entity(position=(200, 200), size=(128, 32)) 
    child = Entity(position=(356, 200), size=(32, 32)) 
    all_sprites = pygame.sprite.Group(parent, child) 
 
    running = True 
    while running: 
 
        dt = clock.tick(FPS) / 1000 
 
        for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                running = False 
            elif event.type == pygame.KEYDOWN: 
                if event.key == pygame.K_q: 
                    update_child_rotation(parent, child, 10, 156) 
                elif event.key == pygame.K_e: 
                    update_child_rotation(parent, child, -10, 156) 
            elif event.type == pygame.KEYUP: 
                if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT: 
                    parent.velocity.x = 0 
                elif event.key == pygame.K_DOWN or event.key == pygame.K_UP: 
                    parent.velocity.y = 0 
 
        all_sprites.update(dt) 
 
        screen.fill(BACKGROUND_COLOR) 
        all_sprites.draw(screen) 
        pygame.display.update() 
 
 
if __name__ == '__main__': 
    main()