xxxxxxxxxx
255
// this version uses purely p5js. It has motion controls and basic gameplay
// there are some errors in the movements, mostly caused due to the confusion between rows and columns
// although this is not functioning properly, I am keeping this because I like the drawing of the sprite
// size of the maze
let nunits = 15;
// global variables
let cols;
let rows;
let space;
let horzBorders;
let vertBorders;
let mazestroke;
let hero;
let heropos;
//utility functions
function getPos(r,c){
x = c*space + space/2
y = r*space + space/2
return [x,y]
}
function boundary(x,y,nx,ny){
if (x==nx){
return horzBorders[min(y,ny)][x];
}
if (y==ny){
return vertBorders[y][min(x, nx)];
}
}
function rand(maxval){
return ~~(Math.random()*maxval);
}
function init2DArray(rows, columns, value){
let a = new Array(rows);
for (var i = 0; i<a.length; i++){
a[i] = new Array(columns);
for (var j=0; j<a[i].length; j++){
a[i][j] = value
}
}
return a;
}
// maze generation
function Viable(x,y,visited){
let viable = []
if (x>0){
if (visited[y][x-1]==0 && boundary(x,y,x-1,y)==1){
viable[viable.length] = [x-1,y]
}
}
if (y>0){
if (visited[y-1][x] ==0 && boundary(x,y, x, y-1)==1){
viable[viable.length] = [x,y-1]
}
}
if (x<rows-1){
if (visited[y][x+1]==0 && boundary(x,y, x+1, y)==1 ){
viable[viable.length] = [x+1, y]
}
}
if (y<cols-1){
if (visited[y+1][x] ==0 && boundary(x,y, x, y+1)==1){
viable[viable.length] = [x, y+1]
}
}
return viable
}
function initMaze(){
// initialzie maze parameters
dimension = min(windowWidth, windowHeight);
space = ~~(dimension/nunits);
cols = ~~(windowWidth/space);
rows = ~~(windowHeight/space);
horzBorders = init2DArray(rows-1, cols, 1);
vertBorders = init2DArray(rows, cols-1, 1);
mazestroke = max(0, space/10);
//generate maze
let visited = init2DArray(rows, cols, 0);
// get starting point
let x = rand(rows);
let y = rand(cols);
mazeIter(x,y, visited)
}
function mazeIter(x,y, visited){
// get viable directions
viable = Viable(x,y, visited)
while (viable.length>0){
[nx,ny] = viable[rand(viable.length)]
visited[ny][nx] = 1;
if (x==nx){
horzBorders[min(y, ny)] [x]= 0;
}
else {
vertBorders[y][min(x, nx)] = 0;
}
mazeIter(nx, ny, visited)
viable = Viable(x,y,visited)
}
}
// drawing functions
function drawHero(r,c, orientation){
//orientation: 0:down 1: up 2: left 3: down
stroke(0)
strokeWeight(max(mazestroke/2,1))
let x,y;
[x,y] = getPos(c,r)
fill(70,100,170)
let nx1, ny1, nx2, ny2;
if (orientation==0){
ny1 = y+space/2
ny2 = y+space/2
nx1 = x-space/6
nx2 = x+space/6
}
if (orientation==2){
nx1 = x-space/2
nx2 = x-space/2
ny1 = y-space/6
ny2 = y+space/6
}
if (orientation==1){
ny1 = y-space/2
ny2 = y-space/2
nx1 = x-space/6
nx2 = x+space/6
}
if (orientation==3){
nx1 = x+space/2
nx2 = x+space/2
ny1 = y-space/6
ny2 = y+space/6
}
triangle(x,y,nx1, ny1, nx2, ny2)
fill(200,120,10)
circle(x,y,space*2/3)
}
function drawMaze(){
strokeWeight(mazestroke)
stroke(0)
// draw horizontal borders
for (var i=0; i<horzBorders.length;i++){
for (var j=0; j<horzBorders[i].length;j++){
if (horzBorders[i][j]==1){
x = j*space
y = (i+1)*space
line(x,y, x+space, y)
}
}
}
// draw vertical borders
for (var i=0; i<vertBorders.length; i++){
for (var j=0; j<vertBorders[i].length; j++){
if (vertBorders[i][j]==1){
x = (j+1)*space
y = i*space
line(x,y, x, y+space)
}
}
}
// draw border
strokeWeight(mazestroke*4)
stroke(0,0,255)
line(0,0,space*cols, 0)
line(0,0,0,space*rows)
line(space*cols, 0, space*cols, space*rows)
line(0, space*rows, space*cols, space*rows)
}
// game functions
function heroGoLeft(){
heropos[2] = 2;
if (heropos[0]>0){
if (boundary(heropos[0], heropos[1], heropos[0]-1, heropos[1])==0){
heropos[0] = heropos[0]-1
}
}
}
function heroGoRight(){
heropos[2] = 3;
if (heropos[0]<cols){
if (boundary(heropos[0], heropos[1], heropos[0]+1, heropos[1])==0){
heropos[0] = heropos[0]+1
}
}
}
function heroGoUp(){
heropos[2] = 1;
if (heropos[1]>0){
if (boundary(heropos[0], heropos[1], heropos[0], heropos[1]-1)==0){
heropos[1] = heropos[1]-1
}
}
}
function heroGoDown(){
heropos[2] = 0;
if (heropos[1]<rows){
if (boundary(heropos[0], heropos[1], heropos[0], heropos[1]-1)==0){
heropos[1] = heropos[1]+1
}
}
}
//p5 functions
function preload(){
// hero = loadAnimation('assets/hero.png', { frameSize: [5,15], frames: 11 });
}
function setup() {
createCanvas(windowWidth, windowHeight);
initMaze()
heropos = [~~(cols/2), ~~(rows/2), 0]
}
function draw() {
background(200);
drawMaze()
drawHero(heropos[0], heropos[1], heropos[2])
}
function keyPressed(){
if (keyCode == LEFT_ARROW){
heroGoLeft()
}
if (keyCode == RIGHT_ARROW){
heroGoRight()
}
if (keyCode == UP_ARROW){
heroGoUp()
}
if (keyCode == DOWN_ARROW){
heroGoDown()
}
}