Ce petit jeu, mon deuxième, génère semi-aléatoirement un labyrinthe de 100 cases (10*10). Ensuite, grâce à l'interface qui vous indique les chemins que vous pouvez prendre et à l'aide des flèches directionnelles, c'est à vous de trouver la sortie. Mais attention, vous ne pouvez voir que les cases immédiatement autour de vous. Vous serez aidé par l'affichage du numéro de la case où vous passez.
Source / Exemple :
package jeux;
import javax.swing.JFrame;
public class Generation extends JFrame{
/**
public static void main(String[] args) {
// TODO Auto-generated method stub
new Generation();
}
private int hor = 11;
private int ver = 11;
private int laby [][] = new int [11][11];
public int barre[] = new int [11];
public int [] chemin = new int [11];
public int [] raccourci = new int [11];
public static int positions [] = new int [101];
public static int position = 91;
public int posInit = 10000;//tout est ouvert
//génération du chemin de sortie
public Generation(){
add(new Tableau3D());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setLocationRelativeTo(null);
setTitle("Labyrinthe 1.0");
setResizable(false);
setVisible(true);
chemin[0] = 10;
chemin[10] = 1;
raccourci[0] = 10;
raccourci[10] = 1;
System.out.print(chemin[0]+"/");
for (int i = 1; i <= chemin.length-2;i++){
int ouvert = (int) (Math.random()*10+1);
chemin[i] = ouvert;
int pseudo = (int) (Math.random()*10+1);
if (pseudo != chemin[i]){
raccourci[i] = pseudo;
} else {
if (pseudo>5){
raccourci[i] = pseudo-1;
}
if (pseudo<=5){
raccourci[i] = pseudo+1;
}
}
barre[i] = (chemin[i] + raccourci[i])/2;
System.out.print(chemin[i]+"/");
}
System.out.println(chemin[10]);
dessin();
}
private void dessin() {
for (int j=0; j<11;j++){
int i;
if (j==0)System.out.print(" ");
if (j!=0)System.out.print("|");
for (i = 1; i<11 ; i++){//boucle sur chaque ligne
if ((chemin[j] == i)|| (raccourci[j] == i)){
System.out.print(" ");
if ((i==10)&&(j!=0)){
if((i==10)&&(j==10))System.out.print("_");
System.out.println("|");
}
} else {
System.out.print("__");
if ((i==10)&&(j!=0)){
if((i==10)&&(j==10))System.out.print("_");
System.out.println("|");
}
}
if (barre[j] == i){
System.out.print("|");
}
}
if (j==0)System.out.println("");
}
for (int i=0 ; i < barre.length;i++){
System.out.print(chemin[i]+" ");
System.out.print(raccourci[i]+" ");
System.out.print(barre[i]);System.out.println();
}
positionner();
}
private void positionner(){
int diz = 1;
int un = 1;
positions[0]=10000;
for (int i = 1 ; i<positions.length ; i++){
//a gauche OK OK
if (((i==1)||(i==11)||(i==21)||(i==31)||(i==41)||(i==51)||(i==61)||(i==71)||
(i==81)||(i==91))||(un==barre[(diz)]+1)){
//positions[i] = posInit-1000;
posInit = posInit + 1000;
}
//en haut OK OK
if ((un != chemin[diz-1])&&(un!=raccourci[diz-1])){
posInit = posInit+100;
}
//a droite OK OK
if (((i == 10)||(i==20)||(i==30)||(i==40)||(i==50)||(i==60)||(i==70)||
(i==80)||(i==90))||(i==100)||(un == barre[diz])){
posInit = posInit+10;
}
//en bas OK OK
if (((un != chemin[diz])&&(un!=raccourci[diz]))||(i==91)){
posInit = posInit+1;
}
positions[i] = posInit;
posInit = 10000;
if ((i==10)||(i==20)||(i==30)||(i==40)||(i==50)||(i==60)||(i==70)||(i==80)||(i==90)){
diz += 1;
un = 0;
}
un++;
System.out.print(i+" ");
System.out.print(diz+" ");
System.out.print(un-1+" ");
System.out.println(positions[i]);
}
}
}
package jeux;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Tableau3D extends JPanel implements Runnable{
private final int NORD = 1;
private final int EST = 2;
private final int SUD = 3;
private final int OUEST = 4;
private int direction;
private Thread animator;
private final int DELAY = 80;
private Image fg, fh, fd, fb, p;
private boolean enJeu = true;
private int position = Generation.position;
private int [] positions = Generation.positions;
private boolean afg, afh, afd, afb;
private Font font;
private FontMetrics metrics;
private int score;
private int nbCoups = 0;
public Tableau3D(){
setBackground(Color.WHITE);
setDoubleBuffered(true);
setFocusable(true);
addKeyListener(new TAdapter());
ImageIcon ii = new ImageIcon(this.getClass().getResource("flecheGauche.jpg"));
ImageIcon iii = new ImageIcon(this.getClass().getResource("flecheHaut.jpg"));
ImageIcon iiii = new ImageIcon(this.getClass().getResource("flecheDroite.jpg"));
ImageIcon iiiii = new ImageIcon(this.getClass().getResource("flecheBas.jpg"));
ImageIcon iiiiii = new ImageIcon(this.getClass().getResource("point.jpg"));
fg = ii.getImage();
fh = iii.getImage();
fd = iiii.getImage();
fb = iiiii.getImage();
p = iiiiii.getImage();
// set up message font
font = new Font("SansSerif", Font.BOLD, 24);
metrics = this.getFontMetrics(font);
}
public void addNotify() {
super.addNotify();
animator = new Thread(this);
animator.start();
}
public void run() {
long beforeTime, timeDiff, sleep;
beforeTime = System.currentTimeMillis();
while (true) {
cycle();
repaint();
timeDiff = System.currentTimeMillis() - beforeTime;
sleep = DELAY - timeDiff;
if (sleep < 0)
sleep = 2;
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
System.out.println("interrupted");
}
beforeTime = System.currentTimeMillis();
}
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
if (enJeu){
g2d.drawString("Case : "+ position, 10, 10);
g2d.drawImage(p, 245, 230, this);
if (afg == true)g2d.drawImage(fg, 50, 200, this);
if (afh == true)g2d.drawImage(fh, 215, 50, this);
if (afd == true)g2d.drawImage(fd, 320, 200, this);
if (afb == true)g2d.drawImage(fb, 215, 300, this);
}
if (!enJeu){
g2d.setFont(font);
String msg = "Bravo, vous avez trouvé la sortie " ;
String msg2 = "en " + nbCoups + " coups !!";
int x = (500 - metrics.stringWidth(msg))/2;
int y = (500 - metrics.getHeight())/2;
g.setColor(Color.red);
g.setFont(font);
g.drawString(msg, x, y);
g.drawString(msg2, x, y+50);
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void cycle() {
String spos = Integer.toString(positions[position]);
char smil = 0;//ouvert par défaut
char scent = 0;
char sdiz = 0;
char sun =0;
try{
smil = spos.charAt(spos.length()-4);
scent = spos.charAt(spos.length()-3);
sdiz = spos.charAt(spos.length()-2);
sun = spos.charAt(spos.length()-1);
} catch (Exception e){
e.printStackTrace();
}
if (smil != '1'){
afg = true;
} else afg = false;
if (scent != '1'){
afh = true;
} else afh = false;
if (sdiz != '1'){
afd = true;
} else afd = false;
if (sun != '1'){
afb = true;
} else afb = false;
// System.out.println(spos.length());
// System.out.println(smil+" "+scent+" "+sdiz+" "+sun);
if (direction == OUEST) {
if (smil!='1'){
position = position - 1;
nbCoups++;
}
direction = 0;
}
if (direction == EST) {
if (sdiz!='1'){
position = position+1;
nbCoups++;
}
direction = 0;
}
if (direction == NORD) {
if (scent!='1'){
position = position - 10;
nbCoups++;
}
direction = 0;
}
if (direction == SUD) {
if (sun !='1'){
position = position + 10;
nbCoups++;
}
direction = 0;
}
if (position == 10){
enJeu = false;
}
}
private class TAdapter extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT)) {
direction = OUEST;
}
if ((key == KeyEvent.VK_RIGHT)) {
direction = EST;
}
if ((key == KeyEvent.VK_UP)) {
direction = NORD;
}
if ((key == KeyEvent.VK_DOWN)) {
direction = SUD;
}
}
}
}
Conclusion :
Bon jeu !!
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.