2012-10-22 83 views
1

当我在PyGame屏幕上移动图像时,如果它恰好经过另一个图像,那么下面的图像会隐藏并且无法再被看到。为什么会这样呢?在PyGame中移动图像隐藏所有其他图像

我的代码是:

def main(): 
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) 

    background = pygame.image.load('Background.png').convert() 
    screen.blit(background, (0, 0)) # draw the background screen 

    human1 = pygame.image.load('Human1.gif').convert() 
    human1_position = pygame.Rect(10, BOTTOM_FLOOR, CHARACTER_WIDTH, CHARACTER_HEIGHT) 
    screen.blit(human1, human1_position) # draw the first human 

    human2 = pygame.image.load('Human2.gif').convert() 
    human2_position = pygame.Rect(100, BOTTOM_FLOOR, CHARACTER_WIDTH, CHARACTER_HEIGHT) 
    screen.blit(human2, human2_position) # draw the second human 

    human3 = pygame.image.load('Human3.gif').convert() 
    human3_position = pygame.Rect(190, BOTTOM_FLOOR, CHARACTER_WIDTH, CHARACTER_HEIGHT) 
    screen.blit(human3, human3_position) # draw the third human 

    pygame.display.update() # display all graphical elements 

    move = STOP # Variable to track what the current move is. Initially, stopped 
    move_character = 1 # Variable to track which character is being moved 

    while True: 
     # 
     # See what the user did... 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       sys.exit() 
      elif event.type == pygame.KEYDOWN: 

       # Check if there is a move currently happening. The program 
       # will only do something if the previous move is stopped 
       if move == STOP: 
        # Check which key was pressed... 
        if event.key == pygame.K_1: 
         # if key pressed is the '1' key, move first human (and check if moving from bottom to top or other way) 
         move_character = 1 # the first character is to be moved 
         if human1_position.top < 300: 
          # If the 'top' of the human's position is less 
          # than 300 then it means the human is currenlty 
          # on the top half of the screen, and so 
          # the next move will be to move it downards 
          move = DOWN 
         else: 
          move = UP 

        # Check which key was pressed... 
        if event.key == pygame.K_2: 
         # if key pressed is the '2' key, move second human (and check if moving from bottom to top or other way) 
         move_character = 2 # the first character is to be moved 
         if human2_position.top < 300: 
          # If the 'top' of the human's position is less 
          # than 300 then it means the human is currenlty 
          # on the top half of the screen, and so 
          # the next move will be to move it downards 
          move = DOWN 
         else: 
          move = UP 

      screen.blit(background, human1_position, human1_position) # erase old position of first human 
      if (move == UP): 
       #they want to move up, so first check the lift is down with them. 
       if (liftLoc == "down"): 
        #they are both down, they can be moved into the lift 
        if (human1_position.left <= 245): 
         human1_position = human1_position.move(2, 0) # move first human 
         pygame.display.update() 
        else: 
         move = STOP 
       else: 
        print "Wrong floor!" 
+1

您可能需要以某种方式刷新屏幕:/ – lolopop

+2

您应该发布代码的其余部分,至少主循环和对此代码的调用。 – pmoleri

+1

特别是在屏幕/游戏区域对象上调用.blit()和.flip()方法的部分 –

回答

3

所以,你所遇到的问题是,你只渲染/块传输时,你开始搬过来的图像,使他们获得“未着色”或呈现在。 为了摆脱这个问题,像所有其他实时渲染引擎一样,您应该呈现您希望某人看到的所有内容,您希望发生的每一帧内容。

我不会给你的代码更正,但只是将每个图像的screen.blit移到你的while循环中。他们的位置显然会影响渲染顺序。