如何确保在小矩形碰到任何边界(即屏幕移动)后立即显示文本

how to ensure the text shows immediately after the small rectangle hits any of the bounderies i.e moves of the screen

我正在尝试在移动的小矩形到达边界后显示 'You loose like the looser you are looser' 文本... 但是相反,矩形正在移动越过边界,并且 'You loose' 文本仅在我按下随机按钮后才会出现... 代码如下

import pygame
import time

#initialize
pygame.init()

#colors
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
cyan = (0, 255, 255)
black = (0, 0, 0)
grey = (92, 92, 92)
whiteish = (192, 192, 192)
 
#dimensions
width = 800
height = 600

#display surface
window = pygame.display.set_mode((width, height))
window.fill(whiteish)
pygame.display.set_caption('Slither')

#some constant variables
my_font = pygame.font.SysFont(None, 25)
clock = pygame.time.Clock()

def message(msg, color):
    txt = my_font.render(msg, True, color )
    window.blit(txt, [0, 0])

def loop():
    #some variables
    x = int(width/2)
    y = int(height/2)
    delta_x = 0
    delta_y = 0

    #misc vars

    block_size = 10
    apple_x = random.randint(0, height-block_size)
    apple_y = random.randint(0, width - block_size)
    
    fps = 13
    run = True

    while run:
        for event in pygame.event.get():
            if event.type ==pygame.QUIT:
                run = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    delta_x = -block_size
                    delta_y = 0
                if event.key == pygame.K_RIGHT:
                    delta_x = block_size
                    delta_y = 0
                if event.key == pygame.K_UP:
                    delta_x = 0
                    delta_y = -block_size
                if event.key == pygame.K_DOWN:
                    delta_x = 0
                    delta_y = block_size
                if x < 0 or x > width - block_size or y < 0 or y > height - block_size:
                    run = False
        x = x + delta_x
        y = y + delta_y

        window.fill(whiteish)
        pygame.draw.rect(window, (0, 192, 0), (x, y, block_size, block_size), 2)
        clock.tick(fps)
        pygame.display.update()

    message('You loose like the loose you are , LOOSER!', red)
    pygame.display.update()
    time.sleep(2)
    pygame.quit()

loop()

如您所见,上面的代码应该有一个小方块,它会在单击箭头按钮时在屏幕上移动,并继续沿它移动的方向移动,直到按下另一个箭头。 现在,当方块到达边界时,应该会出现一条红色文本,程序会在 2 秒后自动关闭。 用于处理边界碰撞的代码是这样的...

if x < 0 or x > width - block_size or y < 0 or y > height - block_size:
                    run = False

以上代码跳出无限'while run'循环,跳转到这部分

    message('You loose like the loose you are , LOOSER!', red)
    pygame.display.update()
    time.sleep(2)
    pygame.quit()

但是,文本不会在方块撞到墙壁后立即显示,而是在按下随机按钮后显示

现在你只在有特定事件时执行你的碰撞代码,因为它在事件循环内缩进,所以只有在有事件发生时才会发生,在你的情况下是按键。

for event in pygame.event.get():
    # other code
    if x < 0 or x > width - block_size or y < 0 or y > height - block_size:
         run = False

解决问题只需un-indent那段代码。

while run:
    for event in pygame.event.get():
        if event.type ==pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                delta_x = -block_size
                delta_y = 0
            if event.key == pygame.K_RIGHT:
                delta_x = block_size
                delta_y = 0
            if event.key == pygame.K_UP:
                delta_x = 0
                delta_y = -block_size
            if event.key == pygame.K_DOWN:
                delta_x = 0
                delta_y = block_size
      if x < 0 or x > width - block_size or y < 0 or y > height - block_size:
          run = False