2013-04-17 5 views
1

플레이어가 느슨해 보이지만 아무 것도 나타나지 않고 게임이 즉시 다시 시작될 때 텍스트를 표시하려고합니다. 그것은 물건을 출력 할 수 있기 때문에 else 서술문을 얻을 수 있지만 서페이스를 'blit'하지 않거나 볼 때 빨리 처리하지는 않습니다.파이 게임 텍스트 표면이 그려지지 않습니다.

코드는 맨 아래에서 쳐다 보지만 주요 기능도 포함될 것이라고 생각했습니다. 내가 맞는지는 모르겠지만 그려지는 즉시 텍스트 상자 위에 배경을 그리는가? 그렇다면 어떻게 해결할 수 있습니까?

def main(): 
    global FPSCLOCK, DISPLAYSURF, BASICFONT, \ 
      TILESIZE, floorx, floory, \ 
      floorCovered, tilesNeeded, OUTSIDEDECOMAPPING, \ 
      L_Monster, R_Monster, BGIMAGE, \ 
      bounceHeight, playerObj, R_Bird, \ 
      L_Bird, direction, birdx, \ 
      birdDir 

    pygame.init() 
    DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT)) 
    FPSCLOCK = pygame.time.Clock() 
    BASICFONT = pygame.font.Font('freesansbold.ttf', 32) 

    pygame.display.set_caption('Alpha One') 
    # Set up the background image. 
    boardImage = pygame.image.load('bg.png') 
    # Use smoothscale() to stretch the board image to fit the entire board: 
    boardImageRect = boardImage.get_rect() 
    boardImageRect.topleft = (0, 0) 
    BGIMAGE = pygame.image.load('bg.png') 
    # Use smoothscale() to stretch the background image to fit the entire window: 
    BGIMAGE = pygame.transform.smoothscale(BGIMAGE, (WINDOWWIDTH, WINDOWHEIGHT)) 
#  BGIMAGE.blit(boardImage, boardImageRect) 
#  #Draw the background  
#  DISPLAYSURF.blit(BGIMAGE, BGIMAGE.get_rect()) 
#  #Draw the Floor 
#  drawFloor() 

    L_Monster = pygame.image.load('ghost.png') 
    R_Monster = pygame.transform.flip(L_Monster, True, False) 
    R_Bird = pygame.image.load('bird.png') 
    L_Bird = pygame.transform.flip(R_Bird, True, False) 


#  pygame.display.flip() 







    #Main Game Loop 
    while True: 

     runGame() 








