snake
This commit is contained in:
BIN
DEV3.2/TP05/04_Serpent/ControleurClavier.class
Normal file
BIN
DEV3.2/TP05/04_Serpent/ControleurClavier.class
Normal file
Binary file not shown.
32
DEV3.2/TP05/04_Serpent/ControleurClavier.java
Normal file
32
DEV3.2/TP05/04_Serpent/ControleurClavier.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import java.awt.event.KeyListener;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
|
||||||
|
public class ControleurClavier implements KeyListener {
|
||||||
|
|
||||||
|
private ArrayDeque<String> touches;
|
||||||
|
|
||||||
|
public ControleurClavier() {
|
||||||
|
this.touches = new ArrayDeque<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyPressed(KeyEvent e) {
|
||||||
|
if (this.touches.isEmpty() || !this.touches.getLast().equals(KeyEvent.getKeyText(e.getKeyCode()))) {
|
||||||
|
this.touches.addLast(KeyEvent.getKeyText(e.getKeyCode()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyReleased(KeyEvent e) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyTyped(KeyEvent e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayDeque<String> getTouches() {
|
||||||
|
return this.touches;
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
@@ -13,7 +13,8 @@ public class Fenetre extends JFrame {
|
|||||||
for (int i = 0; i != 5; i++) {
|
for (int i = 0; i != 5; i++) {
|
||||||
coordSnake.addLast(new Point(i+5, 5));
|
coordSnake.addLast(new Point(i+5, 5));
|
||||||
}
|
}
|
||||||
this.add(new JGrilleDeJeu(coordSnake));
|
JGrilleDeJeu grille = new JGrilleDeJeu(this, coordSnake);
|
||||||
|
this.add(grille);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@@ -3,39 +3,39 @@ import javax.swing.*;
|
|||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.ArrayDeque;
|
import java.util.ArrayDeque;
|
||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
public class JGrilleDeJeu extends JComponent {
|
public class JGrilleDeJeu extends JComponent {
|
||||||
|
|
||||||
private static final int TAILLE_GRILLE = 25;
|
private static final int TAILLE_GRILLE = 25;
|
||||||
private char[][] grille;
|
private char[][] grille;
|
||||||
private ArrayDeque<Point> coordSnake;
|
private ArrayDeque<Point> coordSnake;
|
||||||
|
private Point posPomme;
|
||||||
|
private ControleurClavier controleur;
|
||||||
|
private Fenetre fenetre;
|
||||||
|
private String directionActuelle;
|
||||||
|
private Timer timer;
|
||||||
|
private int intervalleTimer;
|
||||||
|
|
||||||
public JGrilleDeJeu(ArrayDeque<Point> coordSnake) {
|
public JGrilleDeJeu(Fenetre fenetre, ArrayDeque<Point> coordSnake) {
|
||||||
|
this.fenetre = fenetre;
|
||||||
this.grille = new char[TAILLE_GRILLE][TAILLE_GRILLE];
|
this.grille = new char[TAILLE_GRILLE][TAILLE_GRILLE];
|
||||||
this.coordSnake = coordSnake;
|
this.coordSnake = coordSnake;
|
||||||
|
this.controleur = new ControleurClavier();
|
||||||
for (int i = 0; i != TAILLE_GRILLE; i++) {
|
this.fenetre.addKeyListener(controleur);
|
||||||
for (int j = 0; j != TAILLE_GRILLE; j++) {
|
this.directionActuelle = "Right";
|
||||||
this.grille[i][j] = 'g';
|
this.intervalleTimer = 500;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Point point : coordSnake) {
|
|
||||||
this.grille[point.x][point.y] = 'o';
|
|
||||||
}
|
|
||||||
|
|
||||||
Random r = new Random();
|
Random r = new Random();
|
||||||
int coordXPomme = Math.abs(r.nextInt() % TAILLE_GRILLE);
|
int coordXPomme = Math.abs(r.nextInt() % TAILLE_GRILLE);
|
||||||
int coordYPomme = Math.abs(r.nextInt() % TAILLE_GRILLE);
|
int coordYPomme = Math.abs(r.nextInt() % TAILLE_GRILLE);
|
||||||
|
|
||||||
|
this.posPomme = new Point(coordXPomme, coordYPomme);
|
||||||
|
|
||||||
this.grille[coordXPomme][coordYPomme] = 'r';
|
this.grille[coordXPomme][coordYPomme] = 'r';
|
||||||
|
|
||||||
Timer timer = new Timer();
|
this.timer = new Timer();
|
||||||
|
timer.scheduleAtFixedRate(new TacheTimer(this), 0, intervalleTimer);
|
||||||
while (true) {
|
|
||||||
timer.wait(1000L);
|
|
||||||
this.avancerSnake();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -61,17 +61,114 @@ public class JGrilleDeJeu extends JComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
secondPinceau.fillRect(
|
secondPinceau.fillRect(
|
||||||
this.getWidth()/TAILLE_GRILLE*i,
|
this.getWidth()/TAILLE_GRILLE*i+1,
|
||||||
this.getHeight()/TAILLE_GRILLE*j,
|
this.getHeight()/TAILLE_GRILLE*j+1,
|
||||||
this.getWidth()/TAILLE_GRILLE,
|
this.getWidth()/TAILLE_GRILLE-2,
|
||||||
this.getHeight()/TAILLE_GRILLE
|
this.getHeight()/TAILLE_GRILLE-2
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void avancerSnake() {
|
public void avancerSnake() {
|
||||||
this.coordSnake.removeFirst();
|
this.coordSnake.removeFirst();
|
||||||
this.coordSnake.addLast(new Point(this.coordSnake.getLast().x+1, this.coordSnake.getLast().y));
|
if (!this.controleur.getTouches().isEmpty()) {
|
||||||
|
String toucheAModifier = this.controleur.getTouches().removeFirst();
|
||||||
|
switch (this.directionActuelle) {
|
||||||
|
case "Right":
|
||||||
|
if (!toucheAModifier.equals("Left")) {
|
||||||
|
this.directionActuelle = toucheAModifier;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "Left":
|
||||||
|
if (!toucheAModifier.equals("Right")) {
|
||||||
|
this.directionActuelle = toucheAModifier;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "Up":
|
||||||
|
if (!toucheAModifier.equals("Down")) {
|
||||||
|
this.directionActuelle = toucheAModifier;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "Down":
|
||||||
|
if (!toucheAModifier.equals("Up")) {
|
||||||
|
this.directionActuelle = toucheAModifier;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
switch (this.directionActuelle) {
|
||||||
|
case "Right":
|
||||||
|
this.coordSnake.addLast(new Point(this.coordSnake.getLast().x+1, this.coordSnake.getLast().y));
|
||||||
|
break;
|
||||||
|
case "Left":
|
||||||
|
this.coordSnake.addLast(new Point(this.coordSnake.getLast().x-1, this.coordSnake.getLast().y));
|
||||||
|
break;
|
||||||
|
case "Up":
|
||||||
|
this.coordSnake.addLast(new Point(this.coordSnake.getLast().x, this.coordSnake.getLast().y-1));
|
||||||
|
break;
|
||||||
|
case "Down":
|
||||||
|
this.coordSnake.addLast(new Point(this.coordSnake.getLast().x, this.coordSnake.getLast().y+1));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i != TAILLE_GRILLE; i++) {
|
||||||
|
for (int j = 0; j != TAILLE_GRILLE; j++) {
|
||||||
|
this.grille[i][j] = 'g';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.coordSnake.getLast().equals(this.posPomme)) {
|
||||||
|
Random r = new Random();
|
||||||
|
int coordXPomme = Math.abs(r.nextInt() % TAILLE_GRILLE);
|
||||||
|
int coordYPomme = Math.abs(r.nextInt() % TAILLE_GRILLE);
|
||||||
|
|
||||||
|
this.posPomme = new Point(coordXPomme, coordYPomme);
|
||||||
|
|
||||||
|
Point coordQueue = this.coordSnake.removeFirst();
|
||||||
|
Point coordAvantQueue = this.coordSnake.getFirst();
|
||||||
|
Point aAjouter;
|
||||||
|
|
||||||
|
if (coordAvantQueue.equals(new Point(coordQueue.x+1, coordQueue.y))) {
|
||||||
|
aAjouter = new Point(coordQueue.x-1, coordQueue.y);
|
||||||
|
}
|
||||||
|
else if (coordAvantQueue.equals(new Point(coordQueue.x-1, coordQueue.y))) {
|
||||||
|
aAjouter = new Point(coordQueue.x+1, coordQueue.y);
|
||||||
|
}
|
||||||
|
else if (coordAvantQueue.equals(new Point(coordQueue.x, coordQueue.y+1))) {
|
||||||
|
aAjouter = new Point(coordQueue.x, coordQueue.y-1);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
aAjouter = new Point(coordQueue.x, coordQueue.y+1);
|
||||||
|
}
|
||||||
|
this.coordSnake.addFirst(aAjouter);
|
||||||
|
this.coordSnake.addFirst(coordQueue);
|
||||||
|
|
||||||
|
if (this.intervalleTimer != 200) {
|
||||||
|
this.intervalleTimer -= 50;
|
||||||
|
this.timer.cancel();
|
||||||
|
this.timer = new Timer();
|
||||||
|
this.timer.scheduleAtFixedRate(new TacheTimer(this), 0, this.intervalleTimer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
int compteur = this.coordSnake.size();
|
||||||
|
for (Point point : this.coordSnake) {
|
||||||
|
if ((point.equals(this.coordSnake.getLast()) && compteur > 1)) {
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
this.grille[point.x][point.y] = 'o';
|
||||||
|
compteur--;
|
||||||
|
}
|
||||||
|
} catch (ArrayIndexOutOfBoundsException e) {
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.grille[posPomme.x][posPomme.y] = 'r';
|
||||||
|
|
||||||
|
this.repaint();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
8
DEV3.2/TP05/04_Serpent/Makefile
Normal file
8
DEV3.2/TP05/04_Serpent/Makefile
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
JGrilleDeJeu.class TacheTimer.class Fenetre.class ControleurClavier.class Main.class : JGrilleDeJeu.java TacheTimer.java Fenetre.java ControleurClavier.java Main.java
|
||||||
|
javac *.java
|
||||||
|
|
||||||
|
run: Main.class
|
||||||
|
java Main
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm *.class
|
||||||
BIN
DEV3.2/TP05/04_Serpent/TacheTimer.class
Normal file
BIN
DEV3.2/TP05/04_Serpent/TacheTimer.class
Normal file
Binary file not shown.
16
DEV3.2/TP05/04_Serpent/TacheTimer.java
Normal file
16
DEV3.2/TP05/04_Serpent/TacheTimer.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
|
public class TacheTimer extends TimerTask {
|
||||||
|
|
||||||
|
private JGrilleDeJeu grille;
|
||||||
|
|
||||||
|
public TacheTimer(JGrilleDeJeu grille) {
|
||||||
|
this.grille = grille;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
this.grille.avancerSnake();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user