Personal Learning

through game development I learned a lot. I learned how to code in markdown, I learned how to use if statments/booleans, I learned how to make sprites fuction (even when they have diffrent frames per row). I also learned how to work in a team to get things done. At first we were all over the place but after a few weeks we learned how to divide up the work evenly and fine a calm steady pace to get things done.

Sprites

strawberry squisher

we went through three diffrent sprite sheets trying to find one that fit perfectly for the charecter. Some of the sprite sheets were unevenly spaces, others had multiple per row making it harder to create. Finally I found a sprite sheet that was almost even and decided to personally fix the spacing between the sprite to get it to work. additionally the sprite happened to be facing the wrong direction and fliping the sprite sheet just made it moonwalk so I decided to change the gameplay of the sprite so that It can come at you from the other direction

midnight stalker

midnight stalker worked fine just as a sprite (it was originally used in someone elses test code) the only issue was the action we tried to add. We atemted to make it throw a bomb every few secconds that would explode harming the player. We got this to work when you click a button the bomb was launched but to make it automatic it too many tried and many days of coding. I finally got it to work by making an IF ELSE statment that IF __ amound of secconds had passed THEN it would launch a bomb.

Ice Man

Ice man was very difficult to code. At first he was walking strange because the animation that was supposed to be connected was seperated into 4 diffrent lines. To fix this we had to go back into canva and atempt to re space the sprite perfectly even. After a few tried it worked but then he was facing the wrong directiion and it looked a bit strange when he was walking for a long enough time. To counter this we made this charecter come at you every few seconds and disapear when we was not attacking so it would be easier to look at. The only probllem

%%html
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pinky</title>
</head>
<body>
    <div>
        <canvas id="spriteContainer">
            <img id="pinky" src="/nora1/images/pinky.png">
        </canvas>
        <div id="controls">
            <input type="radio" name="animation" id="A" checked>
            <label for="A">A</label><br>
            <input type="radio" name="animation" id="B">
            <label for="B">B</label><br>
            <input type="radio" name="animation" id="C">
            <label for="C">C</label><br>
            <input type="radio" name="animation" id="D">
            <label for="D">D</label><br>
        </div>
    </div>
    <script src="/nora1/assets/BoxGame/js/2023-10-13-Pinky.js"></script>
