2013-10-28 40 views
1

所以我正在做一個基本的pygame,並且我在遊戲地圖上有幾個「障礙」和「門戶」。前者是玩家無法觸及的景點,後者則是改變等級的景點。不能讓精靈在pygame中不可見

我試圖讓它們看不見,所以只需在地圖上有一個看不見的矩形,碰撞檢測會注意到,但現在,當我將它拍攝到地圖時,我有醜陋的黑色斑點。

我嘗試使用髒矩形,但是這似乎並沒有如此出色。

我的代碼(或至少是處理的障礙和門戶斑點):

class Barrier(pygame.sprite.DirtySprite): 
    '''class that builds up the invisible barriers in the game''' 

    #constructor function 
    def __init__(self, posX, posY, width, height): #create a self variable to refer to the object 
    #call up the parent's constructor 
     pygame.sprite.DirtySprite.__init__(self) 
     self.dirty = 1 
     self.visible = 0 

     #Make the barrier. 
     self.image = pygame.Surface([width, height]) 

     #debug code that makes sure that the barrier is in the right place 
     #self.image.fill(white) 

     # place the top left corner of the barrier at the given location 
     self.rect = self.image.get_rect() 
     self.rect.y = posY 
     self.rect.x = posX 

barriers = pygame.sprite.Group() #global barrier list used in all maps 
portals = pygame.sprite.Group() #global portal list used in all maps 


class Portal(pygame.sprite.DirtySprite): 
    '''class that builds up the portals in the game''' 

    #constructor function 
    def __init__(self, posX, posY, width, height): #create a self variable to refer to the object 
    #call up the parent's constructor 
     pygame.sprite.DirtySprite.__init__(self) 

     self.dirty = 1 
     self.visible = 0 

     #Make the barrier. 
     self.image = pygame.Surface([width, height]) 

     #debug code that makes sure that the barrier is in the right place 
     self.image.fill(black) 

     # place the top left corner of the barrier at the given location 
     self.rect = self.image.get_rect() 
     self.rect.y = posY 
     self.rect.x = posX 

def LoadInside(): 

    barriers.empty() 
    portals.empty() 
    #Load up the level image 
    whichLevel = 1 

    background_image = pygame.image.load("House.png").convert() 

    #a list of all the barriers in the room 
    room_barrier_list = pygame.sprite.Group() 

    #make a barrier out of all of the objects in the room 
    barrierTopWall = Barrier(0,125,661,7) 
    barrierLeftWall= Barrier(5, 130,5, 300) 
    barrierBottomWallLeft= Barrier(10,414,292,7) 
    barrierBottomWallRight= Barrier(364,412,298,7) 
    barrierRightWall= Barrier(649,126,5, 294) 
    bed = Barrier(19,199,62,93) 
    smallTableChairs = Barrier(273,220,97,39) 
    pot = Barrier(300,288,34,31) 
    table = Barrier(493,242,151,42) 
    chair1 = Barrier(459,255,28,35) 
    chair2 = Barrier(490,296,31,28) 
    chair3 = Barrier(553,293,31,28) 
    chair4 = Barrier(621,292,31,28) 

    #make a portal to get out 
    door = Portal(300,413, 64,10) 


    #add the barriers to the lists 
    room_barrier_list.add(barrierTopWall, barrierLeftWall, barrierBottomWallLeft, barrierRightWall) 

    barriers.add(room_barrier_list) 

    all_sprites_list.add(barriers) 

    room_barrier_list.add(smallTableChairs,pot,table,chair1,chair2,chair3,chair4, barrierBottomWallRight, bed) 
    barriers.add(room_barrier_list) 
    all_sprites_list.add(barriers) 

    #add the portal to the list 
    all_sprites_list.add(door) 

    mainScreen.blit(background_image, [0,0]) 


# 
# The above code handles the room barriers and portals 
# 


while done==False: 

    for event in pygame.event.get(): #user did something 
     if event.type == pygame.QUIT: #if the user hit the close button 
       done=True 

       # Move the player if an arrow key is pressed 
     key = pygame.key.get_pressed() 
     if key[pygame.K_LEFT]: 
      faceWhatDirection = 'left' # set the faceWhatDirection variable 
      player.updateLeft() # call up the animation function 
      player.move(-10, 0) 

     if key[pygame.K_RIGHT]: 
      faceWhatDirection = 'right' # set the faceWhatDirection variable 
      player.updateRight() # call up the animation function 
      player.move(10, 0) 

     if key[pygame.K_UP]: 
      faceWhatDirection = 'up' # set the faceWhatDirection variable 
      player.updateUp() # call up the animation function 
      player.move(0, -10) 

     if key[pygame.K_DOWN]: 
      faceWhatDirection = 'down' # set the faceWhatDirection variable 
      player.updateDown() # call up the animation function 
      player.move(0, 10) 


     #if the user presses the space bar, the attack button 
     if key[pygame.K_SPACE]: 

       bullet = Bullet() 
       bullet_list.append(bullet) #adds bullet to the bullet list 
       all_sprites_list.add(bullet) #adds the bullet to the sprite list to be drawn 

       #puts the bullet in the same location as player (this needs to be changed to what direction player faces) 
       bullet.rect.x = player.rect.x 
       bullet.rect.y = player.rect.y + 15 # the + 15 places it at a location that is not on the player's face! 
       bullet.bulletDirection = faceWhatDirection 

       #checks to see what direction to move the bullet in 
    for bullet in bullet_list: 
     bullet.move_bullet() 

     #see if the bullet hit anything 
     barrier_hit_list = pygame.sprite.spritecollide(bullet, barriers, False) 

     for barrier in barrier_hit_list: 
     #remove the bullet if it hit something 
      bullet_list.remove(bullet) 
      all_sprites_list.remove(bullet) 

    mainScreen.fill(black)#makes the background white, and thus, the white part of the images will be invisible 


    if whichLevel == 1: 

     LoadInside() 


    else: 
     LoadOutside() 


    #draw the sprites 
    all_sprites_list.draw(mainScreen) 



    #limit the game to 20 fps 
    clock.tick(20) 

    #update the screen on the regular 
    pygame.display.flip() 

pygame.quit() 
+2

你不能讓矩形變成透明嗎? – 2013-10-28 13:49:48

回答

4

它安靜容易,

精靈用的Group.update()方法的位圖混合。

all_sprites_list.draw(mainScreen) 

不要將障礙添加到all_sprites_list。然後,他們永遠不會在屏幕上blitting。

barrier_hit_list仍然可以用於碰撞。

+0

總是很簡單。這是一個全能者會對你的頭腦做的事情。謝謝! =) – user1768884