Skip to content
Snippets Groups Projects
Commit 92be2984 authored by bosskkev's avatar bosskkev
Browse files

Correction débordement et modification du constructeur de movable

parent f90eedcf
No related branches found
No related tags found
No related merge requests found
Pipeline #22755 failed
......@@ -9,6 +9,8 @@ import java.io.IOException;
import java.util.ArrayList;
public class Grid extends JFrame {
public Kernel kernel;
public Grid() throws IOException {
......
......@@ -32,7 +32,7 @@ public class Kernel {
for (PongObject pongObject: PongApp.components) {
//Coordinates2D speed = new Coordinates2D(random.nextInt(2) - 1, random.nextInt(2) - 1);
Coordinates2D speed = new Coordinates2D(0, 1);
pongObjectEntityHashMap.put(pongObject, new Movable(1, pongObject.getPosition(), speed, new Coordinates2D(0, 0), 1));
pongObjectEntityHashMap.put(pongObject, new Movable(1, pongObject.getPosition(), speed, new Coordinates2D(0, 0), pongObject.getWidth(), pongObject.getHeight()));
grid.draw(new ImageComponent(pongObject.getImage(), pongObject.getPosition(), pongObject.getWidth(), pongObject.getHeight()));
}
}
......
package engine;
import pong.PongApp;
public class Movable implements Entity {
private int weight;
private int width;
private int height;
private Coordinates2D speed;
private Coordinates2D position;
private Coordinates2D acceleration;
private int remainingLife;
Movable(int weight, Coordinates2D position, Coordinates2D speed, Coordinates2D acceleration, int remainingLife) {
Movable(int weight, Coordinates2D position, Coordinates2D speed, Coordinates2D acceleration, int width, int height) {
this.weight = weight;
this.speed = speed;
this.position = position;
this.acceleration = acceleration;
this.remainingLife = remainingLife;
this.width = width;
this.height = height;
}
@Override
......@@ -57,10 +62,10 @@ public class Movable implements Entity {
Coordinates2D newPosition = new Coordinates2D(this.position.getX(), this.position.getY());
newPosition.add(this.speed);
if (newPosition.getX() < 0 || newPosition.getX() >= 776) {
if (newPosition.getX() < 0 || newPosition.getX() >= PongApp.width - this.width) {
this.speed = new Coordinates2D(-speed.getX(), speed.getY());
}
if (newPosition.getY() < 0 || newPosition.getY() >= 405) {
if (newPosition.getY() < 0 || newPosition.getY() >= PongApp.height - this.height -20) { // -20 à cause de la barre verticale de la fenêtre
this.speed = new Coordinates2D(speed.getX(), -speed.getY());
}
this.position.add(this.speed); // Modifying the position (x = x + vx ; y = y + vy)
......
......@@ -12,6 +12,8 @@ import java.util.ArrayList;
public class PongApp {
public static ArrayList<PongObject> components = new ArrayList<PongObject>();
public static int width = 850;
public static int height = 500;
public PongApp() throws IOException {
}
......@@ -29,7 +31,7 @@ public class PongApp {
components.add(pongBall);
Grid grid = new Grid(); // Will create a new grid and a new kernel
grid.init("Pong", 850, 500); // Will start the kernel and set the keys listeners
grid.init("Pong", width, height); // Will start the kernel and set the keys listeners
while(true){
try {
grid.kernel.update();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment