tp bd + tp5-6 dev3.2
This commit is contained in:
BIN
DEV/DEV3.2/TP05_Files/Action.class
Normal file
BIN
DEV/DEV3.2/TP05_Files/Action.class
Normal file
Binary file not shown.
59
DEV/DEV3.2/TP05_Files/Action.java
Normal file
59
DEV/DEV3.2/TP05_Files/Action.java
Normal file
@@ -0,0 +1,59 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
|
||||
public class Action implements KeyListener {
|
||||
|
||||
public static final Point HAUT=new Point(0,-1);
|
||||
public static final Point BAS=new Point(0,1);
|
||||
public static final Point GAUCHE=new Point(-1,0);
|
||||
public static final Point DROITE=new Point(1,0);
|
||||
public Serpent jeux;
|
||||
|
||||
public Action(Serpent jeux){
|
||||
this.jeux = jeux;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
int keyCode = e.getKeyCode();
|
||||
if (this.jeux.isFreeze == false){
|
||||
if (keyCode == KeyEvent.VK_UP){
|
||||
if (this.jeux.derniereDirection != Action.BAS){
|
||||
this.jeux.derniereDirection = Action.HAUT;
|
||||
this.jeux.direction.offer(Action.HAUT);
|
||||
}
|
||||
}
|
||||
if (keyCode == KeyEvent.VK_DOWN){
|
||||
if (this.jeux.derniereDirection != Action.HAUT){
|
||||
this.jeux.derniereDirection = Action.BAS;
|
||||
this.jeux.direction.offer(Action.BAS);
|
||||
}
|
||||
}
|
||||
if (keyCode == KeyEvent.VK_LEFT){
|
||||
if (this.jeux.derniereDirection != Action.DROITE){
|
||||
this.jeux.derniereDirection = Action.GAUCHE;
|
||||
this.jeux.direction.offer(Action.GAUCHE);
|
||||
}
|
||||
}
|
||||
if (keyCode == KeyEvent.VK_RIGHT){
|
||||
if (this.jeux.derniereDirection != Action.GAUCHE){
|
||||
this.jeux.derniereDirection = Action.DROITE;
|
||||
this.jeux.direction.offer(Action.DROITE);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (keyCode == KeyEvent.VK_SPACE){
|
||||
this.jeux.isFreeze = !this.jeux.isFreeze;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
}
|
||||
}
|
||||
BIN
DEV/DEV3.2/TP05_Files/Dessin.class
Normal file
BIN
DEV/DEV3.2/TP05_Files/Dessin.class
Normal file
Binary file not shown.
28
DEV/DEV3.2/TP05_Files/Dessin.java
Normal file
28
DEV/DEV3.2/TP05_Files/Dessin.java
Normal file
@@ -0,0 +1,28 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class Dessin extends JComponent {
|
||||
public Color couleur;
|
||||
|
||||
public Dessin(Color couleur){
|
||||
this.couleur = couleur;
|
||||
}
|
||||
|
||||
public void setColor(Color couleur){
|
||||
this.couleur = couleur;
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics pinceau) {
|
||||
Graphics secondPinceau = pinceau.create();
|
||||
Color couleurFond = this.getBackground();
|
||||
if (this.isOpaque()) {
|
||||
secondPinceau.setColor(couleurFond);
|
||||
secondPinceau.fillRect(0, 0, this.getWidth(), this.getHeight());
|
||||
}
|
||||
|
||||
secondPinceau.setColor(this.couleur);
|
||||
secondPinceau.fillRect(2,2, this.getWidth()-2, this.getHeight()-2);
|
||||
}
|
||||
}
|
||||
BIN
DEV/DEV3.2/TP05_Files/Q4Main.class
Normal file
BIN
DEV/DEV3.2/TP05_Files/Q4Main.class
Normal file
Binary file not shown.
22
DEV/DEV3.2/TP05_Files/Q4Main.java
Normal file
22
DEV/DEV3.2/TP05_Files/Q4Main.java
Normal file
@@ -0,0 +1,22 @@
|
||||
import java.util.*;
|
||||
|
||||
public class Q4Main{
|
||||
|
||||
public static void main(String[] args){
|
||||
|
||||
Serpent jeux = new Serpent();
|
||||
|
||||
Timer boucle = new Timer(true);
|
||||
Repetition iteration = new Repetition(jeux, boucle);
|
||||
boucle.scheduleAtFixedRate(iteration, 1000, 100);
|
||||
try {
|
||||
while(true){
|
||||
Thread.sleep(1000000);
|
||||
}
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
boucle.cancel();
|
||||
}
|
||||
}
|
||||
BIN
DEV/DEV3.2/TP05_Files/Repetition.class
Normal file
BIN
DEV/DEV3.2/TP05_Files/Repetition.class
Normal file
Binary file not shown.
61
DEV/DEV3.2/TP05_Files/Repetition.java
Normal file
61
DEV/DEV3.2/TP05_Files/Repetition.java
Normal file
@@ -0,0 +1,61 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.*;
|
||||
|
||||
public class Repetition extends TimerTask{
|
||||
|
||||
public Serpent jeux;
|
||||
public java.util.Timer boucle;
|
||||
|
||||
public Repetition(Serpent jeux, java.util.Timer boucle){
|
||||
this.jeux = jeux;
|
||||
this.boucle = boucle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
try{
|
||||
if (this.jeux.isFreeze == false){
|
||||
Point direction;
|
||||
if (this.jeux.direction.isEmpty()){
|
||||
direction = this.jeux.derniereDirection;
|
||||
}
|
||||
else{
|
||||
direction = this.jeux.direction.poll();
|
||||
}
|
||||
this.jeux.tete = new Point(this.jeux.tete.x + direction.x, this.jeux.tete.y + direction.y);
|
||||
if (this.jeux.serpent.contains(this.jeux.tete)){
|
||||
this.perdu();
|
||||
}
|
||||
this.jeux.serpent.offer(this.jeux.tete);
|
||||
this.jeux.listeCase[this.jeux.tete.x][this.jeux.tete.y].setColor(Serpent.SERPENT);
|
||||
|
||||
int indicePomme = this.jeux.listePomme.indexOf(this.jeux.tete);
|
||||
if (indicePomme == -1){
|
||||
Point queue = this.jeux.serpent.poll();
|
||||
this.jeux.listeCase[queue.x][queue.y].setColor(Serpent.FOND);
|
||||
}
|
||||
else{
|
||||
this.jeux.listePomme.remove(indicePomme);
|
||||
this.jeux.newPomme();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ArrayIndexOutOfBoundsException e) {
|
||||
this.perdu();
|
||||
}
|
||||
}
|
||||
|
||||
private void perdu(){
|
||||
System.out.println("perdu");
|
||||
this.jeux.isFreeze = false;
|
||||
int fermeture = JOptionPane.showConfirmDialog(null,
|
||||
"Score : " + (this.jeux.serpent.size()-3) + "\nVoulez vous rejouer ?");
|
||||
if (fermeture == JOptionPane.YES_OPTION){
|
||||
this.jeux.initialisation();
|
||||
}
|
||||
else{
|
||||
this.boucle.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
DEV/DEV3.2/TP05_Files/Serpent.class
Normal file
BIN
DEV/DEV3.2/TP05_Files/Serpent.class
Normal file
Binary file not shown.
85
DEV/DEV3.2/TP05_Files/Serpent.java
Normal file
85
DEV/DEV3.2/TP05_Files/Serpent.java
Normal file
@@ -0,0 +1,85 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.*;
|
||||
|
||||
public class Serpent{
|
||||
|
||||
public static final int LONGUEUR=25;
|
||||
public static final int HAUTEUR=25;
|
||||
public static final Color SERPENT=Color.ORANGE;
|
||||
public static final Color FOND=Color.GREEN;
|
||||
public static final Color POMME=Color.RED;
|
||||
|
||||
public JFrame fenetre;
|
||||
public Queue<Point> serpent;
|
||||
public Point tete;
|
||||
public ArrayList<Point> listePomme;
|
||||
public Dessin[][] listeCase;
|
||||
public Queue<Point> direction;
|
||||
public Point derniereDirection;
|
||||
public boolean isFreeze;
|
||||
public Random random;
|
||||
|
||||
public Serpent(){
|
||||
this.fenetre = new JFrame("serpent");
|
||||
this.fenetre.setSize(800, 800);
|
||||
this.fenetre.setLocation(0, 0);
|
||||
this.fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
GridLayout grille = new GridLayout(Serpent.HAUTEUR,Serpent.LONGUEUR);
|
||||
this.fenetre.setLayout(grille);
|
||||
|
||||
this.listeCase = new Dessin[Serpent.LONGUEUR][Serpent.HAUTEUR];
|
||||
int x,y;
|
||||
for (y=0; y<Serpent.HAUTEUR; y++){
|
||||
for (x=0; x<Serpent.LONGUEUR; x++){
|
||||
this.listeCase[x][y] = new Dessin(Serpent.FOND);
|
||||
this.fenetre.add(this.listeCase[x][y]);
|
||||
}
|
||||
}
|
||||
|
||||
this.random = new Random();
|
||||
this.initialisation();
|
||||
Action evenement = new Action(this);
|
||||
this.fenetre.addKeyListener(evenement);
|
||||
this.fenetre.setVisible(true);
|
||||
}
|
||||
|
||||
public void initialisation(){
|
||||
int x,y;
|
||||
for (y=0; y<Serpent.HAUTEUR; y++){
|
||||
for (x=0; x<Serpent.LONGUEUR; x++){
|
||||
this.listeCase[x][y].setColor(Serpent.FOND);
|
||||
}
|
||||
}
|
||||
this.direction = new LinkedList<>();
|
||||
this.derniereDirection = Action.DROITE;
|
||||
this.listePomme = new ArrayList<>();
|
||||
this.serpent = new LinkedList<>();
|
||||
this.tete = new Point(2,12);
|
||||
this.serpent.offer(this.tete);
|
||||
this.listeCase[this.tete.x][this.tete.y].setColor(Serpent.SERPENT);
|
||||
this.tete = new Point(3,12);
|
||||
this.serpent.offer(this.tete);
|
||||
this.listeCase[this.tete.x][this.tete.y].setColor(Serpent.SERPENT);
|
||||
this.tete = new Point(4,12);
|
||||
this.serpent.offer(this.tete);
|
||||
this.listeCase[this.tete.x][this.tete.y].setColor(Serpent.SERPENT);
|
||||
|
||||
this.listePomme.add(new Point(15,12));
|
||||
this.listeCase[15][12].setColor(Serpent.POMME);
|
||||
newPomme();
|
||||
newPomme();
|
||||
this.fenetre.repaint();
|
||||
this.isFreeze = true;
|
||||
}
|
||||
|
||||
public void newPomme(){
|
||||
Point emplacement;
|
||||
do{
|
||||
emplacement = new Point(this.random.nextInt(Serpent.LONGUEUR), this.random.nextInt(Serpent.HAUTEUR));
|
||||
} while (this.serpent.contains(emplacement) || this.listePomme.contains(emplacement));
|
||||
|
||||
this.listePomme.add(emplacement);
|
||||
this.listeCase[emplacement.x][emplacement.y].setColor(Serpent.POMME);
|
||||
}
|
||||
}
|
||||
29
DEV/DEV3.2/TP05_Files/Test.java
Normal file
29
DEV/DEV3.2/TP05_Files/Test.java
Normal file
@@ -0,0 +1,29 @@
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
public class Test extends TimerTask {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("hello");
|
||||
}
|
||||
|
||||
public static void main(String args[]){
|
||||
Test timerTask = new Test();
|
||||
//running timer task as daemon thread
|
||||
Timer timer = new Timer(true);
|
||||
timer.scheduleAtFixedRate(timerTask, 0, 1000);
|
||||
try {
|
||||
while(true){
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
timer.cancel();
|
||||
System.out.println("TimerTask cancelled");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user