mirror of
https://github.com/guezoloic/racing-game.git
synced 2026-03-28 18:03:50 +00:00
ajout des actions accélérer/rallentir et correction de la condition de dérapage (>=) (Car)
This commit is contained in:
42
src/Car.java
42
src/Car.java
@@ -47,7 +47,7 @@ public class Car {
|
|||||||
public static enum State {
|
public static enum State {
|
||||||
/**
|
/**
|
||||||
* L'état NORMAL du Vehicule avance selon un chiffre au alentour de 1 à 6 cases
|
* L'état NORMAL du Vehicule avance selon un chiffre au alentour de 1 à 6 cases
|
||||||
* par tour. Il consomme 6 unités de carburant à chaque tour. Si l'on
|
* par tour. Il consomme 2 unités de carburant à chaque tour. Si l'on
|
||||||
* accelere, il passe à l'état BOOST. Si on Rallenti, il passe à l'état LOW.
|
* accelere, il passe à l'état BOOST. Si on Rallenti, il passe à l'état LOW.
|
||||||
*/
|
*/
|
||||||
// @formatter:off
|
// @formatter:off
|
||||||
@@ -202,6 +202,44 @@ public class Car {
|
|||||||
return fuel;
|
return fuel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Retourne l'état courant de la voiture. */
|
||||||
|
public State getState() {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Clique sur "Accelerer" : change d'état et retourne un message (si nécessaire). */
|
||||||
|
public String accelerate() {
|
||||||
|
// Si endommagée => l'énoncé dit qu'on ne peut pas bouger
|
||||||
|
if (state.isDamaged()) {
|
||||||
|
return "Voiture endommagée : impossible d'accélérer";
|
||||||
|
}
|
||||||
|
State next = state.accelerate();
|
||||||
|
|
||||||
|
// Énoncé : en BOOST, si on accélère encore => message "déjà max"
|
||||||
|
if (state == State.BOOST && next == State.BOOST) {
|
||||||
|
return "Déjà à la vitesse maximale";
|
||||||
|
}
|
||||||
|
state = next;
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Clique sur "Rallentir" : change d'état et retourne un message (si nécessaire). */
|
||||||
|
public String decelerate() {
|
||||||
|
if (state.isDamaged()) {
|
||||||
|
return "Voiture endommagée : impossible de ralentir";
|
||||||
|
}
|
||||||
|
|
||||||
|
State next = state.decelerate();
|
||||||
|
|
||||||
|
// Énoncé : en STOPPED, si on ralentit encore => message "déjà arrêtée"
|
||||||
|
if (state == State.STOPPED && next == State.STOPPED) {
|
||||||
|
return "Déjà arrêtée";
|
||||||
|
}
|
||||||
|
|
||||||
|
state = next;
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fait avancer la voiture d'un certain nombre de positions.
|
* Fait avancer la voiture d'un certain nombre de positions.
|
||||||
* <p>
|
* <p>
|
||||||
@@ -227,7 +265,7 @@ public class Car {
|
|||||||
Point point = MAP.getPath(pos);
|
Point point = MAP.getPath(pos);
|
||||||
Map.Circuit element = MAP.getElement(point.x, point.y);
|
Map.Circuit element = MAP.getElement(point.x, point.y);
|
||||||
|
|
||||||
if (element.isYRoad() && element.getValue() < jump) {
|
if (element.isYRoad() && element.getValue() <= jump) {
|
||||||
System.out.println(NAME + " a un\taccident");
|
System.out.println(NAME + " a un\taccident");
|
||||||
setDamage();
|
setDamage();
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -54,7 +54,10 @@ public class Dashboard extends GameView {
|
|||||||
*/
|
*/
|
||||||
public Dashboard(Game game, Car car, String title, int width, int height, int x, int y) {
|
public Dashboard(Game game, Car car, String title, int width, int height, int x, int y) {
|
||||||
super(game, "Dashboard: " + title, width, height, x, y);
|
super(game, "Dashboard: " + title, width, height, x, y);
|
||||||
|
|
||||||
this.car = car;
|
this.car = car;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Fond de la fenêtre = couleur de la voiture
|
// Fond de la fenêtre = couleur de la voiture
|
||||||
frame.setBackground(car.getColor());
|
frame.setBackground(car.getColor());
|
||||||
@@ -64,12 +67,15 @@ public class Dashboard extends GameView {
|
|||||||
centerPanel.add(infoLabel);
|
centerPanel.add(infoLabel);
|
||||||
centerPanel.add(stateLabel);
|
centerPanel.add(stateLabel);
|
||||||
centerPanel.add(messageLabel);
|
centerPanel.add(messageLabel);
|
||||||
|
//pour rendre le panel transparent
|
||||||
|
centerPanel.setOpaque(false);
|
||||||
|
|
||||||
// ----- Partie boutons -----
|
// ----- Partie boutons -----
|
||||||
JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
|
JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
|
||||||
buttonPanel.add(decelerateButton);
|
buttonPanel.add(decelerateButton);
|
||||||
buttonPanel.add(pauseButton);
|
buttonPanel.add(pauseButton);
|
||||||
buttonPanel.add(accelerateButton);
|
buttonPanel.add(accelerateButton);
|
||||||
|
|
||||||
|
|
||||||
setLayout(new BorderLayout());
|
setLayout(new BorderLayout());
|
||||||
add(centerPanel, BorderLayout.CENTER);
|
add(centerPanel, BorderLayout.CENTER);
|
||||||
|
|||||||
Reference in New Issue
Block a user