def runGame(): 
    invulnerableMode = False 
    invulnerableStartTime = 0 
    gameOverMode = False 
    gameOverStartTime = 0 
    winMode = False 
    bounceHeight = 30 
    BOUNCEHEIGHT = 30 
    camerax = 0 
    cameray = 0 
    birdx = WINDOWWIDTH - 50 
    birdDir = 'right' 

    gameOverSurf = BASICFONT.render('Game Over', True, WHITE) 
    gameOverRect = gameOverSurf.get_rect() 
    gameOverRect.center = (WINDOWWIDTH, WINDOWHEIGHT) 






    #declares the player object 
    playerObj = {'surface': pygame.transform.scale(L_Monster,(STARTSIZE, STARTSIZE)), 
       'facing': LEFT, 
       'size': STARTSIZE, 
       'x': WINDOWWIDTH, 
       'y': WINDOWHEIGHT, 
       'bounce':0, 
       'health': MAXHEALTH} 
    #declares the bird object 
    birdsObj = {'surface': pygame.transform.scale(R_Bird,(STARTSIZE, STARTSIZE)), 
       'facing': RIGHT, 
       'size': STARTSIZE, 
       'x': (0 - STARTSIZE*2), 
       'y': STARTSIZE * 2} 




    #declare the current position of keys 
    moveLeft = False 
    moveRight = False 
    moveUp = False 
    moveDown = False 
    spaceDown = False 
    shiftDown = False 







    while True: 
     #redraw the background 
     DISPLAYSURF.blit(BGIMAGE, BGIMAGE.get_rect()) 
     #redraw the floor 
     drawFloor()   


     if invulnerableMode and time.time() - invulnerableStartTime > INVULNTIME: 
      invulnerableMode = False 


     playerCenterx = playerObj['x'] + int(playerObj['size']/2) 
     playerCentery = playerObj['y'] + int(playerObj['size']/2) 
     if (camerax + HALF_WINDOWWIDTH) - playerCenterx > CAMERASLACK: 
      camerax = playerCenterx + CAMERASLACK - HALF_WINDOWWIDTH 
     elif playerCenterx - (camerax +HALF_WINDOWWIDTH) > CAMERASLACK: 
      camerax = playerCenterx - CAMERASLACK - HALF_WINDOWWIDTH 
     if (cameray + HALF_WINDOWHEIGHT) - playerCentery > CAMERASLACK: 
      cameray = playerCentery + CAMERASLACK - HALF_WINDOWHEIGHT 
     elif playerCentery - (cameray +HALF_WINDOWHEIGHT) > CAMERASLACK: 
      cameray = playerCentery - CAMERASLACK - HALF_WINDOWHEIGHT 

     #Initial player positions and facing and making rect 
     flashIsOn = round(time.time(), 1) * 10 % 2 == 1 
     if not gameOverMode and not (invulnerableMode and flashIsOn): 
      playerObj['rect'] = pygame.Rect((playerObj['x'] - (WINDOWWIDTH), 
              playerObj['y'] - (STARTSIZE + FLOORSIZE - 5) - getBounceAmount(playerObj['bounce'] , BOUNCERATE, BOUNCEHEIGHT), 
              playerObj['size'], 
              playerObj['size'])) 
      DISPLAYSURF.blit(playerObj['surface'], playerObj['rect']) 


     #detect when the keys are press and set their vars to True 
     for event in pygame.event.get(): 
      if event.type == QUIT: 
       terminate() 

      elif event.type == KEYDOWN: 
       if event.key in (K_UP, K_w): 
        moveDown = False 
        moveUp = True 
       elif event.key in (K_DOWN, K_s): 
        moveUp = False 
        moveDown = True 
       elif event.key in (K_LEFT, K_a): 
        moveRight = False 
        moveLeft = True 
        if playerObj['facing'] == RIGHT: 
         playerObj['surface'] = pygame.transform.scale(L_Monster, (playerObj['size'], playerObj['size'])) 
        playerObj['facing'] = LEFT 
       elif event.key in (K_RIGHT, K_d): 
        moveLeft = False 
        moveRight = True 
        if playerObj['facing'] == LEFT: 
         playerObj['surface'] = pygame.transform.scale(R_Monster, (playerObj['size'], playerObj['size'])) 
        playerObj['facing'] = RIGHT 
       elif event.key in (K_SPACE, K_BACKSPACE): 
        spaceDown = True 
       elif event.key in (K_LSHIFT, K_RSHIFT): 
        shiftDown = True 
       elif winMode and event.key == K_r: 
        return 

      #Detect when the key comes up and set the var to false 
      elif event.type == KEYUP: 
       if event.key in (K_LEFT, K_a): 
        moveLeft = False 
       elif event.key in (K_RIGHT, K_d): 
        moveRight = False 
       elif event.key in (K_UP, K_w): 
        moveUp = False 
       elif event.key in (K_DOWN, K_s): 
        moveDown = False 
       elif event.key in (K_SPACE, K_BACKSPACE): 
        spaceDown = False 
       elif event.key in (K_LSHIFT, K_RSHIFT): 
        shiftDown = False  
       elif event.key == K_ESCAPE: 
        terminate() 

    #declares the bird rect from the bird object 
     birdsObj['rect'] = pygame.Rect((birdsObj['x'], 
             birdsObj['y'], 
             birdsObj['size'], 
             birdsObj['size']))     



     #Decide what way the bird has to go 
     if birdsObj['x'] >= (WINDOWWIDTH +(STARTSIZE*2)): 
      birdDir = 'left' 
     elif birdsObj['x'] <= (0 - (STARTSIZE*2)): 
      birdDir = 'right' 
     #go that way  
     if birdDir == 'left': 
      birdsObj['x'] -= 5 
      if birdsObj['facing'] == RIGHT: 
       birdsObj['surface'] = pygame.transform.scale(L_Bird, (playerObj['size'], playerObj['size'])) 
       birdsObj['facing'] = LEFT 
     elif birdDir == 'right': 
      birdsObj['x'] += 5 
      if birdsObj['facing'] == LEFT: 
       birdsObj['surface'] = pygame.transform.scale(R_Bird, (playerObj['size'], playerObj['size'])) 
       birdsObj['facing'] = RIGHT 
     #draw the bird  
     DISPLAYSURF.blit(birdsObj['surface'], birdsObj['rect']) 


     if playerObj['rect'].colliderect(birdsObj['rect']): 
      gameOverMode = True 



     #actually move the player    
     if not gameOverMode: 
      if moveLeft and playerObj['x'] > (WINDOWWIDTH): 
       playerObj['x'] -= MOVERATE 
      if moveRight and playerObj['x'] <= ((WINDOWWIDTH*2) - STARTSIZE): 
       playerObj['x'] += MOVERATE 
