Hi my question pretains to Java programing What I am creatin

Hi my question pretains to Java programing, What I am creating is a multi lab pacman type game.

This part one covers movement. I basically need my code below to meet the labs requiremnts. IE I need this game to allow two players to move and i think use a construtor to pass the movement through the player classe.

Thanks for the help

included is some codes

import javax.swing.JFrame;

public class MainFrame {

   public static void main(String[] args)
   {
       // create the frame
       JFrame myFrame = new JFrame(\"Platformer\");
      
       // set up the close operation
       myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       // create panel
       MainPanel myPanel = new MainPanel();
       // add panel
       myFrame.getContentPane().add(myPanel);
       // pack
       myFrame.pack();
       // set visibility to true
       myFrame.setVisible(true);
   }
}

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class MainPanel extends JPanel implements KeyListener{

   int x = 0;
   int y = 0;
   ImageIcon myIcon = new ImageIcon(\"./src/TreasureChest.png\");
   public MainPanel()
   {
       setPreferredSize(new Dimension(1000,1000));
       addKeyListener(this);
       setFocusable(true);
   }

   public void paintComponent(Graphics page)
   {
       super.paintComponent(page);
       page.drawImage(myIcon.getImage(), x, y, null);
   }
     
   @Override
   public void keyPressed(KeyEvent arg0) {
       int keyCode = arg0.getKeyCode();
      
       if (keyCode == KeyEvent.VK_LEFT)
       {

           x -= 100;
       }
       else if (keyCode == KeyEvent.VK_RIGHT)
       {

           x += 100;
       }
       else if (keyCode == KeyEvent.VK_UP)
       {

           y -= 100;
       }
       else if (keyCode == KeyEvent.VK_DOWN)
       {

           y += 100;
       }
       repaint();
      
   }

   @Override
   public void keyReleased(KeyEvent arg0) {
       // TODO Auto-generated method stub
      
   }

   @Override
   public void keyTyped(KeyEvent arg0) {
       // TODO Auto-generated method stub
      
   }

This is the overall goal for the project for this part of the lab I basically just neeed to have two player movemet across the Jpanel so I believe I just need to use a consturer to make two instances of the player class that uses different movement

Solution

import javax.swing.JFrame;

public class MainFrame { public static void main(String[] args)

{

// create the frame JFrame myFrame = new JFrame(\"Platformer\");

// set up the close operation myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// create panel

       MainPanel myPanel = new MainPanel();
       // add panel
       myFrame.getContentPane().add(myPanel);
       // pack
       myFrame.pack();
       // set visibility to true
       myFrame.setVisible(true);
   }
}

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class MainPanel extends JPanel implements KeyListener{

   int x = 0;
   int y = 0;
   ImageIcon myIcon = new ImageIcon(\"./src/TreasureChest.png\");
   public MainPanel()
   {
       setPreferredSize(new Dimension(1000,1000));
       addKeyListener(this);
       setFocusable(true);
   }

   public void paintComponent(Graphics page)
   {
       super.paintComponent(page);
       page.drawImage(myIcon.getImage(), x, y, null);
   }
     
   @Override
   public void keyPressed(KeyEvent arg0) {
       int keyCode = arg0.getKeyCode();
      
       if (keyCode == KeyEvent.VK_LEFT)
       {

           x -= 100;
       }
       else if (keyCode == KeyEvent.VK_RIGHT)
       {

           x += 100;
       }
       else if (keyCode == KeyEvent.VK_UP)
       {

           y -= 100;
       }
       else if (keyCode == KeyEvent.VK_DOWN)
       {

           y += 100;
       }
       repaint();
      
   }

Hi my question pretains to Java programing, What I am creating is a multi lab pacman type game. This part one covers movement. I basically need my code below to
Hi my question pretains to Java programing, What I am creating is a multi lab pacman type game. This part one covers movement. I basically need my code below to
Hi my question pretains to Java programing, What I am creating is a multi lab pacman type game. This part one covers movement. I basically need my code below to

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site