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

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

        <bdo id='MhGXP'></bdo><ul id='MhGXP'></ul>

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

      <tfoot id='MhGXP'></tfoot>

        在pygame中旋转时,船向上和向左移动比向下和向右移动更快

        时间:2023-07-03
          1. <small id='rvLZ6'></small><noframes id='rvLZ6'>

          2. <legend id='rvLZ6'><style id='rvLZ6'><dir id='rvLZ6'><q id='rvLZ6'></q></dir></style></legend>

            • <bdo id='rvLZ6'></bdo><ul id='rvLZ6'></ul>
              <tfoot id='rvLZ6'></tfoot>

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

                  本文介绍了在pygame中旋转时,船向上和向左移动比向下和向右移动更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在开发一个简单的游戏.我创建了一个 pygame 精灵,并通过使其向前移动并以一致的速度旋转来对其进行测试.但是,它似乎向左和向上(sin 和 cos 为负数)比向右和向下(sin 和 cos 为正数)更快.我测试了它没有移动,只是旋转,它可以工作.

                  代码如下:

                  import pygame导入系统从数学导入 cos, sin, pi从时间导入睡眠屏幕宽度 = 800屏幕高度 = 800每秒帧数 = 60pygame.init()DISPLAY = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))时钟 = pygame.time.Clock()红色 = (255, 0, 0)绿色 = (0, 255, 0)蓝色 = (0, 0, 255)白色 = (255, 255, 255)黑色 = (0, 0, 0)SHIP_BLUE_IMG = pygame.image.load('./spaceshooter/PNG/playerShip1_blue.png').convert()SHIP_RED_IMG = pygame.image.load('./spaceshooter/PNG/playerShip1_red.png').convert()LASER_BLUE_IMG = pygame.image.load('./spaceshooter/PNG/Lasers/laserBlue16.png').convert()LASER_RED_IMG = pygame.image.load('./spaceshooter/PNG/Lasers/laserRed16.png').convert()激光速度 = 10PLAYER_SHIP_SPEED = 5all_sprites = pygame.sprite.Group()类播放器(pygame.sprite.Sprite):def __init__(self, img, pos, angle):超级().__init__()img.set_colorkey((0, 0, 0, 0))self.angle = 角度self.original_img = pygame.transform.rotate(img, 180) # 因为这些图片倒过来了self.image = self.original_imgself.rect = self.image.get_rect()self.rect.center = posself._update_image()def _update_image(self):x, y = self.rect.centerself.image = pygame.transform.rotate(self.original_img, self.angle)self.rect = self.image.get_rect()self.rect.center = (x, y)def _get_radeons(self):返回 (self.angle*pi)/180def 旋转(自我,度数):self.angle += 度数self._update_image()定义更新(自我):自旋转(5)x, y = self.rect.centernx = sin(self._get_radeons())*PLAYER_SHIP_SPEED + xny = cos(self._get_radeons())*PLAYER_SHIP_SPEED + yself.rect.center = (nx, ny)玩家 = 玩家(SHIP_BLUE_IMG, (300, 300), 45)all_sprites.add(玩家)而真:对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:sys.exit()DISPLAY.fill(黑色)all_sprites.update()all_sprites.draw(显示)pygame.display.update()CLOCK.tick(FPS)

                  为了描述我运行它时的样子,它是一艘黑色背景上的船,逆时针旋转,同时在应该是一个圆圈的地方向前移动.但相反,它正在形成一种螺旋,慢慢靠近屏幕的左上角.

                  解决方案

                  这是一个准确性问题.由于

                  导入数学导入pygame类播放器(pygame.sprite.Sprite):def __init__(self, img, pos, angle):超级().__init__()img.set_colorkey((0, 0, 0, 0))self.angle = 角度self.original_img = pygame.transform.rotate(img, 180)self.image = self.original_imgself.rect = self.image.get_rect()self.rect.center = posself.pos = 位置自我速度 = 5self.angle_step = 5self._update_image()def _update_image(self):x, y = self.rect.centerself.image = pygame.transform.rotate(self.original_img, self.angle)self.rect = self.image.get_rect()self.rect.center = (x, y)定义更新(自我):self.angle += self.angle_stepself._update_image()nx = math.sin(math.radians(self.angle)) * self.speed + self.pos[0]ny = math.cos(math.radians(self.angle)) * self.speed + self.pos[1]self.pos = (nx, ny)self.rect.center = 圆形(nx),圆形(ny)pygame.init()窗口 = pygame.display.set_mode((300, 300))时钟 = pygame.time.Clock()背景 = pygame.Surface(window.get_size())背景.set_alpha(5)all_sprites = pygame.sprite.Group()ship_image = pygame.image.load('Rocket64.png').convert_alpha()player = Player(ship_image, (window.get_width()//2-40, window.get_height()//2+40), 45)all_sprites.add(玩家)运行=真运行时:时钟.tick(60)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 假all_sprites.update()window.blit(背景, (0, 0))all_sprites.draw(窗口)pygame.display.update()pygame.quit()出口()

                  I am working on a simple game. I created a pygame sprite which and tested it by making it move forward and rotating at a consistent speed. However, it seams to be moving left and up (where sin and cos are negative) quicker than right and down (where sin and cos are positive). I tested it without moving and just rotating and it works.

                  Here is the code:

                  import pygame
                  import sys
                  from math import cos, sin, pi
                  from time import sleep
                  
                  SCREEN_WIDTH = 800
                  SCREEN_HEIGHT = 800
                  
                  FPS = 60
                  
                  pygame.init()
                  DISPLAY = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
                  CLOCK = pygame.time.Clock()
                  
                  RED = (255, 0, 0)
                  GREEN = (0, 255, 0)
                  BLUE = (0, 0, 255)
                  WHITE = (255, 255, 255)
                  BLACK = (0, 0, 0)
                  
                  SHIP_BLUE_IMG = pygame.image.load('./spaceshooter/PNG/playerShip1_blue.png').convert()
                  SHIP_RED_IMG = pygame.image.load('./spaceshooter/PNG/playerShip1_red.png').convert()
                  LASER_BLUE_IMG = pygame.image.load('./spaceshooter/PNG/Lasers/laserBlue16.png').convert()
                  LASER_RED_IMG = pygame.image.load('./spaceshooter/PNG/Lasers/laserRed16.png').convert()
                  
                  LASER_SPEED = 10
                  PLAYER_SHIP_SPEED = 5
                  
                  all_sprites = pygame.sprite.Group()
                  
                  
                  class Player(pygame.sprite.Sprite):
                      def __init__(self, img, pos, angle):
                          super().__init__()
                          img.set_colorkey((0, 0, 0, 0))
                          self.angle = angle
                          self.original_img = pygame.transform.rotate(img, 180) # Becase these images come upside down
                          self.image = self.original_img
                          self.rect = self.image.get_rect()
                          self.rect.center = pos
                  
                          self._update_image() 
                  
                      def _update_image(self):
                          x, y = self.rect.center
                          self.image = pygame.transform.rotate(self.original_img, self.angle)
                          self.rect = self.image.get_rect()
                          self.rect.center = (x, y)
                  
                      def _get_radeons(self):
                          return (self.angle*pi)/180
                  
                      def rotate(self, degrees):
                          self.angle += degrees
                          self._update_image()
                  
                      def update(self):
                          self.rotate(5)
                          x, y = self.rect.center
                          nx = sin(self._get_radeons())*PLAYER_SHIP_SPEED + x
                          ny = cos(self._get_radeons())*PLAYER_SHIP_SPEED + y
                          self.rect.center = (nx, ny)
                  
                  
                  player = Player(SHIP_BLUE_IMG, (300, 300), 45)
                  all_sprites.add(player)
                  
                  while True:
                      for event in pygame.event.get():
                          if event.type == pygame.QUIT:
                              sys.exit()
                  
                      DISPLAY.fill(BLACK)
                      all_sprites.update()
                      all_sprites.draw(DISPLAY)
                      pygame.display.update()
                      CLOCK.tick(FPS)
                  

                  To describe what it looks like when I run it, it is a ship on a black background that rotates counter-clockwise while moving forward in what should be a circle. But instead, it is creating a sort of spiral, slowly getting closer to the top left corner of the screen.

                  解决方案

                  This is an accuracy issue. Since pygame.Rect is supposed to represent an area on the screen, a pygame.Rect object can only store integral data.

                  The coordinates for Rect objects are all integers. [...]

                  The fraction part of the coordinates gets lost when the new position of the player is assigned to the Rect object:

                  self.rect.center = (nx, ny)
                  

                  Since this is done every frame, the position error will accumulate over time.

                  If you want to store object positions with floating point accuracy, you have to store the location of the object in separate variables respectively attributes (self.pos) and to synchronize the pygame.Rect object. round the coordinates and assign it to the location (e.g. .center) of the rectangle:

                  class Player(pygame.sprite.Sprite):
                      def __init__(self, img, pos, angle):
                          # [...]
                  
                          self.pos = pos
                  
                      # [...]
                  
                      def update(self):
                          self.rotate(5)
                          nx = sin(self._get_radeons())*PLAYER_SHIP_SPEED + self.pos[0]
                          ny = cos(self._get_radeons())*PLAYER_SHIP_SPEED + self.pos[1]
                          self.pos = (nx, ny)
                          self.rect.center = round(nx), round(ny)
                  

                  See also Move and rotate.


                  Minimal example:

                  import math
                  import pygame
                  
                  class Player(pygame.sprite.Sprite):
                      def __init__(self, img, pos, angle):
                          super().__init__()
                          img.set_colorkey((0, 0, 0, 0))
                          self.angle = angle
                          self.original_img = pygame.transform.rotate(img, 180)
                          self.image = self.original_img
                          self.rect = self.image.get_rect()
                          self.rect.center = pos
                          self.pos = pos
                          self.speed = 5
                          self.angle_step = 5
                          self._update_image() 
                  
                      def _update_image(self):
                          x, y = self.rect.center
                          self.image = pygame.transform.rotate(self.original_img, self.angle)
                          self.rect = self.image.get_rect()
                          self.rect.center = (x, y)
                  
                      def update(self):
                          self.angle += self.angle_step
                          self._update_image()
                          nx = math.sin(math.radians(self.angle)) * self.speed + self.pos[0]
                          ny = math.cos(math.radians(self.angle)) * self.speed + self.pos[1]
                          self.pos = (nx, ny)
                          self.rect.center = round(nx), round(ny)
                  
                  pygame.init()
                  window = pygame.display.set_mode((300, 300))
                  clock = pygame.time.Clock()
                  background = pygame.Surface(window.get_size())
                  background.set_alpha(5)
                  
                  all_sprites = pygame.sprite.Group()
                  ship_image = pygame.image.load('Rocket64.png').convert_alpha()
                  player = Player(ship_image, (window.get_width()//2-40, window.get_height()//2+40), 45)
                  all_sprites.add(player)
                  
                  run = True
                  while run:
                      clock.tick(60)
                      for event in pygame.event.get():
                          if event.type == pygame.QUIT:
                              run = False
                  
                      all_sprites.update()
                  
                      window.blit(background, (0, 0))
                      all_sprites.draw(window)
                      pygame.display.update()
                  
                  pygame.quit()
                  exit()
                  

                  这篇关于在pygame中旋转时,船向上和向左移动比向下和向右移动更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 PIL 在 python 中旋转并将扩展参数设置为 true 时指定图像填充颜色 下一篇:如何使用 PIL 调整大小并将旋转 EXIF 信息应用于文件?

                  相关文章

                      <bdo id='lZY6O'></bdo><ul id='lZY6O'></ul>

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

                      <tfoot id='lZY6O'></tfoot>

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

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