Skip to content

Commit 348e265

Browse files
author
Brigit Murtaugh
committed
Enhance Pacman game by adding rotation to Pacman's movement based on direction
1 parent 4421101 commit 348e265

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

pacman.html

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,25 @@
242242
}
243243

244244
function drawPacman() {
245+
const x = pacman.x * tileSize + tileSize/2;
246+
const y = pacman.y * tileSize + tileSize/2;
247+
const radius = tileSize/2-2;
248+
249+
// Determine rotation angle based on direction
250+
let rotationAngle = 0;
251+
if (pacman.dx === 1) rotationAngle = 0; // right
252+
else if (pacman.dx === -1) rotationAngle = Math.PI; // left
253+
else if (pacman.dy === -1) rotationAngle = 1.5 * Math.PI; // up
254+
else if (pacman.dy === 1) rotationAngle = 0.5 * Math.PI; // down
255+
245256
ctx.fillStyle = '#FFFF00';
246257
ctx.beginPath();
247-
ctx.arc(pacman.x * tileSize + tileSize/2, pacman.y * tileSize + tileSize/2, tileSize/2-2, 0.25 * Math.PI, 1.75 * Math.PI, false);
248-
ctx.lineTo(pacman.x * tileSize + tileSize/2, pacman.y * tileSize + tileSize/2);
258+
ctx.save();
259+
ctx.translate(x, y);
260+
ctx.rotate(rotationAngle);
261+
ctx.arc(0, 0, radius, 0.25 * Math.PI, 1.75 * Math.PI, false);
262+
ctx.lineTo(0, 0);
263+
ctx.restore();
249264
ctx.fill();
250265
}
251266
function movePacman() {

0 commit comments

Comments
 (0)