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