├── .classpath ├── .project ├── .settings └── org.eclipse.jdt.core.prefs ├── README.md ├── Screenshots ├── Game-Over Screenshot.png ├── In-Game Screenshot.png └── Start Screenshot.png ├── bin └── FlappyBird │ ├── FlappyBird.class │ └── Renderer.class └── src └── FlappyBird ├── FlappyBird.java └── Renderer.java /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | FlappyBirdGame 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=11 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=10 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=11 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Flappy Bird Skill & Reaction Game 2 | 3 | ### An object-oriented and GUI based Java game. Click or press the space bar to make the player controlled bird jump. The bird moves automatically and must avoid all obstacles, to gain points, (basic flappy bird game). 4 | 5 |
6 | 7 | *** 8 | 9 | ##### Install from terminal/shell, in the directory/repo's parent folder (javac command). Run FlappyBird.java (java command). 10 | 11 | *** 12 | 13 |
14 | 15 | |Version| Changes| 16 | |:---|:---| 17 | |Version 1.0.0 [2020-05-01]|| 18 | |Version 1.0.1 [2020-05-22]|| 19 | -------------------------------------------------------------------------------- /Screenshots/Game-Over Screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tberey/java-flappy-bird-game/d84f623fc39a065e72ca6f43a757a434ac8e1f4f/Screenshots/Game-Over Screenshot.png -------------------------------------------------------------------------------- /Screenshots/In-Game Screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tberey/java-flappy-bird-game/d84f623fc39a065e72ca6f43a757a434ac8e1f4f/Screenshots/In-Game Screenshot.png -------------------------------------------------------------------------------- /Screenshots/Start Screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tberey/java-flappy-bird-game/d84f623fc39a065e72ca6f43a757a434ac8e1f4f/Screenshots/Start Screenshot.png -------------------------------------------------------------------------------- /bin/FlappyBird/FlappyBird.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tberey/java-flappy-bird-game/d84f623fc39a065e72ca6f43a757a434ac8e1f4f/bin/FlappyBird/FlappyBird.class -------------------------------------------------------------------------------- /bin/FlappyBird/Renderer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tberey/java-flappy-bird-game/d84f623fc39a065e72ca6f43a757a434ac8e1f4f/bin/FlappyBird/Renderer.class -------------------------------------------------------------------------------- /src/FlappyBird/FlappyBird.java: -------------------------------------------------------------------------------- 1 | package FlappyBird; 2 | 3 | import java.awt.Graphics; 4 | import java.awt.Color; 5 | import java.awt.Font; 6 | import java.awt.event.ActionEvent; 7 | import java.awt.event.ActionListener; 8 | import java.awt.event.KeyEvent; 9 | import java.awt.event.MouseEvent; 10 | import java.awt.event.MouseListener; 11 | import java.awt.event.KeyListener; 12 | import java.awt.Rectangle; 13 | import java.util.ArrayList; 14 | import java.util.Random; 15 | import javax.swing.JFrame; 16 | import javax.swing.Timer; 17 | import javax.swing.WindowConstants; 18 | 19 | public class FlappyBird implements ActionListener, MouseListener, KeyListener { 20 | 21 | public static void main(String[] args) throws Exception { 22 | 23 | flappybird = new FlappyBird(); 24 | } 25 | 26 | public static FlappyBird flappybird; 27 | public Renderer renderer; 28 | public Rectangle bird; 29 | public int ticks, yMotion, score; 30 | public ArrayList columns; 31 | public Random rand; 32 | public boolean gameOver, started; 33 | 34 | public final int WIDTH = 800, HEIGHT = 800; 35 | 36 | public FlappyBird() { 37 | 38 | JFrame jframe = new JFrame(); 39 | Timer timer = new Timer(20, this); 40 | renderer = new Renderer(); 41 | rand = new Random(); 42 | 43 | jframe.add(renderer); 44 | jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 45 | jframe.setSize(WIDTH, HEIGHT); 46 | jframe.addMouseListener(this); 47 | jframe.addKeyListener(this); 48 | jframe.setResizable(false); 49 | jframe.setTitle("Flappy Bird"); 50 | jframe.setVisible(true); 51 | 52 | bird = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 20, 20); 53 | columns = new ArrayList(); 54 | 55 | addColumn(true); 56 | addColumn(true); 57 | addColumn(true); 58 | addColumn(true); 59 | 60 | timer.start(); 61 | } 62 | 63 | public void addColumn(boolean startGame) { 64 | 65 | int space = 300; 66 | int width = 100; 67 | int height = 50 + rand.nextInt(300); // Min height 50, max height 300. 68 | 69 | if (startGame) { 70 | 71 | columns.add(new Rectangle(WIDTH + width + columns.size() * 300, HEIGHT - height - 120, width, height)); // Place 72 | // a 73 | // column/pipe. 74 | columns.add(new Rectangle(WIDTH + width + (columns.size() - 1) * 300, 0, width, HEIGHT - height - space)); // Place 75 | // a 76 | // column/pipe. 77 | } else { 78 | 79 | columns.add(new Rectangle(columns.get(columns.size() - 1).x + 600, HEIGHT - height - 120, width, height)); // Place 80 | // a 81 | // column/pipe. 82 | columns.add(new Rectangle(columns.get(columns.size() - 1).x, 0, width, HEIGHT - height - space)); // Place a 83 | // column/pipe. 84 | } 85 | 86 | } 87 | 88 | public void paintColumn(Graphics g, Rectangle column) { 89 | 90 | g.setColor(Color.green.darker().darker()); 91 | g.fillRect(column.x, column.y, column.width, column.height); 92 | } 93 | 94 | public void jump() { 95 | 96 | if (gameOver) { 97 | 98 | bird = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 20, 20); 99 | 100 | columns.clear(); 101 | yMotion = 0; 102 | score = 0; 103 | 104 | addColumn(true); 105 | addColumn(true); 106 | addColumn(true); 107 | addColumn(true); 108 | 109 | gameOver = false; 110 | } 111 | 112 | if (!started) { 113 | 114 | started = true; 115 | } else if (!gameOver) { 116 | 117 | if (yMotion > 0) { 118 | yMotion = 0; 119 | } 120 | 121 | yMotion -= 10; 122 | } 123 | } 124 | 125 | // To render multiple times, use our Renderer. 126 | public void actionPerformed(ActionEvent arg0) { 127 | 128 | int speed = 10; 129 | 130 | ticks++; 131 | 132 | if (started) { 133 | 134 | for (int i = 0; i < columns.size(); i++) { 135 | 136 | Rectangle column = columns.get(i); 137 | column.x -= speed; 138 | } 139 | 140 | if (ticks % 2 == 0 && yMotion < 15) { 141 | 142 | yMotion += 2; 143 | } 144 | 145 | for (int i = 0; i < columns.size(); i++) { 146 | 147 | Rectangle column = columns.get(i); 148 | 149 | if (column.x + column.width < 0) { 150 | 151 | columns.remove(column); 152 | 153 | if (column.y == 0) { 154 | 155 | addColumn(false); 156 | } 157 | } 158 | } 159 | 160 | bird.y += yMotion; 161 | 162 | for (Rectangle column : columns) { 163 | 164 | if (column.y == 0 && bird.x + bird.width / 2 > column.x + column.width / 2 - 10 && bird.x + bird.width / 2 < column.x + column.width / 2 + 10) { 165 | 166 | score++; 167 | } 168 | 169 | if (column.intersects(bird)) { 170 | 171 | gameOver = true; 172 | 173 | if (bird.x <= column.x) { 174 | 175 | bird.x = column.x - bird.width; 176 | 177 | } else { 178 | 179 | if (column.y != 0) { 180 | 181 | bird.y = column.y - bird.height; 182 | 183 | } else if (bird.y < column.height) { 184 | 185 | bird.y = column.height; 186 | } 187 | } 188 | } 189 | } 190 | 191 | if (bird.y > HEIGHT - 120 || bird.y < 0) { 192 | 193 | gameOver = true; 194 | } 195 | 196 | if (bird.y + yMotion >= HEIGHT - 120) { 197 | 198 | bird.y = HEIGHT - 120 - bird.height; 199 | } 200 | } 201 | renderer.repaint(); 202 | } 203 | 204 | public void repaint(Graphics g) { 205 | // Background Color. 206 | g.setColor(Color.cyan); 207 | g.fillRect(0, 0, HEIGHT, WIDTH); 208 | 209 | // Add Ground. 210 | g.setColor(Color.orange); 211 | g.fillRect(0, HEIGHT - 120, WIDTH, 120); 212 | 213 | // Add Grass. 214 | g.setColor(Color.green); 215 | g.fillRect(0, HEIGHT - 120, WIDTH, 20); 216 | 217 | // Bird (player) icon. 218 | g.setColor(Color.red); 219 | g.fillRect(bird.x, bird.y, bird.height, bird.width); 220 | 221 | for (Rectangle column : columns) { 222 | 223 | paintColumn(g, column); 224 | } 225 | 226 | g.setColor(Color.white); 227 | g.setFont(new Font("Arial", 1, 80)); 228 | 229 | if (!started) { 230 | 231 | g.drawString("Click to Begin", 75, HEIGHT / 2 - 50); 232 | } 233 | 234 | if (gameOver) { 235 | 236 | g.drawString("Game Over", 100, HEIGHT / 2 - 50); 237 | } 238 | 239 | if (!gameOver && started) { 240 | 241 | g.drawString(String.valueOf(score), WIDTH / 2 - 25, 100); 242 | } 243 | } 244 | 245 | public void mouseClicked(MouseEvent e) { 246 | 247 | jump(); 248 | } 249 | 250 | public void keyPressed(KeyEvent e) { 251 | if (e.getKeyCode() == KeyEvent.VK_SPACE) { 252 | jump(); 253 | } 254 | } 255 | 256 | public void keyTyped(KeyEvent e) {} 257 | public void mouseEntered(MouseEvent e) {} 258 | public void mouseExited(MouseEvent e) {} 259 | public void mousePressed(MouseEvent e) {} 260 | public void mouseReleased(MouseEvent e) {} 261 | public void keyReleased(KeyEvent e) {} 262 | } -------------------------------------------------------------------------------- /src/FlappyBird/Renderer.java: -------------------------------------------------------------------------------- 1 | package FlappyBird; 2 | 3 | import java.awt.Graphics; 4 | import javax.swing.JPanel; 5 | 6 | public class Renderer extends JPanel { 7 | 8 | private static final long serialVersionUID = 1L; // For warning. 9 | 10 | // Recursive, to continue rendering itself, when looped. 11 | protected void paintComponent(Graphics g) { 12 | 13 | super.paintComponent(g); // Calls method from parent class (JPanel, which is extended to this class). 14 | FlappyBird.flappybird.repaint(g); 15 | } 16 | } --------------------------------------------------------------------------------