简体   繁体   English

为什么没有出现“游戏结束”文字?

[英]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. function 在游戏循环结束时被调用,而文本短暂出现(实际上只出现过一次,我已经尝试过多次),它不会留下来。 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.碰撞只发生片刻,游戏结束文本仅在 object 碰撞时显示。 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显示文本:

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.请注意,根据对象的 position 在每一帧中设置collision However, gameover is set once when a collision occurs and then maintains its state.但是,当发生碰撞时设置一次游戏结束,然后保持其gameover

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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