Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.43 KB | None | 0 0
  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.applet.Applet;
  5. import java.awt.Graphics;
  6.  
  7.  
  8. public class JEUv2 extends Applet implements KeyListener, MouseListener, Runnable {
  9. boolean gagne=false,n2=true;
  10. Thread pieges;
  11. Image sprite;
  12. Graphics h;
  13. static int c = 75;
  14. int score=0;
  15. static int xPos=c, yPos=7*c, xCase, yCase;
  16. static int tabl [][] = {{1,1,1,1,1,1,1,1,1},
  17. {1,0,4,0,0,3,0,0,1},
  18. {1,9,1,1,1,1,1,1,1},
  19. {1,0,0,0,0,0,0,0,1},
  20. {1,0,1,0,1,1,1,0,1},
  21. {1,0,9,0,1,0,0,0,1},
  22. {1,0,1,0,0,9,1,1,1},
  23. {1,0,1,1,1,1,1,1,1},
  24. {1,0,0,1,0,0,0,0,1},
  25. {1,1,0,1,9,1,0,1,1},
  26. {1,1,0,1,1,1,0,0,1},
  27. {1,1,0,0,0,1,1,0,1},
  28. {1,1,1,0,1,1,1,0,1},
  29. {1,9,0,0,0,0,0,0,1},
  30. {1,1,1,1,1,1,1,0,1},
  31. {1,2,1,0,0,0,1,0,1},
  32. {1,0,0,0,1,0,0,0,1},
  33. {1,1,1,1,1,1,1,1,1}};
  34.  
  35.  
  36. public void init() {
  37. addKeyListener(this);
  38. addMouseListener(this);
  39. setSize(1920,1080);
  40. xCase=xPos/c;
  41. yCase=yPos/c;
  42. pieges=null;
  43. sprite=createImage(20*c,10*c);
  44. h=sprite.getGraphics();
  45. }
  46.  
  47. public void start(){
  48. if (pieges==null)
  49. pieges=new Thread(this);
  50. pieges.start();
  51. }
  52.  
  53. public void stop() {
  54. if(pieges!=null) {
  55. pieges.stop();
  56. pieges = null;
  57. }
  58. }
  59.  
  60. public void run(){
  61. while(true){
  62. try{
  63. repaint();
  64. tabl[0][0]=(tabl[0][0]==3)?4:3;
  65. Thread.sleep(1000);
  66. }
  67.  
  68. catch(InterruptedException e){
  69. stop();
  70.  
  71. }
  72. }
  73. }
  74.  
  75. public void keyPressed( KeyEvent e) {}
  76. public void keyReleased( KeyEvent e) {}
  77. public void keyTyped( KeyEvent e) {
  78. char c = e.getKeyChar();
  79. if (gagne==false && c != KeyEvent.CHAR_UNDEFINED) {
  80.  
  81.  
  82. if ((c=='z') && yCase > 0 && (tabl[xCase][yCase-1]==0)) yCase=yCase-1; //Déplacement Perso en xCase = 0
  83. if ((c=='s') && yCase > 0 && (tabl[xCase][yCase+1]==0)) yCase=yCase+1;
  84. if ((c=='q') && xCase > 0 && (tabl[xCase-1][yCase]==0)) xCase=xCase-1;
  85. if ((c=='d') && xCase > 0 && (tabl[xCase+1][yCase]==0)) xCase=xCase+1;
  86.  
  87.  
  88.  
  89. if ((c=='z') && yCase > 0 && (tabl[xCase][yCase-1]==9)) { //Déplacement Perso en Case = 9
  90. yCase=yCase-1;
  91. tabl[xCase][yCase]=0; //Changement Cases -1 en 0
  92. score+= 1; //Score +1
  93. }
  94.  
  95. if ((c=='s') && yCase > 0 && (tabl[xCase][yCase+1]==9)) {
  96. yCase=yCase+1;
  97. tabl[xCase][yCase]=0;
  98. score+= 1;
  99. }
  100.  
  101. if ((c=='q') && xCase > 0 && (tabl[xCase-1][yCase]==9)) {
  102. xCase=xCase-1;
  103. tabl[xCase][yCase]=0;
  104. score+= 1;
  105. }
  106.  
  107. if ((c=='d') && xCase > 0 && (tabl[xCase+1][yCase]==9)) {
  108. xCase=xCase+1;
  109. tabl[xCase][yCase]=0;
  110. score+= 1;
  111. }
  112.  
  113.  
  114.  
  115.  
  116. if ((c=='z') && yCase > 0 && (tabl[xCase][yCase-1]==2)) { //Déplacement Perso en Case = 2
  117. yCase=yCase-1;
  118. if (score==5) gagne=true; //Gagné devient VRAI
  119. }
  120.  
  121. if ((c=='s') && yCase > 0 && (tabl[xCase][yCase+1]==2)) {
  122. yCase=yCase+1;
  123. if (score==5) gagne=true;
  124. }
  125.  
  126. if ((c=='q') && xCase > 0 && (tabl[xCase-1][yCase]==2)) {
  127. xCase=xCase-1;
  128. if (score==5)gagne=true;
  129. }
  130.  
  131. if ((c=='d') && xCase > 0 && (tabl[xCase+1][yCase]==2)) {
  132. xCase=xCase+1;
  133. if (score==5) gagne=true;
  134. }
  135.  
  136.  
  137. repaint();
  138. e.consume();
  139. }
  140. }
  141.  
  142. public void mouseEntered( MouseEvent e ) {}
  143. public void mouseExited( MouseEvent e ) {}
  144. public void mousePressed (MouseEvent e ) {}
  145. public void mouseReleased( MouseEvent e ) {}
  146. public void mouseClicked( MouseEvent e ) {
  147.  
  148. repaint();
  149. e.consume();
  150. }
  151.  
  152. public void update (Graphics g){
  153. paint(g);
  154.  
  155. }
  156.  
  157. public void paint( Graphics g ) {
  158. h.clearRect(0,0,20*c,10*c);
  159. for(int i=0;i<18;i++){ //Affichage murs
  160. for(int j=0;j<9;j++){
  161. h.setColor(Color.black);
  162. if(tabl[i][j]==1) h.fillRect(c*i,c*j,c,c);
  163.  
  164.  
  165. if(tabl[i][j]==9){ //Affichage pièces cases = -1
  166. h.setColor(Color.yellow);
  167. h.fillOval(c*i+c/2-c/3,c*j+c/2-c/3,2*c/3,2*c/3);
  168. }
  169.  
  170. if(tabl[i][j]==2) { //Definition Cases = 2
  171. h.setColor(Color.blue);
  172. h.fillRect(c*i,c*j,c,c);
  173. h.setColor(Color.cyan);
  174. h.setFont(new Font("default",Font.BOLD,25));
  175. h.drawString("A", (c*16-c/2-c/10),(c*2-c/2+c/10));
  176. }
  177.  
  178.  
  179. if(tabl[i][j]==3) { //Definition Cases = 3
  180. h.setColor(Color.red);
  181. h.fillRect(c*i,c*j,c,c);
  182. }
  183. }
  184. h.setColor(Color.red);
  185. h.setFont(new Font("default",Font.BOLD,25)); //Police texte
  186. h.drawString("X", (c*xCase+c/2-c/10), (c*yCase+c/2+c/10)); //Personnage
  187. h.drawString("Score : " +score,50,50); //Affichage Score
  188. h.setFont(new Font("default",Font.BOLD,20)); //Police texte
  189. if ((score==5)&&(gagne==true)&& tabl[xCase][yCase]==2) h.drawString("VICTORY",500,500); //Affichage "GAGNE"
  190. g.drawImage(sprite,0,0,Color.white,this);
  191. }
  192.  
  193. }
  194.  
  195. public void destroy() {
  196. h.dispose();
  197. sprite.flush();
  198. }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement