xxxxxxxxxx
82
//variables for each image
let astronaut
let ocean
let x = 300;
let y = 300;
let MoveLeft = true;
let MoveUp = true;
let song = ('Masked_Wolf.mp3');
function preload(){
//loading all images
astronaut = loadImage ('Astronaut.png');
ocean = loadImage ('ocean.jpeg');
}
function setup() {
//Load song; Create canvas size; set random values for x and y; set framerate
song = loadSound('Masked_Wolf.mp3');
createCanvas(400, 400);
x = round(random(width));
y = round(random(height));
frameRate(15)
}
function mousePressed() {
if (song.isPlaying()) {
// .isPlaying() returns a boolean
song.stop();
} else {
song.play();
}
}
function draw() {
//drew background and image
background(ocean,x,y,0,0);
image(astronaut,x,y,200,200)
// Horizontal movement range
if (MoveLeft) {
// Goes to the left by 10 values on x-axis
x = x - 10;
} else {
// Goes to the right by 10 values on x-axis
x = x + 10
}
if (x > 300) {
MoveLeft = true;
}
if (x < -100) {
MoveLeft = false;
}
if (MoveUp) {
// The goes to the left by 5 values on y-axis
y = y - 5;
} else {
// The goes to the right by 8 values on y-axis
y = y + 8;
}
if (y > 300) {
MoveUp = true;
}
if (y < 0) {
MoveUp = false;
}
}