</body>
<script>
    window.addEventListener('load', function () {
        const canvas = document.getElementById('spriteContainer');
        const ctx = canvas.getContext('2d');
        const SPRITE_WIDTH = 70;
        const SPRITE_HEIGHT = 70;
        const SCALE_FACTOR = 2;
        const DESIRED_FRAME_RATE = 15;
        const FRAME_INTERVAL = 1000 / DESIRED_FRAME_RATE;
        const animationData = {
            A: { frameLimit: 3, width: 71, height: 72 },
            B: { frameLimit: 8, width: 79.5, height: 72 },
            C: { frameLimit: 5, width: 76, height: 73 },
            D: { frameLimit: 3, width: 76, height: 72 }
        };
        canvas.width = 1000;
        canvas.height = SPRITE_HEIGHT * SCALE_FACTOR;
        class Pinky {
            constructor() {
                this.image = document.getElementById("pinky");
                this.spriteWidth = SPRITE_WIDTH;
                this.spriteHeight = SPRITE_HEIGHT;
                this.width = this.spriteWidth;
                this.height = this.spriteHeight;
                this.x = 0;
                this.y = 0;
                this.scale = SCALE_FACTOR;
                this.minFrame = 0;
                this.frameY = 0;
                this.frameX = 0;
                this.maxFrame = 0;
                this.speed = -10;
                this.animationCounter = 0;
                this.animationLimit = 2;
            }
            setFrameLimit(limit) {
                this.maxFrame = limit;
            }
            setPosition(x, y) {
                this.x = x;
                this.y = y;
            }
            setSize(width, height) {
                this.spriteWidth = width;
                this.spriteHeight = height;
            }
            draw(context) {
                context.drawImage(
                    this.image,
                    this.frameX * this.spriteWidth,
                    this.frameY * this.spriteHeight,
                    this.spriteWidth,
                    this.spriteHeight,
                    this.x,
                    this.y,
                    this.width * this.scale,
                    this.height * this.scale
                );
            }
            update() {
                if (this.frameX < this.maxFrame) {
                    this.frameX++;
                } else {
                    this.frameX = 0;
                    this.animationCounter++;
                    if (this.animationCounter >= this.animationLimit) {
                        this.animationCounter = 0;
                        // Switch to the next animation
                        const nextAnimation = this.getNextAnimation();
                        const animationInfo = animationData[nextAnimation];
                        if (animationInfo) {
                            this.setFrameLimit(animationInfo.frameLimit);
                            this.setSize(animationInfo.width, animationInfo.height);
                        }
                        switch (nextAnimation) {
                            case 'A':
                                this.frameY = 0;
                                break;
                            case 'B':
                                this.frameY = 1;
                                break;
                            case 'C':
                                this.frameY = 2;
                                break;
                            case 'D':
                                this.frameY = 3;
                                break;
                        }
                    }
                }
                this.x += this.speed;
                if (this.x + this.spriteWidth * this.scale < 0) {
                    this.x = canvas.width;
                }
            }
            getNextAnimation() {
                const animations = ['A', 'B', 'C', 'D'];
                const currentIndex = animations.indexOf(this.getCurrentAnimation());
                const nextIndex = (currentIndex + 1) % animations.length;
                return animations[nextIndex];
            }
            getCurrentAnimation() {
                switch (this.frameY) {
                    case 0: return 'A';
                    case 1: return 'B';
                    case 2: return 'C';
                    case 3: return 'D';
                    default: return 'A';
                }
            }
        }
        const pinky = new Pinky();
        const controls = document.getElementById('controls');
        let lastTimestamp = 0;
        function animate(timestamp) {
            const deltaTime = timestamp - lastTimestamp;
            if (deltaTime >= FRAME_INTERVAL) {
                ctx.clearRect(0, 0, canvas.width, canvas.height);
                pinky.draw(ctx);
                pinky.update();
                lastTimestamp = timestamp;
            }
            requestAnimationFrame(animate);
        }
        animate();
        // Controls event listener
        controls.addEventListener('click', function (event) {
            if (event.target.tagName === 'INPUT') {
                const selectedAnimation = event.target.id;
                const animationInfo = animationData[selectedAnimation];
                if (animationInfo) {
                    pinky.setFrameLimit(animationInfo.frameLimit);
                    pinky.setSize(animationInfo.width, animationInfo.height);
                }
                switch (selectedAnimation) {
                    case 'A':
                        pinky.frameY = 0;
                        break;
                    case 'B':
                        pinky.frameY = 1;
                        break;
                    case 'C':
                        pinky.frameY = 2;
                        break;
                    case 'D':
                        pinky.frameY = 3;
                        break;
                }
            }
        });
    });
</script>
</html>
Pinky




Background

photo

the original background took awhile to find. we went through many AI generating image websites and came out with no good photo. Ideally it would be better to draw a spriite but nobody on the team was able to draw so we stuck to google. we found the perfect immage and spent time prefecting it by getting rid of the watermarks and adding in roads and pothole and other personalized touches.

charecter cards

although this was not to important to the game I helped create all 3 charecter cards for the 3 main villans in our game. I went into canva and made a personalized backstory and found a photo for them and then put buttons on the hopepage and linked the buttons to the canvases with the charecter pages. This adds a personalized touch becaue each charecter is specific in its own way!

collisions

platform

1.the platform was hard to work with because the hitbox originaly created was too wide and too tall making it so that the box goes onto the platform too early. I fidxed this by finding the ratio and then finding the exact X,Y value that worked so that the charecter would go onto the platform at the correct time.

2.the second issue we had with the platform is that the box would slowly “eat” or get rid of the sides of the platform when they touched and also when the villian touched the platform would disapear entierly. We fixed this by chagnging the ratio for the box, and changing the villiian so it went over not through the platform

cloud

we created a new platform that was a cloud later in the game. The only issue was that anytime we duplicated the cloud it would delete both the first and the seccond and freeze the game making it un-usable. To fix this we made a seprete entierly new code for the seccond cloud to make it a diffrent assent then the first so that the two would not effect each other.

box charecter (main sprite)

1.At the begining I found the sprite for the box and removed the background to get a png but there was lines around each frame in the sprite sheet, to fix that I had to go over it in another app to get rid of each of the lines on the sprite sheet.

