Skip to content

Commit 6ec3c6c

Browse files
author
Brigit Murtaugh
committed
Add Pacman game and navigation link to topnav
1 parent 5d04338 commit 6ec3c6c

2 files changed

Lines changed: 144 additions & 0 deletions

File tree

_includes/topnav.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ <h1>Development Containers</h1>
4141
<li {% if page.sectionid=='frogger-game' %} class="nav-item active" {% else %} class="nav-item" {% endif %}>
4242
<a class="nav-link" href='{{ "/frogger-game" | prepend: site.baseurl }}'>Frogger Game</a>
4343
</li>
44+
<li {% if page.sectionid=='pacman' %} class="nav-item active" {% else %} class="nav-item" {% endif %}>
45+
<a class="nav-link" href='{{ "/pacman" | prepend: site.baseurl }}'>Pacman</a>
46+
</li>
4447
</ul>
4548
</div>
4649

pacman.html

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
---
2+
layout: singlePage
3+
title: Pacman
4+
sectionid: pacman
5+
---
6+
7+
<!-- Search bar -->
8+
<input type="text" id="searchInput" placeholder="Search" style="margin-bottom: 20px; width: 100%; max-width: 400px;">
9+
10+
<!-- Pacman Game Styles and Script -->
11+
<style>
12+
#pacmanCanvas {
13+
background: #000;
14+
display: block;
15+
margin: 0 auto 20px auto;
16+
border: 2px solid #FFD700;
17+
}
18+
.pacman-link {
19+
color: #FFD700;
20+
text-decoration: underline;
21+
cursor: pointer;
22+
}
23+
</style>
24+
25+
<a href="#pacman-game" class="pacman-link"><h1 id="pacman-game">Pacman Game</h1></a>
26+
<canvas id="pacmanCanvas" width="448" height="496"></canvas>
27+
<p>Use arrow keys to move Pacman. Eat all the dots!</p>
28+
<script>
29+
// Simple Pacman game (mini version)
30+
const canvas = document.getElementById('pacmanCanvas');
31+
const ctx = canvas.getContext('2d');
32+
const tileSize = 16;
33+
const map = [
34+
'############################',
35+
'#............##............#',
36+
'#.####.#####.##.#####.####.#',
37+
'#o####.#####.##.#####.####o#',
38+
'#.####.#####.##.#####.####.#',
39+
'#..........................#',
40+
'#.####.##.########.##.####.#',
41+
'#.####.##.########.##.####.#',
42+
'#......##....##....##......#',
43+
'######.##### ## #####.######',
44+
'######.##### ## #####.######',
45+
'######.## ##.######',
46+
'######.## ######## ##.######',
47+
'######.## ######## ##.######',
48+
'#............##............#',
49+
'#.####.#####.##.#####.####.#',
50+
'#.####.#####.##.#####.####.#',
51+
'#o..##................##..o#',
52+
'###.##.##.########.##.##.###',
53+
'###.##.##.########.##.##.###',
54+
'#......##....##....##......#',
55+
'#.##########.##.##########.#',
56+
'#.##########.##.##########.#',
57+
'#..........................#',
58+
'############################'
59+
];
60+
let pacman = { x: 14, y: 17, dx: 0, dy: 0, nextDx: 0, nextDy: 0 };
61+
let score = 0;
62+
let dots = 0;
63+
for (let row of map) for (let c of row) if (c === '.' || c === 'o') dots++;
64+
function drawMap() {
65+
for (let y = 0; y < map.length; y++) {
66+
for (let x = 0; x < map[y].length; x++) {
67+
if (map[y][x] === '#') {
68+
ctx.fillStyle = '#2222FF';
69+
ctx.fillRect(x * tileSize, y * tileSize, tileSize, tileSize);
70+
} else if (map[y][x] === '.' || map[y][x] === 'o') {
71+
ctx.fillStyle = '#FFD700';
72+
ctx.beginPath();
73+
ctx.arc(x * tileSize + tileSize/2, y * tileSize + tileSize/2, map[y][x] === 'o' ? 4 : 2, 0, 2 * Math.PI);
74+
ctx.fill();
75+
}
76+
}
77+
}
78+
}
79+
function drawPacman() {
80+
ctx.fillStyle = '#FFFF00';
81+
ctx.beginPath();
82+
ctx.arc(pacman.x * tileSize + tileSize/2, pacman.y * tileSize + tileSize/2, tileSize/2-1, 0.25 * Math.PI, 1.75 * Math.PI, false);
83+
ctx.lineTo(pacman.x * tileSize + tileSize/2, pacman.y * tileSize + tileSize/2);
84+
ctx.fill();
85+
}
86+
function movePacman() {
87+
// Try to turn if possible
88+
if (canMove(pacman.x + pacman.nextDx, pacman.y + pacman.nextDy)) {
89+
pacman.dx = pacman.nextDx;
90+
pacman.dy = pacman.nextDy;
91+
}
92+
// Move if possible
93+
if (canMove(pacman.x + pacman.dx, pacman.y + pacman.dy)) {
94+
pacman.x += pacman.dx;
95+
pacman.y += pacman.dy;
96+
}
97+
// Wrap
98+
if (pacman.x < 0) pacman.x = map[0].length - 1;
99+
if (pacman.x >= map[0].length) pacman.x = 0;
100+
}
101+
function canMove(x, y) {
102+
return map[y] && map[y][x] && map[y][x] !== '#';
103+
}
104+
function eatDot() {
105+
let c = map[pacman.y][pacman.x];
106+
if (c === '.' || c === 'o') {
107+
map[pacman.y] = map[pacman.y].substring(0, pacman.x) + ' ' + map[pacman.y].substring(pacman.x + 1);
108+
score++;
109+
}
110+
}
111+
function drawScore() {
112+
ctx.fillStyle = '#FFF';
113+
ctx.font = '16px Arial';
114+
ctx.fillText('Score: ' + score, 10, canvas.height - 10);
115+
}
116+
function gameLoop() {
117+
ctx.clearRect(0, 0, canvas.width, canvas.height);
118+
drawMap();
119+
movePacman();
120+
eatDot();
121+
drawPacman();
122+
drawScore();
123+
if (score >= dots) {
124+
ctx.fillStyle = '#0F0';
125+
ctx.font = '32px Arial';
126+
ctx.fillText('You Win!', canvas.width/2 - 60, canvas.height/2);
127+
return;
128+
}
129+
requestAnimationFrame(gameLoop);
130+
}
131+
document.addEventListener('keydown', e => {
132+
if (["ArrowUp","ArrowDown","ArrowLeft","ArrowRight"].includes(e.key)) {
133+
e.preventDefault();
134+
if (e.key === 'ArrowUp') { pacman.nextDx = 0; pacman.nextDy = -1; }
135+
if (e.key === 'ArrowDown') { pacman.nextDx = 0; pacman.nextDy = 1; }
136+
if (e.key === 'ArrowLeft') { pacman.nextDx = -1; pacman.nextDy = 0; }
137+
if (e.key === 'ArrowRight') { pacman.nextDx = 1; pacman.nextDy = 0; }
138+
}
139+
});
140+
gameLoop();
141+
</script>

0 commit comments

Comments
 (0)