1

Am trying to show the 'You loose like the looser you are looser' text after the small moving rectangle hits the bounderies... but instead, the rectangle is moving past the bounderies and the 'You loose' text only shows up after i hit a random button... the code is below

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

As you can see, the above code is supposed to have a small square that moves around the screen upon clicking an arrow button and continues moving in the direction in which it is moving untill a different arrow is pressed. Now, when the square hits the bounderies, a red piece of text is suppposed to appear and the program closes automatically 2 seconds later. the code am using to handele the boundary collision is this...

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

the above code breaks out of the infinite 'while run' loop and jumps to this part

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

however, the text doesn't show immediately after the square runs into the walls, instead the text shows after a random button is pressed

RCK Euler
  • 35
  • 3

1 Answers1

2

Right now you are only executing your collision code if there is a certain event because it is indented inside of the event loop, so it only happens if there is an event, in your case key press.

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

To solve the problem simply un-indent that block of code.

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