Code Monkey home page Code Monkey logo

box-chaser's Introduction

Project logo

Box Chaser



🧐 About

This game is mainly about JavaScript element selection and manipulation. The grid size will be dynamically builded with JavaScript and the key event listeners will be handling the key press and moving element. User will be able to move the red box around with arror keys, once it overlap with the green box, the score variable will be increment by one and the green box will be randomly placed in a new position within the grid

🏁 Installing

Open box_.html with any web browser. If you are using Microsoft Internet Explorer, it need to be after version 2.0

🦄 Demo

ezgif com-crop

🎈 Code Walk Through

The style section is the CSS defining the size of the grid and how the elements in the game will looks like

In build() we are using objects and Arrays to contain element content and creating elements and adding them to the html dynamically based on the grid size defined in .gameArea.

        function build() {
            box = document.createElement("div");
            box.classList.add("box");
            box.x = gameArea.top;
            box.y = gameArea.left;
            box.style.top = box.y + "px";
            box.style.left = box.x + "px";
            gameAreaEle.appendChild(box);
            let counter = 1;
            for (let y = 0; y < gamebox.y; y++) {
                for (let x = 0; x < gamebox.x; x++) {
                    squares[counter] = document.createElement("div");
                    squares[counter].innerHTML = counter;
                    squares[counter].classList.add("square");
                    gameAreaEle.appendChild(squares[counter]);
                    counter++;
                }
            }
            makeActive();
        }

In handleKeyPress(key), we are validating user's keyboard input. Only arrow keys will be count as valid inputs. Once the valid keyboard received, new position will be updated and displayed on the game grid. For each arrow key press, we are also check whether the current position is about of the grid boundary

        function handleKeyPress(key) {
            console.log(key);
            if (key === "left" && box.x > gameArea.left) {
                box.x -= player.speed;
                player.square--;
            }
            if (key === "right" && box.x < gameArea.right - box.offsetWidth) {
                box.x += player.speed;
                player.square++;
            }
            if (key === "down" && box.y < (gameArea.bottom - box.offsetHeight)) {
                box.y += player.speed;
                player.square += gamebox.x;
            }
            if (key === "up" && box.y > gameArea.top) {
                box.y -= player.speed;
                player.square -= gamebox.x;
            }
            box.style.left = box.x + "px";
            box.style.top = box.y + "px";
            console.log(player.square);
            if (squares[player.square].classList.contains("active")) {
                console.log("FOUND");
                squares[player.square].classList.remove("active");
                makeActive();
                player.score++;
                score.innerHTML = player.score;
            }
        }

In makeActive(), we are selecting a random box within the grid and applying the class of active to that.

        function makeActive() {
            let randomIndex = Math.floor(Math.random() * squares.length);
            if (randomIndex != 0 && player.square != randomIndex) {
                squares[randomIndex].classList.add("active");
            }
            else {
                makeActive();
            }
        }

⛏️ Built Using

  • [HTML] - Programming Language
  • [CSS] - Programming Language
  • [JavaScript] - Programming Language
  • [Atom] - Text Editor

License

🌱 MIT 🌱

box-chaser's People

Contributors

chenhunluo321 avatar

Stargazers

Zachary Romano avatar

Watchers

James Cloos avatar  avatar

Forkers

tech-cow

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.