/
ledart
/
RRP_LedNik
Обзор
Документация
Войти
/
ledart
/
RRP_LedNik
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
client/UI/GameField.java
144 строки
4 KB
Darya
обновлены комментарии
18 дек 2025, 14:46
18 дек 2025, 14:46
95c0a7f
Код
Авторство
О чём код?
package client.UI; import javafx.application.Platform; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.paint.Color; import java.util.LinkedList; import java.util.Map; import java.util.Timer; import java.util.TimerTask; public class GameField extends Canvas { public static final int CELL_SIZE = 20; public static final int GRID_SIZE = 20; public static final int WIDTH = GRID_SIZE * CELL_SIZE; public static final int HEIGHT = GRID_SIZE * CELL_SIZE; private Map<String, SnakeState> state; private Timer timer; private boolean running = false; public GameField() { setWidth(WIDTH); setHeight(HEIGHT); drawGrid(); // Рисуем сетку при создании } public synchronized void updateState(Map<String, SnakeState> state) { System.out.println("[GameField] updateState called, snakes: " + (state != null ? state.size() : 0)); this.state = state; Platform.runLater(this::draw); } public void start() { if (running) return; running = true; timer = new Timer(true); timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { Platform.runLater(GameField.this::draw); } }, 0, 100); // 10 FPS System.out.println("[GameField] GameField started"); } public void stop() { if (timer != null) { timer.cancel(); timer = null; } running = false; System.out.println("[GameField] GameField stopped"); } private synchronized void draw() { GraphicsContext gc = getGraphicsContext2D(); // Очищаем поле gc.setFill(Color.BLACK); gc.fillRect(0, 0, WIDTH, HEIGHT); // Рисуем сетку drawGrid(); if (state == null || state.isEmpty()) { // Рисуем сообщение, если нет змей gc.setFill(Color.WHITE); gc.fillText("Waiting for game state...", 10, 20); return; } // Рисуем змей for (Map.Entry<String, SnakeState> entry : state.entrySet()) { String name = entry.getKey(); SnakeState snake = entry.getValue(); // Выбираем цвет if (name.contains("Bot") || name.toLowerCase().contains("bot")) { gc.setFill(Color.RED); // Бот - красный } else { gc.setFill(Color.LIMEGREEN); // Игрок - зелёный } // Рисуем тело змеи for (int[] p : snake.positions) { if (p.length >= 2) { gc.fillRect( p[0] * CELL_SIZE, p[1] * CELL_SIZE, CELL_SIZE - 1, CELL_SIZE - 1 ); } } // Подписываем имя игрока над головой if (!snake.positions.isEmpty()) { int[] head = snake.positions.getFirst(); if (head.length >= 2) { gc.setFill(Color.WHITE); gc.fillText(name, head[0] * CELL_SIZE, head[1] * CELL_SIZE - 5); } } } // Отображаем количество змей gc.setFill(Color.WHITE); gc.fillText("Snakes: " + state.size(), 10, 20); } private void drawGrid() { GraphicsContext gc = getGraphicsContext2D(); gc.setStroke(Color.GRAY); gc.setLineWidth(0.5); // Вертикальные линии for (int x = 0; x <= WIDTH; x += CELL_SIZE) { gc.strokeLine(x, 0, x, HEIGHT); } // Горизонтальные линии for (int y = 0; y <= HEIGHT; y += CELL_SIZE) { gc.strokeLine(0, y, WIDTH, y); } } public void setPlayerDir(int x, int y) { // Заглушка, направление отправляется через Network } public static class SnakeState { public LinkedList<int[]> positions; public int[] dir; public SnakeState(LinkedList<int[]> positions, int[] dir) { this.positions = positions; this.dir = dir; } } }