mirror of
https://github.com/guezoloic/racing-game.git
synced 2026-03-28 18:03:50 +00:00
feat: ajout et correction multiple classe
This commit is contained in:
@@ -54,6 +54,7 @@ public class Dashboard extends GameView
|
|||||||
{
|
{
|
||||||
isPaused = fn.get();
|
isPaused = fn.get();
|
||||||
updateButtonText();
|
updateButtonText();
|
||||||
|
GameView.update();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -68,5 +69,7 @@ public class Dashboard extends GameView
|
|||||||
+ "</td></tr><tr><td>Nombre de Tour: " + car.getRound()
|
+ "</td></tr><tr><td>Nombre de Tour: " + car.getRound()
|
||||||
+ "</td></tr></table></html>"
|
+ "</td></tr></table></html>"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
updateButtonText();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
247
src/Game.java
247
src/Game.java
@@ -1,95 +1,169 @@
|
|||||||
// import java.awt.Color;
|
import java.awt.Color;
|
||||||
// import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
// public class Game
|
public class Game
|
||||||
// {
|
{
|
||||||
// private boolean paused;
|
public static record CarInfo(String name, Color color) {}
|
||||||
// private Car[] cars;
|
|
||||||
// private Map map;
|
|
||||||
// private ArrayList<Color> colors;
|
|
||||||
|
|
||||||
// private final int time = 1000;
|
public static class Builder
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Liste des voitures ajoutées au jeu.
|
||||||
|
* On utilise ArrayList pour pouvoir ajouter/supprimer des voitures dynamiquement.
|
||||||
|
*/
|
||||||
|
private ArrayList<CarInfo> cars = new ArrayList<>();
|
||||||
|
private State state = new State();
|
||||||
|
private int time = 1000;
|
||||||
|
|
||||||
// public static class Builder
|
private Map map = null;
|
||||||
// {
|
|
||||||
// private int pnumber = 3;
|
|
||||||
// private Map map = null;
|
|
||||||
// private State state = new State();
|
|
||||||
// private ArrayList<Color> colors = new ArrayList<>(3);
|
|
||||||
|
|
||||||
// public Builder setPlayers(int pnumber)
|
public Builder addCar(CarInfo car)
|
||||||
// {
|
{
|
||||||
// this.pnumber = pnumber;
|
cars.add(car);
|
||||||
// return this;
|
return this;
|
||||||
// }
|
}
|
||||||
|
|
||||||
// public Builder addColor(Color color)
|
public Builder remCar(CarInfo car)
|
||||||
// {
|
{
|
||||||
// colors.add(color);
|
cars.remove(car);
|
||||||
// return this;
|
return this;
|
||||||
// }
|
}
|
||||||
|
|
||||||
// public Builder addColor(Color[] c)
|
public Builder setMap(Map map)
|
||||||
// {
|
{
|
||||||
// for (Color color : c)
|
this.map = map;
|
||||||
// colors.add(color);
|
return this;
|
||||||
// return this;
|
}
|
||||||
// }
|
|
||||||
|
public Builder setState(State s)
|
||||||
// public Builder remColor(Color color)
|
{
|
||||||
// {
|
this.state = s;
|
||||||
// colors.remove(color);
|
return this;
|
||||||
// return this;
|
}
|
||||||
// }
|
|
||||||
|
public Builder setTime(int time)
|
||||||
|
{
|
||||||
|
this.time = time;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Game build()
|
||||||
|
{
|
||||||
|
if (map == null)
|
||||||
|
{
|
||||||
|
System.err.println("Vous devez declarer la map avant de build!");
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
return new Game(map, cars, state, time);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private final Map map;
|
||||||
|
private final State state;
|
||||||
|
private final int time;
|
||||||
|
|
||||||
|
private final ArrayList<Car> cars = new ArrayList<>();
|
||||||
|
private final ArrayList<GObserver> obs = new ArrayList<>();
|
||||||
|
|
||||||
|
private boolean isPaused = false;
|
||||||
|
|
||||||
|
public Game(Map map, ArrayList<CarInfo> cars, State state, int time)
|
||||||
|
{
|
||||||
|
this.map = map;
|
||||||
|
this.state = state;
|
||||||
|
this.time = time;
|
||||||
|
|
||||||
|
init(cars);
|
||||||
|
}
|
||||||
|
|
||||||
// public Builder setMap(Map map)
|
|
||||||
// {
|
|
||||||
// this.map = map;
|
|
||||||
// return this;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// public Builder setState(State s)
|
|
||||||
// {
|
|
||||||
// this.state = s;
|
|
||||||
// return this;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// public Builder defaultMap()
|
|
||||||
// {
|
|
||||||
// Integer[][] map = new Integer[][]
|
|
||||||
// {
|
|
||||||
// { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
|
||||||
// { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, -1, -1, 5, 0 },
|
|
||||||
// { 0, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 0, 0, -1, 0, 0, -1, 0 },
|
|
||||||
// { 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0 },
|
|
||||||
// { 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0 },
|
|
||||||
// { 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, -1, -1, 2, 0, 0, -1, 0 },
|
|
||||||
// { 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0 },
|
|
||||||
// { 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0 },
|
|
||||||
// { 0, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -3, 0 },
|
|
||||||
// { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
|
||||||
// };
|
|
||||||
|
|
||||||
// this.map = Map.fromInts(map);
|
|
||||||
// return this;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// public Game build()
|
|
||||||
// {
|
|
||||||
// if (map == null) defaultMap();
|
|
||||||
// return new Game(pnumber, map, state, colors);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// public Game(int pnumber, Map map, State state, ArrayList<Color> colors)
|
private Game init(ArrayList<CarInfo> carInfos)
|
||||||
// {
|
{
|
||||||
// this.map = map;
|
new Track(map, cars, "Piste Formule 1", 1000, 500, 1, 1);
|
||||||
// this.colors = colors;
|
new Rankboard("Score", cars, 200, 200, 0, 510);
|
||||||
|
|
||||||
|
final int loop = map.getPathSize();
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
for (CarInfo ci : carInfos)
|
||||||
|
{
|
||||||
|
Car car = new Car(ci.name, ci.color, loop, state);
|
||||||
|
new Dashboard(car, car.toString(), this::togglePause, 300, 200, 1000, 200*i);
|
||||||
|
cars.add(car);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
// cars = new Car[pnumber];
|
GameView.update();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isFinish()
|
||||||
|
{
|
||||||
|
for (Car car : cars)
|
||||||
|
{
|
||||||
|
if (car.getFuel() == 0)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Game addObserver(GObserver o)
|
||||||
|
{
|
||||||
|
obs.add(o);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Game remObserver(GObserver o)
|
||||||
|
{
|
||||||
|
obs.remove(o);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized boolean togglePause()
|
||||||
|
{
|
||||||
|
if (isPaused) notifyAll();
|
||||||
|
isPaused = !isPaused;
|
||||||
|
GameView.update();
|
||||||
|
return isPaused;
|
||||||
|
}
|
||||||
|
|
||||||
|
private synchronized void step() throws InterruptedException
|
||||||
|
{
|
||||||
|
for (int i = 0; i < cars.size(); i++)
|
||||||
|
{
|
||||||
|
// pause du jeu, while si on notifyall sans faire exprès un autre bout de code
|
||||||
|
while (isPaused)
|
||||||
|
wait();
|
||||||
|
|
||||||
|
cars.get(i).run();
|
||||||
|
|
||||||
|
for (GObserver o : obs)
|
||||||
|
{
|
||||||
|
if (!o.apply())
|
||||||
|
{
|
||||||
|
System.err.println("Une erreur s'est produite pendant le jeu.");
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
while (!isFinish())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
step();
|
||||||
|
GameView.update();
|
||||||
|
Thread.sleep(time);
|
||||||
|
}
|
||||||
|
catch (InterruptedException e)
|
||||||
|
{ e.printStackTrace(); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// init(pnumber, state);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// private Game init(int pnumber, State state)
|
// private Game init(int pnumber, State state)
|
||||||
// {
|
// {
|
||||||
@@ -119,13 +193,6 @@
|
|||||||
// return false;
|
// return false;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// public synchronized boolean togglePause()
|
|
||||||
// {
|
|
||||||
// if (paused) notifyAll();
|
|
||||||
// paused = !paused;
|
|
||||||
// GameView.update();
|
|
||||||
// return paused;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// private void step() throws InterruptedException
|
// private void step() throws InterruptedException
|
||||||
// {
|
// {
|
||||||
|
|||||||
@@ -24,19 +24,28 @@ public class Main {
|
|||||||
|
|
||||||
Map m = Map.fromInts(map);
|
Map m = Map.fromInts(map);
|
||||||
|
|
||||||
// game.run();
|
// // game.run();
|
||||||
|
|
||||||
// Dashboard d = new Dashboard(new Car(10, new State()), "Voiture 1", null, Color.BLUE, 500, 500, 10, 10);
|
// // Dashboard d = new Dashboard(new Car(10, new State()), "Voiture 1", null, Color.BLUE, 500, 500, 10, 10);
|
||||||
Car[] cars = new Car[] { new Car(m.getPathSize(), new State(), Color.PINK)};
|
// Car[] cars = new Car[] { new Car("Voiture 1", Color.PINK, m.getPathSize(), new State())};
|
||||||
System.out.println(cars[0].getLoop());
|
// System.out.println(cars[0].getLoop());
|
||||||
Track t = new Track(m, cars, null, 1080, 512, 0, 0);
|
// Track t = new Track(m, cars, null, 1080, 512, 0, 0);
|
||||||
Dashboard d = new Dashboard(cars[0], null, () -> true, 300, 300, 0, 0);
|
// Dashboard d = new Dashboard(cars[0], null, () -> true, 300, 300, 0, 0);
|
||||||
while(cars[0].getRound() < 1)
|
// Rankboard r = new Rankboard(null, 100, 100, 0, 0, cars);
|
||||||
{
|
// while(cars[0].getRound() < 1)
|
||||||
|
// {
|
||||||
|
|
||||||
cars[0].run();
|
// cars[0].run();
|
||||||
GameView.update();
|
// GameView.update();
|
||||||
Thread.sleep(500);
|
// Thread.sleep(500);
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
Game game = new Game.Builder()
|
||||||
|
.addCar(new Game.CarInfo("Voiture 1", Color.BLUE))
|
||||||
|
.addCar(new Game.CarInfo("Voiture 2", Color.PINK))
|
||||||
|
.addCar(new Game.CarInfo("Voiture 3", Color.RED))
|
||||||
|
.setMap(m)
|
||||||
|
.build();
|
||||||
|
game.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
import java.util.Arrays;
|
import java.util.ArrayList;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
|
|
||||||
public class Rankboard extends GameView
|
public class Rankboard extends GameView
|
||||||
{
|
{
|
||||||
Car[] cars;
|
ArrayList<Car> cars;
|
||||||
private final JLabel label;
|
private final JLabel label;
|
||||||
public Rankboard(String title, int width, int height, int x, int y, Car[] cars)
|
public Rankboard(String title, ArrayList<Car> cars, int width, int height, int x, int y)
|
||||||
{
|
{
|
||||||
super(title, width, height, x, y);
|
super(title, width, height, x, y);
|
||||||
this.cars = cars;
|
this.cars = cars;
|
||||||
@@ -18,9 +18,9 @@ public class Rankboard extends GameView
|
|||||||
|
|
||||||
private void updateRankText()
|
private void updateRankText()
|
||||||
{
|
{
|
||||||
Arrays.sort(cars, Comparator.comparingInt(Car::getScore).reversed());
|
cars.sort(Comparator.comparingInt(Car::getScore).reversed());
|
||||||
StringBuilder s = new StringBuilder();
|
StringBuilder s = new StringBuilder();
|
||||||
s.append("<html><table><tr><td>Score</td></tr>");
|
s.append("<html><table>");
|
||||||
for (Car c : cars)
|
for (Car c : cars)
|
||||||
{
|
{
|
||||||
s.append("<tr><td>" + c + ": " + c.getScore() + "%</td></tr>");
|
s.append("<tr><td>" + c + ": " + c.getScore() + "%</td></tr>");
|
||||||
|
|||||||
@@ -3,14 +3,15 @@ import java.awt.Font;
|
|||||||
import java.awt.FontMetrics;
|
import java.awt.FontMetrics;
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class Track extends GameView {
|
public class Track extends GameView {
|
||||||
private Map map;
|
private Map map;
|
||||||
private Car[] cars;
|
private ArrayList<Car> cars;
|
||||||
|
|
||||||
private final int scale = 75;
|
private final int scale = 75;
|
||||||
|
|
||||||
protected Track(Map map, Car[] cars, String title, int width, int height, int x, int y) {
|
protected Track(Map map, ArrayList<Car> cars, String title, int width, int height, int x, int y) {
|
||||||
super(title, width, height, x, y);
|
super(title, width, height, x, y);
|
||||||
this.map = map;
|
this.map = map;
|
||||||
this.cars = cars;
|
this.cars = cars;
|
||||||
@@ -66,13 +67,16 @@ public class Track extends GameView {
|
|||||||
|
|
||||||
private void drawCars(Graphics g, int[] cellCoord)
|
private void drawCars(Graphics g, int[] cellCoord)
|
||||||
{
|
{
|
||||||
|
int size = 1;
|
||||||
for (Car c : cars)
|
for (Car c : cars)
|
||||||
{
|
{
|
||||||
// index de pathMap
|
// index de pathMap
|
||||||
int i = c.getPos();
|
int i = c.getPos();
|
||||||
Point p = this.map.getPath(i);
|
Point p = this.map.getPath(i);
|
||||||
int[] ncoord = new int[] {p.x, p.y};
|
int[] ncoord = new int[] {p.x, p.y};
|
||||||
drawInnerBlock(g, ncoord, cellCoord, c.getColor());
|
drawInnerBlock(g, ncoord, cellCoord, c.getColor(), scale - size);
|
||||||
|
// ne jamais avoir la meme taille pour les voitures
|
||||||
|
size += 3;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,7 +116,7 @@ public class Track extends GameView {
|
|||||||
g.drawString(s, cx, cy);
|
g.drawString(s, cx, cy);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void drawInnerBlock(Graphics g, int[] coord, int[] cellCoord, Color c)
|
private void drawInnerBlock(Graphics g, int[] coord, int[] cellCoord, Color c, int scale)
|
||||||
{
|
{
|
||||||
// scale de la taille du bloc
|
// scale de la taille du bloc
|
||||||
int w = cellCoord[0] * scale / 100;
|
int w = cellCoord[0] * scale / 100;
|
||||||
@@ -140,7 +144,7 @@ public class Track extends GameView {
|
|||||||
g.setColor(getCellColor(Circuit.Cell.ROAD));
|
g.setColor(getCellColor(Circuit.Cell.ROAD));
|
||||||
g.fillRect(x * cellWidth, y * cellHeight, cellWidth, cellHeight);
|
g.fillRect(x * cellWidth, y * cellHeight, cellWidth, cellHeight);
|
||||||
|
|
||||||
drawInnerBlock(g, coord, cellCoord, getCellColor(cell.getType()));
|
drawInnerBlock(g, coord, cellCoord, getCellColor(cell.getType()), scale);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
g.setColor(getCellColor(cell.getType()));
|
g.setColor(getCellColor(cell.getType()));
|
||||||
|
|||||||
Reference in New Issue
Block a user