xxxxxxxxxx
150
let x = 50; // x position of the ball
let y = 200; // y position of the ball
let speed = 2; // movement speed of the ball
let gravity = 0.5; // gravity pulling the ball down
let baseLift = -8; // base upward force when the space bar is pressed
let liftMultiplier = 0.5; // multiplier for consecutive jumps
let velocity = 0; // starting velocity of the ball is zero
let blocks = []; // array to hold the blocks
let blockSpacing = 150; // space between blocks
let blockWidth = 60; // width of blocks
let gameSpeed = 2; // speed at which the blocks move
let jumpCount = 0; // counter for jumps
let gameOver = false; // game over flag
let score = 0; // player's score
function setup() {
createCanvas(400, 400); // create a 400x400 pixel canvas
resetGame(); // initialize the game
}
function draw() {
if (gameOver) {
background(255, 255, 240); // pastel yellow background
fill(0); // black color for 'game over' text
textSize(32);
textAlign(CENTER);
text("Game Over", width / 2, height / 2); // show 'game over'
textSize(16);
text("Press R to Restart", width / 2, height / 2 + 30); // restart instruction
textSize(16);
text("Score: " + score, width / 2, height / 2 + 60); // display score
return; // stop the game loop
}
background(255, 255, 240); // pastel yellow background
// draw the score in the upper corner
fill(0); // black color for the score text
textSize(16);
textAlign(LEFT);
text("Score: " + score, 10, 20); // display score in the upper left corner
// draw the ball
fill(255, 255, 255);
ellipse(x, y, 30, 30); // drawing the ball with a diameter of 30
// apply gravity constantly
velocity += gravity; // increase downward speed
y += velocity; // move the ball down
// keep the ball within the bottom boundary
if (y > height - 15) {
y = height - 15; // set ball to ground level
velocity = 0; // stop moving once the ball hits the bottom
jumpCount = 0; // reset jump count when on the ground
}
// update block positions
for (let block of blocks) {
block.x -= gameSpeed; // move blocks to the left
// reset block position if it goes off-screen
if (block.x + block.width < 0) {
block.x = width + blockSpacing; // reset to the right side
block.y = random(100, 300); // change the height randomly
block.hasTriangle = random() < 0.3; // 30% chance to have a triangle show up
block.isGreen = false; // reset block color
}
// draw the blocks
fill(block.isGreen ? color(0, 255, 0) : 'black'); // green if landed on, else black
rect(block.x, block.y, block.width, 20); // draw the block
// draw triangles if block has one
if (block.hasTriangle) {
fill(255, 0, 255); // fuchsia color for the triangle
triangle(
block.x + block.width / 2, block.y - 10, // top point
block.x, block.y + 20, // bottom left
block.x + block.width, block.y + 20 // bottom right
);
}
}
// handle keyboard inputs for jumping
if (keyIsDown(65)) { // 'a' key to move left
x -= speed; // move left
}
if (keyIsDown(68)) { // 'd' key to move right
x += speed; // move right
}
// jumping with space bar
if (keyIsDown(32) && jumpCount < 2) { // allow up to 2 jumps
velocity += baseLift * (1 + jumpCount * liftMultiplier); // increase lift for consecutive jumps
jumpCount++; // increment jump count
}
// keep the ball within the canvas boundaries
x = constrain(x, 15, width - 15); // constrain x position
y = constrain(y, 15, height - 15); // constrain y position
// check for collision with blocks
for (let block of blocks) {
if (x > block.x && x < block.x + block.width && y + 15 > block.y && y - 15 < block.y + 20) {
if (block.hasTriangle) {
gameOver = true; // set game over if it hits a block with a triangle
} else {
y = block.y - 15; // stop the ball on top of the block
velocity = 0; // reset velocity
jumpCount = 0; // reset jump count when landing on a block
score += 100; // increment score by 100
block.isGreen = true; // change block color to green
}
}
}
}
function keyPressed() {
if (gameOver && (key === 'r' || key === 'R')) {
resetGame(); // restart the game if r is pressed
}
}
function resetGame() {
// reset all game variables
x = 50; // reset ball x position
y = 200; // reset ball y position
velocity = 0; // reset velocity
jumpCount = 0; // reset jump count
gameOver = false; // game is not over
score = 0; // reset score
blocks = []; // clear existing blocks
// create initial blocks
for (let i = 0; i < 5; i++) {
let blockY = random(100, 300); // random y position
let hasTriangle = random() < 0.3; // 30% chance to have a triangle
blocks.push({
x: width + i * blockSpacing,
y: blockY,
width: blockWidth,
height: 20,
hasTriangle: hasTriangle,
isGreen: false // new property to track color
}); // add block to array
}
}