Advertisement
Debml

videojuegos

Apr 29th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 45.80 KB | None | 0 0
  1.  
  2. package runmaurorun;
  3.  
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.Graphics;
  7. import java.awt.Graphics2D;
  8. import java.awt.Image;
  9. import java.awt.Rectangle;
  10. import java.awt.Toolkit;
  11. import java.awt.event.KeyAdapter;
  12. import java.awt.event.KeyEvent;
  13. import java.awt.event.KeyListener;
  14. import java.awt.event.MouseAdapter;
  15. import java.awt.event.MouseEvent;
  16. import java.awt.event.MouseListener;
  17. import java.awt.event.WindowEvent;
  18. import java.io.BufferedReader;
  19. import java.io.File;
  20. import java.io.FileNotFoundException;
  21. import java.io.FileReader;
  22. import java.io.IOException;
  23. import java.net.URL;
  24. import java.util.ArrayList;
  25. import java.util.Iterator;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import javax.swing.ImageIcon;
  29. import javax.swing.JPanel;
  30.  
  31. /**
  32. * Board
  33. *
  34. * Modela la definición de todos los objetos de tipo <code>Map</code>.
  35. *
  36. * @author Fabián Montemayor A01280156, Mauro Amarante A01191903,
  37. * Patricio Sánchez A01191893 & David Benítez A01191731
  38. * @version 2.0
  39. * @date 8/04/2015
  40. */
  41.  
  42.  
  43. public class Board extends JPanel implements Runnable, KeyListener, MouseListener{
  44. //variables de medidaas del JPanel
  45. private final int iWidth = 1280; //define ancho del JPanel
  46. private final int iHeight = 739; //define altura del JPanel
  47. private final int iHeightMauro = 32;
  48. private final int iWidthMauro = 18;
  49.  
  50. private int iLevel; //define nivel actual en el juego
  51. private int iPosX; //define la ubicacion de un objeto en X
  52. private int iPosY; //define la ubicacion de un objeto en Y
  53. private int iVelocityMauro; //define la velocidad de Mauro
  54. private int iDirectionMauro; //define la direccion de Mauro
  55. private int iDoorOpenKey;
  56. private int iMouseX; // posicion x del mouse
  57. private int iMouseY; // posicion y del mouse
  58.  
  59. private long lImmuneTimeMauro; //define el tiempo que Mauro es immune
  60.  
  61. private long lTime; //tiempo restante en el juego
  62. private long lAnimationTime; //tiempo animacion
  63.  
  64. private boolean bTime; //define si tiempo del juego ha terminado
  65. private boolean bEndGame = false; // boleana de si termian el juego o no
  66. private boolean bPlay = false; // boleana de si esta jugando o no
  67.  
  68. private int iMenu = 3; // variable del menú correspondiente
  69. private MenuBoton mbtBack; // botón de Back
  70. private MenuBoton mbtAbout; // botón de About
  71. private MenuBoton mbtPlay; // botón de Play
  72. private MenuBoton mbtInstruc; // botón de instrucciones
  73. private MenuBoton mbtOptions; // botón de opciones
  74. private MenuBoton mbtScores; // botón de puntajes
  75. private MenuBoton mbtExit; // botón de salida
  76. private MenuBoton mbtGameOverMenu; //boton al menu cuando se pierde
  77.  
  78. private Mauro mauMauro; //objeto para Mauro
  79. private Terreno terMap; //objeto para Terreno
  80.  
  81. private ArrayList arlEnemies; //lista de enemigos
  82. private ArrayList arlMapObjectsLevel1; //lista de terrenos del nivel 1
  83. private ArrayList arlItems; //lista de items
  84.  
  85. //private Graphics graCircleReveal;
  86.  
  87. private Thread thread; //variable para el thread del JPanel
  88.  
  89. //variables de control de tiempo para animacion
  90. private long lCurrentTime; // tiempo actual
  91. private long lInicialTime; // tiempo inicial
  92. private long lElapsedTime; //tiempo transcurrido
  93.  
  94. //variables para controlar la barra de tiempo
  95. private long lBarraInicial; //Tiempo en donde se empieza a contar la barra
  96. private long lBarraActual; //Tiempo actual
  97. private int iBarraElapsed; //tiempo transcurrido
  98. private int iTiempoLimite; //tiempo limite del nivel en segundos
  99.  
  100. //variables para controlar el powerup del foco
  101. private boolean bFocoOn; //Si esta prendido
  102. private long lFocoInicial; //Segundo donde se agarra
  103. private int iFocoElapsed; //Cuantos secs han pasado
  104. private int iFocoLimite; //Cantidad de segundos del powerup
  105.  
  106. private boolean bRelojOn; //Si se agarró un reloj
  107. private boolean bGameOver;
  108.  
  109. //variables para distancia del circulo
  110. private int iDist1;
  111. private int iDist2;
  112.  
  113. //variables para musica
  114. private SoundClip sndSonidoFondo1; //Objeto sonido de fondo nivel 1
  115.  
  116.  
  117. /**
  118. * Board
  119. *
  120. * Metodo sobrescrito de la clase <code>JPanel</code>.<P>
  121. * En este metodo se inizializan las variables o se crean los objetos
  122. * a usarse en el <code>JPanel</code> y se definen funcionalidades.
  123. *
  124. */
  125. public Board() {
  126. /* se le añade la opcio n al JFrame de ser escuchado por los eventos
  127. del teclado y mouse*/
  128. addKeyListener(this);
  129.  
  130. addMouseListener(this);
  131.  
  132. //define en cual panel definir enfoque
  133. setFocusable(true);
  134.  
  135. //iniciar juego
  136. GameInit();
  137. setDoubleBuffered(true);
  138. }
  139.  
  140. /**
  141. * GameInit
  142. *
  143. * Metodo que inicializa el juego.
  144. *
  145. */
  146. public void GameInit() {
  147. //Nivel de inicio, animacion inical
  148. iLevel = 0;
  149.  
  150. bGameOver = false;
  151.  
  152. // Inicializa botones del menu
  153. ImageIcon imiImagen = new ImageIcon(this.getClass().getResource("Menus/bot_back.png"));
  154. mbtBack = new MenuBoton(imiImagen.getImage(), 1280 - imiImagen.getIconWidth(), 30, imiImagen.getIconWidth(), imiImagen.getIconHeight(), false);
  155. imiImagen = new ImageIcon(this.getClass().getResource("Menus/bot_acerca.png"));
  156. mbtAbout = new MenuBoton(imiImagen.getImage(), 1280/2 - imiImagen.getIconWidth()/2, 420, imiImagen.getIconWidth(), imiImagen.getIconHeight(), false);
  157. imiImagen = new ImageIcon(this.getClass().getResource("Menus/bot_instrucciones.png"));
  158. mbtInstruc = new MenuBoton(imiImagen.getImage(), 1280/2 - imiImagen.getIconWidth()/2, 330, imiImagen.getIconWidth(), imiImagen.getIconHeight(), false);
  159. imiImagen = new ImageIcon(this.getClass().getResource("Menus/bot_opciones.png"));
  160. mbtOptions = new MenuBoton(imiImagen.getImage(), 1280/2 - imiImagen.getIconWidth()/2, 360, imiImagen.getIconWidth(), imiImagen.getIconHeight(), false);
  161. imiImagen = new ImageIcon(this.getClass().getResource("Menus/bot_jugar.png"));
  162. mbtPlay = new MenuBoton(imiImagen.getImage(), 1280/2 - imiImagen.getIconWidth()/2, 300, imiImagen.getIconWidth(), imiImagen.getIconHeight(), false);
  163. imiImagen = new ImageIcon(this.getClass().getResource("Menus/bot_puntos.png"));
  164. mbtScores = new MenuBoton(imiImagen.getImage(), 1280/2 - imiImagen.getIconWidth()/2, 390, imiImagen.getIconWidth(), imiImagen.getIconHeight(), false);
  165. imiImagen = new ImageIcon(this.getClass().getResource("Menus/bot_salir.png"));
  166. mbtExit = new MenuBoton(imiImagen.getImage(), 1280/2 - imiImagen.getIconWidth()/2, 450, imiImagen.getIconWidth(), imiImagen.getIconHeight(), false);
  167. imiImagen = new ImageIcon(this.getClass().getResource("Menus/boton_menu.png"));
  168. mbtGameOverMenu = new MenuBoton(imiImagen.getImage(), 1280/2 - imiImagen.getIconWidth()/2, 450, imiImagen.getIconWidth(), imiImagen.getIconHeight(), false);
  169.  
  170. //incializar a Mauro
  171. iVelocityMauro = 2;
  172. iDirectionMauro = 0;
  173. lImmuneTimeMauro = 0;
  174. String sMauro = "Animacion/Mauro/mauro1.png";
  175. ImageIcon imiImage = new ImageIcon(this.getClass().getResource(
  176. sMauro));
  177. mauMauro = new Mauro(70, 640, iVelocityMauro, iDirectionMauro, iWidthMauro, iHeightMauro, imiImage.getImage(), 0, 0, lImmuneTimeMauro);
  178.  
  179. //Inicializa todos los mapas
  180. try {
  181. GameInitMaps();
  182. } catch (IOException ex) {
  183. Logger.getLogger(Board.class.getName()).log(Level.SEVERE, null, ex);
  184. }
  185.  
  186. // inicializa el thread
  187. if (thread == null || !bEndGame) {
  188. thread = new Thread(this);
  189. thread.start();
  190. }
  191. }
  192.  
  193. /**
  194. * GameInitMaps
  195. *
  196. * Metodo que inicializa los mapas del juego.
  197. *
  198. */
  199. private void GameInitMaps() throws IOException {
  200. //variables
  201. String sLevel1WallVertical = "Terrenos/paredVerticalNivel1.jpg";
  202. String sLevel1WallHorizontal = "Terrenos/paredHorizontalNivel1.png";
  203. String sLevel1Trap = "Terrenos/picos0.png";
  204. String sLevel1DoorVertical = "Terrenos/puertaVerticalNivel1.png";
  205. String sLevel1DoorHorizontal = "Terrenos/puertaHorizontalNivel1.png";
  206. String sLevel1Finish = "Terrenos/meta_nivel1.png";
  207. String sLevel1Text = "src/runmaurorun/Terrenos/paredHorizontal.txt";
  208.  
  209. //crea una nueva lista de terrenos
  210. arlMapObjectsLevel1 = new ArrayList();
  211.  
  212. String sDato; //string que almacena lectura de texto
  213. char cCharacter; //variable que permite revisar primer caracter
  214. char cObjectType = 'a';
  215.  
  216. int iWidthObject; //variable apra ancho del objeto
  217. int iHeightObject; //variable para altura del objeto
  218. int iDoorKey; //varaible para control de acceso de puertas
  219.  
  220. boolean bFinishFileRead; //variable que define si se termino la lectura
  221.  
  222. ImageIcon imiImage; //imagen utilizada para crear mapas
  223.  
  224. //buscar y leer archivo de Nivel 1
  225. BufferedReader fileIn;
  226.  
  227. //------------------NIVEL 1-----------------------------------------
  228. try {
  229. fileIn = new BufferedReader(new FileReader(sLevel1Text));
  230. } catch (FileNotFoundException e){
  231. System.out.println("FILE NOT FOUND!");
  232. fileIn = new BufferedReader(new FileReader(
  233. sLevel1Text));
  234. }
  235.  
  236. bFinishFileRead = false;
  237. iWidthObject = 0;
  238. iHeightObject = 0;
  239. imiImage = new ImageIcon(this.getClass().getResource(
  240. sLevel1WallHorizontal));
  241.  
  242. do {
  243. sDato = fileIn.readLine();
  244. cCharacter = sDato.charAt(0);
  245. //definir medidas e imagen a utilizar dependiendo del objeto
  246. switch (cCharacter) {
  247. case 'a': //paredes horizontal
  248. cObjectType = cCharacter;
  249. iWidthObject = 76;
  250. iHeightObject = 19;
  251. imiImage = new ImageIcon(this.getClass().getResource(
  252. sLevel1WallHorizontal));
  253. break;
  254. case 'b': //paredes vertical
  255. cObjectType = cCharacter;
  256. iWidthObject = 19;
  257. iHeightObject = 76;
  258. imiImage = new ImageIcon(this.getClass().getResource(
  259. sLevel1WallVertical));
  260. break;
  261. case 'c': //trampas
  262. cObjectType = cCharacter;
  263. iWidthObject = 32;
  264. iHeightObject = 19;
  265. imiImage = new ImageIcon(this.getClass().getResource(
  266. sLevel1Trap));
  267. break;
  268. case 'd': //puerta horizaontal
  269. cObjectType = cCharacter;
  270. iWidthObject = 92;
  271. iHeightObject = 8;
  272. break;
  273. case 'e': //puerta vertical
  274. cObjectType = cCharacter;
  275. iWidthObject = 8;
  276. iHeightObject = 90;
  277. break;
  278. case 'f': //boton
  279. cObjectType = cCharacter;
  280. iWidthObject = 32;
  281. iHeightObject = 19;
  282. break;
  283. case 'g': //powerup
  284. cObjectType = cCharacter;
  285. iWidthObject = 16;
  286. iHeightObject = 25;
  287. break;
  288. case 'h': //meta
  289. cObjectType = cCharacter;
  290. iWidthObject = 83;
  291. iHeightObject = 47;
  292. imiImage = new ImageIcon(this.getClass().getResource(
  293. sLevel1Finish));
  294. break;
  295. case 'x': //salir
  296. bFinishFileRead = true;
  297. break;
  298. default:
  299. break;
  300. }
  301.  
  302. // agraga los datos a la lista
  303. //si se termino de leer el archivo
  304. if (!bFinishFileRead) {
  305. sDato = fileIn.readLine();
  306. }
  307.  
  308. while (!sDato.equals("999") && !bFinishFileRead) {
  309. //si la linea es un comentario
  310. cCharacter = sDato.charAt(0);
  311. if (cCharacter == '/') {
  312. sDato = fileIn.readLine();
  313. }
  314. int iX = Integer.parseInt(sDato);
  315. sDato = fileIn.readLine();
  316. int iY = Integer.parseInt(sDato);
  317. Terreno terObject;
  318. switch (cObjectType) {
  319. case 'a': //paredes horizontal
  320. terObject = new Terreno(imiImage.getImage(),
  321. iX, iY, iWidthObject, iHeightObject, false, false);
  322. terObject.setImage(imiImage.getImage());
  323. arlMapObjectsLevel1.add(terObject);
  324. break;
  325. case 'b': //paredes vertical
  326. terObject = new Terreno(imiImage.getImage(),
  327. iX, iY, iWidthObject, iHeightObject, false, false);
  328. terObject.setImage(imiImage.getImage());
  329. arlMapObjectsLevel1.add(terObject);
  330. break;
  331. case 'c': //trampas
  332. terObject = new Lava(imiImage.getImage(),
  333. iX, iY, iWidthObject, iHeightObject, -1, false);
  334. terObject.setImage(imiImage.getImage());
  335. arlMapObjectsLevel1.add(terObject);
  336. break;
  337. case 'd': //puerta horizontal
  338. sDato = fileIn.readLine();
  339. iDoorKey = Integer.parseInt(sDato);
  340. sDato = fileIn.readLine();
  341. imiImage = new ImageIcon(this.getClass().getResource(
  342. sDato));
  343. terObject = new Puerta(imiImage.getImage(),
  344. iX, iY, iWidthObject, iHeightObject, false, iDoorKey, false);
  345. terObject.setImage(imiImage.getImage());
  346. arlMapObjectsLevel1.add(terObject);
  347. break;
  348. case 'e': //puerta vertical
  349. sDato = fileIn.readLine();
  350. iDoorKey = Integer.parseInt(sDato);
  351. sDato = fileIn.readLine();
  352. imiImage = new ImageIcon(this.getClass().getResource(
  353. sDato));
  354. terObject = new Puerta(imiImage.getImage(),
  355. iX, iY, iWidthObject, iHeightObject, false, iDoorKey, false);
  356. terObject.setImage(imiImage.getImage());
  357. arlMapObjectsLevel1.add(terObject);
  358. break;
  359. case 'f': //boton
  360. sDato = fileIn.readLine();
  361. iDoorKey = Integer.parseInt(sDato);
  362. sDato = fileIn.readLine();
  363. imiImage = new ImageIcon(this.getClass().getResource(
  364. sDato));
  365. terObject = new Boton(imiImage.getImage(),
  366. iX, iY, iWidthObject, iHeightObject, false, false, iDoorKey);
  367. terObject.setImage(imiImage.getImage());
  368. arlMapObjectsLevel1.add(terObject);
  369. break;
  370. case 'g': //powerup
  371. sDato = fileIn.readLine();
  372. int iPwrUpType = Integer.parseInt(sDato);
  373. sDato = fileIn.readLine();
  374. imiImage = new ImageIcon(this.getClass().getResource(
  375. sDato));
  376. terObject = new PwrUp(false, false, imiImage.getImage(),
  377. iX, iY, iWidthObject, iHeightObject, iPwrUpType, -1);
  378. terObject.setImage(imiImage.getImage());
  379. arlMapObjectsLevel1.add(terObject);
  380. break;
  381. case 'h': //meta
  382. terObject = new Terreno(imiImage.getImage(),
  383. iX, iY, iWidthObject, iHeightObject, false, true);
  384. terObject.setImage(imiImage.getImage());
  385. arlMapObjectsLevel1.add(terObject);
  386. break;
  387. default:
  388. break;
  389. }
  390. sDato = fileIn.readLine();
  391. }
  392. } while (!bFinishFileRead);
  393.  
  394.  
  395. iTiempoLimite = 120; //2 minutos de juego en el nivel 1
  396.  
  397. bFocoOn = false; //no tiene el powerUp
  398. iFocoLimite = 3; //3 segundos de vista mejorada
  399.  
  400. bRelojOn = false; //No tiene el powerup
  401.  
  402. mauMauro.setX(70);
  403. mauMauro.setY(640);
  404.  
  405. }
  406.  
  407. /**
  408. * run
  409. *
  410. * Metodo que corre el juego y actualiza el hilo.
  411. *
  412. */
  413. public void run() {
  414. //Guarda el tiempo actual del sistema
  415. lCurrentTime = System.currentTimeMillis();
  416.  
  417. //variables para el control de tiempo
  418. long lTimeDiff; //diferencia del tiempo contra tiempo actual en un punto
  419. long lSleep; //canitdad limite para dormir el thread en ms
  420.  
  421. //loop que cicla el juego hasta que termina
  422. while (!bEndGame) {
  423. // pinta y actualiza
  424. repaint();
  425.  
  426. //actualiza datos
  427. AnimationCycle();
  428.  
  429. //checa colision
  430. collision();
  431.  
  432. //Calcula la diferencia de tiempo con la ultima marca de tiempo
  433. lTimeDiff = System.currentTimeMillis() - lCurrentTime;
  434. lSleep = 17 - lTimeDiff;
  435.  
  436. //define canitdad a dormir el thread
  437. if (lSleep < 0)
  438. lSleep = 2;
  439. try {
  440. Thread.sleep(lSleep);
  441. } catch (InterruptedException e) {
  442. System.out.println("interrupted");
  443. }
  444.  
  445. //actuializa el tiempo actual
  446. lCurrentTime = System.currentTimeMillis();
  447. }
  448. }
  449.  
  450. /**
  451. * AnimationCycle
  452. *
  453. * Metodo que actualiza el juego
  454. *
  455. */
  456. public void AnimationCycle() {
  457. /*Determina el tiempo que ha transcurrido desde que el Applet inicio su
  458. ejecución*/
  459. lElapsedTime = System.currentTimeMillis() - lCurrentTime;
  460.  
  461. //Guarda el tiempo actual
  462. lCurrentTime += lElapsedTime;
  463.  
  464. //actualiza el jugador
  465. mauMauro.updateMovement(iDirectionMauro);
  466.  
  467. lightCircle();
  468. }
  469.  
  470. /**
  471. * lightCircle
  472. *
  473. * Metodo que controla los objetos a ensenar basados en la distancia de
  474. * Mauro
  475. *
  476. */
  477. public void lightCircle(){
  478. //actualizar en Nivel 1 si objeto es visible
  479. if (iLevel == 1) {
  480. Iterator it = arlMapObjectsLevel1.iterator();
  481.  
  482. if(!bFocoOn){
  483. iDist1 = 50;
  484. iDist2 = 100;
  485. //System.out.println("Chico");
  486. }
  487. else{
  488. iDist1 = 1000;
  489. iDist2 = 1750;
  490. //System.out.println("Grande");
  491.  
  492. long lTiempoAux = System.currentTimeMillis();
  493.  
  494. if((lTiempoAux - lFocoInicial)/1000 >= iFocoLimite){
  495. bFocoOn = !bFocoOn;
  496. }
  497. }
  498.  
  499. // mientras existan objetos en la lista
  500. while (it.hasNext()) {
  501. Terreno terObject = (Terreno) it.next();
  502. // revisar si estan en el area de Mauro
  503. if (showTerreno(terObject, mauMauro, iDist1, iDist2)) {
  504. terObject.setVisible(true);
  505. }
  506. else {
  507. terObject.setVisible(false);
  508. }
  509. //cambiar de nivel
  510. if (terObject.isExit() && mauMauro.colision(terObject)) {
  511. iLevel++;
  512. }
  513. }
  514. }
  515. }
  516.  
  517. /**
  518. * showTerreno
  519. *
  520. * Metodo que checa si algo esta cerca de Mauro
  521. *
  522. * @param objObject es un objeto de la clase <code>Object</code>
  523. * @param objMauro es un objeto de la clase <code>Object</code>
  524. * @return un boleano para saber si intersecta o no
  525. */
  526. public boolean showTerreno(Object objTerreno, Object objMauro, int iFactor1, int iFactor2) {
  527.  
  528. //Crea un rectangulo para el objeto terreno
  529. Terreno terObject = (Terreno) objTerreno;
  530. Rectangle rctEsteObjeto = new Rectangle(terObject.getX(), terObject.getY(),
  531. terObject.getWidth(), terObject.getHeight());
  532.  
  533. //Crea un rectangulo para Mauro, mas grande de lo que es
  534. Mauro mauObject = (Mauro) objMauro;
  535. Rectangle rctObjetoParam = new Rectangle(mauObject.getX()-iFactor1,
  536. mauObject.getY()-iFactor1, mauObject.getWidth()+iFactor2,
  537. mauObject.getHeight()+iFactor2);
  538.  
  539. return rctEsteObjeto.intersects(rctObjetoParam);
  540.  
  541. }
  542.  
  543. public void collision(){
  544.  
  545. //colision con las paredes
  546. Iterator it = arlMapObjectsLevel1.iterator();
  547.  
  548. // mientras existan objetos en la lista
  549. while (it.hasNext()) {
  550. Terreno terObject = (Terreno) it.next();
  551.  
  552. //Si el objeto es solido y existe colision...
  553. if(terObject.isSolid() && mauMauro.colision(terObject)){
  554. //Si es "lava"
  555. if(terObject instanceof Lava){
  556. //regresar a mauro a posicion inicial
  557. mauMauro.setX(70);
  558. mauMauro.setY(640);
  559. }
  560. //Si es un boton, abrir puerta
  561. else if(terObject instanceof Boton){
  562. Boton terBoton = (Boton) terObject;
  563. iDoorOpenKey = terBoton.getButtonNo();
  564. terBoton.setClicked(true);
  565.  
  566. Iterator it1 = arlMapObjectsLevel1.iterator();
  567.  
  568. //busca la puerta con el mismo codigo
  569. while (it1.hasNext()) {
  570. Terreno terObject1 = (Terreno) it1.next();
  571. if(terObject1 instanceof Puerta){
  572. Puerta terPuerta = (Puerta) terObject1;
  573. if(terPuerta.getDoorNo() == iDoorOpenKey){
  574. terPuerta.setOpen(true);
  575. terPuerta.setSolid(false);
  576. //terPuerta.setVisible(false);
  577. }
  578. }
  579. }
  580. }
  581. //Si agarra un PowerUp
  582. else if(terObject instanceof PwrUp){
  583. PwrUp terUp = (PwrUp) terObject;
  584.  
  585. //Si es un foco
  586. if(terUp.getType() == 2){
  587. if(!terUp.getGrabbed()){
  588. lFocoInicial = System.currentTimeMillis();
  589. bFocoOn = true;
  590. terUp.setGrabbed(true);
  591. terUp.setVisible(false);
  592. }
  593. }
  594. //Si es un reloj
  595. else{
  596. if(!terUp.getGrabbed()){
  597. bRelojOn = true;
  598. terUp.setGrabbed(true);
  599. terUp.setVisible(false);
  600. }
  601. }
  602.  
  603. }
  604. //Si es una pared (o puerta solida), anular movimiento
  605. else{
  606. if(iDirectionMauro == 1){
  607. mauMauro.updateMovement(2);
  608. }
  609. else if(iDirectionMauro == 2){
  610. mauMauro.updateMovement(1);
  611. }
  612. else if(iDirectionMauro == 3){
  613. mauMauro.updateMovement(4);
  614. }
  615. else if(iDirectionMauro == 4){
  616. mauMauro.updateMovement(3);
  617. }
  618. }
  619. }
  620. }
  621. }
  622.  
  623. /**
  624. * drawItems
  625. *
  626. * Metodo que dibuja a los items
  627. *
  628. * @param graGrafico es el <code>objeto grafico</code> usado para dibujar.
  629. */
  630. public void drawItems(Graphics graGrafico) {
  631.  
  632. }
  633.  
  634. /**
  635. * drawMauro
  636. *
  637. * Metodo que dibuja a Mauro.
  638. *
  639. * @param graGrafico es el <code>objeto grafico</code> usado para dibujar.
  640. */
  641. public void drawMauro(Graphics graGrafico) {
  642. // mientras exista Mauro
  643. if (mauMauro.isVisible() && iLevel != 0) {
  644. mauMauro.getAnimacion().actualiza(lElapsedTime);
  645. mauMauro.setAnimacion(iDirectionMauro);
  646. graGrafico.drawImage(mauMauro.getAnimacion().getImagen(), mauMauro.getX(),
  647. mauMauro.getY(), this);
  648. }
  649.  
  650. /*// si el juegador se muere termina el juego
  651. if (mauMauro.isDying()) {
  652. plyPlayer.die();
  653. bIngame = false;
  654. }*/
  655. }
  656.  
  657. /**
  658. * drawEnemigos
  659. *
  660. * Metodo que dibuja a los enemigos.
  661. *
  662. * @param graGrafico es el <code>objeto grafico</code> usado para dibujar.
  663. */
  664. public void drawEnemigos(Graphics graGrafico) {
  665.  
  666. }
  667.  
  668. /**
  669. * drawTerrenos
  670. *
  671. * Metodo que dibuja los terrenos
  672. *
  673. * @param graGrafico es el <code>objeto grafico</code> usado para dibujar.
  674. */
  675. public void drawTerrenos(Graphics graGrafico) {
  676. //dependiendo del nivel se pintara dichos objetos
  677. switch (iLevel) {
  678. case 1:
  679. Iterator it = arlMapObjectsLevel1.iterator();
  680.  
  681. // mientras existan objetos en la lista
  682. while (it.hasNext()) {
  683.  
  684. Terreno terObject = (Terreno) it.next();
  685.  
  686. if(terObject instanceof Lava){
  687. Lava terTrap = (Lava) terObject;
  688.  
  689. if (terTrap.isVisible()) {
  690. terTrap.getAnimacion().actualiza(lElapsedTime);
  691. graGrafico.drawImage(terTrap.getAnimacion().getImagen(), terTrap.getX(),
  692. terTrap.getY(), this);
  693. }
  694. }
  695.  
  696. //si es una puerta
  697. else if(terObject instanceof Puerta){
  698.  
  699. Puerta terDoor = (Puerta) terObject;
  700.  
  701. if (terDoor.isVisible() && !terDoor.isOpen()) {
  702. graGrafico.drawImage(terDoor.getImage(), terDoor.getX(),
  703. terDoor.getY(), this);
  704. }
  705.  
  706.  
  707. }
  708. //si es un boton
  709. else if(terObject instanceof Boton){
  710.  
  711. Boton terBoton = (Boton) terObject;
  712.  
  713. if(terBoton.isVisible() && !terBoton.getClicked()){
  714. graGrafico.drawImage(terBoton.getImage(), terBoton.getX(),
  715. terBoton.getY(), this);
  716. }
  717. }
  718. //Si es un powerup
  719. else if(terObject instanceof PwrUp){
  720. PwrUp terUp = (PwrUp) terObject;
  721.  
  722. if(terUp.isVisible() && !terUp.getGrabbed()){
  723. graGrafico.drawImage(terUp.getImage(), terUp.getX(),
  724. terUp.getY(), this);
  725. }
  726. }
  727. //si es una pared
  728. else if (terObject.isVisible()) {
  729. graGrafico.drawImage(terObject.getImage(), terObject.getX(),
  730. terObject.getY(), this);
  731.  
  732. }
  733.  
  734.  
  735.  
  736. }
  737. break;
  738. case 2:
  739.  
  740. break;
  741. case 3:
  742.  
  743. break;
  744. case 4:
  745.  
  746. break;
  747. default:
  748.  
  749. break;
  750. }
  751. }
  752.  
  753. /**
  754. * drawGameOver
  755. *
  756. * Metodo que dibuja el game over
  757. *
  758. * @param graGrafico es el <code>objeto grafico</code> usado para dibujar.
  759. */
  760. public void drawGameOver(Graphics graGrafico) {
  761.  
  762. }
  763.  
  764. /**
  765. * drawTimeBar
  766. *
  767. * Metodo que dibuja la barra del tiempo
  768. *
  769. * @param graGrafico es el <code>objeto grafico</code> usado para dibujar.
  770. */
  771. public void drawTimeBar(Graphics graGrafico){
  772. //Crea el fondo de la barra de tiempo
  773. String sBar = "Menus/barraTiempo_Fondo.png";
  774. ImageIcon imiImage = new ImageIcon(this.getClass().getResource(sBar));
  775. graGrafico.drawImage(imiImage.getImage(), 0, iHeight-41, this);
  776.  
  777. //Saca el tiempo actual
  778. lBarraActual = System.currentTimeMillis();
  779.  
  780. //Saca el tiempo que ha pasado desde el inicio del nivel
  781. iBarraElapsed = (int)(lBarraActual - lBarraInicial)/1000;
  782.  
  783. if(bRelojOn){
  784. lBarraInicial += 8000;
  785. bRelojOn = !bRelojOn;
  786. }
  787.  
  788. if (iTiempoLimite - iBarraElapsed > 0) {
  789. //Se dibuja la barra verde que se modifica con el tiempo
  790. graGrafico.setColor(Color.GREEN);
  791. graGrafico.fillRect(85, iHeight-38,(1115/iTiempoLimite)*(iTiempoLimite-iBarraElapsed),
  792. 13);
  793. }
  794.  
  795. else {
  796. bGameOver = true;
  797. iLevel = 5;
  798. System.out.println("barra");
  799. }
  800.  
  801. /**if (bGameOver) {
  802. String sGameOver = "Menus/gameover.png";
  803. ImageIcon imiImageGameOver = new ImageIcon(this.getClass().getResource(sGameOver));
  804. graGrafico.drawImage(imiImageGameOver.getImage(), 0, 0, this);
  805. //boton
  806. mbtGameOverMenu.paint(graGrafico, this);
  807. mbtGameOverMenu.setVisible(true);
  808. iLevel = 5;
  809. }**/
  810.  
  811.  
  812. }
  813.  
  814. /**
  815. * drawMenu
  816. *
  817. * Metodo que dibuja el menu y los submenús.
  818. *
  819. * @param graGrafico es el <code>objeto grafico</code> usado para dibujar.
  820. */
  821. public void drawMenu(Graphics graGrafico) {
  822. // Switch que se utiliza para ver qué pantalla desplegar
  823. switch (iMenu) {
  824. case 1: { // se despliega la portada
  825. URL urlMenu = this.getClass().getResource("Menus/menu1.png");
  826. Image imaMenu = Toolkit.getDefaultToolkit().getImage(urlMenu);
  827. graGrafico.drawImage(imaMenu, 0, 0, this);
  828. break;
  829. }
  830. case 2: {
  831. // se despliega la segunda portada
  832. }
  833. case 3: { // se despliega el MENU
  834. // dibuja el fondo
  835. URL urlMenu = this.getClass().getResource("Menus/menu3.png");
  836. Image imaMenu = Toolkit.getDefaultToolkit().getImage(urlMenu);
  837. graGrafico.drawImage(imaMenu, 0, 0, this);
  838. // dibuja los botones
  839. mbtPlay.paint(graGrafico, this);
  840. mbtInstruc.paint(graGrafico, this);
  841. mbtAbout.paint(graGrafico, this);
  842. mbtOptions.paint(graGrafico, this);
  843. mbtScores.paint(graGrafico, this);
  844. mbtExit.paint(graGrafico, this);
  845. // pone el estado de los botones en visible
  846. mbtPlay.setVisible(true);
  847. mbtInstruc.setVisible(true);
  848. mbtOptions.setVisible(true);
  849. mbtAbout.setVisible(true);
  850. mbtScores.setVisible(true);
  851. mbtExit.setVisible(true);
  852. break;
  853. }
  854. case 4: { // se dibujan las instrucciones
  855. // dibuja el fondo
  856. URL urlMenu = this.getClass().getResource("Menus/menu4.png");
  857. Image imaMenu = Toolkit.getDefaultToolkit().getImage(urlMenu);
  858. graGrafico.drawImage(imaMenu, 0, 0, this);
  859. // dibuja el botón back y lo pone visible
  860. mbtBack.paint(graGrafico, this);
  861. mbtBack.setVisible(true);
  862. break;
  863. }
  864. case 5: { // se dibujan las opciones
  865. URL urlMenu = this.getClass().getResource("Menus/menu5.png");
  866. Image imaMenu = Toolkit.getDefaultToolkit().getImage(urlMenu);
  867. graGrafico.drawImage(imaMenu, 0, 0, this);
  868. // dibuja el botón back y lo pone visible
  869. mbtBack.paint(graGrafico, this);
  870. mbtBack.setVisible(true);
  871. break;
  872. }
  873. case 6: { // se dibuja el acerca
  874. URL urlMenu = this.getClass().getResource("Menus/menu6.png");
  875. Image imaMenu = Toolkit.getDefaultToolkit().getImage(urlMenu);
  876. graGrafico.drawImage(imaMenu, 0, 0, this);
  877. // dibuja el botón back y lo pone visible
  878. mbtBack.paint(graGrafico, this);
  879. mbtBack.setVisible(true);
  880. break;
  881. }
  882. case 7: { // se dibujan los puntajes
  883. URL urlMenu = this.getClass().getResource("Menus/menu7.png");
  884. Image imaMenu = Toolkit.getDefaultToolkit().getImage(urlMenu);
  885. graGrafico.drawImage(imaMenu, 0, 0, this);
  886. // dibuja el botón back y lo pone visible
  887. mbtBack.paint(graGrafico, this);
  888. mbtBack.setVisible(true);
  889. break;
  890. }
  891. }
  892. }
  893.  
  894. /**
  895. * Metodo <I>paint</I> sobrescrito de la clase <code>JPanel</code>,
  896. * heredado de la clase Container.<P>
  897. * En este metodo se dibuja la imagen con la posicion actualizada,
  898. * ademas que cuando la imagen es cargada te despliega una advertencia.
  899. * @param graGrafico es el <code>objeto grafico</code> usado para dibujar.
  900. */
  901. public void paint(Graphics graGrafico) {
  902. //llama al paint de la clase que heredaste
  903. super.paint(graGrafico);
  904.  
  905. if (!bEndGame) {
  906. switch (iLevel) {
  907. case -1: //animacion inical
  908.  
  909. break;
  910. case 0: //menu
  911. drawMenu(graGrafico);
  912. graGrafico.setColor(Color.decode("#1d2e4c"));
  913. break;
  914. case 1:
  915. Graphics2D g2d = (Graphics2D) graGrafico;
  916. g2d.setColor(Color.decode("#1d2e4c"));
  917. g2d.fillOval(mauMauro.getX()-40, mauMauro.getY()-35, 100, 100);
  918. g2d.drawOval(mauMauro.getX()-40, mauMauro.getY()-35, 100, 100);
  919. setBackground(Color.black);
  920. drawTerrenos(graGrafico);
  921. drawMauro(graGrafico);
  922. if (!bGameOver) {
  923. drawTimeBar(graGrafico);
  924. }
  925. break;
  926. case 2:
  927. String sNextLevel = "Menus/mensaje.png";
  928. ImageIcon imiImageGameOver = new ImageIcon(this.getClass().getResource(sNextLevel));
  929. graGrafico.drawImage(imiImageGameOver.getImage(), 0, 0, this);
  930. //boton
  931. mbtGameOverMenu.paint(graGrafico, this);
  932. mbtGameOverMenu.setVisible(true);
  933. break;
  934. case 3:
  935. graGrafico.setColor(Color.decode("#1d2e4c"));
  936. break;
  937. case 4:
  938. graGrafico.setColor(Color.decode("#1d2e4c"));
  939. break;
  940. default:
  941. graGrafico.setColor(Color.black);
  942. String sGameOver = "Menus/gameover.png";
  943. ImageIcon imiImageGameOver2 = new ImageIcon(this.getClass().getResource(sGameOver));
  944. graGrafico.drawImage(imiImageGameOver2.getImage(), 0, 0, this);
  945. //boton
  946. mbtGameOverMenu.paint(graGrafico, this);
  947. mbtGameOverMenu.setVisible(true);
  948. break;
  949. }
  950.  
  951. }
  952.  
  953. //si el juego termino
  954. else {
  955.  
  956. }
  957.  
  958. //comandos para paint
  959. Toolkit.getDefaultToolkit().sync();
  960. graGrafico.dispose();
  961. }
  962.  
  963. /**
  964. * keyTyped
  965. *
  966. * Metodo sobrescrito de la interface <code>KeyListener</code>.<P>
  967. * En este metodo maneja el evento que se genera al presionar una
  968. * tecla que no es de accion.
  969. *
  970. * @param keyEvent es el <code>KeyEvent</code> que se genera en al
  971. * presionar.
  972. *
  973. */
  974. public void keyTyped(KeyEvent keyEvent) {
  975. // no hay codigo pero se debe escribir el metodo
  976. }
  977.  
  978. /**
  979. * keyPressed
  980. *
  981. * Metodo sobrescrito de la interface <code>KeyListener</code>.<P>
  982. * En este metodo maneja el evento que se genera al dejar presionada
  983. * alguna tecla.
  984. * Se cambia la direccion al presionar una tecla
  985. *
  986. * @param keyEvent es el <code>KeyEvent</code> que se genera en al
  987. * presionar.
  988. *
  989. */
  990. public void keyPressed(KeyEvent keyEvent) {
  991. // si presiono la flecha arriba
  992. if(keyEvent.getKeyCode() == KeyEvent.VK_UP) {
  993. iDirectionMauro = 1;
  994. }
  995. // si presiono la flecha abajo
  996. else if(keyEvent.getKeyCode() == KeyEvent.VK_DOWN) {
  997. iDirectionMauro = 2;
  998. }
  999. // si presiono la flecha izquierda
  1000. else if(keyEvent.getKeyCode() == KeyEvent.VK_LEFT) {
  1001. iDirectionMauro = 3;
  1002. }
  1003. // si presiono la flecha derecha
  1004. else if(keyEvent.getKeyCode() == KeyEvent.VK_RIGHT) {
  1005. iDirectionMauro = 4;
  1006. }
  1007. }
  1008.  
  1009. /**
  1010. * keyReleased
  1011. * Metodo sobrescrito de la interface <code>KeyListener</code>.<P>
  1012. * En este metodo maneja el evento que se genera al soltar la tecla.
  1013. *
  1014. * @param keyEvent es el <code>KeyEvent</code> que se genera en al soltar.
  1015. *
  1016. */
  1017. public void keyReleased(KeyEvent keyEvent) {
  1018. // si presiono la flecha arriba
  1019. if(keyEvent.getKeyCode() == KeyEvent.VK_UP) {
  1020. iDirectionMauro = 0;
  1021. }
  1022. // si presiono la flecha abajo
  1023. else if(keyEvent.getKeyCode() == KeyEvent.VK_DOWN) {
  1024. iDirectionMauro = 0;
  1025. }
  1026. // si presiono la flecha izquierda
  1027. else if(keyEvent.getKeyCode() == KeyEvent.VK_LEFT) {
  1028. iDirectionMauro = 0;
  1029. }
  1030. // si presiono la flecha derecha
  1031. else if(keyEvent.getKeyCode() == KeyEvent.VK_RIGHT) {
  1032. iDirectionMauro = 0;
  1033. }
  1034. }
  1035.  
  1036. /**
  1037. * mouseClicked
  1038. *
  1039. * Metodo sobrescrito de la clase <code>MouseListener</code>.<P>
  1040. * En este metodo maneja el evento que se genera al clickear el mouse.
  1041. *
  1042. * @param mseEvent es el <code>MouseEvent</code> que se genera en
  1043. * al presionar.
  1044. *
  1045. */
  1046. public void mouseClicked(MouseEvent mseEvent) {
  1047. // no se utiliza
  1048. }
  1049.  
  1050. /**
  1051. * mousePressed
  1052. *
  1053. * Metodo sobrescrito de la clase <code>MouseListener</code>.<P>
  1054. * En este metodo maneja el evento que se genera al presionar el mouse.
  1055. *
  1056. * @param mseEvent es el <code>MouseEvent</code> que se genera en
  1057. * al presionar.
  1058. *
  1059. */
  1060. public void mousePressed(MouseEvent mseEvent) {
  1061. // no se utiliza
  1062. }
  1063.  
  1064. /**
  1065. * mouseReleased
  1066. *
  1067. * Metodo sobrescrito de la clase <code>MouseListener</code>.<P>
  1068. * En este metodo maneja el evento que se genera al soltar el mouse.
  1069. *
  1070. * @param mseEvent es el <code>MoseEvent</code> que se genera en
  1071. * al presionar.
  1072. *
  1073. */
  1074. public void mouseReleased(MouseEvent mseEvent) {
  1075. // variables que guardan la posición del mouse
  1076. iMouseX = mseEvent.getX();
  1077. iMouseY = mseEvent.getY();
  1078.  
  1079. /* Checa si el mouse colisiona con algún botón y cambia la variable de
  1080. menú a la adecuada.
  1081. */
  1082. if (mbtBack.isVisible() && mbtBack.intersectaMouse(iMouseX, iMouseY)) {
  1083. iMenu = 3;
  1084. mbtBack.setVisible(false);
  1085. }
  1086. else if (mbtPlay.isVisible() && mbtPlay.intersectaMouse(iMouseX, iMouseY)) {
  1087. iMenu = 3;
  1088. mbtPlay.setVisible(false);
  1089. mbtInstruc.setVisible(false);
  1090. mbtOptions.setVisible(false);
  1091. mbtAbout.setVisible(false);
  1092. mbtScores.setVisible(false);
  1093. mbtExit.setVisible(false);
  1094. bPlay = true;
  1095. iLevel = 1;
  1096. lBarraInicial = System.currentTimeMillis()+5000; //saca el tiempo en donde inicia el juego
  1097. //Se define sonido de fondo
  1098. sndSonidoFondo1 = new SoundClip("Musica/nivel1.wav");
  1099. sndSonidoFondo1.setLooping(true);
  1100. sndSonidoFondo1.play();
  1101. bGameOver = false;
  1102. }
  1103. else if (mbtInstruc.isVisible() && mbtInstruc.intersectaMouse(iMouseX, iMouseY)) {
  1104. iMenu = 4;
  1105. mbtPlay.setVisible(false);
  1106. mbtInstruc.setVisible(false);
  1107. mbtOptions.setVisible(false);
  1108. mbtAbout.setVisible(false);
  1109. mbtScores.setVisible(false);
  1110. mbtExit.setVisible(false);
  1111. }
  1112. else if (mbtOptions.isVisible() && mbtOptions.intersectaMouse(iMouseX, iMouseY)) {
  1113. iMenu = 5;
  1114. mbtPlay.setVisible(false);
  1115. mbtInstruc.setVisible(false);
  1116. mbtOptions.setVisible(false);
  1117. mbtAbout.setVisible(false);
  1118. mbtScores.setVisible(false);
  1119. mbtExit.setVisible(false);
  1120. }
  1121. else if (mbtAbout.isVisible() && mbtAbout.intersectaMouse(iMouseX, iMouseY)) {
  1122. iMenu = 6;
  1123. mbtPlay.setVisible(false);
  1124. mbtInstruc.setVisible(false);
  1125. mbtOptions.setVisible(false);
  1126. mbtAbout.setVisible(false);
  1127. mbtScores.setVisible(false);
  1128. mbtExit.setVisible(false);
  1129. }
  1130. else if (mbtScores.isVisible() && mbtScores.intersectaMouse(iMouseX, iMouseY)) {
  1131. iMenu = 7;
  1132. mbtPlay.setVisible(false);
  1133. mbtInstruc.setVisible(false);
  1134. mbtOptions.setVisible(false);
  1135. mbtAbout.setVisible(false);
  1136. mbtScores.setVisible(false);
  1137. mbtExit.setVisible(false);
  1138. }
  1139. else if (mbtGameOverMenu.isVisible() && mbtGameOverMenu.intersectaMouse(iMouseX, iMouseY)) {
  1140. iMenu = 3;
  1141. iLevel = 0;
  1142. mbtGameOverMenu.setVisible(false);
  1143. try {
  1144. GameInitMaps();
  1145. //bGameOver = false;
  1146. } catch (IOException ex) {
  1147. Logger.getLogger(Board.class.getName()).log(Level.SEVERE, null, ex);
  1148. }
  1149. }
  1150. else if (mbtExit.isVisible() && mbtExit.intersectaMouse(iMouseX, iMouseY)) {
  1151. System.exit(0);
  1152. }
  1153.  
  1154.  
  1155.  
  1156. }
  1157.  
  1158. /**
  1159. * mouseEntered
  1160. *
  1161. * Metodo sobrescrito de la clase <code>MouseListener</code>.<P>
  1162. * En este metodo maneja el evento que se genera cuando el mouse entra en
  1163. * el applet.
  1164. *
  1165. * @param mseEvent es el <code>MoseEvent</code> que se genera en
  1166. * al presionar.
  1167. *
  1168. */
  1169. public void mouseEntered(MouseEvent mseEvent) {
  1170. // no se utiliza
  1171. }
  1172.  
  1173. /**
  1174. * mouseExited
  1175. *
  1176. * Metodo sobrescrito de la clase <code>MouseListener</code>.<P>
  1177. * En este metodo maneja el evento que se genera cuando el mouse sale en
  1178. * el applet.
  1179. *
  1180. * @param mseEvent es el <code>MoseEvent</code> que se genera en
  1181. * al presionar.
  1182. *
  1183. */
  1184. public void mouseExited(MouseEvent mseEvent) {
  1185. // no se utiliza
  1186. }
  1187.  
  1188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement