我是这个论坛的新手(有人问这个问题),而我目前正在开发PyGame游戏,该游戏除其他外还要求很多;处理精灵的透明度。这款游戏是关于避免飞星的宇宙飞船,玩家可以射击并摧毁它们。还值得一提的是,游戏的整个动作(除了玩家的动作)都是通过无限的垂直滚动来处理的。但是,我没有创建这个游戏。如果您有任何人去寻找由“安库尔·阿格加瓦尔(Ankur Aggarwal)”制作的游戏“ Hit The Stone”;您会确切地看到我在说什么游戏。
到目前为止,我已经能够做到所有这些,但是我目前仍无法摆脱一件事:用于“可射击”流星和玩家精灵的所有精灵都有黑色背景,一些理由。而且,尽管我仍然需要解决更多问题,但是这种黑色精灵背景目前是我在该项目中的主要目标之一。
我已经尝试了关于该主题的所有知识:从使用convert_alpha()初始化子画面的图像,到使用setColorKey((0,0,0))将颜色键设置为黑色,甚至返回使用使用convert()而不是convert_alpha()。我从Stackoverflow上其他人回答的问题中拿走了所有这些工具,但这些工具似乎都不适合我。
这是我用来加载背景图像的方法:
pygame.init()
screen=pygame.display.set_mode([640, 480],0,24)
pygame.display.set_caption("Hit The Stone")
fondo = pygame.image.load("imagenes/nebula_640x5052.jpg").convert()
background=pygame.Surface(screen.get_size())
background=background.convert()
screen.blit(background,(0,0))
BLACK = (0, 0, 0)
就像我上面提到的那样,那里的“黑色”元组是用于色键设置的。
这些是我用来生成要放入游戏的2层的类,以便游戏具有垂直视差滚动:
# This variable handles the speed
# at which every non-controllable background layer has to scroll.
velocidad_fondo = 1
# This class manages a layer of "background" asteroids I just want to
# vertically scroll all over the game window.
class StoneField(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("imagenes/asteroid_field_con_transparencia_640x1916.png")
self.rect = self.image.get_rect()
self.rect.bottom = 0
self.rect.left = 0
global velocidad_fondo
self.speed = velocidad_fondo * 2
def update(self):
self.rect.bottom += self.speed
# This class represents the player, and of course,
# the sprite it displays to let the player see where is he moving to.
class Plane(pygame.sprite.Sprite):
def __init__(self, image=None):
pygame.sprite.Sprite.__init__(self, self.containers)
self.image = image or Plane._default_image
self.image.set_colorkey(BLACK)
self.cooldown=15
self.rect=self.image.get_rect()
self.rect.centerx = screen.get_width() / 2
self.distancefromcenter=30
self.rect.centery=screen.get_height()-self.distancefromcenter-40
self.dx=3
self.dy=3
# This line is one of those goals I won't reach
# until I solve the sprite black background issue.
# Long story short, it is meant to generate a sprite
# representing the spaceship's motor, propelling it while it's moving.
self.propulsor = Propulsor()
def update(self):
self.pressed = pygame.key.get_pressed()
if self.pressed[K_DOWN]:
self.rect.centery+=self.dy
propulsores.remove(self.propulsor)
elif self.pressed[K_UP]:
self.rect.centery-=self.dy
propulsores.add(self.propulsor)
if self.pressed[K_LEFT]:
self.rect.centerx-=self.dx
elif self.pressed[K_RIGHT]:
self.rect.centerx+=self.dx
if self.rect.bottom>=screen.get_height():
self.rect.bottom=screen.get_height()
elif self.rect.top<=0:
self.rect.top=0
if self.rect.centerx>=screen.get_width()-self.distancefromcenter:
self.rect.centerx=screen.get_width()-self.distancefromcenter
elif self.rect.centerx<=self.distancefromcenter:
self.rect.centerx=self.distancefromcenter
self.cooldown = max(0, self.cooldown-1)
# The following class is used to spawn meteors
# at random locations and sizes, which the player has to destroy
# to avoid losing a life.
class Stone(pygame.sprite.Sprite):
def __init__(self, image=None):
pygame.sprite.Sprite.__init__(self, self.containers)
self.image = image or Stone._default_image
self.rect = self.image.get_rect()
self.rect.centerx = random.randint(5,630)
self.rect.centery = 0
self.dy = random.randint(1, 3)
self.font = pygame.font.Font(None, 20)
def update(self):
self.rect.centery+=self.dy
if self.rect.bottom>=screen.get_height():
self.rect.centerx=random.randint(5,630)
self.rect.centery=0
# These lines are the ones I use to convert the images into usable sprites.
Plane._default_image = pygame.image.load('plane.png').convert_alpha()
Bullet._default_image = pygame.image.load('geometrybullet.png').convert()
Stone._default_image = pygame.image.load('stone.gif').convert()
最后,以下循环在我的主游戏循环内。 “石头”,“子弹”都是PyGame子画面组,我使用它们来操纵流星和用户发射的子弹之间的碰撞检测:
for stone in stones:
for bullet in bullets:
if bullet.rect.colliderect(stone):
sonido_destruir_meteorito.play()
bullet.kill()
stone.cambiar_sprite_por_500_puntos()
#stone.kill()
stones.remove(stone)
SCORE += 500
if SCORE == 2000:
VIDAS += 1
allSprites.add(mensaje_1up)
clockMensajeVidaExtra.tick(2000)
#allSprites.remove(mensaje_1up)
if stone.rect.colliderect(plane):
VIDAS -= 1
pygame.mixer.music.play()
stone.kill()
if VIDAS == 0:
exit()
plane.kill()
stone_spawn_cooldown -= 1
if len(stones) < max_stones and stone_spawn_cooldown <= 0:
stones.add(Stone())
#Stone()
stone_spawn_cooldown = stone_spawn_delay
allSprites.clear(screen, background)
allSprites.update()
allSprites.draw(screen)
pygame.display.flip()
clock.tick(60)
y += velocidad_fondo
我希望精灵可以透明地显示在屏幕上,但是,正如我前面提到的那样;我一直保持黑色,覆盖播放器和流星精灵的背景。缩进实际上在我的.py文件中是正确的;只是我不习惯在这个论坛上问问题,所以我很可能在共享代码时搞砸了。
所以;这是我的问题。还有其他解决方法吗?还是我做错了什么?滚动部分可以吗?
如果我碰巧在这篇文章上犯了一个错误,请随时告诉我,我会尽快修复。