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
这是我的绘画方法
解决方案
这是计算从子弹到鼠标的轨迹的方法。
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
,所以本质上就是这样。
推荐阅读
- java - 与 JVM 规范中的 classesInOtherPkgWithProtectedMember 混淆,以及更普遍的访问受保护成员的规则
- javascript - 为什么在我实际设置它的值之前似乎设置了 javascript 变量的值?
- python-3.x - Python AttributeError:'str'对象没有属性'read'
- sql - PostgreSQL unnest, int4range with where 子句
- reactjs - 使用钩子简单验证表单,React
- reactjs - 将 JS 状态保存到无序列表
- c - BackTracking 功能未按预期工作
- mongodb - Mongo Distinct Index for Dot Notated
- angular - 以角度模拟可观察对象
- firebase - Firebase CLI:TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。接收类型未定义