xxxxxxxxxx
182
// used: https://www.toptal.com/developers/keycode
let grid =
`WWWWWWWWW
W$$.....+##########
W.......W #
W.......W WWWW+WWW
W.......W W..S...W
W.......+#####+....$.W
W.......W W...$S$W
WWWWWWWWW WWWWWWWW`.split('\n').map( (e)=>e.split('') );
let player = [3,4,'E']; // row,col , facing: NESW
let playerSprite = {'N':'^',
'E':'>',
'S':'V',
'W':'<'
};
let score = 0, hp = 100;
let countDown = -1,countDownText='';
function setup() {
createCanvas(400, 400);
//console.log(grid);
}
function draw() {
background(0);
fill(255);
textSize(14);
text(`score: ${score}`,320,20);
text(`hp: ${hp}`,320,35);
text('Mini-Rogue 2.5D - use arrows to move...',20,380);
// mini-map
textSize(5);
for (let r=0;r<grid.length;r++){
for (let c=0;c<grid[r].length;c++){
text(grid[r][c],c*5 +2,r*5 + 12);
}
}
text(playerSprite[player[2]],
player[1]*5+2,player[0]*5+12);
// large map
textSize(16);
for (let r=0;r<grid.length;r++){
for (let c=0;c<grid[r].length;c++){
if (grid[r][c]=='W'){
push();
fill("darkred");
noStroke();
rect(c*15 +50,r*5 + 120+15-6,15,-2);
fill("red");
stroke("brown");
rect(c*15 +50,r*5 + 120+15,15,-6);
pop();
} else if (grid[r][c]=='.'){
push();
if ((r+c)%2==0)
fill("gray");
else
fill("lightgray");
noStroke();
rect(c*15 +50,r*5 + 120+15,15,5);
pop();
} else if (grid[r][c]=='#'){
push();
if ((r+c)%2==0)
fill("yellow");
else
fill("gold");
noStroke();
rect(c*15 +50,r*5 + 120+15,15,5);
pop();
} else if (grid[r][c]!='+'){
text(grid[r][c],c*15 +50,r*5 + 120+15);
}
if ((c==player[1]) && (r==player[0]) ){
push();
fill("white");
stroke("lightgreen");
rect(player[1]*15+50,player[0]*5+120,15,18);
fill("lightgreen");
noStroke();
circle(player[1]*15+50+4,player[0]*5+120+4,8);
circle(player[1]*15+50+15-4,player[0]*5+120+2,8);
rect(player[1]*15+50+4,player[0]*5+120+11,8,3);
pop();
}
} // end c for loop
} // end r for loop
if (countDown>-1){
countDown--;
textSize(14);
text(countDownText,150,40);
}
if (random(1,100)>95){
keyCode=ENTER;
key = ' ';
keyPressed();
}
if (hp<=0){
textSize(26);
text('☠️ permanently dead ☠️',60,300);
noLoop();
}
}
function keyPressed() {
let newPos = [player];
let facing = '?';
if (keyCode === LEFT_ARROW) {
newPos[1]-=1;
facing = 'W';
}
if (keyCode === RIGHT_ARROW) {
newPos[1]+=1;
facing = 'E';
}
if (keyCode === UP_ARROW) {
newPos[0]-=1;
facing = 'N';
}
if (keyCode === DOWN_ARROW) {
newPos[0]+=1;
facing = 'S';
}
if (facing!='?')
newPos[2] = facing;
let [r,c] = newPos;
if (grid[r][c]=='$'){
grid[r][c] = '.';
score += 100;
countDownText = 'Found gold';
countDown += 100;
}
if (grid[r][c]=='S'){
// encounter with a snake
hp -= int(random(1,6+1));
countDownText = 'Ouch!';
countDown += 100;
if (random(1,100)>80){
// the snake was killed
grid[r][c] = '.';
score += 500;
countDownText = 'Killed a snake';
countDown += 100;
}
}
if ( '.+#'.includes( grid[r][c]) ){
// move on walkable tiles
player = newPos;
}
if ( (random(1,100)>98) &&
(grid[5][16]!='S') ){
// spawn a snake
grid[5][16]='S';
}
if ( (random(1,100)>95) &&
(grid[1][1]!='$') ){
// spawn gold
grid[1][1]='$';
}
}