diff --git a/src/main/java/dev/dvorakmortonxd/Game.java b/src/main/java/dev/dvorakmortonxd/Game.java new file mode 100644 index 0000000..59d15f6 --- /dev/null +++ b/src/main/java/dev/dvorakmortonxd/Game.java @@ -0,0 +1,52 @@ +package dev.dvorakmortonxd; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +public class Game extends JPanel implements ActionListener { + + // this defines the window dimensions + public final int windowWidth = 800; + public final int windowHeight = 600; + + // sets the fps to 30 (1000/30 = around 33) + public Timer timer = new Timer(33, this); + + // defines the current scene + public String scene = "main_menu"; + + // routes to Update.java (actions) + @Override + public void actionPerformed(ActionEvent e) { + Update.performPerTick(this); + } + + // routes to Update.java (rendering) + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + Update.paint(this, g); + } + + // sets up the window and everything + public Game() { + //sets up the window size + setPreferredSize(new Dimension(windowWidth, windowHeight)); + Input in = new Input(this); + addKeyListener(in); + setBackground(new Color(45, 73, 45)); + setFocusable(true); + timer.start(); + } + + public static void main(String[] args) { + JFrame frame = new JFrame("My cool retro game"); + frame.setResizable(false); // NOT resizable + frame.add(new Game()); + frame.pack(); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // enables the X button in the title bar + frame.setLocationRelativeTo(null); // centers the window + frame.setVisible(true); // duh + } +} \ No newline at end of file diff --git a/src/main/java/dev/dvorakmortonxd/Input.java b/src/main/java/dev/dvorakmortonxd/Input.java new file mode 100644 index 0000000..89307d8 --- /dev/null +++ b/src/main/java/dev/dvorakmortonxd/Input.java @@ -0,0 +1,28 @@ +package dev.dvorakmortonxd; + +import java.awt.event.*; + +public class Input implements KeyListener { + private final Game m; + public Input(Game m) { + this.m = m; + } + // what happens when you PRESS a key on your keyboard + @Override + public void keyPressed(KeyEvent e) { + switch (e.getKeyCode()) { + case KeyEvent.VK_ESCAPE -> System.exit(0); + case KeyEvent.VK_SPACE -> System.out.println("Hello world!"); + } + } + // what happens when you RELEASE a key on your keyboard + @Override + public void keyReleased(KeyEvent e) { + switch (e.getKeyCode()) { + case KeyEvent.VK_SPACE -> System.out.println("Goodbye world!"); + } + } + // I don't know what this is, or why it exists, I'll never use it but if this wasn't in the code, it'll throw an error. + @Override + public void keyTyped(KeyEvent e) {} +} diff --git a/src/main/java/dev/dvorakmortonxd/Main.java b/src/main/java/dev/dvorakmortonxd/Main.java deleted file mode 100644 index 8297e2a..0000000 --- a/src/main/java/dev/dvorakmortonxd/Main.java +++ /dev/null @@ -1,17 +0,0 @@ -package dev.dvorakmortonxd; - -//TIP To Run code, press or -// click the icon in the gutter. -public class Main { - public static void main(String[] args) { - //TIP Press with your caret at the highlighted text - // to see how IntelliJ IDEA suggests fixing it. - System.out.printf("Hello and welcome!"); - - for (int i = 1; i <= 5; i++) { - //TIP Press to start debugging your code. We have set one breakpoint - // for you, but you can always add more by pressing . - System.out.println("i = " + i); - } - } -} \ No newline at end of file diff --git a/src/main/java/dev/dvorakmortonxd/update.java b/src/main/java/dev/dvorakmortonxd/update.java new file mode 100644 index 0000000..2192f98 --- /dev/null +++ b/src/main/java/dev/dvorakmortonxd/update.java @@ -0,0 +1,12 @@ +package dev.dvorakmortonxd; + +import java.awt.*; + +public class Update { + static void performPerTick(Game m) { + m.repaint(); + } + static void paint(Game m, Graphics g) { + Toolkit.getDefaultToolkit().sync(); + } +}