<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple RPG</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="start-menu">
<h1>Simple RPG</h1>
<p>Use the arrow keys to move. Move close to the Pokemon and catch it using 'a' key!</p>
<button class="start-button">Start</button>
</div>
<div class="game-container">
<div class="player">
<div class="Character">
<img class="Character_shadow pixelart" src="https://i.imgur.com/ZGxDY2Q.png" alt="Shadow" />
<img class="Character_spritesheet pixelart" src="https://i.imgur.com/qCNmOMr.png" alt="Character">
</div>
</div>
<div class="enemy">
<img src="https://i.imgur.com/L0wjEr4.gif" alt="Enemy">
</div>
</div>
<!-- background music when user clicks the page the sound will start -->
<audio id="bg-music" loop>
<source src="https://cf-media.sndcdn.com/63SCde80ScSe.128.mp3?Policy=eyJTdGF0ZW1lbnQiOlt7IlJlc291cmNlIjoiKjovL2NmLW1lZGlhLnNuZGNkbi5jb20vNjNTQ2RlODBTY1NlLjEyOC5tcDMqIiwiQ29uZGl0aW9uIjp7IkRhdGVMZXNzVGhhbiI6eyJBV1M6RXBvY2hUaW1lIjoxNjgwMjE1NjkxfX19XX0_&Signature=YTniu8mMi7MnZ73piCrPuWN6ablvfBUdiq~Vp-lfpqmvV5x8wpllIOQw-uK3xJA7Sx6VvO9OTDRNSbUDZky2NtvIEyZIusxZA42VbHex4D~v2uk8pRERFCDLju8f8YI~ZOZfx2lejupChPkR9G2szKsIzdOlOQwUz6xkkiun8cTomyJisYoBbgmvKlF1wVJ9anH5KufJEmc~nR~iB2YGsD93GE~HLf6WwDGhsspiQn~BjvK7ZCIeRrcbGbyz8gGP98HwWHqLge9MKbBhXNWpIv5YOu-1990z2FuY1-Pu7CoO97IO~fqhHswdVRam1b4LTNYS4TLWUa4R5i9r4iaRxA__&Key-Pair-Id=APKAI6TU7MMXM5DG6EPQ" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<script>
// Start playing the audio after a user interaction (e.g., a click)
document.body.addEventListener('click', function () {
var bgMusic = document.getElementById('bg-music');
if (!bgMusic.playing) {
bgMusic.play();
bgMusic.playing = true;
}
});
</script>
<script src="script.js"></script>
</body>
</html>
body {
margin: 0;
font-family: sans-serif;
background-color:black;
}
.start-menu {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
backdrop-filter: blur(5px);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 2;
}
.start-menu h1 {
font-size: 48px;
color: white;
margin-bottom: 24px;
}
.start-menu p {
font-size: 24px;
color: white;
margin-bottom: 48px;
}
.start-button {
font-size: 24px;
padding: 16px 32px;
background-color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.game-container {
background: url("https://i.imgur.com/ZGIgXvm.png") ;
position: relative;
width: 800px;
height: 600px;
border: 1px solid black;
margin: 0 auto;
filter: blur(0px);
transition: filter 0.5s ease-in-out;
}
.game-container.blur {
filter: blur(5px);
}
.Character {
position: absolute;
width: 32px;
height: 32px;
overflow: hidden;
}
.Character_spritesheet {
animation: moveSpritesheet 1s steps(4) infinite;
width: 128px;
position: absolute;
}
.Character_shadow {
position: absolute;
width: 32px;
height: 32px;
}
.pixelart {
image-rendering: pixelated;
}
.player {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.enemy {
position: absolute;
}
@keyframes moveSpritesheet {
from {
transform: translate(0px, 0px);
}
to {
transform: translate(-128px, 0px);
}
}
.Character_spritesheet.face-left {
top: -32px;
}
.Character_spritesheet.face-right {
top: -64px;
}
.Character_spritesheet.face-up {
top: -96px;
}
/* pokemon size */
.enemy img {
max-width: 40px;
max-height: 40px;
}
const player = document.querySelector('.player');
const enemy = document.querySelector('.enemy');
const gameContainer = document.querySelector('.game-container');
const startMenu = document.querySelector('.start-menu');
const startButton = document.querySelector('.start-button');
const playerSpeed = 5;
const attackDistance = 50;
// Spawn player and enemy
spawnPlayer();
spawnEnemy();
function spawnPlayer() {
player.style.left = '50%';
player.style.top = '50%';
}
function spawnEnemy() {
const x = Math.random() * (gameContainer.clientWidth - enemy.clientWidth);
const y = Math.random() * (gameContainer.clientHeight - enemy.clientHeight);
enemy.style.left = `${x}px`;
enemy.style.top = `${y}px`;
}
function movePlayer(dx, dy) {
const characterSpritesheet = player.querySelector('.Character_spritesheet');
const newX = parseFloat(player.style.left) + dx;
const newY = parseFloat(player.style.top) + dy;
if (newX >= 0 && newX <= gameContainer.clientWidth - player.clientWidth) {
player.style.left = `${newX}px`;
}
if (newY >= 0 && newY <= gameContainer.clientHeight - player.clientHeight) {
player.style.top = `${newY}px`;
}
// remove all face-* classes
characterSpritesheet.classList.remove('face-left', 'face-right', 'face-up');
// add the appropriate face-* class based on movement direction
if (dx < 0) {
characterSpritesheet.classList.add('face-left');
} else if (dx > 0) {
characterSpritesheet.classList.add('face-right');
} else if (dy < 0) {
characterSpritesheet.classList.add('face-up');
}
}
function attack() {
const playerRect = player.getBoundingClientRect();
const enemyRect = enemy.getBoundingClientRect();
const distance = Math.sqrt(Math.pow((playerRect.x - enemyRect.x), 2) + Math.pow((playerRect.y - enemyRect.y), 2));
if (distance <= attackDistance) {
spawnEnemy();
}
}
document.addEventListener('keydown', (e) => {
switch (e.key) {
case 'ArrowUp':
movePlayer(0, -playerSpeed);
break;
case 'ArrowDown':
movePlayer(0, playerSpeed);
break;
case 'ArrowLeft':
movePlayer(-playerSpeed, 0);
break;
case 'ArrowRight':
movePlayer(playerSpeed, 0);
break;
case 'a':
attack();
break;
}
});
startButton.addEventListener('click', () => {
// hide the start menu
startMenu.style.display = 'none';
// remove the blur effect from the game container
gameContainer.classList.remove('blur');
// set focus on the game container so keyboard events are captured
gameContainer.focus();
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.