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 = 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 通道丢失了。
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()
并用完全透明的颜色填充表面(例如(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))
推荐阅读
- java - 提交远程命令时出错:sun/misc/BASE64Encoder: sun.misc.BASE64Encoder
- sql - 在 SQL 中重新创建移动中位数和移动模式 Excel 公式
- flutter - 如何在 GCP App Engine 上部署 Flutter Web App
- python-3.x - pip3 安装 cbor2 抛出 UnicodeDecodeError
- dns - 尝试创建 Route53 DNS 记录时,等待 ChangeResourceRecordSetsAsync 永远挂起
- wxpython - 如何在 wxPython StaticBitmap 上接收鼠标事件?
- javascript - 节点列表或 Html 集合不迭代
- javascript - 未捕获的类型错误:无法读取未定义的属性“对象”
- rest - 如何找到与模拟 StackOverflow API 类似的 API?
- asp.net-core - Blazor 未正确重定向到 IIS 上的控制器