我正在做一个游戏,当我单击鼠标时,球会射击。我不太熟悉Java,所以很困惑。就像,球从屏幕的底部中间开始(因此,ballx和bally都有其位置),一旦鼠标单击一个方向,球便会朝该方向射击... 谢谢
package graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
public class Ballz extends LASSPanel
{
//Variables for the circle (Global Variables)
int circleX, circleY, circleDX, circleDY, circleSize, circleSpeed;
//Colors
Color circleColor;
Color backgroundColor;
Dimension size;
double mouseX;
double mouseY;
public Ballz()
{
//Ball Variables
circleX = 250;
circleY = 250;
circleSize = 15;
circleSpeed = (1);
circleColor = new Color (245,245,245);
backgroundColor = new Color (28,28,28);
size = new Dimension(0,0);
}
public void update()
{
//Get size of screen
getSize(size);
//Get mouse coordinates
mouseX = getMouseX();
mouseY = getMouseY();
//Call custom method to find angle
//Screen borders
if (circleX >= (size.width) - circleSize)
{
circleDX = -circleDX;
circleX = size.width-circleSize;
}
if (circleX <= 0)
{
circleDX = -circleDX;
circleX = 0;
}
if (circleY >= (size.height -circleSize))
{
circleDY = -circleDY;
circleY = size.height-circleSize;
}
if (circleY <= 0)
{
circleDY = -circleDY;
circleY = 0;
}
circleX += circleDX;
circleY += circleDY;
repaint();
}
public void paint(Graphics g)
{
//Game Colors
g.setColor(circleColor);
g.fillOval(circleX, circleY, circleSize, circleSize);
setBackground(backgroundColor);
}
//This is the code that will get the direction between two points that I give.
public double getAngle (int x1, int x2, int y1, int y2)
{
double Direction = 0;
double dx = y2-y1;
double dy = x2-x1;
Direction = Math.atan2(dy,dx);
return Direction;
}
}