m0_66826441 2022-02-04 18:37 采纳率: 100%
浏览 34
已结题

请问老程序员如何用javascript如何达成火箭喷出子弹的效果

这是我学校拓展题遇到的难题,最后两个要求实在是完成不了,请问有没有老程序员能帮帮我

问题遇到的现象和发生背景

W​hen you have completed the rocket example from the 'This' lecture, attempt the following extensions.

C​hange the look of the rocket to your own design.

​Alter the rocket to move more quickly or slowly.

Change the rocket object to use vectors instead of the x, y and thrust values.

A​dd sounds to the rocket - watch the game project sound video later in the topic first.

Add a fire method to the rocket object which will shoot a bullet out of the top of the rocket (tricky).

完成“This”课程中的火箭示例后,尝试以下扩展。
将火箭的外观更改为您自己的设计。
​改变火箭以更快或更慢的速度移动。
将火箭对象更改为使用矢量,而不是x、y和推力值。
为火箭添加声音。
_向火箭物体添加一种射击方法,这种方法会从火箭顶部射出一颗子弹(很棘手)_。
我就是最后一个让火箭顶部射出一个子弹的方法不会做,子弹偶尔会卡在火箭上面老是发不出去,我也不知道为什么

问题相关代码,请勿粘贴截图
var rocket; 
var baseLine;
var bulletVector;


function setup()
{
    createCanvas(800, 600);
    
    baseLine = height - 100;
    
    bulletVector = createVector(0,0);

     rocket =    {
        x: width/2,
        y: baseLine,
        v: createVector(width/2,baseLine),
        thrust: false, 
        moveLeft: false,
        moveRight: false,
        fire: false,
        fired: false,
        drawRocket: function(){
            fill(180)
            beginShape();
            vertex(this.v.x + 10, this.v.y + 60);
            vertex(this.v.x + 10, this.v.y);
            vertex(this.v.x + 15, this.v.y - 20);
            vertex(this.v.x + 20, this.v.y);
            vertex(this.v.x + 20, this.v.y + 60);
            endShape(CLOSE);

            fill(255, 0, 0);
            beginShape();
            vertex(this.v.x - 10, this.v.y + 60);
            vertex(this.v.x + 10, this.v.y + 30);
            vertex(this.v.x + 10, this.v.y + 60);
            endShape(CLOSE);

            beginShape();
            vertex(this.v.x + 40, this.v.y + 60);
            vertex(this.v.x + 20, this.v.y + 30);
            vertex(this.v.x + 20, this.v.y + 60);
            endShape(CLOSE);

            if (this.thrust)
            {
                fill(255, 150, 0);
                beginShape();
                vertex(this.v.x + 10, this.v.y + 60);
                vertex(this.v.x + 13, this.v.y + 80);
                vertex(this.v.x + 15, this.v.y + 70);
                vertex(this.v.x + 18, this.v.y + 80);
                vertex(this.v.x + 20, this.v.y + 60);
                endShape(CLOSE);
            }
            
            
        },
        moveRocket: function(){
            if (rocket.thrust && rocket.v.y > 0)
            {
                this.v.y -= 3;
            }
            else if (rocket.v.y < baseLine)
            {
                this.v.y += 3;
            }

            if (rocket.moveLeft && rocket.v.x > 0 && rocket.v.y != baseLine)
            {
                this.v.x -= 2;
            }

            if (rocket.moveRight && rocket.v.x < width && rocket.v.y != baseLine)
            {
                this.v.x += 2;
            }
            
        },

    };
    
}

function draw()
{
    //move the rocket
    rocket.moveRocket();
    background(10);
    
    //draw the rocket
    rocket.drawRocket();
    if(rocket.fire && rocket.fired == false){
        rocket.fired = true;
        
    }
    
    if(rocket.fired == true ){
        bulletVector.y -= 5;
    }
    ellipse(bulletVector.x, bulletVector.y,10);
 

    
}
    


function keyPressed()
{
    if (key == "W")
    {
        rocket.thrust = true;
    }

    if (key == "A")
    {
        rocket.moveLeft = true;
    }

    if (key == "D")
    {
        rocket.moveRight = true;
    }
    
    if (key == "Q"){
        rocket.fired = true;
    }
    
    
    
}


    

    

