今天刚开始用pygame,遇到了点小麻烦。我旋转了汽车图像,但似乎矩形区域不适合图像(如图所示,白色区域比我旋转的图像大)。
您可以使矩形区域的颜色透明或与背景相同,以使图像看起来更好吗?
谢谢
您好,这是我的相关代码(来自 Richard Jones 的“Rapid Game Development in Python”):
class CarSprite(pygame.sprite.Sprite):
MAX_FORWARD_SPEED = 10
MAX_REVERSE_SPEED = 10
ACCELERATION = 2
TURN_SPEED = 5
def __init__(self, image, position):
pygame.sprite.Sprite.__init__(self)
self.src_image = pygame.image.load(image)
self.src_image = self.src_image.convert()
self.position = position
self.speed = self.direction = 0
self.k_left = self.k_right = self.k_down = self.k_up = 0
self.rect = self.src_image.get_rect()
self.rect.center = position
def update(self, deltat):
# SIMULATION
self.speed += (self.k_up + self.k_down)
if self.speed > self.MAX_FORWARD_SPEED:
self.speed = self.MAX_FORWARD_SPEED
if self.speed <-self.MAX_REVERSE_SPEED:
self.speed = -self.MAX_REVERSE_SPEED
self.direction += (self.k_right + self.k_left)
x, y = self.position
rad = self.direction * math.pi / 180
x += -self.speed*math.sin(rad)
y += -self.speed*math.cos(rad)
self.position = (x, y)
self.image = pygame.transform.rotate(self.src_image, self.direction)
self.rect = self.image.get_rect()
self.rect.center = self.position
def draw(self,screen):
self.image.set_alpha(150)
screen.blit(self.image, self.rect)