Fruit class:
public class Fruit {
public double x = 0, y =0, fSize;
public boolean eaten = false;
public void drawFruit (Graphics g) {
g.setColor (Color.red);
g.fillOval ((int) x, (int) y, (int) fSize, (int) fSize);
}
}
Restart Class:
class Restart {
public Restart() {
PacmanPanel.xBase = 620;
PacmanPanel.xBase1 = 307;
PacmanPanel.yBase = 620;
PacmanPanel.yBase1 = 307;
PacmanPanel.score = 0;
Pressed.leftPressed = false;
Pressed.rightPressed = false;
Pressed.upPressed = false;
Pressed.downPressed = false;
Pressed.aPressed = false;
Pressed.dPressed = false;
Pressed.wPressed = false;
Pressed.sPressed = false;
for(int i=0; i<400;i++){
PacmanFrame.fruit[i].fSize = 5.0;
}
if(!PacmanPanel.time.isRunning()){
PacmanPanel.time.start();
}
}
}
Pressed Class:
public class Pressed implements KeyListener{
public static boolean leftPressed = false;
public static boolean rightPressed = false;
public static boolean upPressed = false;
public static boolean downPressed = false;
public static boolean aPressed = false;
public static boolean dPressed = false;
public static boolean wPressed = false;
public static boolean sPressed = false;
public static boolean openedYellow;
public static boolean openedBlue;
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
setState(keyCode, true);
}
@Override
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
setState(keyCode, false);
}
@Override
public void keyTyped(KeyEvent e) {}
void setState(int keyCode, boolean state) {
switch(keyCode) {
case KeyEvent.VK_LEFT:
leftPressed = state;
break;
case KeyEvent.VK_RIGHT:
rightPressed = state;
break;
case KeyEvent.VK_UP:
upPressed = state;
break;
case KeyEvent.VK_DOWN:
downPressed = state;
break;
case KeyEvent.VK_A:
aPressed = state;
break;
case KeyEvent.VK_D:
dPressed = state;
break;
case KeyEvent.VK_W:
wPressed = state;
break;
case KeyEvent.VK_S:
sPressed = state;
break;
}
}
}
PacmanPanel Class:
public class PacmanPanel extends JPanel implements ActionListener{ // This method allows us to paint.
public static Timer time;
public static int xBase = 620 ,yBase = 620;
public static int xBase1 = 307, yBase1 = 307;
int x = 0 , i = 0;
public static boolean restart;
public static int score = 0;
public static int scorePercent = 0;
public PacmanPanel(){
// Every time Timer executes, update everything
setSize(669, 699);
time = new Timer(15, this);
time.start();
}
private void updateAll() {
if (Pressed.rightPressed){
xBase += 2;
}
else if (Pressed.leftPressed){
xBase -= 2;
}
if (Pressed.upPressed){
yBase -= 2;
}
else if (Pressed.downPressed){
yBase += 2;
}
if (Pressed.dPressed){
xBase1 += 2;
}
else if (Pressed.aPressed){
xBase1 -= 2;
}
if (Pressed.wPressed){
yBase1 -= 2;
}
else if (Pressed.sPressed){
yBase1 += 2;
}
if(xBase > PacmanFrame.FrameW - 130)
xBase = 20;
if(xBase1 > PacmanFrame.FrameW - 130)
xBase1 = 20;
if(xBase < 0)
xBase = PacmanFrame.FrameW - 130;
if(xBase1 < 0)
xBase1 = PacmanFrame.FrameW - 130;
Rectangle bounds = new Rectangle(xBase, yBase, 24, 24);
bounds.createIntersection(bounds);
Rectangle bounds1 = new Rectangle(xBase1, yBase1, 24, 24);
bounds.createIntersection(bounds1);
if(bounds.intersects(bounds1)){
JOptionPane.showMessageDialog(null, "The Yellow pac man died!!!!\n" + " score = " + score+" Fruits"+
"\n Percentage = "+scorePercent+"%");
time.stop();
}
for(int i = 0; i < PacmanFrame.dotct; i++){
Rectangle bounds2 = new Rectangle((int) PacmanFrame.fruit[i].x, (int) PacmanFrame.fruit[i].y,
(int) PacmanFrame.fruit[i].fSize,(int) PacmanFrame.fruit[i].fSize);
bounds.createIntersection(bounds2);
if(bounds.intersects(bounds2) && PacmanFrame.fruit[i].fSize == 5.0){
score = score + 1;
scorePercent = score / 4;
System.out.println(score);
}
}
for(int i = 0; i < PacmanFrame.dotct; i++){
Rectangle bounds2 = new Rectangle((int) PacmanFrame.fruit[i].x, (int) PacmanFrame.fruit[i].y,
(int) PacmanFrame.fruit[i].fSize,(int) PacmanFrame.fruit[i].fSize);
bounds.createIntersection(bounds2);
if(bounds.intersects(bounds2)){
PacmanFrame.fruit[i].fSize = 0.0;
}
}
if(score == 400){
JOptionPane.showMessageDialog(null, "The yellow pacman WON!!!\n"+" score = "+score+" Fruits"+
"\n Percentage = "+scorePercent+"%");
time.stop();
}
Rectangle bounds3 = new Rectangle(103, 100, 10, 270);
bounds3.createIntersection(bounds3);
if((xBase < 103) && (bounds.intersects(bounds3))){
xBase -= 5;
}
if((xBase1 < 103) && (bounds1.intersects(bounds3))){
xBase1 -= 5;
}
if((xBase > 103)&& (bounds.intersects(bounds3))){
xBase += 5;
}
if((xBase1 > 103) && bounds1.intersects(bounds3)){
xBase1 += 5;
}
Rectangle bounds4 = new Rectangle(0, 650, 653, 10);
bounds4.createIntersection(bounds4);
if((yBase < 650)&& (bounds.intersects(bounds4))){
yBase -= 5;
}
if((yBase1 < 650) && bounds1.intersects(bounds4)){
yBase1 -= 5;
}
Rectangle bounds5 = new Rectangle(50, 403, 450, 10);
bounds5.createIntersection(bounds5);
if((yBase < 403)&& (bounds.intersects(bounds5))){
yBase -= 5;
}
if((yBase1 < 403) && bounds1.intersects(bounds5)){
yBase1 -= 5;
}
if((yBase > 403)&& (bounds.intersects(bounds5))){
yBase += 5;
}
if((yBase1 > 403) && bounds1.intersects(bounds5)){
yBase1 += 5;
}
Rectangle bounds6 = new Rectangle(0, 0, 653, 10);
bounds6.createIntersection(bounds6);
if((yBase > 0)&& (bounds.intersects(bounds6))){
yBase += 5;
}
if((yBase1 > 0) && bounds1.intersects(bounds6)){
yBase1 += 5;
}
Rectangle bounds7 = new Rectangle(585, 70, 10, 200);
bounds7.createIntersection(bounds7);
if((xBase > 585)&& (bounds.intersects(bounds7))){
xBase += 5;
}
if((xBase1 > 585) && bounds1.intersects(bounds7)){
xBase1 += 5;
}
if((xBase < 585)&& (bounds.intersects(bounds7))){
xBase -= 5;
}
if((xBase1 < 585) && bounds1.intersects(bounds7)){
xBase1 -= 5;
}
if(i% 2 == 0){
Pressed.openedYellow = true;
}
else{
Pressed.openedYellow = false;
}
i++;
repaint();
}
public void paintComponent (Graphics g) {
super.paintComponent(g);
setBackground(Color.black);
g.setColor (Color.yellow);
if (Pressed.openedYellow)
g.fillArc (xBase, yBase, 25, 25, 45, 270);
else
g.fillArc (xBase, yBase, 25, 25, 0, 360);
g.setColor (Color.blue);
if (Pressed.openedYellow)
g.fillArc (xBase1, yBase1,25,25,45,270);
else
g.fillArc (xBase1, yBase1,25,25,0,360);
g.setColor(Color.white);
g.fillRect(0, 650, 653, 10);
g.fillRect(0, 0, 653, 10);
g.fillRect(50, 403, 450, 10);
g.fillRect(103, 100, 10, 270);
g.fillRect(585, 70, 10, 200);
g.setColor(Color.ORANGE);
g.drawString("your score is "+ score, PacmanFrame.FrameW - 120, 100);
for (int i=0; i < PacmanFrame.dotct; i++) {
PacmanFrame.fruit[i].drawFruit(g);
}
}
public void actionPerformed(ActionEvent e) {
updateAll();
}
}
PacmanFrame Class:
public class PacmanFrame extends JFrame implements ActionListener, KeyListener{
// Have these set up so they can be seen everywhere
public static final int FrameW = 760;
public static final int FrameH = 720;
public static int place = 30, place1 = 30;
public static Fruit[] fruit = new Fruit[1000];
public static int dotct = 0;
PacmanPanel panel = new PacmanPanel();
JPanel panel1 = new JPanel();
public static boolean died = false;
public PacmanFrame() {
super("Pacman Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(FrameW, FrameH);
add(panel, BorderLayout.CENTER);
for(int i=0;i < 400;i++) {
fruit[i] = new Fruit();
fruit[i].x = place;
fruit[i].y = place1;
if(!fruit[i].eaten){
fruit[i].fSize = 5.0;
}
else
fruit[i].fSize = 0;
place += 30;
if(place == 630){
place1 += 30;
place = 30;
}
dotct++;
repaint();
}
JOptionPane.showMessageDialog(null, "1)\nYellow move with arrows(UP,DOWN,RIGHT,LEFT).\n"+
"2)\nBlue move with letters(W,S,D,A)\n"+
"3)\nThe Yellow pacman must eat as many as it can from the fruits before the blue one catch him.\n"+
"4)\nThe Blue pacman must kill the yellow one by coliding to him before the yellow one eat more Fruits.\n");
setVisible(true);
Pressed press = new Pressed();
setFocusable(true);
requestFocus();
addKeyListener(this);
addKeyListener(press);
JMenu menu2 = new JMenu("OPTIONS");
JMenuBar bar = new JMenuBar();
menu2.setForeground(Color.blue);
JPanel panel9 = new JPanel();
JButton jb1 = new JButton("RESTART (f2)");
JButton jb2 = new JButton("EXIT (f4)");
jb1.addActionListener(this);
jb2.addActionListener(this);
panel9.add(jb1);
panel9.add(jb2);
menu2.add(panel9);
bar.add(menu2);
add(bar, BorderLayout.NORTH);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new PacmanFrame();
}
});
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("RESTART (f2)")){
new Restart();
}
if(e.getActionCommand().equals("EXIT (f4)")){
JOptionPane pane = new JOptionPane(
"Are you sure yo want to Exit ?");
Object[] status = new String[] { "Yes", "No" };
pane.setOptions(status);
JDialog dialog = pane.createDialog(new JFrame(), "Confirmation");
dialog.show();
if (status[0].equals(pane.getValue())){
System.exit(0);
}
}
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_F4){
JOptionPane pane = new JOptionPane(
"Are you sure yo want to Exit ?");
Object[] status = new String[] { "Yes", "No" };
pane.setOptions(status);
JDialog dialog = pane.createDialog(new JFrame(), "Confirmation");
dialog.show();
if (status[0].equals(pane.getValue())){
System.exit(0);
}
}
if (e.getKeyCode() == KeyEvent.VK_F2){
new Restart();
}
}
public void keyReleased(KeyEvent e) {
}
}

New Topic/Question
Reply




MultiQuote








|