xxxxxxxxxx
let playerFrames = new Array(12);
let playerObject_Simon;
let maze;
//Animation
function preload() {
for (let i = 0; i < playerFrames.length; i++) {
let filename = "sprite_character" + i + ".png";
playerFrames[i] = loadImage(filename);
//print("Loading " + filename);
}
maze = loadImage("Mazes-2png.png");
}
function setup() {
createCanvas(1000, 1000); // maze
//noCursor();
imageMode(CENTER);
playerObject_Simon = new Player();
}
function draw() {
background(255);
image(maze, 500, 500, 1000, 1000);
playerObject_Simon.display();
playerObject_Simon.move();
}
// Character class
class Player {
//ATTRIBUTES
constructor() {
this.x = 950; // starting position
this.y = 700;
this.frame = 0;
this.flip = 2;
this.py = this.y;
}
display() {
translate(this.x, this.y);
scale(2, this.flip);
image(playerFrames[this.frame], 0, 0);
if (frameCount % 3 == 0) this.frame++;
if (this.frame >= playerFrames.length) this.frame = 0;
}
//movement
move() {
//Using keys to move
if (keyIsPressed === true) {
//up & down
if (key === 'w') {
print("this is working");
let col = new Array(4);
col = get(this.x, this.y - 17);
print("get col: " + col);
print("get col[0]: " + col[0]);
print("this.x: " + this.x);
print("this.y: " + this.y);
// reaction to black
if (col[0] <= 200) {
//do nothing
} else {
this.y = this.y - 3;
}
} else if (key === 's') {
this.y = this.y + 3;
//left & right
} else if (key === 'd') {
this.x = this.x + 3;
} else if (key === 'a') {
this.x = this.x - 3;
}
if (this.y > this.py) this.flip = 2; //if the mouse is moving right to left, flip the orientation of the image
if (this.y < this.py) this.flip = -2;
}
}
}