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

问题描述

在小移动矩形碰到边界后,试图显示“您像宽松的人一样松动”文本... 但是相反,矩形正在移过边界,并且仅在我按下随机按钮后才会显示“您松散”的文字... 代码在下面

import pygame
import time

#initialize
pygame.init()

#colors
red = (255,0)
green = (0,255,0)
blue = (0,255)
cyan = (0,255)
black = (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,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

上面的代码突破了无限的“运行时”循环,并跳到了这一部分

    message('You loose like the loose you are,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

要解决该问题,只需取消该代码块的缩进即可。

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...