在pygame中移动时,黑盒出现在角色后面,如何让它消失?

时间:2015-11-07 02:58:36

标签: python pygame

我正在尝试在屏幕上左右移动一个角色,为此,它将每次都被绘制和删除。尽管角色的背景是透明的,但是在它背后可见的图像大小有一个黑盒子。它不会离开火车,但更多的行为就像你正在移动的角色。我该如何解决?这是我的播放器代码和游戏代码。

播放器:

Sub CountP()
Dim cell As Range
Dim ws As Worksheet
Dim i As Integer
Dim j As Integer


Set ws = Sheets("Bulk Add")

For Each cell In ws.Range("I2:I2377")
For i = 2 To 49
If cell.Value > ws.Range("N" & i) Then
    For j = 2 To 49
    If cell.Value < ws.Range("O" & i) Then
      ws.Range("J" & j) = ws.Range("P" & i)
    End If
      Next j
    End If
      Next i
Next cell

End Sub

游戏:

class Player:

    def __init__(self,Scr,cx,cy,speed,bcolor):
        self.screen = Scr
        self.surf = pygame.image.load('player.png')
        self.surf = pygame.transform.scale(self.surf, (160, 200))
        self.rect = self.surf.get_rect()
        self.rect.centerx = cx
        self.rect.y = 510
        self.speed = 30
        self.bc = bcolor

    def draw(self):
        self.screen.blit(self.surf,self.rect)
        pygame.display.update([self.rect])

    def undraw(self):
        surf = self.surf.copy()
        surf.fill(self.bc)
        self.screen.blit(surf,self.rect)
        pygame.display.update([self.rect])

    def move(self,mv):
        self.undraw()
        if mv == 'd' and self.rect.bottom < WHT:
            self.rect.top += self.speed
        if mv == 'u' and self.rect.top > 0:
            self.rect.top -= self.speed
        if mv == 'l' and self.rect.left > 0:
            self.rect.left -= self.speed
        if mv == 'r' and self.rect.right < WWD:
            self.rect.right += self.speed
        self.draw()
        pygame.display.update()

    def jump(self,top,left):
        self.undraw()
        self.rect.top = top
        self.rect.left = left
        self.draw()
        pygame.display.update()

主循环

class EggDrop:

def __init__(self,Scr):

    pygame.display.update()
    #pygame.mixer.music.load('background.mid')
    #pygame.mixer.music.play(-1,0.0)
    #self.Mplay = True
    self.Player = Player(Scr,WWD//2,WHT//2,6,BLACK)
    Scr.blit(BG, (0, 0))
    self.Scr = Scr

def togglePlayer(self):
    if self.Pvis:
        self.Player.undraw()
    else:
        self.Player.draw()
    self.Pvis = not self.Pvis

1 个答案:

答案 0 :(得分:0)

使用具有透明度的.png时,请在加载convert_alpha后致电Surface

来自pygame.image.load

  

对于Alpha透明度,与.png图像一样,在加载后使用convert_alpha()方法,以使图像具有每像素透明度。