Code viewer for World: Dino Game
 


//---- normal P5 code -------------------------------------------------------


/*
// Basic sketch 

function setup() {
    createCanvas(400, 400);
}

function draw() {
    if (mouseIsPressed) {
        fill(0);
    } else {
        fill(255);
    }
    ellipse(mouseX, mouseY, 80, 80);
}
*/

// start a sketch for a user controlled chrome's dino game

// dino game

const JUMP_HEIGHT = 15;
const OBSTACLE_ACCELERATION = 0.001;
const OBSTACLE_GENERATION_RATE = 0.000001;


let dinoRun1;
let dinoRun2;
let currentDinoImage;
let frameCounter = 0;
let switchInterval = 20;
let acceleration = 0.5;

let dinoY; // Y-coordinate of the dino
let isJumping = false; // Flag to track if the dino is jumping
let jumpHeight = JUMP_HEIGHT;
let gravity = 0.98; // Gravity of the jump
let obstacleSpeed = 4;
let obstacleCount = 0.01;

let moonX;
let moonY = 100;
let moonSize = 50;

let spots = []; // Array to store the spots
let cactii = []; // Array to store the cactii

let score = 1;

function preload() {
    dinoRun1 = loadImage("/uploads/nithinsai/dinorun0000.png");
    dinoRun2 = loadImage("/uploads/nithinsai/dinorun0001.png");
    dinoJump = loadImage("/uploads/nithinsai/dinoJump0000.png");
    // dinoDuck = loadImage("/uploads/nithinsai/dinoduck0000.png");
    // dinoDuck1 = loadImage("/uploads/nithinsai/dinoduck0001.png");
    smallCactus = loadImage("/uploads/nithinsai/cactusSmall0000.png");
    bigCactus = loadImage("/uploads/nithinsai/cactusBig0000.png");
    manySmallCactus = loadImage("/uploads/nithinsai/cactusSmallMany0000.png");
    // bird = loadImage("/uploads/nithinsai/berd.png");
    // bird1 = loadImage("/uploads/nithinsai/berd2.png");
    moon = loadImage("/uploads/nithinsai/moon.png");
}

function setup() {
    frameRate(60);
    dinoY = height - 100;
    moonX = window.innerWidth - 200;
    currentDinoImage = dinoRun1;
    createCanvas(windowWidth - 20, windowHeight - 20);
}

