Created
October 26, 2014 23:16
-
-
Save Tyrrus/62e03d9c0628d19fc0f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Tron; | |
import javax.swing.JOptionPane; | |
import processing.core.PApplet; | |
import processing.core.PImage; | |
public class Dessin extends PApplet { | |
PImage fond; // image de fond | |
int taille = 10; // taille d'une case | |
int largeur = 650; // fenêtre graphique | |
int hauteur = 650;// fenêtre graphique | |
Personnage hero1; | |
Personnage hero2; | |
// Création de la variable aller pour la direction de départ du héro 1 | |
int aller = RIGHT; | |
// Création de la variable aller2 pour la direction de départ du héro 2 | |
int aller2 = LEFT; | |
public void setup() { | |
size(largeur, hauteur); // dimension de la fenetre | |
// couleur de fong | |
//background(0); | |
//noFill(); | |
// image de fond | |
//fond = loadImage("background.jpg"); | |
// Création des personnages | |
hero1 = new Personnage(10, 10, this); | |
hero2 = new Personnage(620, 620, this); | |
// Dessiner la grille | |
for (int x = 0; x < largeur; x += taille) { | |
for (int y = 0; y < hauteur; y += taille) { | |
noFill(); // Couleur de la grille | |
rect(x, y, taille, taille); | |
} | |
} | |
} | |
public void draw() { | |
hero1.dessiner(27, 217, 222); | |
hero2.dessiner(222, 27, 27); | |
bordure(); | |
controller1(); | |
controller2(); | |
if (get(hero1.xpix, hero1.xpix) != color(0)) | |
{ | |
joueur2win(); | |
} | |
if (get(hero2.xpix, hero2.xpix) !=color(0)) { | |
joueur1win(); | |
} | |
} | |
public void controller1() { | |
// deplacement du personnage 1 | |
switch (aller) { | |
case UP: | |
hero1.ypix -= 10; | |
temporisation(); | |
break; | |
case DOWN: | |
hero1.ypix += 10; | |
temporisation(); | |
break; | |
case LEFT: | |
hero1.xpix -= 10; | |
temporisation(); | |
break; | |
case RIGHT: | |
hero1.xpix += 10; | |
temporisation(); | |
break; | |
} | |
} | |
public void controller2() { | |
// deplacement du personnage 2 | |
switch (aller2) { | |
case UP: | |
hero2.ypix -= 10; | |
temporisation(); | |
break; | |
case DOWN: | |
hero2.ypix += 10; | |
temporisation(); | |
break; | |
case LEFT: | |
hero2.xpix -= 10; | |
temporisation(); | |
break; | |
case RIGHT: | |
hero2.xpix += 10; | |
temporisation(); | |
break; | |
} | |
} | |
public void keyPressed() { | |
// Controller pour héro 1 | |
if (key == 'z' || key =='Z') { | |
aller = UP; | |
} | |
if (key == 's' || key=='S') { | |
aller = DOWN; | |
} | |
if (key == 'q' || key=='Q') { | |
aller = LEFT; | |
} | |
if (key == 'd' || key=='D') { | |
aller = RIGHT; | |
} | |
// Controller pour hero 2 | |
if (key == '8') { | |
aller2 = UP; | |
} | |
if (key == '5') { | |
aller2 = DOWN; | |
} | |
if (key == '4') { | |
aller2 = LEFT; | |
} | |
if (key == '6') { | |
aller2 = RIGHT; | |
} | |
} | |
private void bordure() { | |
// Dans le cas ou le hero 1 sorte du cadre | |
if (hero1.xpix > largeur || hero1.xpix < 0 || hero1.ypix > hauteur | |
|| hero1.ypix < 0) { | |
joueur2win(); | |
} | |
// Dans le cas ou le hero 2 sorte du cadre | |
if (hero2.xpix > largeur || hero2.xpix < 0 || hero2.ypix > hauteur | |
|| hero2.ypix < 0) { | |
joueur1win(); | |
} | |
} | |
private void joueur1win() { | |
JOptionPane.showMessageDialog(null, "Joueur 1 WIN", "TRON", | |
JOptionPane.INFORMATION_MESSAGE); | |
System.exit(0); | |
} | |
private void joueur2win() { | |
JOptionPane.showMessageDialog(null, "Joueur 2 WIN", "TRON", | |
JOptionPane.INFORMATION_MESSAGE); | |
System.exit(0); | |
} | |
private void temporisation() { | |
try { | |
Thread.sleep(25); | |
} catch (Exception e) { | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Tron; | |
public class Personnage { | |
int xpix; //coordonnees graphique en X | |
int ypix; //coordonnees graphique en Y | |
Dessin dessin; //fenetre graphique | |
public Personnage(int xpix, int ypix, Dessin dessin) { | |
this.xpix = xpix; | |
this.ypix = ypix; | |
this.dessin = dessin; | |
} | |
void dessiner(int r, int v, int b) { | |
dessin.noStroke();; // Couleur des bordures de la moto | |
dessin.fill(r,v,b); // couleur de la moto | |
dessin.rect(xpix,ypix,dessin.taille,dessin.taille); // Taille de la moto | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment