繁体   English   中英

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

[英]Firing a Bullet Towards Mouse when Mouse is Clicked

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

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

这是我的绘画方法

以下是计算从子弹到鼠标的轨迹的方法。

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所以本质上就是这样。

暂无
暂无

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

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