Pygame-两个显示同时更新,中间有一个 pygame.time.wait() 函数 [英] Pygame-Two display updates at the same time with a pygame.time.wait() function between

查看:34
本文介绍了Pygame-两个显示同时更新,中间有一个 pygame.time.wait() 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两次显示更新之间添加了一个 pygame.time.wait(2000) 函数,期望在按下键后,它会首先显示一个文本,然后在 2 秒后显示第二个文本.但它最终会在触发后两秒同时显示两个文本.我应该如何正确使用该功能来达到我的目标?

I add a pygame.time.wait(2000) function between two display updates expecting that after a keydown, it will first show up one text, then after 2 seconds, the second one. But it end up showing up the two texts simultaneously two seconds after the trigger. How should I correctly use the function to reach my goal?

import pygame
from pygame.locals import *
from sys import exit

SCREEN_WIDTH = 448
SCREEN_HEIGHT = 384

pygame.init()
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
my_font = pygame.font.SysFont("arial", 16)
textSurfaceObj1 = my_font.render('Hello world!', True, (255,255,255))
textRectObj1 = textSurfaceObj1.get_rect()
textRectObj1.center = (100, 75)
textSurfaceObj2 = my_font.render('Hello world!', True, (255,255,255))
textRectObj2 = textSurfaceObj2.get_rect()
textRectObj2.center = (200, 150)


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        if event.type == KEYDOWN:
            screen.blit(textSurfaceObj1, textRectObj1)
            pygame.display.flip()
            pygame.time.wait(2000)
            screen.blit(textSurfaceObj2, textRectObj2)
            pygame.display.flip()

推荐答案

如果您的代码有效取决于您使用的窗口管理器,但正如您所注意到的,这并不好.

If your code works depends on the window manager you're using, but as you noticed, that's not good.

您需要考虑到您的游戏在循环中运行的事实,并且您为停止循环所做的一切(例如 waitsleep)都不会工作.

You need to wrap your head around the fact that your game runs in a loop, and everything you do to stop the loop (like wait or sleep) will not work.

在您的代码中,您具有三种状态:

In your code, you have three states:

1) 什么也不打印
2)打印第一个文本
3) 打印两个文本

1) print nothing
2) print the first text
3) print both texts

解决问题的简单方法是简单地跟踪变量中的当前状态,如下所示:

so and easy way to solve your problem is to simply keep track of the current state in a variable, like this:

import pygame
from sys import exit

SCREEN_WIDTH = 448
SCREEN_HEIGHT = 384

pygame.init()
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
my_font = pygame.font.SysFont("arial", 16)

text = my_font.render('Hello world!', True, (255,255,255))
text_pos1 = text.get_rect(center=(100, 75))
text_pos2 = text.get_rect(center=(200, 150))

state = 0
ticks = None
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        if event.type == pygame.KEYDOWN and state == 0:
            state = 1
            ticks = pygame.time.get_ticks()

    if state == 1 and ticks and pygame.time.get_ticks() > ticks + 2000:
        state = 2

    screen.fill((30, 30, 30))
    if state > 0: screen.blit(text, text_pos1)
    if state > 1: screen.blit(text, text_pos2)
    pygame.display.flip()

这篇关于Pygame-两个显示同时更新,中间有一个 pygame.time.wait() 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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