0% found this document useful (0 votes)
3 views

main code

Uploaded by

xarakok430
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

main code

Uploaded by

xarakok430
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

let angle = 0;

let radius = 200; // Initial radius of the circular path


let speed = 0.02; // Angular speed
let objectX, objectY; // Position of the moving object
let centerX, centerY; // Center of the circular path
let obstacles = [];
let collectibles = [];
let score = 0;
let gameOver = false;

function setup() {
createCanvas(600, 600);
centerX = width / 2;
centerY = height / 2;

// Generate initial obstacles


for (let i = 0; i < 5; i++) {
createObstacle();
}

// Generate initial collectibles


for (let i = 0; i < 3; i++) {
createCollectible();
}
}

function draw() {
background(30);

if (gameOver) {
showGameOverScreen();
return;
}

// Draw circular path


noFill();
stroke(255);
ellipse(centerX, centerY, radius * 2);

// Update object's position


objectX = centerX + radius * cos(angle);
objectY = centerY + radius * sin(angle);

// Draw the object


fill(0, 255, 0);
ellipse(objectX, objectY, 20);

// Draw and update obstacles


fill(255, 0, 0);
for (let obs of obstacles) {
obs.x = centerX + obs.radius * cos(obs.angle);
obs.y = centerY + obs.radius * sin(obs.angle);
ellipse(obs.x, obs.y, obs.size);

// Check for collision


let d = dist(objectX, objectY, obs.x, obs.y);
if (d < obs.size / 2 + 10) {
gameOver = true;
}

// Move obstacles
obs.angle += obs.speed;
}

// Draw and update collectibles


fill(0, 0, 255);
for (let i = collectibles.length - 1; i >= 0; i--) {
let col = collectibles[i];
ellipse(col.x, col.y, col.size);

// Check if player collects it


let d = dist(objectX, objectY, col.x, col.y);
if (d < col.size / 2 + 10) {
score += col.value;
collectibles.splice(i, 1); // Remove collectible
createCollectible(); // Add a new one
}
}

// Display score
fill(255);
textSize(16);
text(`Score: ${score}`, 10, 20);

// Update angle for the player's motion


angle += speed;

// Increase difficulty over time


if (frameCount % 300 === 0) {
increaseDifficulty();
}
}

function keyPressed() {
if (keyCode === UP_ARROW) {
speed += 0.01; // Increase speed
} else if (keyCode === DOWN_ARROW) {
speed = max(speed - 0.01, 0.01); // Decrease speed
} else if (keyCode === LEFT_ARROW) {
radius = max(radius - 10, 50); // Decrease radius
} else if (keyCode === RIGHT_ARROW) {
radius = min(radius + 10, width / 2 - 20); // Increase radius
} else if (key === 'R' || key === 'r') {
resetGame();
}
}

function resetGame() {
angle = 0;
speed = 0.02;
radius = 200;
score = 0;
gameOver = false;
obstacles = [];
collectibles = [];

// Regenerate obstacles and collectibles


for (let i = 0; i < 5; i++) {
createObstacle();
}
for (let i = 0; i < 3; i++) {
createCollectible();
}
}

function createObstacle() {
let obstacleAngle = random(TWO_PI);
let obstacleRadius = radius + random(-50, 50);
obstacles.push({
angle: obstacleAngle,
radius: obstacleRadius,
size: 20,
speed: random(0.01, 0.03),
});
}

function createCollectible() {
let collectibleAngle = random(TWO_PI);
let collectibleRadius = radius + random(-100, 100);
collectibles.push({
x: centerX + collectibleRadius * cos(collectibleAngle),
y: centerY + collectibleRadius * sin(collectibleAngle),
size: 15,
value: 50,
});
}

function increaseDifficulty() {
for (let obs of obstacles) {
obs.speed += 0.005; // Increase obstacle speed
}
}
function showGameOverScreen() {
textAlign(CENTER, CENTER);
fill(255, 0, 0);
textSize(32);
text("Game Over!", width / 2, height / 2 - 20);
textSize(20);
text("Press 'R' to Restart", width / 2, height / 2 + 20);
}

You might also like