Pygame帮助:游戏文本不断粘在屏幕上(计时器不工作)

时间:2015-05-04 18:38:25

标签: python pygame

我正在尝试在Pygame中制作一个简单的潜艇游戏,当玩家遇到游戏窗口的边界时,我在显示一段名为“崩溃”的文本时遇到问题。

一旦潜水艇与游戏边界发生碰撞(“你崩溃了!”),文本应该出现在屏幕上 - 然后是2秒计时器,然后重新生成潜艇并清除显示屏上的文字。

我遇到的问题是,在2秒计时器结束之前(潜艇重生),屏幕上不会显示“坠毁”文字 - 然后在重新生成后文本不断卡在屏幕上(即每次潜艇产生时都不会重置文本)

我有一个名为'crash'的函数,它包含消息和一个单独的函数来处理名为'message_display'的消息显示。我正在关注YouTube上的教程,但与视频中的项目不同,我有一个文本重叠的背景图像,但是当潜艇首次崩溃时,我无法显示它,或者当子潜艇重生时它会消失。

这是我的完整代码:

**import pygame
import time
pygame.init()
display_width = 1280
display_height = 720
sound = pygame.mixer.Sound('YellowSubmarine.wav')
sound.play()
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
gameDisplay = pygame.display.set_mode((display_width,display_height))

bg = pygame.image.load("bg.jpg") <<<<<<<< THE BACKGROUND IMAGE

pygame.display.set_caption('The Amazing Yellow Submarine!')
clock = pygame.time.Clock()

SubImage = pygame.image.load('sub.png')<<<< <<<<< SUB IMAGE

SubImage = pygame.transform.scale(SubImage,(160,100)) <<< SCALE SUB IMAGE

def submarine(x,y): <<<<<<<<<< THE SUBMARINE OBJECT

    gameDisplay.blit(bg,(0,0)) <<<<<< BLITTING THE BACKGROUND IMAGE

    gameDisplay.blit(SubImage,(x,y)) <<<< BLITTING THE SUB IMAGE

def text_objects(text, font): <<<<< CREATING THE TEXT OBJECT 

    textSurface = font.render(text, True, white)

    return textSurface, textSurface.get_rect()

def message_display(text): <<<< DISPLAYING THE TEXT TO THE SCREEN

    largeText = pygame.font.Font('freesansbold.ttf',80)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))

    bg.blit(TextSurf, TextRect) <<< MAKING SURE THE TEXT OVERLAPS THE BACKROUND IMAGE

    time.sleep(2) <<<<< TWO SECOND TIMER 

    game_loop() <<<< RUN THE MAIN GAME LOOP


def crash():

    message_display('You Crashed!') <<<<< DISPLAYS THE CRASH MESSAGE

def game_loop(): <<<< RUNS UNLESS THE GAME IS QUIT

    x = (display_width * 0.02)
    y = (display_height * 0.4)
    y_change = 0
    x_change = 0
    gameExit = False
    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                    y_change = 0
                if event.key == pygame.K_RIGHT:
                    x_change = 5               <<<<<< CONTROLS
                    y_change = 0
                if event.key == pygame.K_UP:
                    y_change = -2
                if event.key == pygame.K_DOWN:
                    y_change = 2                    
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT: 
                    x_change = -1
                if event.key == pygame.K_RIGHT:
                    x_change = 1
                if event.key == pygame.K_DOWN:
                    y_change = 2
        x += x_change
        y += y_change

        submarine(x,y) 

        if x > 1125 or x < -25 or y > 650 or y < -25: <<<<<< IF BOUNDARY IS HIT

            crash() <<<<< RUNS CRASH FUNCTION (WHICH DISPLAYS THE MESSAGE)

            pygame.mixer.Sound.stop()

        clock.tick(60)
        pygame.display.update() <<<<<<<< UPDATING THE DISPLAY
game_loop()
pygame.quit()

退出()

1 个答案:

答案 0 :(得分:0)

两秒后文字出现,因为在将文字blit到屏幕后,你会在更新屏幕前暂停游戏两秒钟:

bg.blit(TextSurf, TextRect) <<< MAKING SURE THE TEXT OVERLAPS THE BACKROUND IMAGE

time.sleep(2) <<<<< TWO SECOND TIMER 

game_loop() <<<< RUN THE MAIN GAME LOOP

因此,您不必使用time.sleep(2)阻止整个游戏,而是必须保持主循环运行。

您可以使用事件来触发游戏的状态更改。要了解自定义事件的工作原理,请查看herehere

然后文本永远不会消失,因为你将文本blit到你用作背景表面的Surface,然后你继续blit,现在改变了Surface到屏幕:

bg.blit(TextSurf, TextRect) <<< MAKING SURE THE TEXT OVERLAPS THE BACKROUND IMAGE

因此,不要将文字绘制到背景Surface,而是直接将其blit到屏幕Surface

您的代码还存在其他一些问题,但这不是本答案的范围,但您应该了解如何解决主要问题。