xxxxxxxxxx
let marioImage; // Variable to store the Mario image
let cloudPositions = []; // Array to store cloud positions
let x, y; // Position of Mario
let speedX, speedY; // Speed in the x and y directions
let noiseOffsetX, noiseOffsetY; // Noise offsets for smooth movement
function preload() {
// Load the Mario image
marioImage = loadImage('mario.png'); // Ensure the image file is in the sketch folder
}
function setup() {
createCanvas(400, 400);
// Initialize Mario's position and speed
x = width / 2;
y = height / 2;
speedX = random(-5, 5); // Increased speed for faster movement
speedY = random(-5, 5);
// Initialize noise offsets
noiseOffsetX = random(1000);
noiseOffsetY = random(2000);
// Create random positions for clouds
for (let i = 0; i < 5; i++) {
cloudPositions.push({
x: random(width),
y: random(height / 2), // Clouds stay in the upper half of the canvas
speed: random(0.5, 2) // Random speed for clouds
});
}
}
function draw() {
// Draw a sky background
background(135, 206, 235); // Light blue sky color
// Draw and animate clouds
for (let cloud of cloudPositions) {
drawCloud(cloud.x, cloud.y);
cloud.x -= cloud.speed; // Move clouds to the left
if (cloud.x < -50) {
// Reset cloud to the right edge when it moves out of view
cloud.x = width + 50;
cloud.y = random(height / 2);
}
}
// Update Mario's position using noise for smooth randomness
speedX = map(noise(noiseOffsetX), 0, 1, -5, 5); // Increased speed
speedY = map(noise(noiseOffsetY), 0, 1, -5, 5);
x += speedX;
y += speedY;
// Update noise offsets
noiseOffsetX += 0.02; // Faster noise changes
noiseOffsetY += 0.02;
// Constrain Mario's position within the canvas
x = constrain(x, 25, width - 25); // Ensure Mario stays within the canvas horizontally
y = constrain(y, 25, height - 25); // Ensure Mario stays within the canvas vertically
// Draw Mario
imageMode(CENTER); // Set image mode to center
image(marioImage, x, y, 50, 50); // Draw Mario image with a size of 50x50
}
// Function to draw a simple cloud
function drawCloud(cloudX, cloudY) {
fill(255, 255, 255, 200); // White clouds with slight transparency
noStroke();
ellipse(cloudX, cloudY, 40, 30); // Main cloud body
ellipse(cloudX + 15, cloudY + 10, 30, 20); // Smaller cloud parts
ellipse(cloudX - 15, cloudY + 10, 30, 20);
}
// Key press functionality for saving
function keyPressed() {
if (key == 's' || key == 'S') {
saveCanvas("mario.jpg"); // Save the canvas as a .jpg file when 's' is pressed
}
}