pygame 轮换

Rotation in pygame

我一直在寻找关于如何让我的图像围绕一个点旋转的解决方案,但我发现没有任何效果。

我所知道的

一开始图片是这样的

然后当我旋转它几次时它看起来像这样

这是我的代码

import pygame


class pyGameSetup:

def __init__(self, title, width, height):

    # initialize pygame library
    pygame.init()

    # set up specific display properties
    self.game_display = pygame.display.set_mode((width, height))
    pygame.display.set_caption(title)

    # update display
    pygame.display.update()

    # create new global variables for the class
    # game state
    self.running = True

    # colors, only main
    # TODO: implement more color RGB values, if updated, update corresponding method
    self.white = (255, 255, 255)
    self.black = (0, 0, 0)
    self.grey = (128, 128, 128)
    self.pink = (255, 102, 178)
    self.blue = (0, 0, 255)
    self.red = (255, 0, 0)
    self.green = (0, 255, 0)
    self.purple = (127, 0, 255)
    self.yellow = (255, 255, 0)
    self.teal = (0, 204, 204)
    self.orange = (255, 128, 0)

    # player coordinates and properties
    self.player_x = int(width / 2)
    self.player_y = int(height / 2)
    self.player_width = 100
    self.player_height = 50
    self.player_speed_x = 0
    self.player_speed_y = 0
    self.move_speed = 5

    # FPS controllers (note start speed should always be 60 / 5 so, 1 / 300
    self.clock = pygame.time.Clock()
    self.fps = 60

    # screen values
    self.width = width
    self.height = height

    # rotation for car
    self.angle = 200

    # car image
    self.car1 = pygame.image.load("Images\Car1.png")
    self.update_size()
    self.car_rect = self.car1.get_rect()
    self.car_rect.center = (self.player_x, self.player_y)


def exit(self):
    pygame.quit()
    self.running = False
    quit()

def game_loop(self):
    while self.running:
        for event in pygame.event.get():
            self.event_handler(event)

        # update player position
        # self.player_x += self.player_speed_x
        self.angle = self.player_speed_x
        self.player_y += self.player_speed_y

        # check to make sure it's in bounds
        self.check_valid_move()

        # reprint canvas
        self.game_display.fill(self.white)

        # update angle
        self.update_angle()

        # print player
        self.game_display.blit(self.car1, (self.player_x, self.player_y, self.player_width,
                                           self.player_height))

        # update display
        pygame.display.update()

        # FPS
        self.clock.tick(self.fps)

def event_handler(self, e):
    if e.type == pygame.QUIT:
        self.exit()
    if e.type == pygame.KEYDOWN:
            if e.key == pygame.K_LEFT:
                self.player_speed_x = -self.move_speed
            if e.key == pygame.K_RIGHT:
                self.player_speed_x = self.move_speed
            if e.key == pygame.K_UP:
                self.player_speed_y = -self.move_speed
            if e.key == pygame.K_DOWN:
                self.player_speed_y = self.move_speed
    if e.type == pygame.KEYUP:
            if e.key == pygame.K_LEFT:
                self.player_speed_x = 0
            if e.key == pygame.K_RIGHT:
                self.player_speed_x = 0
            if e.key == pygame.K_UP:
                self.player_speed_y = 0
            if e.key == pygame.K_DOWN:
                self.player_speed_y = 0

def check_valid_move(self):
    if self.player_x <= 0:
        self.player_x = 0
    if self.player_x >= self.width - self.player_width:
        self.player_x = self.width - self.player_width
    if self.player_y <= 0:
        self.player_y = 0
    if self.player_y >= self.height - self.player_height:
        self.player_y = self.height - self.player_height

def update_size(self):
    self.car1 = pygame.transform.scale(self.car1, (self.player_width, self.player_height))
    self.car1 = pygame.transform.rotate(self.car1, -90)

def update_angle(self):
    old_center = self.car_rect.center
    self.car1 = pygame.transform.rotate(self.car1, self.angle)
    self.car_rect.center = old_center

x = pyGameSetup('Game', 800, 600)
x.game_loop()

错误在 update_angle 中,老实说,我完全迷路了,甚至不知道它在做什么。提前致谢。

对于没有抗锯齿的低分辨率图像旋转几次后,这个结果似乎很正常。 你应该做的是:

  • 如果可能,使用正好大 2 倍的源图像。首先旋转它 然后精确缩小 2 倍 - 如果可能的话,在 blit 期间,所以你 不需要将其作为单独的步骤进行缩放。
  • 不要旋转旋转的图像。 IE。不要将它旋转3次5度,而是每次复制原始图像并将其旋转5度, 10、15度。这样你就可以将失真降到最低, 也不必重新分配中心。

您的问题是您正在旋转旋转的图像。因此,图像看起来是歪斜的,这是意料之中的。相反,有两个变量,一个用于原始变量,一个用于最近的旋转。

所以代替:

car1 = original_image_rect
car1 = pygame.transform.rotate(car1, 10)
car1 = pygame.transform.rotate(car1, 10)
...

你会做:

car1 = original_image_rect
car2 = pygame.transform.rotate(car1, 10)
car2 = pygame.transform.rotate(car1, 20)
...