python - 如何删除图像周围的黑色矩形 Pygame

标签 python pygame

我正在尝试删除此video我车上的黑色矩形,但无论我尝试什么,它似乎都没有被移除,要么我的车停止工作,黑色矩形仍然存在

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 pygame.Surface 引起的。对象,不提供每像素 alpha。因此“car6_red.png”的Alpha channel 丢失了。

self.surface = pygame.Surface((height, width)) # 1
self.surface.blit(img, (0, 0))

使用pygame.SRCALPHA标志创建一个具有每像素alpha的表面:

self.surface = pygame.Surface((height, width), pygame.SRCALPHA)
self.surface.blit(img, (0, 0))

或者,您可以更改表面的像素格式,包括每个像素 alpha,通过 convert_alpha()并用完全透明的颜色填充 Surface(例如 (0, 0, 0, 0)):

self.surface = pygame.Surface((height, width)).convert_alpha()
self.surface.fill((0, 0, 0, 0))
self.surface.blit(img, (0, 0))

关于python - 如何删除图像周围的黑色矩形 Pygame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62960139/

相关文章:

python - 运行 pythoncom 代码时,Spyder 抛出 "assert sys.modules[modname] is not old_mod"

Python 日志记录 : propagate messages of level below current logger level

python - 如何更改 ARMAX.predict 的 maxlag?

python - 游戏图形数组的快速操作

python - 我如何让汽车沿着它指向的方向移动(使用 pygame.translation.rotate 后)

javascript - 使用 PyQt5 和 QWebEngineView 抓取 javascript 页面

python - 通过 NumPy 函数修改 Pandas 数据框

python - Pygame 和线程 : locked when accessing to globals?

python - 尝试在程序启动后立即播放音乐

python - 如何修复Pygame减速后角色不断向两个方向加速的问题?