Skip to content Skip to sidebar Skip to footer

Hide And Seek/maze Game In Pygame Not Working

I'm making a hide and seek/maze game. I'v bean working in the maze but when i move it sometimes skips past it even though i made conditions. I haven't added the other player yet, r

Solution 1:

Please learn about lists, loops and pygame.Rect.

For instance:

import pygame
pygame.init()
screen = pygame.display.set_mode((500, 500))

wrects = [(50, 50, 200, 25), (200, 50, 25, 250), (250, 200, 200, 25), (50, 200, 25, 200), (250, 100, 25, 200),
          (300, 250, 25, 200), (350, 225, 25, 200), (75, 325, 200, 25), (25, 150, 150, 25), (100, 200, 75, 100)]
walls = [pygame.Rect(r) for r in wrects]
Svel = 1
red = pygame.Rect(225, 225, 25, 25)

running = True
clock = pygame.time.Clock()
while running:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = Falseif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        red.x = max(red.x - Svel, 0)
        for wall in walls: 
            if red.colliderect(wall):
                red.left = max(red.left, wall.right)
    if keys[pygame.K_RIGHT]:
        red.x = min(red.x + Svel, 500)
        for wall in walls: 
            if red.colliderect(wall):
                red.right = min(red.right, wall.left)
    if keys[pygame.K_UP]:
        red.y = max(red.y - Svel, 0)
        for wall in walls: 
            if red.colliderect(wall):
                red.top = max(red.top, wall.bottom)
    if keys[pygame.K_DOWN]:
        red.y = min(red.y + Svel, 500)
        for wall in walls: 
            if red.colliderect(wall):
                red.bottom = min(red.bottom, wall.top)
        
    screen.fill([255,255,255])
    for wall in walls: 
        pygame.draw.rect(screen, [0,0,0], wall)
    pygame.draw.rect(screen, [255,0,0], red)
    pygame.display.flip()

pygame.quit()

Post a Comment for "Hide And Seek/maze Game In Pygame Not Working"