|
| 1 | +package com.pi4j.examples.games.bricks; |
| 2 | + |
| 3 | +import com.pi4j.drivers.display.graphics.GraphicsDisplay; |
| 4 | +import com.pi4j.drivers.input.GameController; |
| 5 | +import com.pi4j.io.ListenableOnOffRead; |
| 6 | +import com.pi4j.util.DeferredDelay; |
| 7 | + |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.function.Consumer; |
| 11 | + |
| 12 | +public class Bricks { |
| 13 | + |
| 14 | + private static final int FIELD_SIZE = 64; |
| 15 | + private static final int BALL_SIZE = 4; |
| 16 | + private static final int PADDLE_WIDTH = 16; |
| 17 | + private static final int PADDLE_HEIGHT = 4; |
| 18 | + private static final int PADDLE_Y = FIELD_SIZE - PADDLE_HEIGHT * 3 / 2; |
| 19 | + |
| 20 | + private final GraphicsDisplay display; |
| 21 | + private final GameController controller; |
| 22 | + private final DeferredDelay delay = new DeferredDelay(); |
| 23 | + private final Map<ListenableOnOffRead<?>, Consumer<Boolean>> keys = new HashMap<>(); |
| 24 | + |
| 25 | + private final int scale; |
| 26 | + private final int x0; |
| 27 | + private final int y0; |
| 28 | + private final Map<ListenableOnOffRead<?>, Consumer<Boolean>> activeKeys = new HashMap<>(); |
| 29 | + |
| 30 | + private float ballX; |
| 31 | + private int ballY; |
| 32 | + private float ballDx; |
| 33 | + private int ballDy; |
| 34 | + |
| 35 | + private int paddleX; |
| 36 | + private int paddleDx; |
| 37 | + private boolean exit; |
| 38 | + private boolean gameOver; |
| 39 | + private int stepTime = 25; |
| 40 | + |
| 41 | + boolean[][] bricks = new boolean[4][3]; |
| 42 | + int remainingBrickCount; |
| 43 | + |
| 44 | + public Bricks(GraphicsDisplay display, GameController controller) { |
| 45 | + this.display = display; |
| 46 | + this.controller = controller; |
| 47 | + int displayWidth = display.getWidth(); |
| 48 | + int displayHeight = display.getHeight(); |
| 49 | + |
| 50 | + assignKeys(on -> setMovement(on, -2), GameController.Key.LEFT, GameController.Key.LT); |
| 51 | + assignKeys(on -> setMovement(on, 2), GameController.Key.RIGHT, GameController.Key.RT); |
| 52 | + assignKeys(on -> actionKey(on), GameController.Key.CENTER, GameController.Key.A, GameController.Key.START); |
| 53 | + |
| 54 | + scale = Math.min(displayHeight / FIELD_SIZE, displayWidth / FIELD_SIZE); |
| 55 | + x0 = (displayWidth - FIELD_SIZE * scale) / 2; |
| 56 | + y0 = (displayHeight - FIELD_SIZE * scale) / 2; |
| 57 | + |
| 58 | + resetBall(); |
| 59 | + initializeBricks(); |
| 60 | + } |
| 61 | + |
| 62 | + private void assignKeys(Consumer<Boolean> consumer, GameController.Key... keys) { |
| 63 | + for (GameController.Key key : keys) { |
| 64 | + ListenableOnOffRead<?> lor = controller.getKey(key); |
| 65 | + if (lor != null) { |
| 66 | + this.activeKeys.put(lor, lor.addConsumer(consumer)); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private void actionKey(boolean on) { |
| 72 | + if (!on && gameOver) { |
| 73 | + exit = true; |
| 74 | + } else if (on && ballDy == 0) { |
| 75 | + ballDy = -1; |
| 76 | + ballDx = 0.1f; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private void initializeBricks() { |
| 81 | + display.fillRect(0, 0, display.getWidth(), display.getHeight(), 0); |
| 82 | + for (int i = 0; i < 4; i++) { |
| 83 | + for (int j = 0; j < 3; j++) { |
| 84 | + setBrick(i, j, true); |
| 85 | + } |
| 86 | + } |
| 87 | + paddleX = (FIELD_SIZE - PADDLE_WIDTH) / 2; |
| 88 | + remainingBrickCount = 4 * 3; |
| 89 | + } |
| 90 | + |
| 91 | + private void moveBall() { |
| 92 | + if (gameOver) { |
| 93 | + |
| 94 | + } else if (ballDy == 0) { |
| 95 | + ballX = paddleX + (PADDLE_WIDTH-BALL_SIZE) / 2; |
| 96 | + } else { |
| 97 | + ballX += ballDx; |
| 98 | + ballY += ballDy; |
| 99 | + |
| 100 | + if (ballX < 0 || ballX >= FIELD_SIZE - BALL_SIZE) { |
| 101 | + // Side reflection |
| 102 | + ballDx = -ballDx; |
| 103 | + ballX += ballDx; |
| 104 | + } |
| 105 | + |
| 106 | + if (ballY < 0) { |
| 107 | + // Top reflection |
| 108 | + ballDy = -ballDy; |
| 109 | + ballY += ballDy; |
| 110 | + } else if (ballY + BALL_SIZE > PADDLE_Y) { |
| 111 | + // paddle or miss |
| 112 | + int paddleCenter = paddleX + PADDLE_WIDTH / 2; |
| 113 | + float paddleCenterDistance = ballX + BALL_SIZE / 2f - paddleCenter; |
| 114 | + float absolutePaddleCenterDistance = Math.abs(paddleCenterDistance); |
| 115 | + |
| 116 | + if (absolutePaddleCenterDistance > (PADDLE_WIDTH + BALL_SIZE) / 2f) { |
| 117 | + gameOver = true; |
| 118 | + paddleDx = 0; |
| 119 | + } else { |
| 120 | + ballDy = -ballDy; |
| 121 | + ballY += ballDy; |
| 122 | + |
| 123 | + ballDx += 2 * paddleCenterDistance / PADDLE_WIDTH; |
| 124 | + ballDx = Math.max(-1.2f, Math.min(ballDx, 1.2f)); |
| 125 | + |
| 126 | + if (remainingBrickCount == 0) { |
| 127 | + initializeBricks(); |
| 128 | + if (stepTime > 10) { |
| 129 | + stepTime--; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } else { |
| 134 | + int row = ballY / 8; |
| 135 | + if (row >= 1 && row <= 3) { |
| 136 | + int i = (int) (ballX / 16); |
| 137 | + int j = row - 1; |
| 138 | + if (bricks[i][j]) { |
| 139 | + setBrick(i, j, false); |
| 140 | + ballDy = -ballDy; |
| 141 | + ballY += ballDy; |
| 142 | + remainingBrickCount--; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private void resetBall() { |
| 150 | + ballDx = 0; |
| 151 | + ballDy = 0; |
| 152 | + ballY = PADDLE_Y - BALL_SIZE; |
| 153 | + ballX = paddleX + (8-BALL_SIZE) / 2; |
| 154 | + } |
| 155 | + |
| 156 | + |
| 157 | + private void releaseKeys() { |
| 158 | + for (Map.Entry<ListenableOnOffRead<?>, Consumer<Boolean>> entry : activeKeys.entrySet()) { |
| 159 | + entry.getKey().removeConsumer(entry.getValue()); |
| 160 | + } |
| 161 | + activeKeys.clear(); |
| 162 | + } |
| 163 | + |
| 164 | + private void renderPaddle(boolean on) { |
| 165 | + display.fillRect(x0 + paddleX * scale, y0 + PADDLE_Y * scale, PADDLE_WIDTH * scale, PADDLE_HEIGHT * scale, on ? 0x888888 : 0); |
| 166 | + } |
| 167 | + |
| 168 | + private void renderBall(boolean on) { |
| 169 | + display.fillRect(Math.round(x0 + ballX * scale), y0 + ballY * scale, BALL_SIZE * scale , BALL_SIZE * scale, on ? 0x888888 : 0); |
| 170 | + } |
| 171 | + |
| 172 | + |
| 173 | + public void run() { |
| 174 | + while (!exit) { |
| 175 | + delay.setDelayMillis(stepTime); |
| 176 | + renderPaddle(false); |
| 177 | + paddleX = Math.max(0, Math.min(paddleX + paddleDx, FIELD_SIZE - PADDLE_WIDTH)); |
| 178 | + renderPaddle(true); |
| 179 | + |
| 180 | + renderBall(false); |
| 181 | + moveBall(); |
| 182 | + renderBall(true); |
| 183 | + |
| 184 | + delay.materializeDelay(); |
| 185 | + } |
| 186 | + releaseKeys(); |
| 187 | + } |
| 188 | + |
| 189 | + private void setMovement(boolean on, int dx) { |
| 190 | + if (gameOver) { |
| 191 | + if (on) { |
| 192 | + exit = true; |
| 193 | + } |
| 194 | + } else { |
| 195 | + paddleDx = on ? dx : 0; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + private void setBrick(int x, int y, boolean alive) { |
| 200 | + bricks[x][y] = alive; |
| 201 | + display.fillRect( |
| 202 | + x0 + 16 * x * scale + 1, |
| 203 | + y0 + 8 * (y + 1) * scale + 1, |
| 204 | + 14 * scale, |
| 205 | + 6 * scale, |
| 206 | + alive ? (y == 0 ? 0xff0000 : y == 1 ? 0xff00 : 0xff) : 0); |
| 207 | + } |
| 208 | +} |
0 commit comments