旋转图像会使背景变脏

时间:2018-10-12 04:34:38

标签: python rotation pygame

我正在与pygame一起使用,并希望使汽车在地图内运行,但是当我调用rotate函数时,当汽车旋转并覆盖背景时,它会在车外制作框架(图片)。 这是我的代码:

import pygame
import time

class Background(pygame.sprite.Sprite):
    def __init__(self, image_file, location):
        pygame.sprite.Sprite.__init__(self)  #call Sprite initializer
        self.image = pygame.image.load(image_file)
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = location

class Car(pygame.sprite.Sprite):
    def __init__(self, image_file, location):
        pygame.sprite.Sprite.__init__(self)  #call Sprite initializer
        self.original_image = pygame.image.load(image_file).convert()
        self.image = self.original_image
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = location

    def rotate(self, angle) :
        self.image = pygame.transform.rotate(self.original_image, angle)

    def move(self, location) :
        self.rect.left, self.rect.top = location

pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((800,600))

background = Background('image/raw_map.png', [0,0])
car = Car('image/car.png', [100,100])

pygame.display.flip()

for i in range(90) :
    screen.fill((0, 0, 0))
    car.rotate(i)

    screen.blit(background.image, background.rect)
    screen.blit(car.image, car.rect)
    clock.tick(10)
    pygame.display.update()

这是汽车旋转时的照片:

enter image description here

还有一个问题,我的图像背景清晰,但是仍然覆盖背景。

1 个答案:

答案 0 :(得分:1)

如果图像具有透明背景,则必须调用convert_alpha方法。如果背景为单色,则可以调用set_colorkey使其透明,但是用convert_alpha转换的图像会更快地变色,因此我建议使用此方法。

我还修复了您的轮换代码,并添加了一些注释。

import pygame
# No need to import time, since you're using the pygame.time module.


class Background(pygame.sprite.Sprite):
    def __init__(self, image_file, location):
        pygame.sprite.Sprite.__init__(self)
        # Call the convert method, then the image can be blitted faster.
        self.image = pygame.image.load(image_file).convert()
        # You can pass the location as the `topleft` argument.
        self.rect = self.image.get_rect(topleft=location)


class Car(pygame.sprite.Sprite):
    def __init__(self, image_file, location):
        pygame.sprite.Sprite.__init__(self)
        # Call the `convert_alpha` method if the image has a transparent
        # background, otherwise you could use the `set_colorkey` method.
        self.original_image = pygame.image.load(image_file).convert_alpha()
        self.image = self.original_image
        self.rect = self.image.get_rect(topleft=location)

    def rotate(self, angle):
        self.image = pygame.transform.rotate(self.original_image, angle)
        # Get a new rect and pass the center of the previous rect. This
        # will keep the car centered.
        self.rect = self.image.get_rect(center=self.rect.center)

    def move(self, location):
        self.rect.topleft = location


pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((800,600))

background = Background('image/raw_map.png', [0,0])
car = Car('image/car.png', [100,100])
angle = 0

done = False
while not done:
    # Need to handle events each frame or the window will freeze.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    # Update the game.
    angle += 5
    car.rotate(angle)

    # Draw everything.
    screen.fill((0, 0, 0))
    screen.blit(background.image, background.rect)
    screen.blit(car.image, car.rect)
    clock.tick(30)
    pygame.display.update()