这是我学校拓展题遇到的难题,最后两个要求实在是完成不了,请问有没有老程序员能帮帮我
问题遇到的现象和发生背景
When you have completed the rocket example from the 'This' lecture, attempt the following extensions.
Change 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.
Add 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;
}
}
运行结果及报错内容
我总是只有一个可以移动的火箭,但是我子弹老是发射不出去,有时只能稍微挪动一下,请老程序员帮帮忙看一下
我的解答思路和尝试过的方法
我想要达到的结果
我想要有子弹能够射出去然后火箭也可以随便移动的效果