1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
package pacmandebug;
/**
*
*/
//import deug.fr.jussieu.script.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.Graphics2D;
import java.lang.Object;
import javax.imageio.*;
/**
* @author humel
*
*/
public class decor extends Canvas implements KeyListener { //La classe décor s'occupe de tracer le décor et de le garder à jour, elle contient aussi le keylistener qui sera ajouté a la JFrame(propriété de notre classe décor)
public int[][] lab; //le labyrinthe passer en argument
private static JFrame f; //Notre frame, ou fenetre pour etre plus explicite
private int width, height, x,y; //La taille de frame, et 2 coordonnées utilisé par paint(Graphics g) pour mettre a jour le graphic et le redessiner en utilisant le double buffering hardware(pour eviter les scintillements)
private Graphics Gr;
private Image Im;
private BufferedImage[] Images;
public BufferStrategy strategy;
public char directionPressed;
public decor(int c){ //a la place de int[][]t
this.lab=new int[c][c];
genLab2(this.lab);
Images = new BufferedImage[4];
this.x=0; this.y=0;
}
public void setDirectionPressed(int i) {
if (i == 37) this.directionPressed='g';
if (i == 38) this.directionPressed='h';
if (i == 39) this.directionPressed='d';
if (i == 40) this.directionPressed='b';
}
public void keyPressed(KeyEvent e){
this.setDirectionPressed(e.getKeyCode());
System.out.println(e.getKeyCode());
}
public void keyReleased(KeyEvent e){ }
public void keyTyped(KeyEvent e){ }
public Dimension getPreferredSize() {
return new Dimension(width,height);
}
public JFrame getf() { return f;}
public void paint(Graphics g) {
if (Im==null) {
Im = createImage(this.width,this.height);
Gr = Im.getGraphics();
}
g.drawImage(this.Im,this.x*25,this.y*25,this.f);
}
public void update(Graphics g) {
paint(g);
}
public void start(String s) {
if (f!=null) return;
f = new JFrame(s);
f.pack();
f.setLocation(0,0);
f.resize(lab.length*25+10,lab.length*25+35);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sleep(3);
Images[0] = loadImage("C:\\Documents and Settings\\Superjoe\\Bureau\\pacmanDebug\\src\\pacmandebug\\brick.gif");
Images[1] = loadImage("C:\\Documents and Settings\\Superjoe\\Bureau\\pacmanDebug\\src\\pacmandebug\\vide.gif");
Images[2] = loadImage("C:\\Documents and Settings\\Superjoe\\Bureau\\pacmanDebug\\src\\pacmandebug\\pacman.gif");
Images[3] = loadImage("C:\\Documents and Settings\\Superjoe\\Bureau\\pacmanDebug\\src\\pacmandebug\\wall.gif");
//this.f.setVisible(true);
f.createBufferStrategy( 2 );
f.setIgnoreRepaint( false );
strategy = f.getBufferStrategy();
this.Gr = strategy.getDrawGraphics();
f.getContentPane().add(this);
f.addKeyListener(this);
f.show();
}
public void sleep(double secondes) {
try {
Thread.sleep((int)(secondes*1000));
} catch(InterruptedException ie) {}
}
public void traceBloc(int i,int j){
this.Im = this.Images[0];
saveCoo(j,i);
repaint();
}
public void traceVide(int i,int j){
this.Im = this.Images[1];
saveCoo(j,i);
repaint();
}
public void traceWall(int i,int j){
this.Im = this.Images[3];
saveCoo(j,i);
repaint();
}
public void tracePacman(int i,int j){
this.Im = this.Images[2];
saveCoo(j,i);
repaint();
}
public void saveCoo(int i,int j) {
x=i;
y=j;
}
public static BufferedImage loadImage(String i)
{
try{
return ImageIO.read(new File(i));
}catch(Exception e)
{
System.out.println(e.toString());
return null;
}
}
public static void genLab2(int tab[][]) { //rasoir d'occam
int t,v,k;
for (int i=1;i<tab.length;i++) {
for(int j=1; j<tab[0].length-1;j++){
v=(int)(6*Math.random());
if(v < 5 && tab[i-1][j-1]==0 && tab[i-1][j]==0 && tab[i][j+1]==0 && tab[i-1][j+1]==0) tab[i][j]=1;
}
k=0;
while(k<tab[0].length-1){
t=min((int)(Math.random()*4),tab.length-i-1);
for (int j=k; j<tab[0].length;j++){
k++;
if (tab[i][j]==0) break;
for(int z=i;z<i+t;z++) tab[z][j]=1;
}
//t=min((int)(Math.random()*8),tab.length-i-1);
}
}
/* for (int i=2;i<tab.length-2;i++) {
for(int j=2;j<tab[0].length-2;j++) {
if( (tab[i][j]+tab[i][j-1]==0 && tab[i][j-2]+tab[i][j+1]==2) || (tab[i][j]+tab[i-1][j]==0 && tab[i-2][j]+tab[i+1][j]==2) || (tab[i-1][j]+tab[i][j]+tab[i+1][j]==0 && tab[i-2][j]+tab[i+2][j]==2) ) { tab[i][j]=1; }
}
}*/
for (int i = 0;i<tab.length;i++) {
tab[i][0] = 2;
tab[i][1] = 0;
tab[i][tab[0].length-1] = 2;
tab[i][tab[0].length-2] = 0;
}
for (int j = 1;j<tab[0].length-1;j++) {
tab[0][j] = 2;
tab[1][j] = 0;
tab[tab.length-1][j] = 2;
tab[tab.length-2][j] = 0;
}
getTab(tab);
}
public static void getTab(int[][] lab) {
for (int i=0;i<lab.length;i++){
for (int j=0;j<lab[0].length;j++)
System.out.print(lab[i][j]+" ");
System.out.println();
}
}
public static int min(int a,int b){
if(a>b) return b;
return a;
}
} |
Partager