I am trying to make a particular "Pacman Game".
Basically the main objects are: pacman and fruit.
The background image is a picture of a certain map, meaning the game is bound by an exterior frame.
The frame itself is set in pixels which I convert to coordinates (Conversion of lat/lng coordinates to pixels on a given map (with JavaScript)).
coordinates have the following parameters:
public Point3D(double x,double y,double z)
{
_x=x;// latitude
_y=y;// longtitude
_z=z;// altitude
}
public boolean isValid_GPS_Point(Point3D p)
{
return (p.x()<=180&&p.x()>=-180&&p.y()<=90&&p.y()>=-90&&p.z()>=-450);
}
Fruits are static objects that do not move.
All pacmen have a universal speed of 1 meter per second and an eating radius of 1 meter, meaning every fruit in the eating radius of the particular pacman will be eaten and removed from fruit list(The 'Eating Radius' is more like a third dimensional sphere having a 1 meter radius).
Pacmen movement is determined by an algorithm (Algorithm: using a list of fruits and pacmen, we calculate the distance of 2 objects from both lists, taking the minimal distance needed for a specific pacman to travel.
Distance Formula:
- Determining the vector by 2 GPS points on map.
- Using square function on the vector:
sqrt(delta(x)^2+delta(y)^2+delta(z)^2)
In terms of Gui:
package gui;
import java.awt.Graphics;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class MainWindow extends JFrame implements MouseListener
{
public BufferedImage myImage;
public MainWindow()
{
initGUI();
this.addMouseListener(this);
}
private void initGUI()
{
MenuBar menuBar = new MenuBar();
Menu File = new Menu("File");
Menu Run=new Menu("Run");
Menu Insert=new Menu("Insert");
MenuItem New=new MenuItem("New");
MenuItem Open = new MenuItem("Open");
MenuItem Save=new MenuItem("Save");
MenuItem start=new MenuItem("start");
MenuItem stop=new MenuItem("stop");
MenuItem packman=new MenuItem("packman");
MenuItem fruit=new MenuItem("fruit");
menuBar.add(File);
menuBar.add(Run);
menuBar.add(Insert);
File.add(New);
File.add(Open);
File.add(Save);
Run.add(start);
Run.add(stop);
Insert.add(packman);
Insert.add(fruit);
this.setMenuBar(menuBar);
try {
myImage = ImageIO.read(new File("C:\\Users\\Owner\\Desktop\\Matala3\\Ariel1.png"));//change according to your path
} catch (IOException e) {
e.printStackTrace();
}
}
int x = -1;
int y = -1;
public void paint(Graphics g)
{
g.drawImage(myImage, 0, 0, this);
if(x!=-1 && y!=-1)
{
int r = 10;
x = x - (r / 2);
y = y - (r / 2);
g.fillOval(x, y, r, r);
}
}
@Override
public void mouseClicked(MouseEvent arg) {
System.out.println("mouse Clicked");
System.out.println("("+ arg.getX() + "," + arg.getY() +")");
x = arg.getX();
y = arg.getY();
repaint();
}
@Override
public void mouseEntered(MouseEvent arg0) {
System.out.println("mouse entered");
}
@Override
public void mouseExited(MouseEvent arg0) {
System.out.println("mouse exited");
}
@Override
public void mousePressed(MouseEvent arg0) {
System.out.println("mouse Click pressed");
}
@Override
public void mouseReleased(MouseEvent arg0) {
System.out.println("mouse Click released");
}
}
Running it from this class:
package gui;
import javax.swing.JFrame;
import Geom.Point3D;
import gis.Fruit;
public class Main
{
public static void main(String[] args)
{
MainWindow window = new MainWindow();
window.setVisible(true);
window.setSize(window.myImage.getWidth(),window.myImage.getHeight());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
My main goal is after that after I place my objects(pacmens and fruits) on the map it should somehow look like this: https://media.giphy.com/media/5UDHI0JLxBBElUFuCs/giphy.gif
I'm looking for a general direction for how to make my packmen move like this and not stay stationary.