In this tutorial, we'll enhance our ball shooter game by adding player controls. We'll allow the player to move a shooter left and right and fire beams to interact with falling balls.
Implementing Player Movement
To control the shooter, we need to handle keyboard inputs. We’ll use the keydown and keyup events to move the shooter left and right.
Updating Our Game Code
1. Add Shooter Object
First, define the shooter object in game.js:
javascript
const shooter = {
x: canvas.width / 2,
y: canvas.height - 30,
width: 60,
height: 20,
speed: 5,
};
function drawShooter() {
ctx.fillStyle = 'blue';
ctx.fillRect(shooter.x, shooter.y, shooter.width, shooter.height);
}
2. Handle Keyboard Input
Add event listeners to capture keyboard input:
javascript
let keys = {};
window.addEventListener('keydown', (e) => {
keys[e.key] = true;
});
window.addEventListener('keyup', (e) => {
keys[e.key] = false;
});
3. Update Shooter Position
Update the shooter’s position based on keyboard input in the draw function:
javascript
function updateShooter() {
if (keys['ArrowLeft'] && shooter.x > 0) {
shooter.x -= shooter.speed;
}
if (keys['ArrowRight'] && shooter.x < canvas.width - shooter.width) {
shooter.x += shooter.speed;
}
}
4. Integrate Shooter Update and Draw Functions
Call updateShooter and drawShooter in the draw function:
javascript
function draw() {
// Clear the canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update and draw the shooter
updateShooter();
drawShooter();
// Draw other game elements here
requestAnimationFrame(draw);
}
draw();
Adding Beam Shooting
Let’s add functionality for shooting beams:
1. Define Beam Object
Add the beam object:
javascript
const beams = [];
const beamSpeed = 7;
function shootBeam() {
beams.push({
x: shooter.x + shooter.width / 2,
y: shooter.y,
width: 5,
height: 10,
});
}
2. Handle Shooting Input
Update the keydown event to shoot beams:
javascript
window.addEventListener('keydown', (e) => {
keys[e.key] = true;
if (e.key === ' ') {
shootBeam();
}
});
3. Update Beam Position
Update beam positions and draw them:
javascript
function updateBeams() {
beams.forEach((beam, index) => {
beam.y -= beamSpeed;
if (beam.y < 0) {
beams.splice(index, 1);
}
});
}
function drawBeams() {
ctx.fillStyle = 'red';
beams.forEach((beam) => {
ctx.fillRect(beam.x, beam.y, beam.width, beam.height);
});
}
4. Integrate Beam Update and Draw Functions
Call updateBeams and drawBeams in the draw function:
javascript
function draw() {
// Clear the canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update and draw the shooter
updateShooter();
drawShooter();
// Update and draw beams
updateBeams();
drawBeams();
// Draw other game elements here
requestAnimationFrame(draw);
}
draw();
Conclusion
With player controls and shooting mechanics added, your game is now more interactive! You can continue to enhance the game by adding features like beam collision detection, scoring, and more. Keep experimenting and have fun with game development!
Stay tuned for more tutorials and tips on building engaging and fun games!
Comments
Post a Comment