|
| 1 | +import java.awt.*; |
| 2 | +import java.awt.event.*; |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Random; |
| 5 | +import javax.swing.*; |
| 6 | + |
| 7 | +public class FlappyBird extends JPanel implements ActionListener, KeyListener { |
| 8 | + int boardWidth = 360; |
| 9 | + int boardHeight = 640; |
| 10 | + |
| 11 | + // images |
| 12 | + Image backgroundImg; |
| 13 | + Image birdImg; |
| 14 | + Image topPipeImg; |
| 15 | + Image bottomPipeImg; |
| 16 | + |
| 17 | + // bird class |
| 18 | + int birdX = boardWidth / 8; |
| 19 | + int birdY = boardHeight / 2; |
| 20 | + int birdWidth = 34; |
| 21 | + int birdHeight = 24; |
| 22 | + |
| 23 | + class Bird { |
| 24 | + int x = birdX; |
| 25 | + int y = birdY; |
| 26 | + int width = birdWidth; |
| 27 | + int height = birdHeight; |
| 28 | + Image img; |
| 29 | + |
| 30 | + Bird(Image img) { |
| 31 | + this.img = img; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // pipe class |
| 36 | + int pipeX = boardWidth; |
| 37 | + int pipeY = 0; |
| 38 | + int pipeWidth = 64; // scaled by 1/6 |
| 39 | + int pipeHeight = 512; |
| 40 | + |
| 41 | + class Pipe { |
| 42 | + int x = pipeX; |
| 43 | + int y = pipeY; |
| 44 | + int width = pipeWidth; |
| 45 | + int height = pipeHeight; |
| 46 | + Image img; |
| 47 | + boolean passed = false; |
| 48 | + |
| 49 | + Pipe(Image img) { |
| 50 | + this.img = img; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // game logic |
| 55 | + Bird bird; |
| 56 | + int velocityX = -4; // move pipes to the left speed (simulates bird moving right) |
| 57 | + int velocityY = 0; // move bird up/down speed. |
| 58 | + int gravity = 1; |
| 59 | + |
| 60 | + ArrayList<Pipe> pipes; |
| 61 | + Random random = new Random(); |
| 62 | + |
| 63 | + Timer gameLoop; |
| 64 | + Timer placePipeTimer; |
| 65 | + boolean gameOver = false; |
| 66 | + double score = 0; |
| 67 | + |
| 68 | + FlappyBird() { |
| 69 | + setPreferredSize(new Dimension(boardWidth, boardHeight)); |
| 70 | + // setBackground(Color.blue); |
| 71 | + setFocusable(true); |
| 72 | + addKeyListener(this); |
| 73 | + |
| 74 | + // load images |
| 75 | + backgroundImg = new ImageIcon(getClass().getResource("./flappybirdbg.png")).getImage(); |
| 76 | + birdImg = new ImageIcon(getClass().getResource("./flappybird.png")).getImage(); |
| 77 | + topPipeImg = new ImageIcon(getClass().getResource("./toppipe.png")).getImage(); |
| 78 | + bottomPipeImg = new ImageIcon(getClass().getResource("./bottompipe.png")).getImage(); |
| 79 | + |
| 80 | + // bird |
| 81 | + bird = new Bird(birdImg); |
| 82 | + pipes = new ArrayList<Pipe>(); |
| 83 | + |
| 84 | + // place pipes timer |
| 85 | + placePipeTimer = new Timer(1500, new ActionListener() { |
| 86 | + @Override |
| 87 | + public void actionPerformed(ActionEvent e) { |
| 88 | + // Code to be executed |
| 89 | + placePipes(); |
| 90 | + } |
| 91 | + }); |
| 92 | + placePipeTimer.start(); |
| 93 | + |
| 94 | + // game timer |
| 95 | + gameLoop = new Timer(1000 / 60, this); // how long it takes to start timer, milliseconds gone between frames |
| 96 | + gameLoop.start(); |
| 97 | + } |
| 98 | + |
| 99 | + void placePipes() { |
| 100 | + // (0-1) * pipeHeight/2. |
| 101 | + // 0 -> -128 (pipeHeight/4) |
| 102 | + // 1 -> -128 - 256 (pipeHeight/4 - pipeHeight/2) = -3/4 pipeHeight |
| 103 | + int randomPipeY = (int) (pipeY - pipeHeight / 4 - Math.random() * (pipeHeight / 2)); |
| 104 | + int openingSpace = boardHeight / 4; |
| 105 | + |
| 106 | + Pipe topPipe = new Pipe(topPipeImg); |
| 107 | + topPipe.y = randomPipeY; |
| 108 | + pipes.add(topPipe); |
| 109 | + |
| 110 | + Pipe bottomPipe = new Pipe(bottomPipeImg); |
| 111 | + bottomPipe.y = topPipe.y + pipeHeight + openingSpace; |
| 112 | + pipes.add(bottomPipe); |
| 113 | + } |
| 114 | + |
| 115 | + public void paintComponent(Graphics g) { |
| 116 | + super.paintComponent(g); |
| 117 | + draw(g); |
| 118 | + } |
| 119 | + |
| 120 | + public void draw(Graphics g) { |
| 121 | + // background |
| 122 | + g.drawImage(backgroundImg, 0, 0, this.boardWidth, this.boardHeight, null); |
| 123 | + |
| 124 | + // bird |
| 125 | + g.drawImage(birdImg, bird.x, bird.y, bird.width, bird.height, null); |
| 126 | + |
| 127 | + // pipes |
| 128 | + for (int i = 0; i < pipes.size(); i++) { |
| 129 | + Pipe pipe = pipes.get(i); |
| 130 | + g.drawImage(pipe.img, pipe.x, pipe.y, pipe.width, pipe.height, null); |
| 131 | + } |
| 132 | + |
| 133 | + // score |
| 134 | + g.setColor(Color.white); |
| 135 | + |
| 136 | + g.setFont(new Font("Arial", Font.PLAIN, 32)); |
| 137 | + if (gameOver) { |
| 138 | + g.drawString("Game Over: " + String.valueOf((int) score), 10, 35); |
| 139 | + } else { |
| 140 | + g.drawString(String.valueOf((int) score), 10, 35); |
| 141 | + } |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | + public void move() { |
| 146 | + // bird |
| 147 | + velocityY += gravity; |
| 148 | + bird.y += velocityY; |
| 149 | + bird.y = Math.max(bird.y, 0); // apply gravity to current bird.y, limit the bird.y to top of the canvas |
| 150 | + |
| 151 | + // pipes |
| 152 | + for (int i = 0; i < pipes.size(); i++) { |
| 153 | + Pipe pipe = pipes.get(i); |
| 154 | + pipe.x += velocityX; |
| 155 | + |
| 156 | + if (!pipe.passed && bird.x > pipe.x + pipe.width) { |
| 157 | + score += 0.5; // 0.5 because there are 2 pipes! so 0.5*2 = 1, 1 for each set of pipes |
| 158 | + pipe.passed = true; |
| 159 | + } |
| 160 | + |
| 161 | + if (collision(bird, pipe)) { |
| 162 | + gameOver = true; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + if (bird.y > boardHeight) { |
| 167 | + gameOver = true; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + boolean collision(Bird a, Pipe b) { |
| 172 | + return a.x < b.x + b.width && // a's top left corner doesn't reach b's top right corner |
| 173 | + a.x + a.width > b.x && // a's top right corner passes b's top left corner |
| 174 | + a.y < b.y + b.height && // a's top left corner doesn't reach b's bottom left corner |
| 175 | + a.y + a.height > b.y; // a's bottom left corner passes b's top left corner |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public void actionPerformed(ActionEvent e) { // called every x milliseconds by gameLoop timer |
| 180 | + move(); |
| 181 | + repaint(); |
| 182 | + if (gameOver) { |
| 183 | + placePipeTimer.stop(); |
| 184 | + gameLoop.stop(); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public void keyPressed(KeyEvent e) { |
| 190 | + if (e.getKeyCode() == KeyEvent.VK_SPACE) { |
| 191 | + // System.out.println("JUMP!"); |
| 192 | + velocityY = -10; |
| 193 | + |
| 194 | + if (gameOver) { |
| 195 | + // restart game by resetting conditions |
| 196 | + bird.y = birdY; |
| 197 | + velocityY = 0; |
| 198 | + pipes.clear(); |
| 199 | + gameOver = false; |
| 200 | + score = 0; |
| 201 | + gameLoop.start(); |
| 202 | + placePipeTimer.start(); |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + // not needed |
| 208 | + @Override |
| 209 | + public void keyTyped(KeyEvent e) { |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public void keyReleased(KeyEvent e) { |
| 214 | + } |
| 215 | +} |
0 commit comments