添加并修改index.html和server.js
This commit is contained in:
parent
64ab2be1f7
commit
5a31135339
47
index.html
Normal file
47
index.html
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<link rel="stylesheet icon" href="data:" type="image/x-icon" />
|
||||||
|
<title>陆战棋游戏</title>
|
||||||
|
<style>
|
||||||
|
button {
|
||||||
|
padding: 0.5rem;
|
||||||
|
color: lightgray;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.player1 {
|
||||||
|
color: black;
|
||||||
|
}
|
||||||
|
|
||||||
|
.player2 {
|
||||||
|
color: red;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>陆战棋游戏</h1>
|
||||||
|
<h2 id="role"></h2>
|
||||||
|
<button id="gameButton">玩家1</button>
|
||||||
|
<script src="/socket.io/socket.io.min.js"></script>
|
||||||
|
<script>
|
||||||
|
const client = io();
|
||||||
|
|
||||||
|
client.on("role", (name) => {
|
||||||
|
role.innerText = name;
|
||||||
|
});
|
||||||
|
|
||||||
|
client.on("player", (plaayer) => {
|
||||||
|
gamepadButton.classList = plaayer;
|
||||||
|
gamepadButton.classList = plaayer == "player" ? "玩家1" : "玩家2";
|
||||||
|
});
|
||||||
|
|
||||||
|
gamepadButton.onclick = () => {
|
||||||
|
client.emit("click");
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
40
server.js
Normal file
40
server.js
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
const express = require('express')
|
||||||
|
const app = express()
|
||||||
|
app.use(express.static('public'))
|
||||||
|
const host = process.env.HOST || '0.0.0.0'
|
||||||
|
const port = process.env.PORT || 8000
|
||||||
|
const server = app.listen(port, host, () => console.log(`陆战棋游戏运行在:http://${host}:${port}/`))
|
||||||
|
const io = require('socket.io')(server)
|
||||||
|
|
||||||
|
const clients = []
|
||||||
|
let currentPlayer = null
|
||||||
|
|
||||||
|
io.on('connection', (client) => {
|
||||||
|
// 来人了
|
||||||
|
clients.push(client)
|
||||||
|
|
||||||
|
const i = clients.indexOf(client)
|
||||||
|
if (i == 0) client.emit('role', '玩家1')
|
||||||
|
else if (i == 1) client.emit('role', '玩家2')
|
||||||
|
else client.emit('role', '旁观者')
|
||||||
|
|
||||||
|
if (clients.length == 2) {
|
||||||
|
currentPlayer = client[0]
|
||||||
|
} else {
|
||||||
|
currentPlayer = null
|
||||||
|
}
|
||||||
|
io.emit('player', clients.length < 2 || currentPlayer != clients[1] ? 'player1' : 'player2')
|
||||||
|
})
|
||||||
|
|
||||||
|
client.on('click', () => {
|
||||||
|
if (clients.length >= 2 && client == currentPlayer) {
|
||||||
|
// 切换玩家
|
||||||
|
if (currentPlayer === clients[0]) {
|
||||||
|
currentPlayer = clients[1]
|
||||||
|
io.emit('player', 'player2')
|
||||||
|
} else {
|
||||||
|
currentPlayer = clients[0]
|
||||||
|
io.emit('player', 'player1')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user