单击鼠标时向鼠标发射子弹

时间:2019-05-04 02:27:13

标签: java

我正在创建一个自上而下的地牢游戏,当单击鼠标时,需要在鼠标位置发射子弹。我知道如何获取鼠标的坐标,但是我不确定如何在单击鼠标时向该位置发射子弹。

public void paint(Graphics g) 
    {//opens paint method
        super.paint(g);//allows for painting and

        g.drawImage(background, xBackground, yBackground, 1380, 720, this); //Background

        myCharacter(x, y, g); //Character Sprite
        spriteObjectX = x; //Sets the object x of the sprite equal to the 8 bit of the sprite
        spriteObjectY = y; //Sets the object y of the sprite equal to the 8 bit of the sprite

        sprite(g);
        wallOne(g);
        wallTwo(g);
        wallThree(g);
        wallFour(g);
        wallOneTop(g);
        wallOneBottom(g);
        wallOneLeft(g);
        wallOneRight(g);
        wallTwoTop(g);
        wallTwoBottom(g);
        wallTwoLeft(g);
        wallTwoRight(g);
        wallThreeTop(g);
        wallThreeBottom(g);
        wallThreeLeft(g);
        wallThreeRight(g);
        wallFourTop(g);
        wallFourBottom(g);
        wallFourLeft(g);
        wallFourRight(g);
        //
        sprite = new Rectangle(spriteObjectX, spriteObjectY, spriteObjectW, spriteObjectH);
        //
        wallOne = new Rectangle(wallOneX, wallOneY, wallOneW, wallOneH);
        wallTwo = new Rectangle(wallTwoX, wallTwoY, wallTwoW, wallTwoH);
        wallThree = new Rectangle(wallThreeX, wallThreeY, wallThreeW, wallThreeH);
        wallFour = new Rectangle(wallFourX, wallFourY, wallFourW, wallFourH);
        //
        wallOneTop = new Rectangle(wallOneX, wallOneY, wallOneW, 1);
        wallOneBottom = new Rectangle(wallOneX, (wallOneY + wallOneH), wallOneW, 1);
        wallOneLeft = new Rectangle(wallOneX, wallOneY, 1, wallOneH);
        wallOneRight = new Rectangle((wallOneX + wallOneW), wallOneY, 1, wallOneH);

        wallTwoTop = new Rectangle(wallTwoX, wallTwoY, wallTwoW, 1);
        wallTwoBottom = new Rectangle(wallTwoX, (wallTwoY + wallTwoH), wallTwoW, 1);
        wallTwoLeft = new Rectangle(wallTwoX, wallOneY, 1, wallTwoH);
        wallTwoRight = new Rectangle((wallTwoX + wallTwoW), wallTwoY, 1, wallTwoH);

        wallThreeTop = new Rectangle(wallThreeX, wallThreeY, wallThreeW, 1);
        wallThreeBottom = new Rectangle(wallThreeX, (wallThreeY + wallThreeH), wallThreeW, 1);
        wallThreeLeft = new Rectangle(wallThreeX, wallThreeY, 1, wallThreeH);
        wallThreeRight = new Rectangle((wallThreeX + wallThreeW), wallThreeY, 1, wallThreeH);

        wallFourTop = new Rectangle(wallFourX, wallFourY, wallFourW, 1);
        wallFourBottom = new Rectangle(wallFourX, (wallFourY + wallFourH), wallFourW, 1);
        wallFourLeft = new Rectangle(wallFourX, wallFourY, 1, wallFourH);
        wallFourRight = new Rectangle((wallFourX + wallFourW), wallFourY, 1, wallFourH);

        mouseX = MouseInfo.getPointerInfo().getLocation().x; //Finding the x of the mouse
        mouseY = MouseInfo.getPointerInfo().getLocation().y; //Finding the y of the mouse

        g.setColor(Color.red);

        g.drawLine(x + 90, y + 50, MouseInfo.getPointerInfo().getLocation().x - 8, MouseInfo.getPointerInfo().getLocation().y - 30); //Drawing a line from the player's gun to the mouse

        repaint();//allows for repainting to look normal
}//close paint method

这是我的绘画方法

1 个答案:

答案 0 :(得分:0)

这是您计算从项目符号到鼠标的轨迹的方法。

Point get_trajectory(Point mouse,Point bullet){
    return new Point(mouse.getX()-bullet.getX(),mouse.getY()-bullet.getY());
}

在您的游戏循环中,您应该调用

void bullet_go_to(Point point){
    Point trajectory=get_trajectory(mouseLocation,bullet);
    bullet.x+=trajectory.getX();
    bullet.y+=trajectory.getY();
}

您知道如何找到mouseLocation,从本质上讲就是这样。