A 2D survival shooter game built with HTML5 Canvas and JavaScript where you fight against waves of zombies (jombies) while managing your health and score.
Fight against endless waves of zombies in this side-scrolling survival game. Use your shooting skills and movement abilities to survive as long as possible while the difficulty increases with your score.
- Modern web browser with HTML5 Canvas support
- Local web server (recommended for image loading)
zombie-fighter/
├── index.html # Main HTML file
├── style.css # Game styling
├── main.js # Game logic (paste the provided JavaScript code)
└── images/
├── survivor1.png # Player character sprite
└── jombie1.png # Zombie enemy sprite
- Create the project folder and add the files above
- Paste the JavaScript code into
main.js
- Add sprite images to the
images/
folder:survivor1.png
- Player character (100x100px recommended)jombie1.png
- Zombie enemy (100x100px recommended)
- Start a local server to avoid CORS issues with image loading:
# Using Python 3 python -m http.server 8000 # Using Node.js (if you have http-server installed) npx http-server
- Open your browser and navigate to
http://localhost:8000
Survive as long as possible by shooting zombies and avoiding contact with them.
- Arrow Keys: Move left/right
- Arrow Up / Spacebar: Jump
- Mouse Click: Aim and shoot bullets
- Left Button: Move left
- Jump Button: Jump
- Right Button: Move right
- Pause Button: Pause/resume game
- Touch Screen: Tap to aim and shoot
-
Health System: Start with 10 health points
- Lose 1 health when touched by a zombie
- Game ends when health reaches 0
-
Scoring: Earn 10 points for each zombie eliminated
-
Difficulty Scaling:
- Score 0-99: Zombies move at speed 2
- Score 100-199: Zombies move at speed 4
- Score 200+: Zombies move at speed 6
-
Physics:
- Gravity affects both player and bullets
- Player can jump and move horizontally
- Bullets arc due to gravity
-
Environmental Elements:
- Green blocks provide cover and can block zombies
- Blocks are destroyed when hit by zombies
- HTML5 Canvas for rendering
- JavaScript ES6 for game logic
- CSS3 for styling and layout
- Survivor Class: Player character with movement and gravity
- Jombie Class: Zombie enemies with AI movement
- Bullet System: Projectile physics with gravity
- Collision Detection: AABB collision detection for all entities
- Game Loop: RequestAnimationFrame for smooth animation
animate()
: Main game loopcollisionbulletenemy()
: Bullet-zombie collision detectioncollision()
: General AABB collision detectionhandleKeyPress()
: Keyboard input handling
- Efficient sprite removal when off-screen
- Optimized collision detection
- Smooth 60 FPS animation loop
- Power-ups: Add special abilities or weapons
- Different Enemy Types: Create zombies with unique behaviors
- Level System: Add different backgrounds or obstacles
- Sound Effects: Integrate Web Audio API for immersive audio
Edit these values in the code:
// Zombie spawn rate (currently every 2-4 seconds)
setInterval(() => { /* spawn logic */ }, 2000);
// Zombie speeds by score range
if (score < 100) { speed = 2; }
else if (score < 200) { speed = 4; }
else { speed = 6; }
// Player health (currently 10)
let health = 10;
Modify style.css
to change:
- Button appearance and positioning
- Canvas border and background
- Control layout and responsiveness
- Image Loading: Sprites may not load without a local server due to CORS restrictions
- Mobile Responsiveness: Game may need adjustments for different screen sizes
- Block Collision: Block collision detection could be more precise
- Main Menu: Instructions and start button
- Playing: Active gameplay with enemies spawning
- Paused: Game temporarily stopped
- Game Over: Score display and restart option
- Add sound effects and background music
- Implement different weapon types
- Create multiple levels with varying backgrounds
- Add particle effects for explosions
- Implement a high score system
- Add mobile touch controls optimization
- Create different zombie types with unique abilities
- Chrome 60+ ✅
- Firefox 55+ ✅
- Safari 11+ ✅
- Edge 79+ ✅
Feel free to fork this project and submit pull requests for improvements!
This project is open source and available under the MIT License.