Download game.js from Bubaa001/Fast2: direct link, hf CLI and curl.
- Browser
- Download file 2.69 kB
-
https://huggingface.co/spaces/Bubaa001/Fast2/resolve/main/game.js
- Command line
-
hf download hf://spaces/Bubaa001/Fast2/game.js
-
curl -L -o game.js https://huggingface.co/spaces/Bubaa001/Fast2/resolve/main/game.js
2.69 kB
| const canvas = document.getElementById('gameCanvas'); | |
| const ctx = canvas.getContext('2d'); | |
| const statusText = document.getElementById('status'); | |
| // Game settings | |
| const GRID_SIZE = 20; | |
| const CELL_SIZE = Math.min(window.innerWidth, window.innerHeight * 0.8) / GRID_SIZE; | |
| canvas.width = GRID_SIZE * CELL_SIZE; | |
| canvas.height = GRID_SIZE * CELL_SIZE; | |
| // Player data | |
| const playerId = Math.random().toString(36).substring(2, 9); | |
| let direction = 'right'; | |
| let gameState = { | |
| players: {}, | |
| fruits: [], | |
| obstacles: [] | |
| }; | |
| // WebSocket connection | |
| const ws = new WebSocket(`wss://${window.location.host}/ws/${playerId}`); | |
| ws.onopen = () => { | |
| statusText.textContent = "Connected! Waiting for players..."; | |
| }; | |
| ws.onmessage = (event) => { | |
| const data = JSON.parse(event.data); | |
| if (data.type === "game_state") { | |
| gameState = data.state; | |
| renderGame(); | |
| } else if (data.type === "player_disconnected") { | |
| alert(`Player ${data.player_id} disconnected!`); | |
| } | |
| }; | |
| // Control buttons | |
| document.getElementById('up').addEventListener('touchstart', () => changeDirection('up')); | |
| document.getElementById('left').addEventListener('touchstart', () => changeDirection('left')); | |
| document.getElementById('right').addEventListener('touchstart', () => changeDirection('right')); | |
| document.getElementById('down').addEventListener('touchstart', () => changeDirection('down')); | |
| function changeDirection(newDirection) { | |
| direction = newDirection; | |
| ws.send(JSON.stringify({ | |
| type: "direction", | |
| direction: newDirection | |
| })); | |
| } | |
| function renderGame() { | |
| ctx.clearRect(0, 0, canvas.width, canvas.height); | |
| // Draw grid | |
| ctx.strokeStyle = '#333'; | |
| ctx.lineWidth = 0.5; | |
| for (let i = 0; i <= GRID_SIZE; i++) { | |
| ctx.beginPath(); | |
| ctx.moveTo(i * CELL_SIZE, 0); | |
| ctx.lineTo(i * CELL_SIZE, canvas.height); | |
| ctx.stroke(); | |
| ctx.beginPath(); | |
| ctx.moveTo(0, i * CELL_SIZE); | |
| ctx.lineTo(canvas.width, i * CELL_SIZE); | |
| ctx.stroke(); | |
| } | |
| // Draw players | |
| Object.entries(gameState.players).forEach(([id, player]) => { | |
| ctx.fillStyle = id === playerId ? '#ff3300' : '#0095ff'; | |
| ctx.beginPath(); | |
| ctx.arc( | |
| player.x * CELL_SIZE + CELL_SIZE / 2, | |
| player.y * CELL_SIZE + CELL_SIZE / 2, | |
| CELL_SIZE * 0.4, | |
| 0, | |
| Math.PI * 2 | |
| ); | |
| ctx.fill(); | |
| }); | |
| } | |
| // Handle window resize | |
| window.addEventListener('resize', () => { | |
| const newCellSize = Math.min(window.innerWidth, window.innerHeight * 0.8) / GRID_SIZE; | |
| if (newCellSize !== CELL_SIZE) { | |
| location.reload(); | |
| } | |
| }); |