From f8b38808d533ca8fe6df8b470ba265b428bbc391 Mon Sep 17 00:00:00 2001 From: bosskkev <kossivikevin29@gmail.com> Date: Sun, 12 Nov 2023 13:44:19 +0100 Subject: [PATCH] Correction bug vitesse. Modification de la boucle while(true) et augmentation de la vitesse dans PhysicEngine --- src/main/java/engine/physic/PhysicEngine.java | 2 +- src/main/java/pong/PongApp.java | 25 ++++++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/main/java/engine/physic/PhysicEngine.java b/src/main/java/engine/physic/PhysicEngine.java index d2f7163..82cfeeb 100644 --- a/src/main/java/engine/physic/PhysicEngine.java +++ b/src/main/java/engine/physic/PhysicEngine.java @@ -18,7 +18,7 @@ public class PhysicEngine implements Engine { for (PongObject pongObject: Kernel.gameObjects) { Coordinates2D speed = new Coordinates2D(0, 0); if(pongObject.getName().equals("Ball")){ - speed = new Coordinates2D(1, 1); + speed = new Coordinates2D(2, 2); } //Coordinates2D speed = new Coordinates2D(random.nextInt(2) - 1, random.nextInt(2) - 1); pongObjectEntityHashMap.put(pongObject, new Movable(1, pongObject.getPosition(), speed, new Coordinates2D(0, 0), pongObject.getWidth(), pongObject.getHeight())); diff --git a/src/main/java/pong/PongApp.java b/src/main/java/pong/PongApp.java index 8a34fb9..5d71dbf 100644 --- a/src/main/java/pong/PongApp.java +++ b/src/main/java/pong/PongApp.java @@ -35,14 +35,27 @@ public class PongApp { //kernel.setGameObject(components); - while(true){ - try { - kernel.update(); + final long MIN_INTERVAL_TIME = 10; // Minimum time between two iterations - Thread.sleep(1); - } catch (InterruptedException e) { - e.printStackTrace(); + + long lastIterationTime = System.currentTimeMillis(); //Initialize the last iteration time + + while (true) { + + kernel.update(); // Updating the kernel and the game state + + long elapsedTime = System.currentTimeMillis() - lastIterationTime; + + // If the elapsed time is less than the minimum interval + if (elapsedTime < MIN_INTERVAL_TIME) { + try { + Thread.sleep(MIN_INTERVAL_TIME - elapsedTime); // Wait until the minimum interval is reached + } catch (InterruptedException e) { + e.printStackTrace(); + } } + lastIterationTime = System.currentTimeMillis(); // Update the last iteration time with the current time } + } } -- GitLab