<i id='gnsoU'><tr id='gnsoU'><dt id='gnsoU'><q id='gnsoU'><span id='gnsoU'><b id='gnsoU'><form id='gnsoU'><ins id='gnsoU'></ins><ul id='gnsoU'></ul><sub id='gnsoU'></sub></form><legend id='gnsoU'></legend><bdo id='gnsoU'><pre id='gnsoU'><center id='gnsoU'></center></pre></bdo></b><th id='gnsoU'></th></span></q></dt></tr></i><div id='gnsoU'><tfoot id='gnsoU'></tfoot><dl id='gnsoU'><fieldset id='gnsoU'></fieldset></dl></div>

      <legend id='gnsoU'><style id='gnsoU'><dir id='gnsoU'><q id='gnsoU'></q></dir></style></legend>

      <small id='gnsoU'></small><noframes id='gnsoU'>

    1. <tfoot id='gnsoU'></tfoot>
        <bdo id='gnsoU'></bdo><ul id='gnsoU'></ul>
      1. 如何设置 pygame.transform.rotate() 的轴心点(旋转中心)?

        时间:2023-07-03
          <tbody id='242cS'></tbody>
      2. <tfoot id='242cS'></tfoot>
          • <small id='242cS'></small><noframes id='242cS'>

            <i id='242cS'><tr id='242cS'><dt id='242cS'><q id='242cS'><span id='242cS'><b id='242cS'><form id='242cS'><ins id='242cS'></ins><ul id='242cS'></ul><sub id='242cS'></sub></form><legend id='242cS'></legend><bdo id='242cS'><pre id='242cS'><center id='242cS'></center></pre></bdo></b><th id='242cS'></th></span></q></dt></tr></i><div id='242cS'><tfoot id='242cS'></tfoot><dl id='242cS'><fieldset id='242cS'></fieldset></dl></div>
              <bdo id='242cS'></bdo><ul id='242cS'></ul>

                1. <legend id='242cS'><style id='242cS'><dir id='242cS'><q id='242cS'></q></dir></style></legend>

                2. 本文介绍了如何设置 pygame.transform.rotate() 的轴心点(旋转中心)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想围绕中心以外的点旋转一个矩形.到目前为止我的代码是:

                  I want to rotate a rectangle about a point other than the center. My code so far is:

                  import pygame
                  
                  pygame.init()
                  w = 640
                  h = 480
                  degree = 45
                  screen = pygame.display.set_mode((w, h))
                  
                  surf = pygame.Surface((25, 100))
                  surf.fill((255, 255, 255))
                  surf.set_colorkey((255, 0, 0))
                  bigger = pygame.Rect(0, 0, 25, 100)
                  pygame.draw.rect(surf, (100, 0, 0), bigger)
                  rotatedSurf = pygame.transform.rotate(surf, degree)
                  screen.blit(rotatedSurf, (400, 300))
                  
                  running = True
                  while running:
                      event = pygame.event.poll()
                      if event.type == pygame.QUIT:
                          running = False
                      pygame.display.flip()
                  

                  我可以改变度数来获得不同的旋转,但旋转是关于中心的.我想设置一个矩形中心以外的点作为旋转点.

                  I can change the degree to get different rotation but the rotation is about the center. I want to set a point other than the center of the rectangle as the rotation point.

                  推荐答案

                  要围绕中心旋转表面,我们首先旋转图像,然后获取一个新的矩形,我们将 center 坐标传递到该矩形的前一个矩形保持居中.要围绕任意点旋转,我们几乎可以做同样的事情,但我们还必须向中心位置(枢轴点)添加一个偏移向量来移动矩形.每次我们旋转图像时都需要旋转这个向量.

                  To rotate a surface around its center, we first rotate the image and then get a new rect to which we pass the center coordinates of the previous rect to keep it centered. To rotate around an arbitrary point, we can do pretty much the same, but we also have to add an offset vector to the center position (the pivot point) to shift the rect. This vector needs to be rotated each time we rotate the image.

                  所以我们必须存储轴心点(图像或精灵的原始中心)——在元组、列表、向量或矩形中——和偏移向量(我们移动矩形的量)并传递它们rotate 函数.然后我们旋转图像和偏移向量,得到一个新的矩形,将枢轴 + 偏移作为 center 参数传递,最后返回旋转后的图像和新的矩形.

                  So we have to store the pivot point (the original center of the image or sprite) - in a tuple, list, vector or a rect - and the offset vector (the amount by which we shift the rect) and pass them to the rotate function. Then we rotate the image and offset vector, get a new rect, pass the pivot + offset as the center argument and finally return the rotated image and the new rect.

                  import pygame as pg
                  
                  
                  def rotate(surface, angle, pivot, offset):
                      """Rotate the surface around the pivot point.
                  
                      Args:
                          surface (pygame.Surface): The surface that is to be rotated.
                          angle (float): Rotate by this angle.
                          pivot (tuple, list, pygame.math.Vector2): The pivot point.
                          offset (pygame.math.Vector2): This vector is added to the pivot.
                      """
                      rotated_image = pg.transform.rotozoom(surface, -angle, 1)  # Rotate the image.
                      rotated_offset = offset.rotate(angle)  # Rotate the offset vector.
                      # Add the offset vector to the center/pivot point to shift the rect.
                      rect = rotated_image.get_rect(center=pivot+rotated_offset)
                      return rotated_image, rect  # Return the rotated image and shifted rect.
                  
                  
                  pg.init()
                  screen = pg.display.set_mode((640, 480))
                  clock = pg.time.Clock()
                  BG_COLOR = pg.Color('gray12')
                  # The original image will never be modified.
                  IMAGE = pg.Surface((140, 60), pg.SRCALPHA)
                  pg.draw.polygon(IMAGE, pg.Color('dodgerblue3'), ((0, 0), (140, 30), (0, 60)))
                  # Store the original center position of the surface.
                  pivot = [200, 250]
                  # This offset vector will be added to the pivot point, so the
                  # resulting rect will be blitted at `rect.topleft + offset`.
                  offset = pg.math.Vector2(50, 0)
                  angle = 0
                  
                  running = True
                  while running:
                      for event in pg.event.get():
                          if event.type == pg.QUIT:
                              running = False
                  
                      keys = pg.key.get_pressed()
                      if keys[pg.K_d] or keys[pg.K_RIGHT]:
                          angle += 1
                      elif keys[pg.K_a] or keys[pg.K_LEFT]:
                          angle -= 1
                      if keys[pg.K_f]:
                          pivot[0] += 2
                  
                      # Rotated version of the image and the shifted rect.
                      rotated_image, rect = rotate(IMAGE, angle, pivot, offset)
                  
                      # Drawing.
                      screen.fill(BG_COLOR)
                      screen.blit(rotated_image, rect)  # Blit the rotated image.
                      pg.draw.circle(screen, (30, 250, 70), pivot, 3)  # Pivot point.
                      pg.draw.rect(screen, (30, 250, 70), rect, 1)  # The rect.
                      pg.display.set_caption('Angle: {}'.format(angle))
                      pg.display.flip()
                      clock.tick(30)
                  
                  pg.quit()
                  

                  这是一个带有 pygame.sprite.Sprite 的版本:

                  Here's a version with a pygame.sprite.Sprite:

                  import pygame as pg
                  from pygame.math import Vector2
                  
                  
                  class Entity(pg.sprite.Sprite):
                  
                      def __init__(self, pos):
                          super().__init__()
                          self.image = pg.Surface((122, 70), pg.SRCALPHA)
                          pg.draw.polygon(self.image, pg.Color('dodgerblue1'),
                                          ((1, 0), (120, 35), (1, 70)))
                          # A reference to the original image to preserve the quality.
                          self.orig_image = self.image
                          self.rect = self.image.get_rect(center=pos)
                          self.pos = Vector2(pos)  # The original center position/pivot point.
                          self.offset = Vector2(50, 0)  # We shift the sprite 50 px to the right.
                          self.angle = 0
                  
                      def update(self):
                          self.angle += 2
                          self.rotate()
                  
                      def rotate(self):
                          """Rotate the image of the sprite around a pivot point."""
                          # Rotate the image.
                          self.image = pg.transform.rotozoom(self.orig_image, -self.angle, 1)
                          # Rotate the offset vector.
                          offset_rotated = self.offset.rotate(self.angle)
                          # Create a new rect with the center of the sprite + the offset.
                          self.rect = self.image.get_rect(center=self.pos+offset_rotated)
                  
                  
                  def main():
                      screen = pg.display.set_mode((640, 480))
                      clock = pg.time.Clock()
                      entity = Entity((320, 240))
                      all_sprites = pg.sprite.Group(entity)
                  
                      while True:
                          for event in pg.event.get():
                              if event.type == pg.QUIT:
                                  return
                  
                          keys = pg.key.get_pressed()
                          if keys[pg.K_d]:
                              entity.pos.x += 5
                          elif keys[pg.K_a]:
                              entity.pos.x -= 5
                  
                          all_sprites.update()
                          screen.fill((30, 30, 30))
                          all_sprites.draw(screen)
                          pg.draw.circle(screen, (255, 128, 0), [int(i) for i in entity.pos], 3)
                          pg.draw.rect(screen, (255, 128, 0), entity.rect, 2)
                          pg.draw.line(screen, (100, 200, 255), (0, 240), (640, 240), 1)
                          pg.display.flip()
                          clock.tick(30)
                  
                  
                  if __name__ == '__main__':
                      pg.init()
                      main()
                      pg.quit()
                  

                  这篇关于如何设置 pygame.transform.rotate() 的轴心点(旋转中心)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:每次启动应用程序时轮换日志文件(Python) 下一篇:Pygame-同时旋转精灵并跟随路径

                  相关文章

                3. <legend id='D1CiL'><style id='D1CiL'><dir id='D1CiL'><q id='D1CiL'></q></dir></style></legend>

                  <i id='D1CiL'><tr id='D1CiL'><dt id='D1CiL'><q id='D1CiL'><span id='D1CiL'><b id='D1CiL'><form id='D1CiL'><ins id='D1CiL'></ins><ul id='D1CiL'></ul><sub id='D1CiL'></sub></form><legend id='D1CiL'></legend><bdo id='D1CiL'><pre id='D1CiL'><center id='D1CiL'></center></pre></bdo></b><th id='D1CiL'></th></span></q></dt></tr></i><div id='D1CiL'><tfoot id='D1CiL'></tfoot><dl id='D1CiL'><fieldset id='D1CiL'></fieldset></dl></div>

                4. <small id='D1CiL'></small><noframes id='D1CiL'>

                    <bdo id='D1CiL'></bdo><ul id='D1CiL'></ul>
                  1. <tfoot id='D1CiL'></tfoot>