0

I made a program in Pygame where you walk in a maze. The maze is a 2D array which contains either
True: the wall is there or
False: the wall is not there
It works fine. But when I go to position 8;9 I can now go right - through a wall. It happens only on this y position. When I check the right variable, it is False.

enter image description here

import pygame
from pygame.locals import *
from pygame import gfxdraw
white = (255, 255, 255)
width, height = 220, 220
screen = pygame.display.set_mode((width, height))
psurf = pygame.Surface((20, 20))
psurf.fill((0, 0, 0))
wallsurf = pygame.Surface((20, 20))
wallsurf.fill((255, 0, 0))

t = True
f = False

walls = [[f, t, f, f, f, f, f, f, f, f, f],
         [f, t, f, f, f, f, f, f, f, f, f],
         [f, t, f, f, t, t, t, t, t, f, f],
         [f, t, f, f, t, f, f, f, t, f, f],
         [f, t, f, f, t, f, f, f, t, f, f],
         [f, t, f, f, t, f, f, f, t, f, t],
         [f, t, f, f, t, f, f, f, t, f, t],
         [f, t, f, f, t, f, f, f, t, f, t],
         [f, t, f, f, t, f, f, f, f, f, t],
         [f, t, t, f, t, t, t, t, t, t, t],
         [f, f, f, f, f, f, f, f, f, t, t]]

px = 0
py = 0
move_ticker = 0
right = True
left = True
delay = 500
maze = pygame.Surface((220, 220))
maze.fill(white)
placex = 0
placey = 0
for row in walls:
        placex = 0
        for wall in row:
            if wall == True:
               maze.blit(wallsurf, (placex * 20, placey * 20))
            placex += 1
        placey += 1
while True:
    pygame.event.get()
    screen.fill(white)
    screen.blit(maze, (0, 0))
    screen.blit(psurf, (px, py))
    keys=pygame.key.get_pressed()
    if keys[K_LEFT]:
        if left:
           if move_ticker == 0:
               move_ticker = delay
               px -= 20
               if px < -1:
                   px = 0
    if keys[K_RIGHT]:
        if right:
           if move_ticker == 0:   
               move_ticker = delay
               px += 20
               if px > 200:
                   px = 200
    if keys[K_DOWN]:
        if down:
            if move_ticker == 0:   
                move_ticker = delay   
                py += 20
                if py >= 200:
                    py = 200
    if keys[K_UP]:
        if up:
            if move_ticker == 0:   
                move_ticker = delay 
                py -= 20
                if py < 0:
                    py = 0
    if move_ticker > 0:
       move_ticker -= 1
    truex = int(px / 20)
    truey = int(py / 20)
    right = True
    left = True
    up = True
    down = True
    try:
        if walls[truey-1][truex]:
            up = False
        if walls[truey+1][truex]:
            down = False
        if walls[truey][truex-1]:
            left = False
        if walls[truey][truex+1]:
            right = False
    except IndexError:
        pass       
    pygame.display.flip()

1 Answer 1

3

The issue is that you have one try statement instead of 4. When you get to the lowest row walls[truey+1][truex] will return an index error. Right is still set to True and is therefor never updated to False. A quick (and ugly) fix is:

try:
    if walls[truey-1][truex]:
        up = False
except IndexError:
        up = False
try:
    if walls[truey+1][truex]:
        down = False
except IndexError:
        down = False
try:
    if walls[truey][truex-1]:
        left = False
except IndexError:
        left = False    
try:
    if walls[truey][truex+1]:
        right = False
except IndexError:
        right = False

I think a prettier way is to use a function, a function is a piece of code you can re-use multiple times so you don't have to rewrite the same thing over and over.

def can_move(pos_x, pos_y, dx, dy, walls):
    move = True
    try:
        if walls[pos_y + dy][pos_x + dx]:
            move = False
    except IndexError:
        move = False
    return move

Which you can shorten to:

def can_move(pos_x, pos_y, dx, dy, walls):
    try:
        move = not(walls[pos_y + dy][pos_x + dx])
    except IndexError:
        move = False
    return move

Using this function you can define your movements:

right = can_move(truex, truey, 1, 0, walls)
left = can_move(truex, truey, -1, 0, walls)
up = can_move(truex, truey, 0, -1, walls)
down = can_move(truex, truey, 0, 1, walls)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.