#   if moveUp and playerObj['y'] > (0 + STARTSIZE * 2): 
#    playerObj['y'] -= MOVERATE 
      if moveDown and playerObj['y'] < WINDOWHEIGHT: 
       playerObj['y'] += MOVERATE 
      if (moveLeft or moveRight or moveUp or moveDown) or playerObj['bounce'] != 0: 
       playerObj['bounce'] += 1 
      if moveLeft and spaceDown: 
       BOUNCEHEIGHT = 400 
      elif moveRight and spaceDown: 
       BOUNCEHEIGHT = 400 
      else: 
       BOUNCEHEIGHT = 30 
      if moveLeft and shiftDown: 
       MOVERATE = 18 
      elif moveRight and shiftDown: 
       MOVERATE = 18 
      else: 
       MOVERATE = 9 

      if playerObj['bounce'] > BOUNCERATE: 
       playerObj['bounce'] = 0 



     *else: 
      # game is over, show "game over" text 
      DISPLAYSURF.blit(gameOverSurf, gameOverRect) 
      print (gameOverMode)   
      if time.time() - gameOverStartTime > GAMEOVERTIME: 
       return* 




     if winMode: 
      DISPLAYSURF.blit(winSurf, winRect) 
      DISPLAYSURF.blit(winSurf2, winRect2) 


     pygame.display.update() 
     FPSCLOCK.tick(FPS) 
+0

누군가가 대답하기를 원하면 들여 쓰기가 필요합니다. – PygameNerd

+0

코드가 들여 쓰기됩니다. 이것은 정확하게 실행되는 방법입니다. – ddaniels

+0

내가 한 것은 게임 실행에 영향을 미치지 않는 물건을 남기는 것입니다. 기능을 좋아해. – ddaniels

답변

2

당신은 게임 오버 텍스트 그리기,하지만 루프 (그래서 게임 오버 텍스트가 아마 단지 1 프레임 렌더링되는) 또 다시 시작하기 전에 다음 마우스 오른쪽 때까지 디스플레이를 업데이트하지 않습니다. 당신이 GAMEOVERTIME (에 의해 정의 된 시간을 초과 할 때까지 당신이 실제로 생각하지 않는 루프 후,

else: 
    # game is over, show "game over" text 
    gameOverStartTime = time.time() 
    DISPLAYSURF.blit(gameOverSurf, gameOverRect) 
    pygame.display.update() 
    print (gameOverMode)   
    while time.time() - gameOverStartTime < GAMEOVERTIME: 
     pass 

이 게임 오버 텍스트를 그릴 것입니다 화면을 업데이트하고,이 같은 것을보십시오 어디서나, 적어도 게시 한 코드는 제외). 내가 지정한이 루프는 할당 된 시간이 만료 될 때까지 입력 이벤트 나 그 밖의 이벤트를 처리 할 수는 없지만 게임 오버 텍스트가 화면에 표시 될 것임을 보장합니다. 필요.