function draw() {
    background(255);
    let centerX = width / 2;
    let centerY = height / 2;

    textSize(16);
    textStyle(NORMAL);
    strokeWeight(0);
    fill(0);
    text("Click \"SPACE\" to jump.\nAvoid all obstacles.", 20, 30);

    image(currentDinoImage, centerX / 2, centerY - 25 + dinoY, 35, 35);
    frameCounter++;

    if (frameCounter >= switchInterval) {
        if (currentDinoImage === dinoRun1) {
            currentDinoImage = dinoRun2;
        } else {
            currentDinoImage = dinoRun1;
        }
        switchInterval -= acceleration;
        frameCounter = 0;
    }

    if (switchInterval < 4) {
        switchInterval = 4;
    }

    stroke(150); // line color
    strokeWeight(1); // line thickness

    let middleY = height / 2;
    line(0, middleY + 10, width, middleY + 10);

    // generate random dots below the line that move slow

    let randomX = random(0, width);
    let randomY = random(middleY + 10, height);

    stroke(0, 0, 255);
    point(randomX, randomY);


    if (isJumping) {
        dinoY -= jumpHeight;
        jumpHeight -= gravity;

        if (dinoY >= -0) {
            isJumping = false;
            dinoY = 0;
            jumpHeight = JUMP_HEIGHT;
        } else {
            currentDinoImage = dinoJump;
        }
    }

    if (random(1) < 0.01) {
        let spotX = random(width, width + 10);
        let spotY = random(0, height / 2 - 40);
        spots.push({ x: spotX, y: spotY });
    }

    // Draw and update the spots
    for (let i = 0; i < spots.length; i++) {
        let spot = spots[i];
        spot.x -= 1; // Move the spot to the left

        // Remove spots that are out of the canvas
        if (spot.x < -10) {
            spots.splice(i, 1);
        }

        // Draw the black spot
        fill(0); // Black color
        noStroke();
        ellipse(spot.x, spot.y, 3, 3);
    }


    // draw the cactii randomly on the line use the images instead of "fill"
    let cactusRadndom = random(1);
    if (cactusRadndom < obstacleCount) {
        let cactusX = random(width, width + 10);
        let cactusY = middleY - 10;

        // randomly choose one of the cactus images
        let obstacleImage;
        let sizeX = 20;
        let sizeY = 30;
        let cactusImageRandom = random(1);

        if (cactusImageRandom < 0.25) {
            obstacleImage = smallCactus;
            cactusY = middleY - 18;
        } else if (cactusImageRandom < 0.5) {
            obstacleImage = bigCactus;
            sizeX = 30;
            sizeY = 50;
            cactusY = middleY - 40;
        } else if (cactusImageRandom < 0.75) {
            obstacleImage = manySmallCactus;
            sizeX = 40;
            sizeY = 20;
        }
        // else {
        //     // obstacleImage = bird; // make bird fly
        // }
        if (obstacleImage)
            cactii.push({
                x: cactusX, y: cactusY, obstacle: {
                    img: obstacleImage,
                    sizeX: sizeX,
                    sizeY: sizeY
                }
            });
    }

    obstacleCount += OBSTACLE_GENERATION_RATE;

    // Draw and update the cactii
    for (let i = 0; i < cactii.length; i++) {
        let cactus = cactii[i];
        cactus.x -= obstacleSpeed; // Move the cactus to the left

        // Remove cactii that are out of the canvas
        if (cactus.x < 0) {
            cactii.splice(i, 1);
        }

        // Draw the cactus
        fill(0); // Black color
        noStroke();

        // draw one of the cactus images
        image(cactus.obstacle.img, cactus.x, cactus.y, cactus.obstacle.sizeX, cactus.obstacle.sizeY);
    }
    obstacleSpeed += OBSTACLE_ACCELERATION;

    // draw a moon that moves very slowly from right to left
    image(moon, moonX, moonY, moonSize, moonSize);
    moonX -= 0.001;

    score += 0.02;

    let collision = detectCollision(dinoY);

    if (collision) {
        console.log("Game over");

        // game over (for single player)
        noLoop();

        textAlign(CENTER, CENTER);
        textSize(30);
        textStyle(BOLD);
        fill(0);
        text("GAME OVER", width / 2, height / 2 - 200)
        textSize(17);
        textStyle(BOLD);
        text("Score: " + floor(score), width / 2, height / 2 - 160)

    }

    // display the score
    textSize(16);
    textStyle(BOLD);
    strokeWeight(0);
    fill(0);
    text("Score: " + floor(score), 20, 100);

}

function keyPressed() {
    if (key === ' ' && !isJumping) {
        isJumping = true;
        console.log("Space pressed");
    }
}

function detectCollision(dinoYY) {
    for (let i = 0; i < cactii.length; i++) {
        let cactus = cactii[i];
        let cactusX = cactus.x;
        let cactusY = cactus.y;
        let cactusSizeX = cactus.obstacle.sizeX - 10;
        let cactusSizeY = cactus.obstacle.sizeY - 10;

        let dinoX = width / 2 / 2;
        let dinoY = height / 2 - 30 + dinoYY;
        let dinoSizeX = 30;
        let dinoSizeY = 30;

        if (dinoX + dinoSizeX >= cactusX && dinoX <= cactusX + cactusSizeX) {
            if (dinoY + dinoSizeY >= cactusY && dinoY <= cactusY + cactusSizeY) {
                return true;
            }
        }
    }
    return false;
}