A Java implementation of a Flappy Bird-style game featuring physics-based gameplay and collision detection.
FlappyBox is a side-scrolling arcade game where players control a falling box that must navigate through randomly-generated pipe obstacles. The game demonstrates object-oriented programming principles and physics simulation in Java.
- Physics-based movement with gravity and acceleration
- Randomly generated pipe obstacles with varying gap positions
- Real-time collision detection
- Score tracking system
- Smooth animations using double buffering
- Customizable game constants (colors, speeds, difficulty)
- SPACE: Make the box jump
- Avoid hitting the pipes or going off-screen
- Navigate through the gaps to increase your score
- The game ends on collision or boundary violation
- Java Development Kit (JDK) 8 or higher
- No external dependencies required
# Navigate to the project directory
cd FlappyBox
# Run directly without compiling first
java src/main/java/flappybox/Game.java# Compile the source files (creates out/ directory)
javac -d out src/main/java/flappybox/*.java
# Run the game
java -cp out flappybox.GameSimply open the project in IntelliJ IDEA, Eclipse, or VS Code and run Game.java - the IDE will handle compilation automatically.
# Compile and run tests
javac -cp src/main/java:src/test/java src/test/java/flappybox/*.java
java -cp src/main/java:src/test/java org.junit.runner.JUnitCore flappybox.GameTestFlappyBox/
├── src/
│ ├── main/
│ │ └── java/
│ │ └── flappybox/
│ │ ├── Block.java # Abstract base class for rectangular objects
│ │ ├── Sprite.java # Movable objects with velocity/acceleration
│ │ ├── Box.java # Square sprite with fixed dimensions
│ │ ├── FallingBox.java # Box affected by gravity
│ │ ├── FlappyBox.java # Player-controlled box with jump ability
│ │ ├── Pipe.java # Obstacle composed of boxes with gaps
│ │ ├── Game.java # Main game loop and logic
│ │ ├── GameConstant.java # Configuration constants
│ │ ├── GameObject.java # Interface for drawable objects
│ │ └── StdDraw.java # Graphics library
│ └── test/
│ └── java/
│ └── flappybox/ # Unit tests
└── README.md
- Gravity constantly pulls the box downward
- Jumping applies an upward velocity
- Velocity increases over time due to acceleration
- Movement calculations update every frame
- Axis-aligned bounding box (AABB) collision detection
- Checks for intersections between the player box and pipe obstacles
- Game ends immediately upon collision
- Pipes move from right to left across the screen
- Each pipe has a randomly-positioned gap (2 boxes tall)
- Old pipes are recycled when they leave the screen
- New pipes spawn at regular intervals
Game parameters can be adjusted in GameConstant.java:
- Canvas dimensions
- Box size and spacing
- Movement speeds
- Jump velocity
- Gravity strength
- Colors and visual settings
- Frame delay (FPS)
The project demonstrates several OOP concepts:
- Inheritance:
FlappyBox→FallingBox→Box→Sprite→Block - Encapsulation: Private fields with public getter/setter methods
- Polymorphism: Method overriding for specialized behavior
- Abstraction: Abstract classes and interfaces
- Composition:
Pipecontains multipleBoxobjects
- Graphics rendering powered by StdDraw library
- Inspired by the original Flappy Bird game