0 Replies - 227 Views - Last Post: 16 December 2015 - 08:50 PM Rate Topic: -----

#1 amanuel12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-December 15

How do i make it so it goes through diffrent levels

Posted 16 December 2015 - 08:50 PM

Hello There!! So im basically making a Space Invaders game. The trouble part now is that i need to making a Level 2. I tried to use a global counter so it counts then do it but it loops so many times the variable becomes millions. Do you guys have any solution to this problem? Thankyou Very Much.

Here is my code where i tried to put the counter as a global variable but it looped so many times, it made game.gameWon2 and game.gameWon both true:

 <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset="utf-8">
        <title>Space Invaders</title>
        <script src="http://code.jquery.com/jquery-latest.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <style>
            canvas {
                position: absolute;
                top: 0px;
                left: 0px;
                background: transparent;
            }
            
            #backgroundCanvas {
                background-color: black;
            }
        </style>
    </head>
    
    <body>
    
        <canvas id="backgroundCanvas" width="550" height="600"></canvas>
        <canvas id="playerCanvas" width="550" height="600"></canvas>
        <canvas id="enemiesCanvas" width="550" height="600"></canvas>
    
        <script>
        
            (function() {
                var gamelevel = 1;
    
                do{
                $(document).ready(function() {
                    var gameWonSoundCount = 0;
                    var gameEndSoundCount = 0;
                    var game = {};
                    
                    game.stars = [];
                    game.width = 550;
                    game.height = 600;
                    game.images = [];
    
                    game.doneImages = 0;
                    game.requiredImages = 0;
                    game.keys = [];
                    game.projectiles = [];
                    game.enemies = [];
    
                    game.count = 0;
                    game.division = 48;
                    game.left = false;
                    game.enemySpeed = 3;
    
                    game.fullShootTimer = 10;
                    game.shootTimer = game.fullShootTimer;
    
                    game.gameOver = false;
                    game.gameWon = false;
                    game.gameWon2 = false;
                    game.moving = false;
                    
                    game.explodeSound = new Audio("Explosion.wav");
                    game.gameWonSound = new Audio("GameWon.mp3");
                    game.gameOverSound = new Audio("GameOver.mp3");
                    game.gameEndSound = new Audio("GameWon.mp3");
                    game.shootSound = new Audio("Shoot.wav");
    
    
    
                    game.contextBackground = document.getElementById("backgroundCanvas").getContext('2d');
    
                    game.contextPlayer = document.getElementById("playerCanvas").getContext('2d');
    
                    game.contextEnemies = document.getElementById("enemiesCanvas").getContext('2d');
    
                    game.player = {
                        x: game.width / 2 - 50,
                        y: game.height - 103,
                        width: 80,
                        height: 100,
                        speed: 3,
                        rendered: false
                    }
    
    
    
    
                    $(document).keydown(function(e) {
                        game.keys[e.keyCode ? e.keyCode : e.which] = true;
    
                    })
                    $(document).keyup(function(e) {
                        delete game.keys[e.keyCode ? e.keyCode : e.which];
    
                    })
    
                    /*
          up -38
          down-40
          left -37
          right-39
          w-87
          a-65
          s-83
          d-68
          space-32
        */
                    function addBullet() {
                        game.projectiles.push({
                            x: game.player.x,
                            y: game.player.y,
                            size: 20,
                            speed: 3,
                            image: 2
    
                        })
    
                    }
    
                    function init() {
                        for (i = 0; i < 600; i++) {
                            game.stars.push({
                                x: Math.floor(Math.random() * game.width),
                                y: Math.floor(Math.random() * game.height),
                                size: Math.random() * 5
                            })
                        }
    
                        for (y = 0; y < 5; y++) {
                            for (x = 0; x < 5; x++) {
                                game.enemies.push({
                                    x: (x * 70) + (70 * x) + 10,
                                    y: (y * 70) + (10 * y) + 40,
                                    width: 70,
                                    height: 70,
                                    image: 1,
                                    dead: false,
                                    deadTime: 20
                                })
                            }
                        }
    
    
                        loop();
                        setTimeout(function() {
                            game.moving = true;
                        }, 5000)
                    }
    
                    function addStars(num) {
                        for (i = 0; i < num; i++) {
                            game.stars.push({
                                x: Math.floor(Math.random() * game.width),
                                y: game.height + 10,
                                size: Math.random() * 5
                            })
                        }
    
                    }
    
                    function update() {
                        addStars(1);
                        game.count++;
    
                        if (game.shootTimer > 0) {
                            game.shootTimer--;
                        }
                        for (i in game.stars) {
                            if (game.stars[i].y <= -5) {
                                game.stars.splice(i, 1);
                            }
                            game.stars[i].y--;
                        }
    
                        if (game.keys[37] || game.keys[65]) {
                            if (!game.gameOver) {
                                if (game.player.x >= 0) {
                                    game.player.x -= game.player.speed;
                                    game.player.rendered = false;
                                }
                            }
    
                        }
                        if (game.keys[39] || game.keys[68]) {
                            if (!game.gameOver) {
                                if (game.player.x <= 500 - 50) {
                                    game.player.x += game.player.speed;
                                    game.player.rendered = false;
                                }
                            }
    
                        }
                        if (game.count % game.division == 0) {
                            game.left = !game.left;
                        }
    
                        for (i in game.enemies) {
                            if (!game.moving) {
                                if (game.left) {
    
                                    game.enemies[i].x -= game.enemySpeed;
                                } else {
                                    game.enemies[i].x += game.enemySpeed;
                                }
                            }
                            if (game.moving) {
                                game.enemies[i].y++;
                            }
                            if (game.enemies[i].y >= 670) {
                                game.gameOver = true;
                            }
                        }
    
                        for (i in game.projectiles) {
                            game.projectiles[i].y -= 3;
                            if (game.projectiles[i].y <= -10) {
                                game.projectiles.splice(i, 1)
                            }
                        }
    
                        if (game.keys[32] && game.shootTimer <= 0) {
                            addBullet();
                            game.shootSound.play();
                            game.shootTimer = game.fullShootTimer
                        }
    
                        for (m in game.enemies) {
                            for (p in game.projectiles) {
                                if (collision(game.enemies[m], game.projectiles[p])) {
                                    game.explodeSound.play();
                                    game.enemies[m].dead = true;
                                    game.enemies[m].image = 3;
                                    game.projectiles.splice(p, 1)
                                }
                            }
                        }
                        for (i in game.enemies) {
                            if (game.enemies[i].dead) {
                                game.enemies[i].deadTime--;
                            }
                            if (game.enemies[i].dead && game.enemies[i].deadTime <= 0) {
    
                                game.contextEnemies.clearRect(game.enemies[i].x, game.enemies[i].y, game.enemies[i].size, game.enemies[i].size);
                                game.enemies.splice(i, 1);
                            }
                        }
    
                        if (game.enemies.length <= 0) {
                            
                            gamelevel = gamelevel +1;
                            if (gamelevel >= 3) {
                                console.log("3rd Level");
                                game.gameWon = true;
                            }
                            if (gamelevel== 2) {
                                
                                game.division = 24;// Makes the enemies go left and right faster
                                game.gameWon2 = true;
    
                            }
    
                        }
                    }
    
                    function render() {
                        game.contextBackground.clearRect(0, 0, game.width, game.height)
                        game.contextBackground.fillStyle = "white";
                        for (i in game.stars) {
                            var star = game.stars[i];
                            game.contextBackground.fillRect(star.x, star.y, star.size, star.size);
                        }
                        if (!game.player.rendered) {
                            game.contextPlayer.clearRect(0, 0, game.width, game.height);
                            game.contextPlayer.drawImage(game.images[0], game.player.x, game.player.y, game.player.width, game.player.height);
                            game.player.rendered = true;
                        }
    
                        game.contextBackground.clearRect(0, 0, game.width, game.height);
                        game.contextEnemies.clearRect(0, 0, game.width, game.height);
                        for (i in game.enemies) {
                            var enemy = game.enemies[i];
                            game.contextEnemies.drawImage(game.images[enemy.image], enemy.x, enemy.y, enemy.width, enemy.height);
                        }
    
                        for (i in game.projectiles) {
                            var proj = game.projectiles[i];
                            game.contextEnemies.drawImage(game.images[proj.image], proj.x, proj.y, proj.size, proj.size);
    
                        }
    
                        if (game.gameOver) {
    
                            var gradient = game.contextPlayer.createLinearGradient(0, 0, game.width, 0);
                            gradient.addColorStop("0", "magenta");
                            gradient.addColorStop("0.5", "white");
                            gradient.addColorStop("1.0", "black");
                            // Fill with gradient
                            game.contextPlayer.fillStyle = gradient;
    
                            game.contextPlayer.font = "bold 50px monaco"
                            game.contextPlayer.fillText("Game Over!", game.width / 2 - 130, game.height / 2 - 25);
                            if (gameEndSoundCount < 2) {
                                gameEndSoundCount += 1;
                                game.gameOverSound.play();
                            }
                        }
    
                        if (game.gameWon) {
                            var gradient = game.contextPlayer.createLinearGradient(0, 0, game.width, 0);
                            gradient.addColorStop("0", "magenta");
                            gradient.addColorStop("0.5", "white");
                            gradient.addColorStop("1.0", "black");
                            // Fill with gradient
                            game.contextPlayer.fillStyle = gradient;
    
                            game.contextPlayer.font = "bold 50px monaco"
                            game.contextPlayer.fillText("Game Won!", game.width / 2 - 130, game.height / 2 - 25);
                            if (gameWonSoundCount < 2) {
                                gameWonSoundCount += 1;
                                game.gameWonSound.play();
                            }
    
                        }
    
                        if (game.gameWon2) {
                            console.log("Came")
                            var gradient2 = game.contextPlayer.createLinearGradient(0, 0, game.width, 0);
                            gradient2.addColorStop("0", "magenta");
                            gradient2.addColorStop("0.5", "white");
                            gradient2.addColorStop("1.0", "black");
                            // Fill with gradient
                            game.contextPlayer.fillStyle = gradient2;
    
                            game.contextPlayer.font = "bold 50px monaco"
                            game.contextPlayer.fillText("Level 2", game.width / 2 - 130, game.height / 2 - 25);
                            if (gameWonSoundCount < 2) {
                                gameWonSoundCount += 1;
                                game.gameWonSound.play();
                            }
    
                        }
    
                    }
    
                    function loop() {
                        requestAnimFrame(function() {
                            loop();
                        });
                        update();
                        render();
                    }
    
                    function initImages(paths) {
                        game.requiredImages = paths.length;
                        for (i in paths) {
                            var img = new Image;
                            img.src = paths[i];
                            game.images[i] = img;
                            game.images[i].onload = function() {
                                game.doneImages++;
                            }
                        }
                    }
    
                    function collision(first, second) {
                        return !(first.x > second.x + second.width ||
                            first.x + first.width < second.x ||
                            first.y > second.y + second.height ||
                            first.y + first.height < second.y);
    
                    }
    
                    function checkImages() {
    
                        if (game.doneImages >= game.requiredImages) {
                            init();
                        } else {
                            setTimeout(function() {
                                checkImages();
    
                            }, 1)
                        }
                    }
                    game.contextBackground.font = "bold 50px monaco"
                    game.contextBackground.fillStyle = "white";
                    game.contextBackground.fillText("loading", game.width / 2 - 100, game.height / 2 - 25)
                    initImages(["player.gif", "enemy.png", "bullet.png", "explosion.gif"])
                    checkImages();
                });
    
                window.requestAnimFrame = (function() {
                    return window.requestAnimationFrame ||
                        window.webkitRequestAnimationFrame ||
                        window.mozRequestAnimationFrame ||
                        window.oRequestAnimationFrame ||
                        window.msRequestAnimationFrame ||
                        function(callback) {
                            window.setTimeout(callback, 1000 / 60);
                        };
                })();
        }while(gamelevel>=3)
            })();
        
        </script>
    
    </body>
    
    </html>



Is This A Good Question/Topic? 0
  • +

Page 1 of 1