/
alex_ep0411
/
java_lab2
Обзор
Документация
Войти
/
alex_ep0411
/
java_lab2
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/model/GameModel.java
99 строк
3 KB
александр
лабораторная работа 2
08 июн 2026, 13:18
08 июн 2026, 13:18
6f466e8
Код
Авторство
О чём код?
package model; import javafx.beans.property.*; import javafx.geometry.Point2D; import java.util.Timer; import java.util.TimerTask; public class GameModel { public IntegerProperty scoreProperty = new SimpleIntegerProperty(0); public BooleanProperty gameActiveProperty = new SimpleBooleanProperty(true); public IntegerProperty ballSpeedProperty = new SimpleIntegerProperty(20); public ObjectProperty<Point2D> ballPosition = new SimpleObjectProperty<>(new Point2D(400, 300)); public Timer timer; private TimerTask task; private double dx = 0.8; private double dy = 0.8; private double width = 800; private double height = 600; private int radius = 30; public GameModel() { startTimer(); } private void startTimer() { if (timer != null) timer.cancel(); timer = new Timer(true); task = new TimerTask() { @Override public void run() { javafx.application.Platform.runLater(() -> move()); } }; timer.schedule(task, 0, ballSpeedProperty.get()); } public void restartTimer() { startTimer(); } public void move() { if (!gameActiveProperty.get()) return; Point2D pos = ballPosition.get(); double newX = pos.getX() + dx; double newY = pos.getY() + dy; if (newX < radius) { newX = radius; dx = -dx; } if (newX > width - radius) { newX = width - radius; dx = -dx; } if (newY < radius) { newY = radius; dy = -dy; } if (newY > height - radius) { newY = height - radius; dy = -dy; } ballPosition.set(new Point2D(newX, newY)); } public void hit() { if (gameActiveProperty.get()) { scoreProperty.set(scoreProperty.get() + 1); } } public void reset() { scoreProperty.set(0); gameActiveProperty.set(true); ballPosition.set(new Point2D(width / 2, height / 2)); dx = 0.8; dy = 0.8; restartTimer(); } public void setSize(double w, double h) { width = w; height = h; } public void runFromCursor(double mx, double my) { if (!gameActiveProperty.get()) return; Point2D pos = ballPosition.get(); double dxToBall = pos.getX() - mx; double dyToBall = pos.getY() - my; double dist = Math.sqrt(dxToBall*dxToBall + dyToBall*dyToBall); if (dist < 100 && dist > 0) { double force = (100 - dist) / 200; dx = dx + (dxToBall / dist) * force * 0.3; dy = dy + (dyToBall / dist) * force * 0.3; double maxSpeed = 2.0; double mag = Math.sqrt(dx*dx + dy*dy); if (mag > maxSpeed) { dx = dx / mag * maxSpeed; dy = dy / mag * maxSpeed; } } } }