Я работал над игрой, в которой мяч стреляет, когда я щелкаю мышью. Я не слишком опытен в 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;
}
}