Skip to content
Advertisement

How to make different animations for keyboard input

I’m making a top-down shooter game in java where I have a stationary player who can turn side to side and can shoot a gun at enemies that move to the center(where the player is). I have made different faces for each side when my player turns, but I’m not able to figure out how to switch the faces when I use the arrow keys. Here is my main panel:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

public class FinalPanel extends JPanel
{
   private static final int FRAME = 1600;
   private static final Color BACKGROUND = new Color(255, 255, 255);
   private Player player;
   private ImageIcon faces;
   private ImageIcon playerFace = new ImageIcon("PlayerUpImage.png");

   private BufferedImage myImage;
   private Graphics myBuffer;
   private Timer timer;   
   
   public FinalPanel()
   {
      myImage =  new BufferedImage(FRAME, FRAME, BufferedImage.TYPE_INT_RGB);
      myBuffer = myImage.getGraphics();

      

      addKeyListener(new Key());
      setFocusable(true);  
   }
   
   private class Listener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         ImageIcon player = new ImageIcon("PlayerUpImage.png");
         myBuffer.drawImage(player.getImage(), 800, 800, null);
         repaint();
      }
   }


   public void paintComponent(Graphics g)
   {
      super.paintComponenet(g);
      g.drawImage(myImage, 0, 0, getWidth(), getHeight(), null);
   }
   private class Key extends KeyAdapter//The arrow keys make the player change directions and it replaces his face with a different animation to show the change in direction
   {
      public void keyPressed(KeyEvent e)
      {
         if(e.getKeyCode() == KeyEvent.VK_W)
         {
            playerFace = new ImageIcon("PlayerUpImage.png");
         }
         if(e.getKeyCode() == KeyEvent.VK_S)
         {
            playerFace = new ImageIcon("PlayerDownImage.png");
         }
         if(e.getKeyCode() == KeyEvent.VK_A)
         {
            playerFace = new ImageIcon("PlayerLeftImage.png");
         }
         if(e.getKeyCode() == KeyEvent.VK_D)
         {
            playerFace = new ImageIcon("PlayerRightImage.png");
         }
         repaint();
      }
   }
}

Player class:

import java.awt.*;
import javax.swing.*;

public class Player
{
   //Private fields for player class
   private int myX;
   private int myY;
   private int myXWidth;
   private int myYWidth;
   
   public Player()
   {
      myX = 775;
      myY = 775;
      myXWidth = 50;
      myYWidth = 50;
   }
   
   public Player(int x, int y, int xWidth, int yWidth)
   {
      myX = x;
      myY = y;
      myXWidth = xWidth;
      myYWidth = yWidth;
   }
   
   //I borrowed this health code from: https://stackoverflow.com/questions/9834609/how-to-make-a-player-class-that-holds-lives-inheritance//
   int liveCount = 10;

   public boolean damage() {
      --liveCount;
      return isDead();
   }

   public boolean isDead() {
      return liveCount < 1;
   }

   public void boostLives(int moreLives) {
      liveCount += moreLives;
   }

   //Accessor Methods//
   public int getX() 
   { 
      return myX;
   }
   public int getY()      
   { 
      return myY;
   }
   public int getXWidth() 
   { 
      return myXWidth;
   }
   public int getYWidth() 
   { 
      return myYWidth;
   }
   //Modifier Methods//
   public void setX(int x)
   {
      myX = x;
   } 
   public void setY(int y)
   {
      myY = y;
   } 
   public void setXWidth(int xWidth)
   {
      myXWidth = xWidth;
   }
   public void setYWidth(int yWidth)
   {
      myYWidth = yWidth;
   }

}

Advertisement

Answer

I have some suggestions that might help:

Don’t use the variable faces, instead, have an ImageIcon called playerFace with a default direction of up

private ImageIcon playerFace = new ImageIcon(“PlayerUpImage.png”);

Your paint method could then look like this

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(playerFace.getImage(), 800, 800, null);
}

In your keyPressed method, all you have to do is change the player image and call the method repaint so that the panel calls the paintComponent method again:

public void keyPressed(KeyEvent e) {
//pressed d for example
    if(e.getKeyCode() == KeyEvent.VK_D)
        playerFace = new ImageIcon(“PlayerImageRight.png”);
    ... //if else’s for other keys

    repaint();
}

Preferably, you’d want to have playerFaces as an attribute of the Player class with a getter method to get the playerFace direction (so making playerFace a variable of Player). But this is to get you started

You’ll also want to set up a frame instead of using your current variable FRAME

public FinalPanel() {
    JFrame frame = new JFrame();
    frame.setSize(1600, 1600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(this);
    frame.setVisible(true);

    addKeyListener(new Key());
    setFocusable(true);
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement