I have been making a maze game on the SenseHAT in Python. I am trying to not let the player move through walls, but it just erases them. It should use the get_pixel() function to check if the player is trying to go through a wall (a blue pixel) but it lets the player through. Here is my code:
from sense_hat import SenseHat
import time
import pygame
import os
from senseColor import *
from pygame.locals import *
sense = SenseHat()
sense.clear()
w = [200,200,200]
loadingScreen = [
n,n,n,bl,bl,n,n,n,
n,n,bl,w,g,bl,n,n,
n,bl,w,w,g,w,bl,n,
bl,w,w,r,r,g,w,bl,
bl,w,w,r,r,w,w,bl,
n,bl,w,w,w,w,bl,n,
n,n,bl,w,w,bl,n,n,
n,n,n,bl,bl,n,n,n
]
sense.set_pixels(loadingScreen)
pygame.init()
pygame.display.set_mode((640, 480))
maze = [
n,n,n,bl,n,n,n,n,
n,bl,n,bl,bl,n,bl,bl,
n,bl,n,bl,bl,n,n,n,
n,bl,n,n,bl,n,bl,bl,
n,n,n,n,n,n,n,n,
n,bl,n,bl,n,n,bl,n,
bl,bl,bl,n,n,n,bl,n,
bl,n,n,n,bl,bl,bl,n
]
time.sleep(2)
sense.set_pixels(maze)
playerColour = [0,255,0]
ghostColour = [255,0,0]
floorColour = [0,0,0]
playerPosition = [0,0]
oldPlayerPosition = [0,0]
sense.set_pixel(0,0,playerColour)
#ghostPosition = [0,7]
#oldGhostPosition = [0,7]
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
sense.clear()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
if event.key == pygame.K_DOWN and sense.get_pixel(playerPosition[0],playerPosition[1] + 1) != [0,0,255]:
playerPosition[1] += 1
oldPlayerPosition[0] = playerPosition[0]
oldPlayerPosition[1] = playerPosition[1] - 1
elif event.key == pygame.K_UP and not sense.get_pixel(playerPosition[0],playerPosition[1] - 1) != [0,0,255]:
playerPosition[1] -= 1
oldPlayerPosition[0] = playerPosition[0]
oldPlayerPosition[1] = playerPosition[1] + 1
elif event.key == pygame.K_LEFT and not sense.get_pixel(playerPosition[0] - 1,playerPosition[1]) != [0,0,255]:
playerPosition[0] -= 1
oldPlayerPosition[1] = playerPosition[1]
oldPlayerPosition[0] = playerPosition[0] + 1
elif event.key == pygame.K_RIGHT and not sense.get_pixel(playerPosition[0] + 1,playerPosition[1]) != [0,0,255]:
playerPosition[0] += 1
oldPlayerPosition[1] = playerPosition[1]
oldPlayerPosition[0] = playerPosition[0] - 1
sense.set_pixel(playerPosition[0],playerPosition[1],playerColour)
sense.set_pixel(oldPlayerPosition[0],oldPlayerPosition[1], floorColour)
time.sleep(0.05)
Sorry for the long code, I have tried to make it as short as possible while still making and keeping it understandable. In case you didn't know, the get_pixel(xPos, yPos)
function should get the colour of a pixel at an exact position. Bear in mind that the SenseHAT's LED Matrix is 8x8, but the SenseHAT pixel setting functions take it as 0 - 7 each way.