File size: 2,687 Bytes
436be24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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();
    }
});