xxxxxxxxxx
74
let bball;
let stickman;
let t = 0; // Time variable for dribbling effect
function setup() {
createCanvas(400, 400);
bball = new Bball(144, 225, 50, 4); // Create a new ball object
stickman = new Stickman(227, 133, 70); // Create a new stickman object
}
function draw() {
frameRate(30);
background(220);
// Stickman and ball follow mouse's x movement
stickman.x = mouseX;
bball.x = mouseX;
// Show stickman
stickman.show();
// Show and dribble the ball
bball.dribble(t);
// Increment time for the next dribbling effect
t += 0.1;
}
// Stickman class
class Stickman {
constructor(x, y, size) {
this.x = x;
this.y = y;
this.size = size;
}
show() {
// Draw the stickman's body parts
circle(this.x, this.y, this.size); // Head
line(this.x, this.y + 37, this.x, this.y + 186); // Body
line(this.x, this.y + 186, this.x - 49, height); // Left leg
line(this.x, this.y + 186, this.x + 52, height); // Right leg
line(this.x - 2, this.y + 60, this.x - 81, this.y + 65); // Left arm
line(this.x - 2, this.y + 60, this.x + 36, this.y + 112); // Right arm
}
}
// Ball class
class Bball {
constructor(x = 144, y, xspeed, yspeed, bwidth = 50, bheight = 50) {
this.x = x;
this.y = y;
this.xspeed = xspeed;
this.yspeed = yspeed;
this.bwidth = bwidth; // Size of the ball (width)
this.bheight = bheight; // Size of the ball (height)
}
dribble(t) {
// Simulate dribbling motion by adjusting the ball's y position
let dribbleOffset = sin(t) * 10; // Sinusoidal dribbling effect
fill(248, 129, 88);
ellipse(this.x, this.y + dribbleOffset, this.bwidth, this.bheight); // Add dribble effect to y
noFill();
strokeWeight(2);
// Draw the lines and arcs for the basketball design
line(this.x - 25, this.y + dribbleOffset, this.x + 25, this.y + dribbleOffset);
line(this.x, this.y + dribbleOffset - 25, this.x, this.y + dribbleOffset + 25);
arc(this.x, this.y + dribbleOffset - 12.5, 40, 7, 0, PI);
arc(this.x, this.y + dribbleOffset + 12.5, 40, 7, PI, 0);
}
}