4 thoughts on “Java Game Design – Making A Moving Player

  1. It says the Screen is not abstract and does not override abstract method ActionPerformed(ActionEvent) in ActionListener. Here is the code:

    package project;

    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;

    import javax.swing.JPanel;
    import javax.swing.Timer;

    public class Screen extends JPanel implements ActionListener, KeyListener{
    Timer t = new Timer(10,this);
    player p = new player(10,10,10,10,0,0);

    public Screen(){
    addKeyListener(this);
    setFocusable(true); //to use keyboard

    t.start();
    }

    public void keyPressed(KeyEvent arg0){
    p.tick();
    repaint();
    }
    public void paint(Graphics g){
    g.clearRect(0,0,getWidth(),getHeight());

    }
    public void keyPressed(KeyEvent k){
    switch(k.getKeyCode()){
    case KeyEvent.VK_D:
    p.setDx(1);
    break;
    case KeyEvent.VK_S:
    p.setDy(1);
    break;
    case KeyEvent.VK_A:
    p.setDx(-1);
    break;
    case KeyEvent.VK_W:
    p.setDy(-1);
    break;
    }
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *