Code Monkey home page Code Monkey logo

javascript_game_development_course's People

Contributors

bijoyandas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

javascript_game_development_course's Issues

Snake is not moving in the snake game.

my code

<html>

<head>
    <title>Snake Game</title>
</head>

<body>
    <center><canvas height="500" width="500" id="ctx" style="border:2px solid #000000"></canvas>
    </center>
    <script>
        var ctx = document.getElementById('ctx').getContext('2d');
        var WIDTH = 500;
        var HEIGHT = 500;
        ctx.font = "20px Calibri";
        var snakeList, foodList, direction;
        var snakeBody = {
            width: 20,
            height: 20,
            color: 'purple'
        };
        var food = {
            width: 20,
            width: 20,
            color: 'orange'
        };
        document.onkeydown = function(event) {
            // 0 - Left
            // 1 - Up
            // 2 - Right
            // 3 - Down
            if (event.keyCode == 37) {
                direction = 0;
                console.log('0');
            } else if (event.keyCode == 38) {
                direction = 1;
                console.log('1')
            } else if (event.keyCode == 39) {
                direction = 2;
                console.log('2')
            } else if (event.keyCode == 40) {
                direction = 3;
                console.log('3')
            }
        }
        drawSnake = function(sb, i) {
            ctx.save();
            if (i == 0) {
                ctx.fillStyle = 'black';
            } else {
                ctx.fillStyle = snakeBody.color;
            }
            ctx.fillRect(sb.x, sb.y, snakeBody.width, snakeBody.height);
            ctx.restore();
        }
        drawFood = function(f, i) {
            ctx.save();
            ctx.fillStyle = food.color;
            ctx.fillRect(f.x, f.y, food.width, food.height);
            ctx.restore();
        }
        var updateSnakeList = function() {
            for (let i = snakeList.length; i >= 0; i++) {
                if (direction == 0) {
                    if (i == 0) {
                        snakeList[i].x = snakeList[i].x - 5
                    } else {
                        snakeList[i].x = snakeList[i - 1].x
                        snakeList[i].y = snakeList[i - 1].y
                    }
                } else if (direction == 1) {
                    if (i == 0) {
                        snakeList[i].x = snakeList[i].y - 5
                    } else {
                        snakeList[i].x = snakeList[i - 1].x
                        snakeList[i].y = snakeList[i - 1].y
                    }
                } else if (direction == 2) {
                    if (i == 0) {
                        snakeList[i].x = snakeList[i].x + 5
                    } else {
                        snakeList[i].x = snakeList[i - 1].x
                        snakeList[i].y = snakeList[i - 1].y
                    }
                } else if (direction == 3) {
                    if (i == 0) {
                        snakeList[i].x = snakeList[i].y + 5
                    } else {
                        snakeList[i].x = snakeList[i - 1].x
                        snakeList[i].y = snakeList[i - 1].y
                    }
                }
            }
        }
        var updateSnakePosition = function() {
            ctx.clearRect(0, 0, WIDTH, HEIGHT);
            snakeList.forEach(drawSnake);
            updateSnakeList();
        }
        var startGame = function() {
            snakeList = [{
                x: 220,
                y: 200
            }, {
                x: 210,
                y: 200
            }, {
                x: 200,
                y: 200
            }];
            foodList = [];
            direction = 99;
            setInterval(updateSnakePosition, 1000)
        }
        startGame();
    </script>
</body>

</html>

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.