Python 如何删除图像周围的黑色矩形
我正试图把这个黑色的矩形从我的车上取下来,但它似乎从来没有被取下来。我的车停止工作,黑色矩形仍然存在Python 如何删除图像周围的黑色矩形,python,pygame,Python,Pygame,我正试图把这个黑色的矩形从我的车上取下来,但它似乎从来没有被取下来。我的车停止工作,黑色矩形仍然存在 class Car: def __init__(self, x, y, height, width): self.x = x - width / 2 self.y = y - height / 2 self.height = height self.width = width self.rect = pyga
class Car:
def __init__(self, x, y, height, width):
self.x = x - width / 2
self.y = y - height / 2
self.height = height
self.width = width
self.rect = pygame.Rect(x, y, height, width)
self.surface = pygame.Surface((height, width)) # 1
self.surface.blit(img, (0, 0))
self.angle = 0
self.speed = 0 # 2
def draw(self): # 3
self.rect.topleft = (int(self.x), int(self.y))
rotated = pygame.transform.rotate(self.surface, self.angle)
surface_rect = self.surface.get_rect(topleft = self.rect.topleft)
new_rect = rotated.get_rect(center = surface_rect.center)
window.blit(rotated, new_rect.topleft)
car1 = Car(300, 300, 100, 100) # 4
clock = pygame.time.Clock()
我的完整代码
import pygame, math
pygame.init()
width = 700
height = 700
window = pygame.display.set_mode((width, height), pygame.NOFRAME)
pygame.display.set_caption("car game")
img = pygame.image.load("car6_red.png")
class black:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.blackimage = pygame.image.load("black.png")
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
window.blit(self.blackimage,self.rect)
blacks = []
platformGroup = pygame.sprite.Group
level = [
"d d d d d d d d d d d d d d d d",
"d d d d d d d d d d d d d d d d",
"d d d d d d d d d d d d d d d d",
"d d d d d d d d d d d d d d d d",
"d d d d d d d d d d d d d d d d",
"d d d d d d d d d d d d d d d d"]
for iy, row in enumerate(level):
for ix, col in enumerate(row):
if col == "d":
new_platforms = black(ix*9.8, iy*50, 10,10,(255,255,255))
blacks.append(new_platforms)
class Car:
def __init__(self, x, y, height, width):
self.x = x - width / 2
self.y = y - height / 2
self.height = height
self.width = width
self.rect = pygame.Rect(x, y, height, width)
self.surface = pygame.Surface((height, width)) # 1
self.surface.blit(img, (0, 0))
self.angle = 0
self.speed = 0 # 2
def draw(self): # 3
self.rect.topleft = (int(self.x), int(self.y))
rotated = pygame.transform.rotate(self.surface, self.angle)
surface_rect = self.surface.get_rect(topleft = self.rect.topleft)
new_rect = rotated.get_rect(center = surface_rect.center)
window.blit(rotated, new_rect.topleft)
car1 = Car(300, 300, 100, 100) # 4
clock = pygame.time.Clock()
def ReDrawWindow():
for black in blacks:
black.draw()
car1.draw()
runninggame = True
while runninggame:
for event in pygame.event.get():
if event.type == pygame.QUIT:
runninggame = False
pressed = pygame.key.get_pressed()
car1.speed *= 0.9 # 5
if pressed[pygame.K_UP]: car1.speed += 0.5 # 6
if pressed[pygame.K_DOWN]: car1.speed -= 0.5 # 6
if pressed[pygame.K_LEFT]: car1.angle += car1.speed / 2 # 7
if pressed[pygame.K_RIGHT]: car1.angle -= car1.speed / 2 # 7
car1.x -= car1.speed * math.sin(math.radians(car1.angle)) # 8
car1.y -= car1.speed * math.cos(math.radians(-car1.angle)) # 8
window.fill((0, 0, 0)) # 9
ReDrawWindow()
pygame.display.flip()
clock.tick(60) # 10
pygame.quit()
这个问题是由这样一个事实引起的,即您在一个对象上blit汽车图像,它不提供每像素alpha。因此,“car6_red.png”的alpha通道丢失
self.surface=pygame.surface((高度、宽度))#1
自表面blit(img,(0,0))
使用pygame.SRCALPHA
标志创建每像素alpha的曲面:
self.surface=pygame.surface((高度、宽度),pygame.SRCALPHA)
自表面blit(img,(0,0))
或者,您可以更改曲面的像素格式(包括每像素alpha),并使用完全透明的颜色(例如(0,0,0,0),填充曲面:
self.surface=pygame.surface((高度、宽度)).convert_alpha()
自表面填充((0,0,0,0))
自表面blit(img,(0,0))