From 90fde2647471ee80561ae00742d0860b4a9858b0 Mon Sep 17 00:00:00 2001 From: Elias Date: Fri, 10 Dec 2021 04:41:10 +0900 Subject: [PATCH] Add help menu --- src/engine/Core.java | 22 ++++++-- src/engine/DrawManager.java | 100 +++++++++++++++++++++++++++++---- src/screen/HelpScreen.java | 64 +++++++++++++++++++++ src/screen/SettingsScreen.java | 64 +++++++++++++++++++++ src/screen/TitleScreen.java | 4 +- 5 files changed, 236 insertions(+), 18 deletions(-) create mode 100644 src/screen/HelpScreen.java create mode 100644 src/screen/SettingsScreen.java diff --git a/src/engine/Core.java b/src/engine/Core.java index 35cc8274c..4bb8469fc 100644 --- a/src/engine/Core.java +++ b/src/engine/Core.java @@ -8,11 +8,7 @@ import java.util.logging.Level; import java.util.logging.Logger; -import screen.GameScreen; -import screen.HighScoreScreen; -import screen.ScoreScreen; -import screen.Screen; -import screen.TitleScreen; +import screen.*; /** * Implements core game logic. @@ -172,6 +168,22 @@ public static void main(final String[] args) { returnCode = frame.setScreen(currentScreen); LOGGER.info("Closing high score screen."); break; + case 4: + // Help. + currentScreen = new HelpScreen(width, height, FPS); + LOGGER.info("Starting " + WIDTH + "x" + HEIGHT + + " help screen at " + FPS + " fps."); + returnCode = frame.setScreen(currentScreen); + LOGGER.info("Closing help screen."); + break; + case 5: + // Settings. + currentScreen = new SettingsScreen(width, height, FPS); + LOGGER.info("Starting " + WIDTH + "x" + HEIGHT + + " settings screen at " + FPS + " fps."); + returnCode = frame.setScreen(currentScreen); + LOGGER.info("Closing settings screen."); + break; default: break; } diff --git a/src/engine/DrawManager.java b/src/engine/DrawManager.java index 3d61ca503..b01e6ad65 100644 --- a/src/engine/DrawManager.java +++ b/src/engine/DrawManager.java @@ -283,15 +283,18 @@ public void drawHorizontalLine(final Screen screen, final int positionY) { */ public void drawTitle(final Screen screen) { String titleString = "Invaders"; - String instructionsString = - "select with w+s / arrows, confirm with space"; + String[] instructionsStrings = {"Select with w+s / arrows", "Confirm with space"}; + int i = 0; backBufferGraphics.setColor(Color.GRAY); - drawCenteredRegularString(screen, instructionsString, - screen.getHeight() / 2); + for (String instructionString : instructionsStrings) { + drawCenteredRegularString(screen, instructionString, + screen.getHeight() * 4 / 15 + fontRegularMetrics.getHeight() * 2 * i); + i++; + } backBufferGraphics.setColor(Color.GREEN); - drawCenteredBigString(screen, titleString, screen.getHeight() / 3); + drawCenteredBigString(screen, titleString, screen.getHeight() / 6); } /** @@ -305,26 +308,40 @@ public void drawTitle(final Screen screen) { public void drawMenu(final Screen screen, final int option) { String playString = "Play"; String highScoresString = "High scores"; - String exitString = "exit"; + String helpString = "Help"; + String settingsString = "Settings"; + String exitString = "Exit"; if (option == 2) backBufferGraphics.setColor(Color.GREEN); else backBufferGraphics.setColor(Color.WHITE); drawCenteredRegularString(screen, playString, - screen.getHeight() / 3 * 2); + screen.getHeight() * 11 / 20); if (option == 3) backBufferGraphics.setColor(Color.GREEN); else backBufferGraphics.setColor(Color.WHITE); - drawCenteredRegularString(screen, highScoresString, screen.getHeight() - / 3 * 2 + fontRegularMetrics.getHeight() * 2); + drawCenteredRegularString(screen, highScoresString, + screen.getHeight() * 11 / 20 + fontRegularMetrics.getHeight() * 2); + if (option == 4) + backBufferGraphics.setColor(Color.GREEN); + else + backBufferGraphics.setColor(Color.WHITE); + drawCenteredRegularString(screen, helpString, + screen.getHeight() * 11 / 20 + fontRegularMetrics.getHeight() * 4); + if (option == 5) + backBufferGraphics.setColor(Color.GREEN); + else + backBufferGraphics.setColor(Color.WHITE); + drawCenteredRegularString(screen, settingsString, + screen.getHeight() * 11 / 20 + fontRegularMetrics.getHeight() * 6); if (option == 0) backBufferGraphics.setColor(Color.GREEN); else backBufferGraphics.setColor(Color.WHITE); - drawCenteredRegularString(screen, exitString, screen.getHeight() / 3 - * 2 + fontRegularMetrics.getHeight() * 4); + drawCenteredRegularString(screen, exitString, + screen.getHeight() * 11 / 20 + fontRegularMetrics.getHeight() * 8); } /** @@ -487,6 +504,67 @@ public void drawHighScores(final Screen screen, } } + /** + * Draws help screen + * + * @param screen + * Screen to draw on. + */ + public void drawHelp(final Screen screen) { + String helpString = "Help"; + String instructionsString = "Press Space to return"; + String[] rule1 = {"Press the arrow keys", "(or the A or D keys)", "to move your ship"}; + String[] rule2 = {"Press space to shoot", "missiles at the enemy ships"}; + String[] rule3 = {"Avoid the missiles shot", "by the enemy ships"}; + String[] rule4 = {"The color of an enemy ship", "depends on their remaining", "health points"}; + String[][] rules = {rule1, rule2, rule3, rule4}; + int i = 0; + int j = 0; + + backBufferGraphics.setColor(Color.GREEN); + drawCenteredBigString(screen, helpString, screen.getHeight() / 8); + + backBufferGraphics.setColor(Color.GRAY); + drawCenteredRegularString(screen, instructionsString, + screen.getHeight() / 5); + + backBufferGraphics.setColor(Color.WHITE); + for (String[] rule : rules) { + for (String ruleString : rule) { + drawCenteredRegularString(screen, ruleString, + screen.getHeight() * 3 / 10 + + fontRegularMetrics.getHeight() * i + + fontRegularMetrics.getHeight() * 2 * j); + i++; + } + j++; + } + } + + /** + * Draws settings screen + * + * @param screen + * Screen to draw on. + */ + public void drawSettings(final Screen screen) { + String settingsString = "Settings"; + String[] instructionsStrings = {"Press Space to return", "Use the arrow keys to", "enable/disable settings"}; + int i = 0; + + backBufferGraphics.setColor(Color.GREEN); + drawCenteredBigString(screen, settingsString, screen.getHeight() / 8); + + backBufferGraphics.setColor(Color.GRAY); + for (String instructionsString : instructionsStrings) { + drawCenteredRegularString(screen, instructionsString, + (int) (screen.getHeight() / 5 + Math.round(fontRegularMetrics.getHeight() * 1.5 * i))); + i++; + } + + backBufferGraphics.setColor(Color.WHITE); + } + /** * Draws a centered string on regular font. * diff --git a/src/screen/HelpScreen.java b/src/screen/HelpScreen.java new file mode 100644 index 000000000..e78c66bac --- /dev/null +++ b/src/screen/HelpScreen.java @@ -0,0 +1,64 @@ +package screen; + +import java.awt.event.KeyEvent; +import java.io.IOException; +import java.util.List; + +import engine.Core; +import engine.Score; + +/** + * Implements the help screen, it shows the player instructions on how to play the game. + */ + +public class HelpScreen extends Screen { + /** + * Constructor, establishes the properties of the screen. + * + * @param width + * Screen width. + * @param height + * Screen height. + * @param fps + * Frames per second, frame rate at which the game is run. + */ + public HelpScreen(final int width, final int height, final int fps) { + super(width, height, fps); + + this.returnCode = 1; + } + + /** + * Starts the action. + * + * @return Next screen code. + */ + public final int run() { + super.run(); + + return this.returnCode; + } + + /** + * Updates the elements on screen and checks for events. + */ + protected final void update() { + super.update(); + + draw(); + if (inputManager.isKeyDown(KeyEvent.VK_SPACE) + && this.inputDelay.checkFinished()) + this.isRunning = false; + } + + /** + * Draws the elements associated with the screen. + */ + private void draw() { + drawManager.initDrawing(this); + + drawManager.drawHelp(this); + + drawManager.completeDrawing(this); + } +} \ No newline at end of file diff --git a/src/screen/SettingsScreen.java b/src/screen/SettingsScreen.java new file mode 100644 index 000000000..a257afe4f --- /dev/null +++ b/src/screen/SettingsScreen.java @@ -0,0 +1,64 @@ +package screen; + +import java.awt.event.KeyEvent; +import java.io.IOException; +import java.util.List; + +import engine.Core; +import engine.Score; + +/** + * Implements the help screen, it shows the player instructions on how to play the game. + */ + +public class SettingsScreen extends Screen { + /** + * Constructor, establishes the properties of the screen. + * + * @param width + * Screen width. + * @param height + * Screen height. + * @param fps + * Frames per second, frame rate at which the game is run. + */ + public SettingsScreen(final int width, final int height, final int fps) { + super(width, height, fps); + + this.returnCode = 1; + } + + /** + * Starts the action. + * + * @return Next screen code. + */ + public final int run() { + super.run(); + + return this.returnCode; + } + + /** + * Updates the elements on screen and checks for events. + */ + protected final void update() { + super.update(); + + draw(); + if (inputManager.isKeyDown(KeyEvent.VK_SPACE) + && this.inputDelay.checkFinished()) + this.isRunning = false; + } + + /** + * Draws the elements associated with the screen. + */ + private void draw() { + drawManager.initDrawing(this); + + drawManager.drawSettings(this); + + drawManager.completeDrawing(this); + } +} \ No newline at end of file diff --git a/src/screen/TitleScreen.java b/src/screen/TitleScreen.java index a8229b463..ab328b69b 100644 --- a/src/screen/TitleScreen.java +++ b/src/screen/TitleScreen.java @@ -77,7 +77,7 @@ protected final void update() { * Shifts the focus to the next menu item. */ private void nextMenuItem() { - if (this.returnCode == 3) + if (this.returnCode == 5) this.returnCode = 0; else if (this.returnCode == 0) this.returnCode = 2; @@ -90,7 +90,7 @@ else if (this.returnCode == 0) */ private void previousMenuItem() { if (this.returnCode == 0) - this.returnCode = 3; + this.returnCode = 5; else if (this.returnCode == 2) this.returnCode = 0; else