Generelt set skal du kun være i din game-loop mens du spiller. når du er udenfor spillet - fx. i menuer behøver du ikke være i gameloop, når man så trykker 'r' kalder du så den metode som indeholder dit gameloop
Indtil videre har jeg ingen menu, kun spil.
Har ikke lavet nogle Threads - har lavet det så hvert "stage" i spillet er en JPanel, som så bliver added til JFrame, alt efter hvilket "stage" spilleren befinder sig i. Hvis I vil se al' koden, then here you go
Frame.java
- import java.awt.Image;
- import java.io.File;
- import java.io.IOException;
-
- import javax.imageio.ImageIO;
- import javax.swing.*;
-
- public class Frame extends JFrame
- {
- // Window variables
- public int windowx, windowy;
- Image taskbarIcon;
-
- // Custom objects
- Keyboard keyboardListener;
- GamePanel gamePanel;
- //MenuPanel menuPanel;
-
- // Game variables
- boolean menu, game, gameOver, programStarted;
-
- ///////////////////////// !!!!!!!!!!!!!!!!!! CONSTRUCTOR !!!!!!!!!!!!!!!!!! ////////////////////
- public Frame()
- {
- windowx = 600;
- windowy = 400;
-
- // Inits our shit
- init();
-
- // Shit for the window
- this.setVisible(true);
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- this.setSize(windowx, windowy);
- this.setFocusable(true); // Make sure you can put a keyboardListener
- this.addKeyListener(keyboardListener); // Adds the custom keyboardlistener
- System.out.println("Succesfully added Keylistener");
-
- //Taskbaricon
- this.setIconImage(taskbarIcon);
- System.out.println("Succesfully added an icon to the taskbar");
-
- this.add(gamePanel);
- gamePanel.run();
- }
-
- ///////////////////////// Game METHOD ////////////////////
- public void game()
- {
- while(programStarted == true)
- {
- try{ Thread.sleep(50); }catch(Exception e){} // Sets the FPMS
-
- if(menu == true)
- {
-
- }
- if(game == true)
- {
- this.add(gamePanel); // Adds the game
- gamePanel.run(); // Runs the game :i
- }
- else if(game == true)
- {
- }
- }
- }
-
- ///////////////////////// INIT METHOD (Only run on start)////////////////////
- public void init()
- {
- menu = true;
- programStarted = true;
-
- gamePanel = new GamePanel(this);
- keyboardListener = new Keyboard(gamePanel, this);
-
- //Images
- try
- {
- taskbarIcon = ImageIO.read(new File("res/TaskbarIcon.png"));
- }
- catch (IOException e)
- {
- System.out.println("Couldn't init pictures");
- }
- }
-
- public Frame getObject()
- {
- return this;
- }
-
- ///////////////////////// MAIN METHOD //////////////////////
- public static void main(String args[])
- {
- Frame main = new Frame();
- }
- }
GamePanel.java
- import java.awt.*;
- import java.io.File;
- import java.io.IOException;
- import java.util.Random;
-
- import javax.imageio.ImageIO;
- import javax.swing.*;
- /**
- * Game DotMS
- * @author Jacob Andreas Sneding Rohde
- * @version 1.0
- */
- public class GamePanel extends JPanel
- {
- // Custom objects
- Frame frame;
-
- // Lib objects
- Random random;
-
- // Game variables
- boolean gameOver;
- int mousex, mousey; // Mouse POS
- int ballposx, ballposy, ballsizex, ballsizey; // Ball variables
- int cheeseposx, cheeseposy, cheesesizex, cheesesizey, ratx, raty; // Cheese and rat variables
- int cheesesEaten;
-
- ///////////////////////// CONSTRUCTOR ////////////////////
- public GamePanel(Frame frame)
- {
- init(frame); // Inits our shit so we can use it
- this.setBackground(Color.BLACK);
- }
-
- ///////////////////////// RUN METHOD ////////////////////
- public void run()
- {
- while(gameOver == false)
- {
- //////////// SOME SHIT THAT MUST BE USED /////////////////////////
-
- reloadInit(); // Setting variables to new values
- this.repaint(); // Changing frame
-
- try // Must be used when using Threads
- {
- Thread.sleep(50); // Making the FPMS (Frames Per Milisecond)
- }
- catch (InterruptedException e) // Must be used when using Threads
- {
- e.printStackTrace();
- System.out.println("Yeah, something went wrong when trying to make this Thread sleep");
- }
- ///////////// HERE YOU CAN CUSTOMIZE ////////////////////////////
-
- if(areTheyTouching() == true)
- {
- cheesesEaten++;
- initCheese();
- }
- }
-
- System.out.println("gameOver");
- }
-
- //////////////////// INIT METHOD (Only run on start)////////////////////
- public void init(Frame frame)
- {
- // Custom objects
- this.frame = frame;
-
- // Lib objects
- random = new Random();
-
- // Getting the mouse pos
- mousex = MouseInfo.getPointerInfo().getLocation().x;
- mousey = MouseInfo.getPointerInfo().getLocation().y;
-
- // Ball
- ballposx = frame.windowx/2;
- ballposy = frame.windowy/2;
- ballsizex = 25;
- ballsizey = 25;
-
- //Cheese
- initCheese();
- cheesesizex = 12;
- cheesesizey = 12;
-
- cheesesEaten = 0;
- }
- //////////////////// RELOAD INIT METHOD////////////////////
- public void reloadInit()
- {
- // Getting the mouse pos
- mousex = MouseInfo.getPointerInfo().getLocation().x;
- mousey = MouseInfo.getPointerInfo().getLocation().y;
- }
-
- //////////////////// INITCHEESE METHOD////////////////////
- public void initCheese()
- {
- // Setting the cheese postition to random
- cheeseposx = random.nextInt(frame.windowx - 100);
- cheeseposy = random.nextInt(frame.windowy - 100);
- }
-
- ////////////////////BALL EAT CHEESE METHOD (CHEESY BALLS HIHI)////////////////////
- public boolean areTheyTouching()
- {
- if(ballposx + ballsizex > cheeseposx && ballposx < cheeseposx + cheesesizex && ballposy + ballsizey > cheeseposy && ballposy < cheeseposy + cheesesizex )
- {
- System.out.println("Ball just ate the cheese!");
-
- return true;
- }
- else
- {
- return false;
- }
- }
-
- //////////////////// PAINT METHOD////////////////////
- public void paint(Graphics g)
- {
- super.paintComponent(g); // Needed
-
- g.setColor(Color.RED);
- //g.drawString("Mouse pos: " + mousex + ", " + mousey, 5, 10);
- g.drawString("Cheeses eaten: " + cheesesEaten, 5, 10);
-
- //Ball
- g.fillRect(ballposx, ballposy, ballsizex, ballsizey);
-
- //Cheese
- g.setColor(Color.YELLOW);
- g.fillRect(cheeseposx, cheeseposy, cheesesizex, cheesesizey);
- }
- }
Keyboard.java
- import java.awt.event.KeyListener;
- import java.awt.event.KeyEvent;
- import java.util.HashSet;
- import java.util.Set;
- /**
- * Game DotMS
- * @author Jacob Andreas Sneding Rohde
- * @version 1.0
- */
- public class Keyboard implements KeyListener
- {
- // Custom objects
- GamePanel gamePanel;
- Frame frame;
-
- //////////////// CONSTRUCTOR /////////////////
- public Keyboard(GamePanel gamePanel, Frame frame)
- {
- this.gamePanel = gamePanel;
- this.frame = frame;
- }
-
- public void keyPressed(KeyEvent e)
- {
- char key = e.getKeyChar();
-
- if(key == 'w')
- {
- gamePanel.ballposy--;
- gamePanel.ballposy--;
- gamePanel.ballposy--;
- gamePanel.ballposy--;
- }
- else if(key == 's')
- {
- gamePanel.ballposy++;
- gamePanel.ballposy++;
- gamePanel.ballposy++;
- gamePanel.ballposy++;
- }
- else if(key == 'a')
- {
- gamePanel.ballposx--;
- gamePanel.ballposx--;
- gamePanel.ballposx--;
- gamePanel.ballposx--;
- }
- else if(key == 'd')
- {
- gamePanel.ballposx++;
- gamePanel.ballposx++;
- gamePanel.ballposx++;
- gamePanel.ballposx++;
- }
- }
-
- public void keyReleased(KeyEvent e)
- {
-
- }
-
- public void keyTyped(KeyEvent e)
- {
- char key = e.getKeyChar();
-
- // For restarting the game
- if(key == 'r')
- {
- gamePanel.gameOver = true;
- gamePanel.gameOver = false;
- gamePanel.init(frame);
- gamePanel.run();
- System.out.println("r was pressed");
- }
- }
-
- }