Question

I am trying to make some text display when the player looses but nothing is showing up and the game immediatly restarts. It can get to the else statement because it is able to print things but it doesn't seem to 'blit' the surfaces or it does it to fast to see.

The code in question is stared at the bottom but I figured I would include the main function as well. I don't know if I am correct but is it drawing the background over the textbox as soon as they are drawn? If so how can I remedy this?

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)
Was it helpful?

Solution

You are drawing the game-over text, but then not updating the display until right before the loop starts over again (so the game-over text is probably only being rendered for 1 frame). Try something like this:

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

This will draw the game-over text, update the screen, and then loop until you have exceeded the amount of time defined by GAMEOVERTIME (which I don't think you're actually doing anywhere, at least not in the code you posted). Note that this loop I have given you won't allow you to process any input events or anything until after the allotted time has expired, but it will guarantee that the game-over text will be rendered to the screen for the amount of time you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top