function keyReleased(){
    if(key == "W")
    {
       rocket.thrust = false;
    }
    
    if(key == "A")
    {
       rocket.moveLeft = false;
    }
    
    if(key == "D")
    {
       rocket.moveRight = false;
    }
    
    if(key == "Q"){
        rocket.fired = false;
    }
}

运行结果及报错内容

我总是只有一个可以移动的火箭,但是我子弹老是发射不出去,有时只能稍微挪动一下,请老程序员帮帮忙看一下

我的解答思路和尝试过的方法

img

我想要达到的结果

我想要有子弹能够射出去然后火箭也可以随便移动的效果

  • 写回答

2条回答 默认 最新

  • 关注
    var rocket; 
    var baseLine;
    var bulletVector;
     
     
    function setup()
    {
        createCanvas(800, 600);
        
        baseLine = height - 100;
        
        bulletVector = createVector(0,0);
     
         rocket =    {
            x: width/2,
            y: baseLine,
            v: createVector(width/2,baseLine),
            thrust: false, 
            moveLeft: false,
            moveRight: false,
            fire: false,
            fired: false,
            firesta: false,
            drawRocket: function(){
                fill(180)
                beginShape();
                vertex(this.v.x + 10, this.v.y + 60);
                vertex(this.v.x + 10, this.v.y);
                vertex(this.v.x + 15, this.v.y - 20);
                vertex(this.v.x + 20, this.v.y);
                vertex(this.v.x + 20, this.v.y + 60);
                endShape(CLOSE);
     
                fill(255, 0, 0);
                beginShape();
                vertex(this.v.x - 10, this.v.y + 60);
                vertex(this.v.x + 10, this.v.y + 30);
                vertex(this.v.x + 10, this.v.y + 60);
                endShape(CLOSE);
     
                beginShape();
                vertex(this.v.x + 40, this.v.y + 60);
                vertex(this.v.x + 20, this.v.y + 30);
                vertex(this.v.x + 20, this.v.y + 60);
                endShape(CLOSE);
     
                if (this.thrust)
                {
                    fill(255, 150, 0);
                    beginShape();
                    vertex(this.v.x + 10, this.v.y + 60);
                    vertex(this.v.x + 13, this.v.y + 80);
                    vertex(this.v.x + 15, this.v.y + 70);
                    vertex(this.v.x + 18, this.v.y + 80);
                    vertex(this.v.x + 20, this.v.y + 60);
                    endShape(CLOSE);
                }
                
                
            },
            moveRocket: function(){
                if (rocket.thrust && rocket.v.y > 0)
                {
                    this.v.y -= 3;
                }
                else if (rocket.v.y < baseLine)
                {
                    this.v.y += 3;
                }
     
                if (rocket.moveLeft && rocket.v.x > 0 && rocket.v.y != baseLine)
                {
                    this.v.x -= 2;
                }
     
                if (rocket.moveRight && rocket.v.x < width && rocket.v.y != baseLine)
                {
                    this.v.x += 2;
                }
                
            },
     
        };
        
    }
     
    function draw()
    {
        //move the rocket
        rocket.moveRocket();
        background(10);
        
        //draw the rocket
        rocket.drawRocket();
        
        if(rocket.firesta == true){
            bulletVector.y = rocket.v.y;
            bulletVector.x = rocket.v.x;
            rocket.firesta = false;
        }
        if (bulletVector.y > 0) {
            bulletVector.y -= 5;
        } else if(rocket.fired == true){
            rocket.firesta = true;
        }
        ellipse(bulletVector.x, bulletVector.y,10);
     
     
        
    }
        
     
     
    function keyPressed()
    {
        if (key == "W")
        {
            rocket.thrust = true;
        }
     
        if (key == "A")
        {
            rocket.moveLeft = true;
        }
     
        if (key == "D")
        {
            rocket.moveRight = true;
        }
        
        if (key == "Q"){
            rocket.fired = true;
            rocket.firesta = true;
        }
        
        
        
    }
     
     
        
     
        
     
    function keyReleased(){
        if(key == "W")
        {
           rocket.thrust = false;
        }
        
        if(key == "A")
        {
           rocket.moveLeft = false;
        }
        
        if(key == "D")
        {
           rocket.moveRight = false;
        }
        
        if(key == "Q"){
            rocket.fired = false;
        }
    }
     
    

    大致是这样.你代码不全,我也没有法运行调试

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 2月16日
  • 已采纳回答 2月8日
  • 创建了问题 2月4日

悬赏问题

  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因