简体   繁体   中英

Why does “Game over” text not appear?

I am currently attempting to programm a small game. An enemy is supposed to chase the player, the actual chasing is not implemented yet. I still need to figure out how to do that, but that's not the question. I have made it so that the player returns to the start point once they collide with the enemy. In addition to that, a text 'Game over' is supposed to appear. The function for that is called at the end of the game loop and while the text appeared briefly(it actually only appeared once, I have tried it multiple times), it does not stay. I was planing on making it appear and then disappear after a few seconds so that the player can play again, but I'm not sure why it disappears instantly. If this is the wrong place to post this, please tell me, I will delete this post. This is my code, would be amazing if somebody could help me out:

import pygame #imports pygame
import math
pygame.init()
#screen
screen = pygame.display.set_mode((800,600)) #width and height

#title and icon
pygame.display.set_caption("The Great Chase") #changes title
icon = pygame.image.load('game-controller.png')
pygame.display.set_icon(icon) #setting icon

#player
playerImg = pygame.image.load('scary-monster.png')
playerX = 370
playerY = 480
playerX_change = 0
playerY_change = 0

#enemy
enemyImg = pygame.image.load('caterpillar.png')
enemyX = 370
enemyY = 50
enemyX_change = 0
enemyY_change = 0

#GAME OVER
game_over_font = pygame.font.Font('Bubblegum.ttf',64)

def game_over_text():
    game_over_text = game_over_font.render("GAME OVER", True, (255, 0, 0))
    screen.blit(game_over_text, (200, 250))

def player(x, y): #function for player
    screen.blit(playerImg, (x, y)) #draws image of player, blit -> means to draw

def enemy(x, y): #function for enemy
    screen.blit(enemyImg,(x,y))

def isCollision(playerX, playerY, enemyX, enemyY):
    distance = math.sqrt((math.pow(playerX-enemyX,2)) + (math.pow(playerY - enemyY,2)))
    if distance < 25:
        return True
    else:
        return False

def chase():
    distance = math.sqrt((math.pow(playerX-enemyX,2)) + (math.pow(playerY - enemyY,2)))

#Game loop
running = True
while running:
    screen.fill((255,255,0)) #0-255 RGB-> red, green & blue

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
           running = False
        # if keystroke check whether right or left or up or down
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                playerX_change = -0.2
            if event.key == pygame.K_RIGHT:
                playerX_change = 0.2
            if event.key == pygame.K_UP:
                playerY_change = -0.2
            if event.key == pygame.K_DOWN:
                playerY_change = 0.2
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                playerX_change = 0
            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                playerY_change = 0

    playerX += playerX_change
    playerY += playerY_change

    #creating borders so that player won't leave screen
    if playerX <=0:
        playerX = 0
    elif playerX >=768:
        playerX = 768
    if playerY <= 0:
        playerY = 0
    elif playerY >=568:
        playerY = 568

    #collision
    collision = isCollision(playerX, playerY, enemyX, enemyY)
    if collision:
        playerY = 480
        game_over_text()

    player(playerX, playerY)
    enemy(enemyX, enemyY)
    pygame.display.update()

The collision only occurs for a moment and the game over text is only shown when the object collides. If you want to persist the text, set a gameover variable when the collision is detected and display the text based on the state of the variable:

gameover = False
running = True
while running:
    # [...]

    collision = isCollision(playerX, playerY, enemyX, enemyY)
    if collision:
        playerY = 480
        gameover = True

    player(playerX, playerY)
    enemy(enemyX, enemyY)

    if gameover:
        game_over_text()

    pygame.display.update()

Note, collision is set in every frame depending on the position of the objects. However, gameover is set once when a collision occurs and then maintains its state.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM