Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.applet.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.applet.Applet;
- import java.awt.Graphics;
- public class JEUv2 extends Applet implements KeyListener, MouseListener, Runnable {
- boolean gagne=false,n2=true;
- Thread pieges;
- Image sprite;
- Graphics h;
- static int c = 75;
- int score=0;
- static int xPos=c, yPos=7*c, xCase, yCase;
- static int tabl [][] = {{1,1,1,1,1,1,1,1,1},
- {1,0,4,0,0,3,0,0,1},
- {1,9,1,1,1,1,1,1,1},
- {1,0,0,0,0,0,0,0,1},
- {1,0,1,0,1,1,1,0,1},
- {1,0,9,0,1,0,0,0,1},
- {1,0,1,0,0,9,1,1,1},
- {1,0,1,1,1,1,1,1,1},
- {1,0,0,1,0,0,0,0,1},
- {1,1,0,1,9,1,0,1,1},
- {1,1,0,1,1,1,0,0,1},
- {1,1,0,0,0,1,1,0,1},
- {1,1,1,0,1,1,1,0,1},
- {1,9,0,0,0,0,0,0,1},
- {1,1,1,1,1,1,1,0,1},
- {1,2,1,0,0,0,1,0,1},
- {1,0,0,0,1,0,0,0,1},
- {1,1,1,1,1,1,1,1,1}};
- public void init() {
- addKeyListener(this);
- addMouseListener(this);
- setSize(1920,1080);
- xCase=xPos/c;
- yCase=yPos/c;
- pieges=null;
- sprite=createImage(20*c,10*c);
- h=sprite.getGraphics();
- }
- public void start(){
- if (pieges==null)
- pieges=new Thread(this);
- pieges.start();
- }
- public void stop() {
- if(pieges!=null) {
- pieges.stop();
- pieges = null;
- }
- }
- public void run(){
- while(true){
- try{
- repaint();
- tabl[0][0]=(tabl[0][0]==3)?4:3;
- Thread.sleep(1000);
- }
- catch(InterruptedException e){
- stop();
- }
- }
- }
- public void keyPressed( KeyEvent e) {}
- public void keyReleased( KeyEvent e) {}
- public void keyTyped( KeyEvent e) {
- char c = e.getKeyChar();
- if (gagne==false && c != KeyEvent.CHAR_UNDEFINED) {
- if ((c=='z') && yCase > 0 && (tabl[xCase][yCase-1]==0)) yCase=yCase-1; //Déplacement Perso en xCase = 0
- if ((c=='s') && yCase > 0 && (tabl[xCase][yCase+1]==0)) yCase=yCase+1;
- if ((c=='q') && xCase > 0 && (tabl[xCase-1][yCase]==0)) xCase=xCase-1;
- if ((c=='d') && xCase > 0 && (tabl[xCase+1][yCase]==0)) xCase=xCase+1;
- if ((c=='z') && yCase > 0 && (tabl[xCase][yCase-1]==9)) { //Déplacement Perso en Case = 9
- yCase=yCase-1;
- tabl[xCase][yCase]=0; //Changement Cases -1 en 0
- score+= 1; //Score +1
- }
- if ((c=='s') && yCase > 0 && (tabl[xCase][yCase+1]==9)) {
- yCase=yCase+1;
- tabl[xCase][yCase]=0;
- score+= 1;
- }
- if ((c=='q') && xCase > 0 && (tabl[xCase-1][yCase]==9)) {
- xCase=xCase-1;
- tabl[xCase][yCase]=0;
- score+= 1;
- }
- if ((c=='d') && xCase > 0 && (tabl[xCase+1][yCase]==9)) {
- xCase=xCase+1;
- tabl[xCase][yCase]=0;
- score+= 1;
- }
- if ((c=='z') && yCase > 0 && (tabl[xCase][yCase-1]==2)) { //Déplacement Perso en Case = 2
- yCase=yCase-1;
- if (score==5) gagne=true; //Gagné devient VRAI
- }
- if ((c=='s') && yCase > 0 && (tabl[xCase][yCase+1]==2)) {
- yCase=yCase+1;
- if (score==5) gagne=true;
- }
- if ((c=='q') && xCase > 0 && (tabl[xCase-1][yCase]==2)) {
- xCase=xCase-1;
- if (score==5)gagne=true;
- }
- if ((c=='d') && xCase > 0 && (tabl[xCase+1][yCase]==2)) {
- xCase=xCase+1;
- if (score==5) gagne=true;
- }
- repaint();
- e.consume();
- }
- }
- public void mouseEntered( MouseEvent e ) {}
- public void mouseExited( MouseEvent e ) {}
- public void mousePressed (MouseEvent e ) {}
- public void mouseReleased( MouseEvent e ) {}
- public void mouseClicked( MouseEvent e ) {
- repaint();
- e.consume();
- }
- public void update (Graphics g){
- paint(g);
- }
- public void paint( Graphics g ) {
- h.clearRect(0,0,20*c,10*c);
- for(int i=0;i<18;i++){ //Affichage murs
- for(int j=0;j<9;j++){
- h.setColor(Color.black);
- if(tabl[i][j]==1) h.fillRect(c*i,c*j,c,c);
- if(tabl[i][j]==9){ //Affichage pièces cases = -1
- h.setColor(Color.yellow);
- h.fillOval(c*i+c/2-c/3,c*j+c/2-c/3,2*c/3,2*c/3);
- }
- if(tabl[i][j]==2) { //Definition Cases = 2
- h.setColor(Color.blue);
- h.fillRect(c*i,c*j,c,c);
- h.setColor(Color.cyan);
- h.setFont(new Font("default",Font.BOLD,25));
- h.drawString("A", (c*16-c/2-c/10),(c*2-c/2+c/10));
- }
- if(tabl[i][j]==3) { //Definition Cases = 3
- h.setColor(Color.red);
- h.fillRect(c*i,c*j,c,c);
- }
- }
- h.setColor(Color.red);
- h.setFont(new Font("default",Font.BOLD,25)); //Police texte
- h.drawString("X", (c*xCase+c/2-c/10), (c*yCase+c/2+c/10)); //Personnage
- h.drawString("Score : " +score,50,50); //Affichage Score
- h.setFont(new Font("default",Font.BOLD,20)); //Police texte
- if ((score==5)&&(gagne==true)&& tabl[xCase][yCase]==2) h.drawString("VICTORY",500,500); //Affichage "GAGNE"
- g.drawImage(sprite,0,0,Color.white,this);
- }
- }
- public void destroy() {
- h.dispose();
- sprite.flush();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement