|
| 1 | +package com.pi4j.examples.games; |
| 2 | + |
| 3 | +import com.pi4j.drivers.display.BitmapFont; |
| 4 | +import com.pi4j.drivers.display.graphics.Graphics; |
| 5 | +import com.pi4j.drivers.display.graphics.GraphicsDisplay; |
| 6 | +import com.pi4j.drivers.input.GameController; |
| 7 | +import com.pi4j.drivers.sound.SoundDriver; |
| 8 | +import com.pi4j.util.DeferredDelay; |
| 9 | + |
| 10 | +import java.util.concurrent.locks.LockSupport; |
| 11 | + |
| 12 | +/** Abstract base class for games tailored towards small displays. */ |
| 13 | +public abstract class MiniGame { |
| 14 | + // Drivers and constructor params |
| 15 | + |
| 16 | + protected final GraphicsDisplay display; |
| 17 | + protected final GameController controller; |
| 18 | + protected final SoundDriver soundDriver; |
| 19 | + protected final String gameName; |
| 20 | + protected final Graphics graphics; |
| 21 | + protected final DeferredDelay delay = new DeferredDelay(); |
| 22 | + protected int backgroundColor; |
| 23 | + protected int borderColor; |
| 24 | + |
| 25 | + // Sizing and scaling |
| 26 | + |
| 27 | + protected int cellSize; |
| 28 | + protected int scale; |
| 29 | + protected int scaledCellSize; |
| 30 | + protected int size; |
| 31 | + protected int x0; |
| 32 | + protected int y0; |
| 33 | + |
| 34 | + // Internals |
| 35 | + |
| 36 | + private GameController.Direction previousDirection = GameController.Direction.NONE; |
| 37 | + private int textX; |
| 38 | + private int textY; |
| 39 | + private String scrollText; |
| 40 | + private int scrollPos; |
| 41 | + private boolean soundEnabled; |
| 42 | + private boolean running = false; |
| 43 | + private boolean exit = false; |
| 44 | + private int score = 0; |
| 45 | + private int highScore = 0; |
| 46 | + |
| 47 | + /** |
| 48 | + * Adds the "delta" value to a value and returns the sum. If the distance to the next integer is less than |
| 49 | + * delta / 2, the returned value "snaps" to this integer. |
| 50 | + */ |
| 51 | + public static float addAndSnap(float value, float delta) { |
| 52 | + float newValue = value + delta; |
| 53 | + float rounded = Math.round(newValue); |
| 54 | + return (Math.abs(newValue - rounded) <= Math.abs(delta)/2) ? rounded : newValue; |
| 55 | + } |
| 56 | + |
| 57 | + protected MiniGame(GraphicsDisplay display, |
| 58 | + GameController controller, |
| 59 | + SoundDriver soundDriver, |
| 60 | + String name, |
| 61 | + int backgroundColor, |
| 62 | + int borderColor) { |
| 63 | + this.display = display; |
| 64 | + this.controller = controller; |
| 65 | + this.soundDriver = soundDriver; |
| 66 | + this.gameName = name; |
| 67 | + this.backgroundColor = backgroundColor; |
| 68 | + this.borderColor = borderColor; |
| 69 | + this.soundEnabled = soundDriver != null; |
| 70 | + |
| 71 | + graphics = display.getGraphics(); |
| 72 | + |
| 73 | + int minDim = Math.min(display.getWidth(), display.getHeight()); |
| 74 | + int rawCellSize = minDim / 8; |
| 75 | + |
| 76 | + if (rawCellSize < 8) { |
| 77 | + cellSize = 1; |
| 78 | + scale = cellSize; |
| 79 | + } else { |
| 80 | + int remainder10 = rawCellSize % 10; |
| 81 | + int remainder8 = rawCellSize % 8; |
| 82 | + |
| 83 | + if (remainder10 <= remainder8) { |
| 84 | + cellSize = 10; |
| 85 | + } else { |
| 86 | + cellSize = 8; |
| 87 | + } |
| 88 | + scale = rawCellSize / cellSize; |
| 89 | + } |
| 90 | + scaledCellSize = scale * cellSize; |
| 91 | + size = 8 * scaledCellSize; |
| 92 | + |
| 93 | + x0 = (display.getWidth() - size) / 2; |
| 94 | + y0 = (display.getHeight() - size) / 2; |
| 95 | + |
| 96 | + System.out.println("cellSize: " + cellSize + " scale: " + scale + " x0: " + x0 + " y0: " + y0); |
| 97 | + |
| 98 | + renderTitleScreen(); |
| 99 | + } |
| 100 | + |
| 101 | + public void addPoints(int points) { |
| 102 | + score += points; |
| 103 | + } |
| 104 | + |
| 105 | + protected void clearScreen() { |
| 106 | + graphics.setColor(borderColor); |
| 107 | + graphics.fillRect(0, 0, display.getWidth(), display.getHeight()); |
| 108 | + graphics.setColor(backgroundColor); |
| 109 | + graphics.fillRect(x0, y0, size, size); |
| 110 | + } |
| 111 | + |
| 112 | + protected void clearScreen(int ms) { |
| 113 | + graphics.setColor(backgroundColor); |
| 114 | + |
| 115 | + for (int i = 0; i < size; i++) { |
| 116 | + graphics.drawRect(i + x0, i + y0, size - 2 * i, size - 2 * i); |
| 117 | + sleep(Math.max(1, ms / size)); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + protected void println(String text) { |
| 122 | + if (cellSize >= 8) { |
| 123 | + graphics.renderText(textX, textY, text); |
| 124 | + textX = x0 + scaledCellSize / 2; |
| 125 | + textY += scaledCellSize; |
| 126 | + } |
| 127 | + scrollText += " -- " + text; |
| 128 | + } |
| 129 | + |
| 130 | + protected void renderTitleScreen() { |
| 131 | + clearScreen(); |
| 132 | + scrollText = ""; |
| 133 | + if (score > highScore) { |
| 134 | + highScore = score; |
| 135 | + } |
| 136 | + |
| 137 | + if (cellSize >= 8) { |
| 138 | + graphics.setTextScale(scale, scale); |
| 139 | + graphics.setColor(0xffffffff); |
| 140 | + graphics.setFont(cellSize == 10 ? BitmapFont.get5x10Font() : BitmapFont.get5x8Font()); |
| 141 | + |
| 142 | + textX = (display.getWidth() - gameName.length() * 6 * scale) / 2; |
| 143 | + textY = scaledCellSize * 5 / 4; |
| 144 | + } else { |
| 145 | + scrollText = ""; |
| 146 | + } |
| 147 | + println(gameName); |
| 148 | + |
| 149 | + textY += scaledCellSize / 4; |
| 150 | + |
| 151 | + println("Hi: " + highScore); |
| 152 | + println("Sc: " + score); |
| 153 | + println("> Start"); |
| 154 | + println("< Exit"); |
| 155 | + if (soundDriver != null) { |
| 156 | + println("^ Sound: " + (soundEnabled ? "On" : "Off")); |
| 157 | + } |
| 158 | + scrollText += scrollText; |
| 159 | + } |
| 160 | + |
| 161 | + protected void titleScreenStep() { |
| 162 | + GameController.Direction direction = controller.getDirection(); |
| 163 | + if (direction == GameController.Direction.NONE) { |
| 164 | + switch (previousDirection) { |
| 165 | + case GameController.Direction.EAST -> { |
| 166 | + clearScreen(); |
| 167 | + startGame(); |
| 168 | + running = true; |
| 169 | + } |
| 170 | + case GameController.Direction.WEST -> { |
| 171 | + clearScreen(); |
| 172 | + exit = true; |
| 173 | + } |
| 174 | + case GameController.Direction.NORTH -> { |
| 175 | + soundEnabled = !soundEnabled; |
| 176 | + renderTitleScreen(); |
| 177 | + } |
| 178 | + } |
| 179 | + previousDirection = GameController.Direction.NONE; |
| 180 | + } else if (previousDirection == GameController.Direction.NONE) { |
| 181 | + previousDirection = direction; |
| 182 | + } |
| 183 | + |
| 184 | + if (cellSize < 8) { |
| 185 | + graphics.setFont(BitmapFont.get5x8Font()); |
| 186 | + graphics.setColor(backgroundColor); |
| 187 | + graphics.fillRect(0, 0, 1000, 64); |
| 188 | + graphics.setColor(0xffffffff); |
| 189 | + int len = graphics.renderText(scrollPos--/2, 8, scrollText); |
| 190 | + if (scrollPos < -len) { |
| 191 | + scrollPos = 0; |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + public void run() { |
| 197 | + while (!exit) { |
| 198 | + delay.setDelayMillis(17); |
| 199 | + if (running) { |
| 200 | + running = step(); |
| 201 | + if (!running) { |
| 202 | + renderTitleScreen(); |
| 203 | + } |
| 204 | + } else { |
| 205 | + titleScreenStep(); |
| 206 | + } |
| 207 | + display.flush(); |
| 208 | + delay.materializeDelay(); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + public void play(String mml) { |
| 213 | + if (soundDriver != null && soundEnabled) { |
| 214 | + soundDriver.playNotes(mml); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + public void sleep(int ms) { |
| 219 | + if (running) { |
| 220 | + LockSupport.parkNanos(ms * 1_000_000L); |
| 221 | + } else { |
| 222 | + try { |
| 223 | + Thread.sleep(ms); |
| 224 | + } catch (InterruptedException e) { |
| 225 | + Thread.currentThread().interrupt(); |
| 226 | + throw new RuntimeException(e); |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + /** Implement the logic for starting a new game here. */ |
| 232 | + protected abstract void startGame(); |
| 233 | + |
| 234 | + /** Invoked every ~17ms. Implemenet the game logic and rendering here. */ |
| 235 | + protected abstract boolean step(); |
| 236 | +} |
0 commit comments