Search code examples
javakeypress

need help taking my key press listeners out of main


I have started to create a game where you can move freely. you move by using your keyboard I have it working but all I want to do is to take it out of main so I can call if multiply times from different classes. I have tried to put it in a public static void and call that method from main but it doesn't work. my code is provided below. I have been stuck on this for a long time know.

I have a picture in the frame to make it visible the picture is named 1.

import java.awt.event.KeyEvent;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.*;
public class StartGame {
    public static void main(String[] argv) throws Exception {
        JFrame MainFrame = new JFrame();
        MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MainFrame.setSize(1210, 700);
        MainFrame.setLocation(new java.awt.Point(150, 30));
        MainFrame.setLayout(null);
        MainFrame.setFocusable(true);
        MainFrame.setFocusTraversalKeysEnabled(true);
        MainFrame.setIconImage(new ImageIcon("images\\sword.png").getImage());
        JLabel thing = new JLabel();
        thing.setIcon(new ImageIcon("image\\1.gif"));
        thing.setBounds(300, 300, thing.getPreferredSize().width, thing.getPreferredSize().height);

        MainFrame.addKeyListener(new java.awt.event.KeyListener() {
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
                    if (0 < thing.getY()) { 
                        thing.setLocation(thing.getX(), thing.getY() - 10);
                    }
                }
                if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) {
                    if (thing.getY() < 570) {
                        thing.setLocation(thing.getX(), thing.getY() + 10);
                    }
                }
                if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D) {
                    if (thing.getX() < 1110) {
                        thing.setLocation(thing.getX()+ 10, thing.getY());
                    }
                }
                if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A) {        
                    if (0 < thing.getX()) {
                        thing.setLocation(thing.getX() - 10, thing.getY());
                    }
                }

            }

            public void keyTyped(KeyEvent e) {

            }

            public void keyReleased(KeyEvent e) {


            }
        });

        MainFrame.add(thing);
        MainFrame.setVisible(true);
    }
}

Solution

  • First you need to use a game loop pattern. The game loop pattern is a loop that process the game events in a specific order. First you processes input, update objects and last render the screen. This image is a example of that rendering:

    enter image description here

    If you don't follow this pattern right in the beginning of your project, latter you will find more difficulties to handle.

    With this pattern you can create a class Game with a method update that calls an Input handle class. With this you can move the input handle to another class that you can create. Some example:

    class Game {
    
        void update(){
            InputManager.handleInput();
            updateObjects();
            render();
    
        }
    }
    
    
    class InputManager {
    
        void handleInput(){
            //do whatever you like
    
        }
    
    }
    

    Please read this blog to find more info about it: https://gamesprogramminggroup02.wordpress.com/2016/03/04/design-patterns-game-loop/