/** * This method is called once per frame and updates and renders everything in the game including * the gui. */ public void update() { // WE ONLY PERFORM GAME LOGIC // IF THE GAME IS UNDERWAY if (data.inProgress() || data.won() || data.lost()) { // data.updateDebugText(this); if (!data.isPaused()) { data.updateAll(this); } } // WE ALWAYS HAVE TO WORRY ABOUT UPDATING THE GUI updateGUI(); }
/** * Displays the window, allowing the MiniGame application to start accepting user input and allow * the user to actually play the game. */ public void startGame() { // DISPLAY THE WINDOW window.setVisible(true); // LET'S NOW CORRECT THE WINDOW SIZE. THE REASON WE DO THIS // IS BECAUSE WE'RE USING null LAYOUT MANAGER, SO WE NEED TO // SIZE STUFF OURSELVES, AND WE WANT THE WINDOW SIZE TO BE THE // SIZE OF THE CANVAS + THE BORDER OF THE WINDOW, WHICH WOULD // INCLUDE THE TITLE BAR. THIS IS CALLED THE WINDOW'S INSETS Insets insets = window.getInsets(); int correctedWidth = data.getGameWidth() + insets.left + insets.right; int correctedHeight = data.getGameHeight() + insets.top + insets.bottom; window.setSize(correctedWidth, correctedHeight); }
/** * This method sets up everything, including the GUI and the game data, and starts the timer, * which will force state updates and rendering. Note that rendering will not be seen until the * game's window is set visible. * * @param appTitle the name of the game application, it will be put in the window's title bar. * @param initFramesPerSecond the frame rate to be used for running the game application. This * refers to how many times each second the game state is updated and rendered. */ public void initMiniGame(String appTitle, int initFramesPerSecond) { // KEEP THE TITLE AND FRAME RATE name = appTitle; framesPerSecond = initFramesPerSecond; // CALCULATE THE TIME EACH FRAME SHOULD TAKE frameDuration = 1000 / framesPerSecond; // CONSTRUCT OUR LOCK, WHICH WILL MAKE SURE // WE ARE NOT UPDATING THE GAME DATA SIMULATEOUSLY // IN TWO DIFFERENT THREADS dataLock = new ReentrantLock(); // AND NOW SETUP THE FULL APP. NOTE THAT SOME // OF THESE METHODS MUST BE CUSTOMLY PROVIDED FOR // EACH GAME IMPLEMENTATION initAudio(); initWindow(); initData(); initGUI(); initHandlers(); initTimer(); // LET THE USER START THE GAME ON DEMAND data.setGameState(MiniGameState.NOT_STARTED); }
/** * This should initialize and setup all GUI components. Note that it will invoke the * custom-implemented initGUIControls method, which the game developer must provide to setup * buttons. */ private void initGUI() { // INITIALIZE OUR GUI DATA STRUCTURES guiButtons = new TreeMap(); guiDecor = new TreeMap(); guiDialogs = new TreeMap(); // WE'LL LAYOUT EVERYTHING USING PIXEL COORDINATES window.setLayout(null); // GUI CONTROLS ARE SETUP BY THE GAME DEVELOPER // USING THIS FRAMEWORK initGUIControls(); // ULTIMATELY, EVERYTHING SHOULD BE INSIDE THE CANVAS window.add(canvas); canvas.setBounds(0, 0, data.getGameWidth(), data.getGameHeight()); }