-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b1bcf7
commit 79f638c
Showing
4 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Document</title> | ||
<link rel="stylesheet" href="css/test.css" /> | ||
<!-- 匯入jquery --> | ||
<script | ||
src="https://code.jquery.com/jquery-3.7.1.js" | ||
integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" | ||
crossorigin="anonymous" | ||
></script> | ||
<script src=" | ||
https://cdn.jsdelivr.net/npm/[email protected]/lib/anime.min.js"></script> | ||
<link | ||
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" | ||
rel="stylesheet" | ||
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" | ||
crossorigin="anonymous" | ||
/> | ||
<script | ||
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" | ||
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" | ||
crossorigin="anonymous" | ||
></script> | ||
<link rel="stylesheet" href="css/minigame2.css" /> | ||
<script src="js/minigame2.js"></script> | ||
</head> | ||
<body> | ||
<div id="player" class="text-bg-primary">player</div> | ||
<div id="boss" class="text-bg-danger">boss</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
var horizontal = 0; | ||
var vertical = 0; | ||
var speed = 10; | ||
var keys = {}; | ||
var playerPosition = { x: 0, y: 0 }; | ||
var projectiles = []; | ||
var projectileIndex = 0; | ||
var shootDirection = "right"; | ||
var windowWidth = 0; | ||
var windowHeight = 0; | ||
|
||
$(document).ready(() => { | ||
windowWidth = document.documentElement.clientWidth; | ||
windowHeight = document.documentElement.clientHeight; | ||
window.addEventListener("keydown", (e) => { | ||
keys[e.key] = true; // Mark the key as pressed | ||
}); | ||
|
||
window.addEventListener("keyup", (e) => { | ||
keys[e.key] = false; // Mark the key as released | ||
}); | ||
|
||
setInterval(() => { | ||
getInput(); | ||
updatePlayerPosition(); | ||
movemPosition(); | ||
}, 100); | ||
}); | ||
|
||
function getInput() { | ||
if (keys["a"]) { | ||
shootDirection = "left"; | ||
horizontal -= speed; | ||
} | ||
if (keys["d"]) { | ||
shootDirection = "right"; | ||
horizontal += speed; | ||
} | ||
if (keys["w"]) { | ||
vertical -= speed; | ||
} | ||
if (keys["s"]) { | ||
vertical += speed; | ||
} | ||
// 這是空白鍵 | ||
if (keys[" "]) { | ||
spawnProjectile(); | ||
} | ||
horizontal = Clamp(horizontal, 0, windowWidth); | ||
vertical = Clamp(vertical, 0, windowHeight); | ||
} | ||
|
||
function Clamp(num, min, max) { | ||
return Math.min(Math.max(num, min), max); | ||
} | ||
|
||
function movemPosition() { | ||
anime({ | ||
targets: "#player", | ||
translateX: horizontal, | ||
translateY: vertical, | ||
}); | ||
} | ||
|
||
function updatePlayerPosition() { | ||
playerPosition.x = horizontal; | ||
playerPosition.y = vertical; | ||
} | ||
|
||
function spawnProjectile() { | ||
var projectile = $( | ||
`<div id = "projectile${projectileIndex}" class = "projectile"></div>` | ||
); | ||
|
||
projectile.css({ | ||
border: "0px", | ||
position: "absolute", | ||
left: `${playerPosition.x}px`, | ||
top: `${playerPosition.y}px`, | ||
width: "20px", | ||
height: "10px", | ||
backgroundColor: "red", | ||
}); | ||
|
||
$("body").append(projectile); | ||
|
||
shoot(`#projectile${projectileIndex}`); | ||
projectileIndex++; | ||
} | ||
|
||
function shoot(target) { | ||
direction = shootDirection == "right" ? 1000 : -1000; | ||
|
||
anime({ | ||
targets: target, | ||
translateX: direction, | ||
// 透明 | ||
background: "rgba(255, 0, 0, 0)", | ||
duration: 500, | ||
easing: "linear", | ||
begin: () => { | ||
console.log(`begin`); | ||
}, | ||
update: () => { | ||
// console.log(target); | ||
// var top = $(target).css("top"); | ||
// var left = $(target).css("left"); | ||
// projectiles[target] = { top: top, left: left }; | ||
// console.log(projectiles[target]); | ||
}, | ||
complete: () => { | ||
console.log(target); | ||
var top = $(target).css("top"); | ||
var left = $(target).css("left"); | ||
projectiles[target] = { top: top, left: left }; | ||
console.log(projectiles[target]); | ||
}, | ||
}); | ||
} | ||
|
||
function detect() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters