xxxxxxxxxx
245
// this is a randomly generated maze with a player that can be freely navigated
nunits = 20;
//global constants
let space;
let maze;
let rightborder, downboarder;
let rows, columns;
let heroRow,heroCol,heroDir;
// utility functions
function initArray(rows, columns, value){
//create a dynamic array of size rows x columns and fill it with values
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
}
function rand(maxval){
return ~~(Math.random()*maxval);
}
// maze generation
function generateMaze(){
i = rand(rows);
j = rand(columns);
visited = initArray(rows,columns,0);
mazeiter(i,j,visited);
}
function mazeiter(r,c,visited){
visited[r][c] = 1;
while (true){
neighbours = [];
if (r>0){
if (visited[r-1][c]==0){
neighbours[neighbours.length] = [r-1,c];
}
}
if (c>0){
if (visited[r][c-1]==0){
neighbours[neighbours.length] = [r,c-1];
}
}
if (r<rows-1){
if (visited[r+1][c]==0){
neighbours[neighbours.length] = [r+1,c];
}
}
if (c<columns-1){
if (visited[r][c+1]==0){
neighbours[neighbours.length] = [r,c+1];
}
}
if (neighbours.length>0){
index = rand(neighbours.length);
[nr,nc] = neighbours[index];
if (r==nr){
rightborder[r][min(c,nc)] = 0;
}
if (c==nc){
downborder[min(r,nr)][c] = 0;
}
mazeiter(nr,nc,visited);
}
else {
break;
}
}
}
// drawing functions
function topleftpoint(row, col){
return [col*space,row*space]
}
function toprightpoint(row, col){
return [(col+1)*space, row*space]
}
function bottomleftpoint(row, col){
return [col*space, (row+1)*space]
}
function bottomrightpoint(row, col){
return [(col+1)*space, (row+1)*space]
}
function middlepoint(row, col){
return [col*space + space/2, row*space+space/2]
}
function drawBorder(){
[x1,y1] = topleftpoint(0,0);
[x2,y2] = toprightpoint(0,columns-1);
[x3,y3] = bottomrightpoint(rows-1, columns-1);
[x4,y4] = bottomleftpoint(rows-1,0)
strokeWeight(space/4);
stroke(0,0,255);
line(x1,y1,x2,y2)
line(x2,y2,x3,y3)
line(x3,y3,x4,y4)
line(x4,y4,x1,y1)
}
function drawMaze(){
stroke(0);
strokeWeight(max(space/8,1));
for (var i=0;i<rows;i++){
for (var j=0;j<columns;j++){
if (rightborder[i][j]==1){
[x1,y1] = toprightpoint(i,j);
[x2, y2] = bottomrightpoint(i,j);
line(x1,y1,x2,y2)
}
if (downborder[i][j]==1){
[x1,y1] = bottomleftpoint(i,j);
[x2,y2] = bottomrightpoint(i,j);
line(x1,y1,x2,y2);
}
}
}
}
function drawHero(){
// directions 0: top 1: bottom 2: left 3: right
[x,y] = middlepoint(heroRow,heroCol);
stroke(0);
strokeWeight(max(space/20,1));
fill(70,100,170)
if (heroDir==0){
x1 = x-space/8;
x2 = x+space/8;
y1 = y-space/2.5;
y2 = y-space/2.5;
}
if (heroDir==1){
x1 = x-space/8;
x2 = x+space/8;
y1 = y+space/2.5;
y2 = y+space/2.5;
}
if (heroDir==2){
x1 = x-space/2.5;
x2 = x-space/2.5;
y1 = y-space/8;
y2 = y+space/8;
}
if (heroDir==3){
x1 = x+space/2.5;
x2 = x+space/2.5;
y1 = y-space/8;
y2 = y+space/8;
}
triangle(x,y,x1,y1, x2,y2);
fill(200,120,10)
circle(x,y,space/2.5)
}
// game functions
function heroGoLeft(){
heroDir = 2;
if (heroCol>0){
if (rightborder[heroRow][heroCol-1]==0){
heroCol = heroCol-1;
}
}
}
function heroGoRight(){
heroDir=3;
if (heroCol<columns-1){
if (rightborder[heroRow][heroCol]==0){
heroCol = heroCol+1;
}
}
}
function heroGoUp(){
heroDir=0;
if (heroRow>0){
if (downborder[heroRow-1][heroCol]==0){
heroRow = heroRow-1;
}
}
}
function heroGoDown(){
heroDir=1;
if (heroRow<rows-1){
if (downborder[heroRow][heroCol]==0){
heroRow = heroRow+1;
}
}
}
//pygame functions
function setup() {
createCanvas(windowWidth, windowHeight);
// determine maze constants
var dimension = min(windowWidth, windowHeight);
space = dimension/nunits;
// rows go vertically and columns go horizontally
rows = ~~(windowHeight/space);
columns = ~~(windowWidth/space);
maze = initArray(rows, columns, 0);
rightborder = initArray(rows, columns, 1);
downborder = initArray(rows, columns, 1);
generateMaze();
heroRow = rand(rows);
heroCol = rand(columns);
heroDir = 0;
}
function draw() {
background(220);
drawMaze();
drawBorder();
drawHero();
}
function keyPressed(){
if (keyCode == LEFT_ARROW){
heroGoLeft()
}
if (keyCode == RIGHT_ARROW){
heroGoRight()
}
if (keyCode == UP_ARROW){
heroGoUp()
}
if (keyCode == DOWN_ARROW){
heroGoDown()
}
}