Почему пуля не отображается на фоне? - PullRequest
0 голосов
/ 06 мая 2020

Я пытаюсь создать объекты-пули для своей HTML игры, но они не отображаются, хотя фон (который отрисовывается так же, как и до пуль) работает.

<!DOCTYPE html>
<html>
    <head>
        <title>
            Shooter
        </title>
        <style>
            body{
                background-color: blue;
            }
            canvas{
                position: absolute;
                top: 50px;
                left: 50px;
            }
            h1{
                color: yellow;
                position: absolute;
                top: 0px;
                left: 50px;
            }
            p{
                color: red;
                position: absolute;
                top: 450px;
                left: 50px;
            }
        </style>
    </head>
    <body onkeydown='getKeyAndMove(event)'>
        <h1>
            Shooter
        </h1>
        <canvas width='800' height='400' id='myCanvas'></canvas>
        <p>
            Use Up and Down to move, Space to shoot. Don't get hit!
        </p>
        <script>
            var start = null;
            var playery = 0;
            var canvas = document.getElementById("myCanvas");
            var ctx = canvas.getContext("2d");
            var radius = 1;
            var bullets = [null];
            var enemies = new Array(null);
            function fire(playery, bullets, enemies){
                if (bullets[0] === null){
                    console.log(9)
                    bullets[0] = {
                        x:0, 
                        y:playery, 
                        length:50,
                        height:25,
                        update: function(enemies){
                            bullets[0].x += 5;
                            var maxCount = enemies.length;
                            /*for (i=0; i<maxCount; i++){
                                var enemy = enemies[i];
                                if (enemy.x <= bullets[0].x + bullets[0].length){
                                    if (enemy.x + enemy.length >= bullets[0].x){
                                        if (enemy.y + enemy.height >= bullets[0].y){
                                            if (enemy.y <= bullets[0].y + bulets[0].height){
                                                enemies[i] = null;
                                                bullets[0] = null;
                                            }
                                        }
                                    } 
                                }
                            }*/
                            if (bullets[0].x >= 800){
                                bullets[0] = null;
                                return;
                            }
                        }
                    };
                }
            };

            function getKeyAndMove(e){              
                var key_code=e.which||e.keyCode;
                switch(key_code){
                    case 38:
                        playery -= 5
                        break;
                    case 40:
                        playery += 5
                        break;
                    case 32:
                        console.log(32)
                        fire(playery, bullets, enemies);
                        break;
                }
            };
            function step(timestamp){
                if (!start) start = timestamp;
                var progress = timestamp - start;
                ctx.fillStyle = 'white'
                ctx.fillRect(0, 0, canvas.width, canvas.height)
                ctx.fillStyle = 'rgb(0, 0, 0)'
                ctx.fillRect(1, 1, canvas.width - 2, canvas.height - 2)
                var i=0;
                var j=0;
                for (i=0; i < 800; i++){
                    for (j=0; j < 400; j++){
                        if (Math.random() > 0.9999) {
                            ctx.beginPath();
                            ctx.arc(i, j, radius, 0, 2 * Math.PI, false);
                            ctx.fillStyle = 'white';
                            ctx.fill();
                            ctx.lineWidth = 0;
                            ctx.strokeStyle = '#FFFFFF';
                            ctx.stroke();
                            ctx.closePath();
                        }
                    }
                }
                if (bullets[0] != null){
                    bullets[0].update(enemies)
                    console.log(0)        
                    if(bullets[0] != null){
                        ctx.fillStyle = '#DD0000'
                        ctx.fillRect(bullets[0].x, bullets[0].y, bullets[0].width, bullets[0].height)
                    }
                }
                window.requestAnimationFrame(step)
            }
            window.requestAnimationFrame(step)
        </script>
    </body>
</html>

Консоль не показывает ошибок, когда я запускаю код. Я настроил его для регистрации каждого кадра, в котором есть пуля, и журнал отображается, пуля просто никогда не появляется. Что я делаю не так?

1 Ответ

0 голосов
/ 06 мая 2020

Я случайно набрал ширину вместо длины в строке 126. Проблема решена.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...