xxxxxxxxxx
48
// Set up variables
let ballX = 200; // Ball's initial x position
let ballY = 50; // Ball's initial y position
let ballSize = 25; // Ball's size
let ballSpeed = 3; // Ball's speed
let ballXDirection = ballSpeed; // Ball's x direction
let ballYDirection = ballSpeed; // Ball's y direction
function setup() {
createCanvas(400, 400); // Create a canvas
}
function draw() {
background(220); // Set the background color
// Draw the ball
ellipse(ballX, ballY, ballSize, ballSize);
// Update the ball's position
ballX += ballXDirection;
ballY += ballYDirection;
// Check if the ball hits the left or right wall
if (ballX + ballSize/2 >= width || ballX - ballSize/2 <= 0) {
print("Collision");
ballXDirection *= -1; // Reverse the ball's x direction
}
// Check if the ball hits the top or bottom wall
if (ballY + ballSize/2 >= height || ballY - ballSize/2 <= 0) {
print("Collision");
ballYDirection *= -1; // Reverse the ball's y direction
}
// Check for arrow key controls
if (keyIsDown(LEFT_ARROW)) {
ballX -= ballSpeed; // Move the ball left
} else if (keyIsDown(RIGHT_ARROW)) {
ballX += ballSpeed; // Move the ball right
}
if (keyIsDown(UP_ARROW)) {
ballY -= ballSpeed; // Move the ball up
} else if (keyIsDown(DOWN_ARROW)) {
ballY += ballSpeed; // Move the ball down
}
}