2.latter the original box sprite was scrapped as the animation was glitchy and messed up. at first we used the code to draw a rectange and we colored it brown but that did not look good enough so we decided to use an image of a box my friend drew for me and imput that into the code. it worked perfectly as it was an image not an animation.

%%html

<style>
    #canvas {
        margin: 0;
        border: 1px solid black;
    }
</style>

<canvas id='canvas'></canvas>

<script>
    
    let canvas = document.getElementById('canvas');
    let c = canvas.getContext('2d');
    canvas.width = 1500;
    canvas.height = 400;
    let gravity = 1.5;

    let playerImage = new Image();
    playerImage.src = '/nora1/images/amazonBOX.png';

    class Player {
        constructor() {
            this.position = {
                x: 100,
                y: 200
            };
            this.velocity = {
                x: -10,
                y: 0
            };
            this.width = 50;
            this.height = 30;
            this.image = playerImage;
        }
        draw() {
            c.drawImage(this.image, this.position.x, this.position.y, this.width, this.height);
        }
        update() {
            this.draw();
            this.position.y += this.velocity.y;
            this.position.x += this.velocity.x;
            if (this.position.y + this.height + this.velocity.y <= canvas.height)
                this.velocity.y += gravity;
            else
                this.velocity.y = 0;
        }
    }

    class Platform {
        constructor(image) {
            this.position = {
                x: 0,
                y: 310
            }
            this.image = image;
            this.width = 1500;
            this.height = 300;
        }
        draw() {
            c.drawImage(this.image, this.position.x, this.position.y, this.width, this.height);
        }
    }

    class Tube {
        constructor(image) {
            this.position = {
                x: 1200,
                y: 190
            }
            this.image = image;
            this.width = 100;
            this.height = 120;
        }
        draw() {
            c.drawImage(this.image, this.position.x, this.position.y, this.width, this.height);
        }
    }

    class BlockObject {
        constructor(image) {
            this.position = {
                x: 200,
                y: 50
            };
            this.image = image;
            this.width = 300;
            this.height = 160;
        }
        draw() {
            c.drawImage(this.image, this.position.x, this.position.y, this.width, this.height);
        }
    }

    class Goomba {
        constructor(image) {
            this.position = {
                x: 250,
                y: 260
            };
            this.image = image;
            this.width = 55;
            this.height = 55;
            this.velocity = {
                x: -2,
                y: 0
            }
        }
        draw() {
            c.drawImage(this.image, this.position.x, this.position.y, this.width, this.height);
        }
        update() {
            this.position.x += this.velocity.x;
            this.draw();
        }
    }

    let image = new Image()
    let imageTube = new Image()
    let imageBlock = new Image()
    image.src = '/nora1/images/other_road.png'
    imageTube.src = '/nora1/images/house.png'
    imageBlock.src = '/nora1/images/Cloud.png';
    let imageGoomba = new Image()
    imageGoomba.src = '/nora1/images/Uno-Stalker.png';
    let platform = new Platform(image)
    let tube = new Tube(imageTube)
    let blockObject = new BlockObject(imageBlock)
    let goomba = new Goomba(imageGoomba)
    player = new Player()
    let keys = {
        right: {
            pressed: false
        },
        left: {
            pressed: false
        }
    }

    class GenericObject {
        constructor({ x, y, image }) {
            this.position = {
                x,
                y
            };
            this.image = image;
            this.width = 1500; // Adjust the width to fit your canvas
            this.height = 400; // Adjust the height to fit your canvas
        }

        draw() {
            c.drawImage(this.image, this.position.x, this.position.y, this.width, this.height);
        }
    }

    let imageBackground = new Image();
    imageBackground.src = '/nora1/images/Background-With-Road-6.1.png';

    let background = new GenericObject({
        x: 0,
        y: 0,
        image: imageBackground
    });

    let genericObjects = [background];

    function animate() {
        requestAnimationFrame(animate);
        c.clearRect(0, 0, canvas.width, canvas.height);

        genericObjects.forEach(genericObject => {
            genericObject.draw()
        });

        platform.draw();
        player.update();
        tube.draw();
        blockObject.draw();

        goomba.update();
        if (
            player.position.y + player.height <= blockObject.position.y + 50 && // Add desired value to lower the player
            player.position.y + player.height + player.velocity.y >= blockObject.position.y &&
            player.position.x + player.width >= blockObject.position.x + 50 && // Add desired value to shorten the collision width
            player.position.x <= blockObject.position.x + blockObject.width - 50 // Subtract desired value to shorten the collision width
        )
        {
            player.velocity.y = 0;
            player.position.y = blockObject.position.y - player.height + 50; // Adjust the player's position
        }

        if (keys.right.pressed && player.position.x + player.width <= canvas.width - 50) {
            player.velocity.x = 15;
        } else if (keys.left.pressed && player.position.x >= 50) {
            player.velocity.x = -15;
        } else {
            player.velocity.x = 0;
        }

        if (
                player.position.y + player.height <= platform.position.y &&
                player.position.y + player.height + player.velocity.y >= platform.position.y &&
                player.position.x + player.width >= platform.position.x &&
                player.position.x <= platform.position.x + platform.width
            )
            {
                player.velocity.y = 0;
            }

        if (
                player.position.y + player.height <= tube.position.y &&
                player.position.y + player.height + player.velocity.y >= tube.position.y &&
                player.position.x + player.width >= tube.position.x &&
                player.position.x <= tube.position.x + tube.width
            ) {
                player.velocity.y = 0;
                player.position.y += 0.1
                player.velocity.y = 0.0001
                gravity = 0.2
            }

            if (player.position.y + player.height == tube.position.y + tube.height ||
                    player.position.y + player.height <= tube.position.y ||
                    player.position.x + player.width <= tube.position.x ||
                    player.position.x >= tube.position.x + tube.width) {
                        gravity = 1.5
                    }

        if (
                player.position.x + player.width<= tube.position.x &&
                player.position.x + player.width + player.velocity.x >= tube.position.x &&
                player.position.y + player.height >= tube.position.y &&
                player.position.y <= tube.position.y + tube.height
            )
            {
                player.velocity.x = 0;
            }

        if (
                player.position.x >= tube.position.x + tube.width &&
                player.position.x + player.velocity.x <= tube.position.x + tube.width &&
                player.position.y + player.height >= tube.position.y &&
                player.position.y <= tube.position.y + tube.height
            )
            {
                player.velocity.x = 0;
            }

        if (
                player.position.x >= tube.position.x &&
                player.position.x + player.velocity.x <= tube.position.x &&
                player.position.y + player.height >= tube.position.y &&
                player.position.y <= tube.position.y + tube.height
            )
            {
                player.velocity.x = 0;
            }

        if (
                player.position.x + player.width <= tube.position.x + tube.width &&
                player.position.x + player.width + player.velocity.x >= tube.position.x + tube.width &&
                player.position.y + player.height >= tube.position.y &&
                player.position.y <= tube.position.y + tube.height
            )
            {
                player.velocity.x = 0;
            }

        if(
            player.position.y + player.height <= goomba.position.y &&
            player.position.y + player.height + player.velocity.y >= goomba.position.y &&
            player.position.x + player.width >= goomba.position.x &&
            player.position.x <= goomba.position.x + goomba.width
        )
        {
            player.velocity.y = -20;
        }

        if (
            player.position.x < goomba.position.x + goomba.width &&
            player.position.x + player.width > goomba.position.x &&
            player.position.y < goomba.position.y + goomba.height &&
            player.position.y + player.height > goomba.position.y
        ) {
            // Player collided with goomba, refresh the page
            location.reload();
        }

        if (
            goomba.position.x >= platform.position.x &&
            goomba.position.x <= platform.position.x
        )
        {
            goomba.velocity.x = 2;
        }

        if (
            goomba.position.x + goomba.width <= tube.position.x &&
            goomba.position.x + goomba.width + goomba.velocity.x >= tube.position.x
        )
        {
            goomba.velocity.x = -2;
        }
    }

    animate();

    addEventListener('keydown', ({ keyCode }) => {
        switch (keyCode) {
            case 65:
                keys.left.pressed = true;
                break;
            case 68:
                keys.right.pressed = true;
                break;
            case 87:
                player.velocity.y -= 20;
                break;
        }
    });

    addEventListener('keyup', ({ keyCode }) => {
        switch (keyCode) {
            case 65:
                keys.left.pressed = false;
                break;
            case 68:
                keys.right.pressed = false;
                break;
            case 87:
                player.velocity.y = -20;
                break;
        }
    })
</script>