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

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

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

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

        Pygame-同时旋转精灵并跟随路径

        时间:2023-07-03

            <tbody id='YuCMj'></tbody>

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

            <tfoot id='YuCMj'></tfoot>

          1. <small id='YuCMj'></small><noframes id='YuCMj'>

                <bdo id='YuCMj'></bdo><ul id='YuCMj'></ul>
              • <i id='YuCMj'><tr id='YuCMj'><dt id='YuCMj'><q id='YuCMj'><span id='YuCMj'><b id='YuCMj'><form id='YuCMj'><ins id='YuCMj'></ins><ul id='YuCMj'></ul><sub id='YuCMj'></sub></form><legend id='YuCMj'></legend><bdo id='YuCMj'><pre id='YuCMj'><center id='YuCMj'></center></pre></bdo></b><th id='YuCMj'></th></span></q></dt></tr></i><div id='YuCMj'><tfoot id='YuCMj'></tfoot><dl id='YuCMj'><fieldset id='YuCMj'></fieldset></dl></div>
                  本文介绍了Pygame-同时旋转精灵并跟随路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试制作一个被抛出的球的动画,并且我希望它同时沿着平滑的抛物线路径旋转.但是,我似乎无法让 pygame.transform.rotate() 合作.

                  这是我迄今为止尝试过的:

                  导入pygamescreen = pygame.display.set_mode((500, 500))计时器 = pygame.time.Clock()球 = pygame.sprite.Sprite()ball.image = pygame.image.load("throwball.png")ball.image = pygame.transform.scale(ball.image, (48,48))ball.rect = ball.image.get_rect(topleft = (50,50))ball.orig_image = ball.imagerotaterect = ball.rect对于范围内的我(60):#pygame.draw.rect(屏幕,颜色(255,255,255),ball.rect)ball.image = pygame.transform.rotate(ball.orig_image, i*20)rotaterect.topleft = (5*i,((i-20)*(i-20) + 1)/5)ball.rect.center = rotaterect.centerscreen.blit(ball.image,ball.rect)pygame.display.update()计时器.tick(60)对于 pygame.event.get() 中的 e:如果 e.type == pygame.QUIT:pygame.quit()sys.exit()而1:对于 pygame.event.get() 中的 e:如果 e.type == pygame.QUIT:pygame.quit()sys.exit()

                  每当我运行代码时,球都会以不规则的方式移动.我做了第二个矩形,试图控制定位,但球最终形成了这样的路径:

                  显然每个框架的定位有问题,但我不确定为什么任何框架会不合适,因为它们的位置是由它们的中心点决定的.

                  这是球的图像:

                  它是 16x16(正方形),所以我对为什么图像没有遵循干净的路径感到目瞪口呆.

                  解决方案

                  注意,旋转后的图像大小不同.请参阅

                  for i in range(60):ball.image = pygame.transform.rotate(ball.orig_image, i*20)ball.rect = ball.image.get_rect()rotaterect.topleft = (5*i,((i-20)*(i-20) + 1)/5)ball.rect.center = rotaterect.centerscreen.blit(ball.image,ball.rect)pygame.display.update()

                  I'm trying to animate a ball getting thrown, and I want it to rotate an follow a smooth, parabolic path at the same time. However, I just can't seem to get pygame.transform.rotate() to cooperate.

                  Here's what I've tried so far:

                  import pygame
                  screen = pygame.display.set_mode((500, 500))
                  timer = pygame.time.Clock()
                  ball = pygame.sprite.Sprite()
                  ball.image = pygame.image.load("throwball.png")
                  ball.image = pygame.transform.scale(ball.image, (48,48))
                  ball.rect = ball.image.get_rect(topleft = (50,50))
                  ball.orig_image = ball.image
                  rotaterect = ball.rect
                  
                  for i in range(60):
                      #pygame.draw.rect(screen, Color(255,255,255), ball.rect)
                      ball.image = pygame.transform.rotate(ball.orig_image, i*20)
                      rotaterect.topleft = (5*i,((i-20)*(i-20) + 1)/5)
                      ball.rect.center = rotaterect.center
                      screen.blit(ball.image, ball.rect)
                      pygame.display.update()
                      timer.tick(60)
                      for e in pygame.event.get():
                          if e.type == pygame.QUIT:
                              pygame.quit()
                              sys.exit()
                  while 1:
                      for e in pygame.event.get():
                          if e.type == pygame.QUIT:
                              pygame.quit()
                              sys.exit()
                  

                  Whenever I run the code, the ball moves in an irregular pattern. I made a second rect to try to keep the positioning under control, but the ball ends up forming a path like this:

                  Obviously there's something wrong with the positioning on each frame, but I'm not sure why any of the frames would be out of place, since their location is determined by their center point.

                  Here's the ball image:

                  It's 16x16 (square), so I'm dumbfounded as to why the image isn't following a clean path.

                  解决方案

                  Note, a rotated image varies in size. See How do I rotate an image around its center using Pygame?.

                  Get the rectangle of the rotated image and set the center of the rectangle by the center of the helper rectangle rotaterect. This causes that the image is symmetrically aligned around the center.
                  You missed to update the ball.rect to the actual size of the rotated image:

                  ball.rect = ball.image.get_rect()
                  rotaterect.topleft = (5*i,((i-20)*(i-20) + 1)/5)
                  ball.rect.center = rotaterect.center
                  

                  Example:

                  for i in range(60):
                      ball.image = pygame.transform.rotate(ball.orig_image, i*20)
                      ball.rect = ball.image.get_rect()
                      rotaterect.topleft = (5*i,((i-20)*(i-20) + 1)/5)
                      ball.rect.center = rotaterect.center
                      screen.blit(ball.image, ball.rect)
                      pygame.display.update()
                  

                  这篇关于Pygame-同时旋转精灵并跟随路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何设置 pygame.transform.rotate() 的轴心点(旋转中心)? 下一篇:scipy.ndimage.interpolation.rotate 之后旋转的图像坐标?

                  相关文章

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

                      <legend id='lihOz'><style id='lihOz'><dir id='lihOz'><q id='lihOz'></q></dir></style></legend>
                    1. <i id='lihOz'><tr id='lihOz'><dt id='lihOz'><q id='lihOz'><span id='lihOz'><b id='lihOz'><form id='lihOz'><ins id='lihOz'></ins><ul id='lihOz'></ul><sub id='lihOz'></sub></form><legend id='lihOz'></legend><bdo id='lihOz'><pre id='lihOz'><center id='lihOz'></center></pre></bdo></b><th id='lihOz'></th></span></q></dt></tr></i><div id='lihOz'><tfoot id='lihOz'></tfoot><dl id='lihOz'><fieldset id='lihOz'></fieldset></dl></div>
                        <bdo id='lihOz'></bdo><ul id='lihOz'></ul>
                    2. <tfoot id='lihOz'></tfoot>