旋转图像使背景变脏
Rotating image makes the background dirty
我正在使用 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()
这是汽车旋转时的照片:
还有一个问题,我的图片有清晰的背景,但它仍然覆盖了背景。
如果您的图像具有透明背景,则必须调用 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()
我正在使用 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()
这是汽车旋转时的照片:
还有一个问题,我的图片有清晰的背景,但它仍然覆盖了背景。
如果您的图像具有透明背景,则必须调用 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()