/
mmarakayeva
/
lab2
Обзор
Документация
Войти
/
mmarakayeva
/
lab2
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/main/java/com/example/catchtheballgame/model/GameModel.java
161 строка
5 KB
Миленикс
first_commit
14 июн 2026, 22:25
14 июн 2026, 22:25
43f8825
Код
Авторство
О чём код?
package com.example.catchtheballgame.model; import javafx.beans.property.*; import java.util.Random; import java.util.Timer; import java.util.TimerTask; public class GameModel { private final DoubleProperty ballX = new SimpleDoubleProperty(); private final DoubleProperty ballY = new SimpleDoubleProperty(); private final double radius = 30; private double speedX = 150; private double speedY = 150; private final IntegerProperty score = new SimpleIntegerProperty(0); private final BooleanProperty gameActive = new SimpleBooleanProperty(true); private final StringProperty gameStatus = new SimpleStringProperty("Игра активна"); private final DoubleProperty currentSpeed = new SimpleDoubleProperty(150); private double fieldWidth = 800; private double fieldHeight = 600; private Timer timer; private final Random random = new Random(); private long lastUpdateTime = System.nanoTime(); public GameModel() { ballX.set(fieldWidth / 2); ballY.set(fieldHeight / 2); randomDirection(); } public void startGameLoop() { timer = new Timer("GameTimer", true); TimerTask task = new TimerTask() { @Override public void run() { if (gameActive.get()) { updatePosition(); } } }; timer.schedule(task, 0, 16); } private void updatePosition() { long now = System.nanoTime(); double deltaTime = (now - lastUpdateTime) / 1_000_000_000.0; lastUpdateTime = now; if (deltaTime > 0.05) deltaTime = 0.05; double newX = ballX.get() + speedX * deltaTime; double newY = ballY.get() + speedY * deltaTime; if (newX - radius < 0) { newX = radius; speedX = -speedX; } else if (newX + radius > fieldWidth) { newX = fieldWidth - radius; speedX = -speedX; } if (newY - radius < 0) { newY = radius; speedY = -speedY; } else if (newY + radius > fieldHeight) { newY = fieldHeight - radius; speedY = -speedY; } ballX.set(newX); ballY.set(newY); currentSpeed.set(Math.sqrt(speedX * speedX + speedY * speedY)); } public void onBallClick() { if (!gameActive.get()) return; score.set(score.get() + 1); speedX *= 1.05; speedY *= 1.05; double maxSpeed = 600; speedX = Math.min(Math.abs(speedX), maxSpeed) * Math.signum(speedX); speedY = Math.min(Math.abs(speedY), maxSpeed) * Math.signum(speedY); randomDirection(); } public void runAwayFromMouse(double mouseX, double mouseY) { if (!gameActive.get()) return; double dx = ballX.get() - mouseX; double dy = ballY.get() - mouseY; double distance = Math.sqrt(dx * dx + dy * dy); if (distance < 100 && distance > 1) { double angle = Math.atan2(dy, dx); double currentSpeedVal = Math.sqrt(speedX * speedX + speedY * speedY); double escapeSpeed = Math.min(currentSpeedVal * 1.5, 600); speedX = Math.cos(angle) * escapeSpeed; speedY = Math.sin(angle) * escapeSpeed; } } private void randomDirection() { double speed = Math.sqrt(speedX * speedX + speedY * speedY); if (speed < 50) speed = 150; double angle = random.nextDouble() * 2 * Math.PI; speedX = Math.cos(angle) * speed; speedY = Math.sin(angle) * speed; } public void togglePause() { gameActive.set(!gameActive.get()); gameStatus.set(gameActive.get() ? "Игра активна" : "ПАУЗА"); if (gameActive.get()) { lastUpdateTime = System.nanoTime(); } } public void resetGame() { score.set(0); gameActive.set(true); gameStatus.set("Игра активна"); speedX = 150; speedY = 150; randomDirection(); ballX.set(fieldWidth / 2); ballY.set(fieldHeight / 2); lastUpdateTime = System.nanoTime(); } public void updateFieldSize(double width, double height) { this.fieldWidth = width; this.fieldHeight = height; double newX = Math.max(radius, Math.min(ballX.get(), fieldWidth - radius)); double newY = Math.max(radius, Math.min(ballY.get(), fieldHeight - radius)); ballX.set(newX); ballY.set(newY); } public void stopGame() { if (timer != null) { timer.cancel(); } } public DoubleProperty ballXProperty() { return ballX; } public DoubleProperty ballYProperty() { return ballY; } public IntegerProperty scoreProperty() { return score; } public BooleanProperty gameActiveProperty() { return gameActive; } public StringProperty gameStatusProperty() { return gameStatus; } public DoubleProperty currentSpeedProperty() { return currentSpeed; } public double getRadius() { return radius; } }