简体   繁体   English

我该如何发射移动的子弹?

[英]How can I shoot a moving bullet?

Hi I'm new here and also in the programming world, so, I have a little problem: I'm making a simple game engine in JS because I want to replicate the original Space Invaders game, the problem is just the bullet is not moving, only appears and disappears, like a laser shot. 嗨,我是这里的新手,也是编程界的新手,所以我有一个小问题:我要用JS制作一个简单的游戏引擎,因为我想复制原始的《太空入侵者》游戏,问题只是子弹不是移动,只会出现和消失,就像激光一样。 I want to have a moving bullet. 我想拥有一颗动人的子弹。 Anyone can help me? 有人可以帮助我吗? PS I don't wanna use object oriented programming here, I want to make the whole game in a single .js file. PS:我不想在这里使用面向对象的编程,我想将整个游戏制作在一个.js文件中。

console.log('GAME LOADED//SPACE INVADERS');

////////////////////////////////////////////////

let screen = document.createElement('canvas');
screen.width = 800;
screen.height = 900;
document.body.appendChild(screen);
document.addEventListener('keydown', keyDown, false);
document.addEventListener('keyup', keyUp, false);
let ctx = screen.getContext('2d');

////////////////////////////////////////////////

let x = screen.width / 2;
let y = screen.height / 2;
let bulletY = screen.height - 20;
let bulletSpeed = 10;
let bulletLifeTime = 3;
let size = 20;
let sWidth = screen.width;
let sHeight = screen.height;
let columns = screen.width / size;
let rows = screen.height / size;
let speed = 5;
let right = false;
let left = false;
let up = false;
let down = false;
let shot = false;
let gravity = 10;
let jumpForce = 40 ;

/////////////////////////////////////////////////

function keyDown() {
    switch(event.keyCode) {
        case 39: right = true;
                 break;
        case 37: left = true;
                 break;
        //case 40: up = true;
                 //break;
        //case 38: down = true;
                 //break;
        case 32: shot = true;  
                 break;        
    }
}

function keyUp() {
    switch(event.keyCode) {
        case 39: right = false;
                 break;
        case 37: left = false;
                 break;
        //case 40: up = false;
                 //break;
        //case 38: down = false;
                 //break;
        case 32: shot = false; 
                 break;        
    }
}

function move() {
    if(right == true) {
        x += 1 * speed;
    } else if(left == true) {
        x -= 1 * speed;
    }
    //if(up == true) {
        //y += 1 * speed;
    //} else if(down == true) {
        //y -= 1 * speed;
    //}
}

 function player() {
    ctx.fillStyle = '#1CE80D';
    ctx.fillRect(x, y, size, size);
    ctx.fillRect(x+10, y, size, size);
    ctx.fillRect(x+20, y, size, size);
    ctx.fillRect(x-10, y, size, size);
    ctx.fillRect(x-20, y, size, size);
    ctx.fillRect(x+5, y-1, 10, 10);
    ctx.fillRect(x+5, y-2, 10, 10);
    ctx.fillRect(x+5, y-3, 10, 10);
    ctx.fillRect(x+5, y-4, 10, 10);
    ctx.fillRect(x+5, y-5, 10, 10);
    ctx.fillRect(x+5, y-6, 10, 10);
    ctx.fillRect(x+5, y-7, 10, 10);
    ctx.fillRect(x+9, y-8, 2, 2);
    ctx.fillRect(x+9, y-9, 2, 2);
    ctx.fillRect(x+9, y-10, 2, 2);
    ctx.fillStyle = 'black';
    ctx.fillRect(x+35, y, 5, 5);
    ctx.fillRect(x-20, y, 5, 5);
}

function enemy1() {
    ctx.fillStyle = '#1CE80D';
    ctx.fillRect(444, 375, 5, 5);
    ctx.fillRect(411, 375, 5, 5);
    ctx.fillRect(416, 380, 5, 5);
    ctx.fillRect(439, 380, 5, 5);
    ctx.fillRect(410, 385, 40, 5);
    ctx.fillRect(405, 390, 50, 5);
    ctx.fillRect(400, 395, 60, 5);
    ctx.fillRect(400, 400, 60, 5);
    ctx.fillRect(400, 405, 5, 5);
    ctx.fillRect(455, 405, 5, 5);
    ctx.fillRect(410, 405, 40, 5);
    ctx.fillStyle = '#001119';
    ctx.fillRect(415, 405, 30, 5);
    ctx.fillStyle = '#1CE80D';
    ctx.fillRect(415, 410, 30, 5);
    ctx.fillStyle = '#001119';
    ctx.fillRect(440, 390, 5, 5);
    ctx.fillRect(414, 390, 5, 5);
    ctx.fillRect(428, 410, 4, 5);  
}

function draw() {
    ctx.fillStyle = '#001119';
    let background = ctx.fillRect(0, 0, sWidth, sHeight);
    player();

    ctx.fillStyle = '#EAEEB7';
    let wall1 = ctx.fillRect(0, 0, 10, sHeight);
    let wall2 = ctx.fillRect(0, 0, sHeight, 10);
    let wall3 = ctx.fillRect(0, sHeight - 10, sWidth, 10);
    let wall4 = ctx.fillRect(sWidth - 10, 0, 10, sHeight);
}

function gravityForce() {
    y += gravity;
}

function jumpAct() {
    if(jump == true) {
        y -= jumpForce;
    } else if(jump == false) {
        gravityForce();
   }
}

function bulletDraw() {
    let bulletX = x;
    ctx.fillStyle = '#1CE80D';
    ctx.fillRect(bulletX + 9, bulletY, 1, 8);
}

function bulletPos() {
    bulletY -= bulletSpeed;    
}

function attack() {
    if(shot == true) {
        for(let i = screen.height; i > 0; i--) {
        bulletDraw();
        bulletPos();
        }
        console.log('shooting');
    } else if(shot == false) {
        bulletY = 880;
    }
}

function gameLoop() {
    ctx.clearRect(0, 0, screen.width, screen.height);
    draw();
    move(); 
    gravityForce();
    attack(); 
    enemy1();
    bulletPos();
    requestAnimationFrame(gameLoop); 
    if(x > screen.width - size - 30) {
        x = 30
    } else if(x < 30) {
        x = screen.width - 50;
    }
    if(y > screen.height - size - 10) {
        y -= (size - 10);
    } else if(y < 0) {
        y += size;
    } 
}
////////////////////////////////////////////////

requestAnimationFrame(gameLoop);

I think you have to manage both timer interval and canvas redraw function. 我认为您必须同时管理计时器间隔和画布重绘功能。 I don't thing it is a good idea to redraw the entire canvas in each interval, instead you can redraw only the required canvas area. 我没关系在每个间隔重新绘制整个画布是个好主意,相反,您只能重新绘制所需的画布区域。 I have modified your code with setTimeout() function without redraw the entire canvas. 我已经使用setTimeout()函数修改了您的代码,而没有重绘整个画布。 You can see the complete code in this fiddler path https://jsfiddle.net/SyamKumarD/knyt7shf/18/ 您可以在此提琴手路径中看到完整的代码https://jsfiddle.net/SyamKumarD/knyt7shf/18/

Some of the key code you need to check 您需要检查的一些关键代码

1. setTimeout() method
2. hitTarget() method and its instantiation
3. Key down and up events

Hope this will helps 希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM