This is a fully refactored implementation of the classic 2048 puzzle game in Python using Pygame. The game features a clean, modular architecture with improved performance, better user experience, and robust error handling.
In 2048, players slide numbered tiles on a 4ร4 grid to combine tiles with the same value, creating larger numbers. The goal is to reach the tile with value 2048 (or beyond).
- Modern Architecture: Object-oriented design with separate classes for game logic, rendering, and score management
- Enhanced UX: Victory and game over overlays with clear instructions
- Flexible Controls: Support for both arrow keys and WASD
- Robust Score System: Persistent best score with error handling
- Performance Optimized: 60 FPS gameplay with efficient algorithms
- Type Safety: Full type hints for better code reliability
- Extensible Design: Easy to add new features like animations or themes
- Python 3.7 or higher
- pip package manager
-
Clone the repository:
git clone https://github.com/AlexTkDev/game2048.git cd game2048 -
Install dependencies:
pip install -r requirements.txt
python main.py- Arrow Keys: โ/โ/โ/โ to move tiles
- WASD Keys: W/A/S/D as alternative movement controls
- R Key: Restart the game
- ESC/Q Key: Quit the game
- Space: Continue playing after reaching 2048
- Movement: Use controls to slide all tiles in the chosen direction
- Combining: When two tiles with identical numbers collide, they merge into one tile with double the value
- New Tiles: After each move, a new tile (2 or 4) appears in a random empty spot
- Victory: Reach the 2048 tile to win (you can continue playing for higher scores)
- Game Over: When no more moves are possible, the game ends
- Points: Earned by combining tiles (equal to the new tile's value)
- Best Score: Automatically saved and persists between game sessions
- File Storage: Scores saved in
best_score.jsonwith error handling
The refactored codebase follows modern software engineering principles:
Game2048: Main game logic and state managementGameRenderer: All visual rendering and display logicScoreManager: Score tracking and persistenceGameEventHandler: Input processing and event managementGame2048App: Application orchestration and main game loop
- Separation of Concerns: Each class has a single responsibility
- State Management: Clear game states (Playing, Game Over, etc.)
- Error Handling: Comprehensive exception handling for file operations
- Type Safety: Full type annotations for better maintainability
The game is designed for easy customization:
- Colors: Modify the
Colorsdataclass for different themes - Grid Size: Change
GRID_SIZEconstant (requires UI adjustments) - Tile Values: Extend
TILE_COLORSdictionary for higher numbers - Controls: Add new key mappings in
GameEventHandler.KEY_MAP
The modular architecture makes it easy to add:
- Smooth tile animations
- Sound effects and music
- Multiple grid sizes (3ร3, 5ร5, etc.)
- Undo functionality
- Online leaderboards
- Custom themes and skins
- Mobile touch controls
- Type Hints: Full typing support for better IDE integration
- Error Handling: Robust exception management
- Performance: Optimized algorithms and 60 FPS rendering
- Documentation: Comprehensive docstrings and comments
The modular design enables easy unit testing of individual components:
- Game logic testing
- Score management verification
- Renderer component testing
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! The refactored architecture makes it easy to:
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
Found a bug? Please create an issue with:
- Python version
- Operating system
- Steps to reproduce
- Expected vs actual behavior
- Python: 3.7+
- Pygame: 2.6.0
- Memory: ~10MB RAM
- Storage: <1MB disk space
Enjoy playing 2048! ๐ฎ
This refactored version provides a solid foundation for further development while maintaining the classic 2048 gameplay experience.