Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ronin game #2

Merged
merged 3 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ronin Battle</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Ronin Boss Battle</h1>
<div id="game">
<div id="player" class="entity"></div>
<div id="ronin" class="entity"></div>
</div>
<script src="script.js"></script>
</body>
</html>
42 changes: 42 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
document.addEventListener('keydown', movePlayer);
let player = document.getElementById('player');
let ronin = document.getElementById('ronin');

function movePlayer(e) {
let playerStyle = window.getComputedStyle(player);
let playerLeft = parseInt(playerStyle.left);
let playerTop = parseInt(playerStyle.top);

if (e.key === 'ArrowLeft' && playerLeft > 0) {
player.style.left = (playerLeft - 10) + 'px';
}
if (e.key === 'ArrowRight' && playerLeft < 550) {
player.style.left = (playerLeft + 10) + 'px';
}
if (e.key === 'ArrowUp' && playerTop > 0) {
player.style.top = (playerTop - 10) + 'px';
}
if (e.key === 'ArrowDown' && playerTop < 350) {
player.style.top = (playerTop + 10) + 'px';
}

checkCollision();
}

function checkCollision() {
let playerRect = player.getBoundingClientRect();
let roninRect = ronin.getBoundingClientRect();

if (playerRect.x < roninRect.x + roninRect.width &&
playerRect.x + playerRect.width > roninRect.x &&
playerRect.y < roninRect.y + roninRect.height &&
playerRect.y + playerRect.height > roninRect.y) {
alert("Battle with Ronin!");
resetPositions();
}
}

function resetPositions() {
player.style.left = '0px';
player.style.top = '0px';
}
28 changes: 28 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
body {
display: flex;
flex-direction: column;
align-items: center;
font-family: Arial, sans-serif;
}

#game {
position: relative;
width: 600px;
height: 400px;
border: 2px solid black;
}

.entity {
position: absolute;
width: 50px;
height: 50px;
}

#player {
background-color: blue;
}

#ronin {
background-color: red;
left: 550px;
}