Initial commit
This commit is contained in:
17
testing/SocketIO_Testing/frontend/index.html
Normal file
17
testing/SocketIO_Testing/frontend/index.html
Normal file
@@ -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>Erste Socket.io Seite</title>
|
||||
<link rel="stylesheet" href="style/style.css" />
|
||||
</head>
|
||||
<body>
|
||||
<h1>Multigame</h1>
|
||||
</body>
|
||||
|
||||
<script src="https://cdn.socket.io/4.7.2/socket.io.min.js"></script>
|
||||
<script src="index.js" type="module"></script>
|
||||
|
||||
<canvas id="game" height="300px" width="300px" id> </canvas>
|
||||
</html>
|
||||
37
testing/SocketIO_Testing/frontend/index.js
Normal file
37
testing/SocketIO_Testing/frontend/index.js
Normal file
@@ -0,0 +1,37 @@
|
||||
/** @type {import("../backend/node_modules/socket.io-client").Socket} */
|
||||
const socket = io("http://localhost:3000");
|
||||
|
||||
const gameScreen = document.getElementById("game");
|
||||
const ctx = gameScreen.getContext("2d")
|
||||
|
||||
ctx.fillStyle = "red";
|
||||
|
||||
socket.on("connect", () => {
|
||||
console.log(`Connected as ${socket.id}`);
|
||||
});
|
||||
|
||||
let keysPressed = new Set();
|
||||
|
||||
document.addEventListener("keydown", (e) => {
|
||||
keysPressed.add(e.key);
|
||||
});
|
||||
|
||||
document.addEventListener("keyup", (e) => {
|
||||
keysPressed.delete(e.key);
|
||||
});
|
||||
|
||||
setInterval(() => {
|
||||
if (keysPressed.has("ArrowUp")) socket.emit("moveUp");
|
||||
if (keysPressed.has("ArrowDown")) socket.emit("moveDown");
|
||||
if (keysPressed.has("ArrowRight")) socket.emit("moveRight");
|
||||
if (keysPressed.has("ArrowLeft")) socket.emit("moveLeft");
|
||||
}, 50);
|
||||
|
||||
socket.on("updatePos", (map) => {
|
||||
ctx.clearRect(0, 0, gameScreen.width, gameScreen.height);
|
||||
map.forEach(e => {
|
||||
ctx.beginPath();
|
||||
ctx.arc(e.x, e.y, 10, 0, Math.PI * 2, false);
|
||||
ctx.fill();
|
||||
});
|
||||
})
|
||||
3
testing/SocketIO_Testing/frontend/style/style.css
Normal file
3
testing/SocketIO_Testing/frontend/style/style.css
Normal file
@@ -0,0 +1,3 @@
|
||||
#game{
|
||||
background-color: grey;
|
||||
}
|
||||
Reference in New Issue
Block a user