本文介绍了pygame中的旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been looking for solutions on how to make my image rotate around a single point but from what I've found nothing has really worked.

我所知道的

  • 我可以使用 pygame.transform.rotate(surface, angle)

我需要不断地重置中心,这样图像就不会在旋转后旋转.

I need to constantly reset the center so the image does not rotate on its rotated self.

起初图像是这样的

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

Then when I rotate it a couple times it looks like this

这是我的代码

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 中,老实说,我完全迷失了,甚至不知道它在做什么.提前致谢.

The error is in update_angle where I honestly am completely lost and do not even know what it's doing. Thanks in advance.

推荐答案

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

Your problem is that you are rotating a rotated image. Thus, the image is looking skewed, which would be expected. Instead, have two variables, one for the original variable, one for the most recent rotation.

所以代替:

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)
...

这篇关于pygame中的旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:03