Pygame 帮助:游戏文本不断卡在屏幕上(计时器不工作) [英] Pygame help: Game text constantly stuck on screen (timer not working)

查看:53
本文介绍了Pygame 帮助:游戏文本不断卡在屏幕上(计时器不工作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Pygame 中制作一个简单的潜艇游戏,但每当玩家到达游戏窗口的边界时,我都会遇到显示一段名为crashed"的文本的问题.

一旦潜艇与游戏边界相撞(你坠毁了!"),文本应该立即出现在屏幕上——然后是 2 秒的计时器,然后重新生成潜艇并从显示器上清除文本.

我遇到的问题是,直到 2 秒计时器结束(当潜艇重生)时,崩溃"文本才会出现在屏幕上 - 然后文本在重生后不断卡在屏幕上(即每次潜艇生成时文本都不会重置)

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

这是我的完整代码:

**import pygame导入时间pygame.init()显示宽度 = 1280显示高度 = 720sound = pygame.mixer.Sound('YellowSubmarine.wav')声音播放()黑色 = (0,0,0)白色 = (255,255,255)红色 = (255,0,0)gameDisplay = pygame.display.set_mode((display_width,display_height))bg = pygame.image.load("bg.jpg") <<<<<<<<背景图片pygame.display.set_caption('神奇的黄色潜水艇!')时钟 = pygame.time.Clock()SubImage = pygame.image.load('sub.png')<<<<<<<<<子图像SubImage = pygame.transform.scale(SubImage,(160,100)) <<<缩放子图像定义潜艇(x,y):<<<<<<<<<<<<<<<<潜艇目标gameDisplay.blit(bg,(0,0)) <<<<<<BLITTING背景图像gameDisplay.blit(SubImage,(x,y)) <<<<BLITTING子图像def text_objects(text, font): <<<<<创建文本对象textSurface = font.render(text, True, white)返回 textSurface, textSurface.get_rect()def message_display(text): <<<<在屏幕上显示文本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) <<<确保文本与背景图像重叠time.sleep(2) <<<<<<<两秒钟game_loop() <<<<运行主游戏循环定义崩溃():message_display('你崩溃了!') <<<<<显示崩溃信息def game_loop(): <<<<除非游戏退出,否则运行x = (display_width * 0.02)y = (display_height * 0.4)y_change = 0x_change = 0游戏退出 = 假而不是游戏退出:对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:pygame.quit()放弃()如果 event.type == pygame.KEYDOWN:如果 event.key == pygame.K_LEFT:x_change = -5y_change = 0如果 event.key == pygame.K_RIGHT:x_change = 5 <<<<<<控制y_change = 0如果 event.key == pygame.K_UP:y_change = -2如果 event.key == pygame.K_DOWN:y_change = 2如果 event.type == pygame.KEYUP:如果 event.key == pygame.K_LEFT:x_change = -1如果 event.key == pygame.K_RIGHT:x_change = 1如果 event.key == pygame.K_DOWN:y_change = 2x += x_changey += y_change潜艇(x,y)如果 x >1125 或 x <-25 或 y >650 或 y <-25:<<<<<<如果边界被击中崩溃()<<<<<运行崩溃功能(显示消息)pygame.mixer.Sound.stop()时钟滴答(60)pygame.display.update() <<<<<<<<更新显示游戏循环()pygame.quit()

退出()

解决方案

文本会在两秒后出现,因为在将文本传送到屏幕后,您在更新屏幕前暂停游戏两秒钟:

bg.blit(TextSurf, TextRect) <<<确保文本与背景图像重叠time.sleep(2) <<<<<<<两秒钟game_loop() <<<<运行主游戏循环

因此,与其使用 time.sleep(2) 阻塞整个游戏,不如让主循环继续运行.

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

<小时>

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

bg.blit(TextSurf, TextRect) <<<确保文本与背景图像重叠

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

<小时>

您的代码还有一些其他问题,但这超出了本答案的范围,但您应该了解如何解决主要问题.

I'm trying to make a simple submarine game in Pygame and I'm having problems displaying a piece of text called 'crashed' whenever the player hits the boundaries of the game window.

The text is supposed to appear on the screen as soon as the submarine collides with the game boundary ("You crashed!") - followed by a 2 second timer before respawning the submarine and clearing the text from the display.

The problem I'm having is that the 'crashed' text doesn't appear on the screen until the 2 second timer has ended (as the submarine respawns) - then the text is constantly stuck on the screen after respawning (i.e the text isn't resetting every time the submarine spawns)

I've got a function called 'crash' which contains the message and a separate function to handle the message display called 'message_display'. I'm following a tutorial on YouTube but unlike the project in the video I have a background image which the text overlaps, but I can't get it to display when the submarine first crashes or make it disappear when the sub respawns.

Here's my code in full:

**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()

quit()

解决方案

The text appears after two seconds because after blitting the text to the screen, you pause your game for two seconds before updating the screen:

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

So instead of blocking your entire game with time.sleep(2), you have to keep your main loop running.

You could use an event to trigger a state change of your game. To get an idea of how custom events work, take a look here and here.


Then the text never disappears because you blit the text to the Surface you use as your background surface, and you continue to blit this now altered Surface to the screen:

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

So instead of drawing the text to the background Surface, blit it to the screen Surface directly.


There are some other issues with your code but that's out of scope of this answer, but you should get the idea of how to solve your main issues.

这篇关于Pygame 帮助:游戏文本不断卡在屏幕上(计时器不工作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