/* ***************************************************************************************** AUTHOR: Simona Pappalardo MAIL: pacana@libero.it * *****************************************************************************************/ import java.lang.*; import java.awt.*; import java.awt.event.*; import java.applet.AudioClip; public class AppletEnglish extends java.applet.Applet implements Runnable { static int punteggio; static Color color; static int livello; Tetrisgame game; Thread gameRunner; Image immagFuoriSchermo;//double buffering Graphics gFuoriSchermo;//double buffering AudioClip suono_apertura; AudioClip suono_gameover; AudioClip suono_riga; AudioClip suono_posiz_pezzo; AudioClip suono_cambio_liv; AudioClip suono_sfondo; int key; int maxX;// x grid size int maxY;//y grid size int x;// x applet size int y;// y applet size boolean gameOver; boolean eseguito1,eseguito2,eseguito3; boolean fine; boolean disegna; static boolean modifica_livello = false; Base pezzo; Font carattere1,carattere2; int appletX = 100;// x applet coordinate int appletY = 50;// y applet coordinate int elemBaseX = 10;// little square width that build up a piece int elemBaseY = 10;// little square height that build up a piece public AppletEnglish() { addKeyListener(new KeyListener() { public void keyPressed(KeyEvent e) { key=e.getKeyCode(); switch (key) { case 65: case 0: game = new Tetrisgame(maxX,maxY); stop(); start(); break; } if (!gameOver) { switch(key) { case 39:// right arrow keycode //System.out.println("Ricevuto tasto"); disegna = game.pezzo.right_translation(); if(disegna) repaint(); break; case 37:// left arrow keycode disegna = game.pezzo.left_translation(); if (disegna) repaint(); break; case 38:// down arrow keycode disegna = game.pezzo.anticlockwiseRotation(); if (disegna) repaint(); break; case 40: // up arrow keycode //System.out.println("Ricevuto tasto"); disegna = game.pezzo.clockwiseRotation(); if(disegna) repaint(); break; case 32: // char barra spaziatrice //System.out.println("Ricevuto tasto"); disegna = game.pezzo.fall_down_fast(); if(disegna) repaint(); break; } } } public void keyTyped(KeyEvent e) { //System.out.println("keyTyped" + e); } public void keyReleased(KeyEvent e) { //System.out.println("keyReleased" + e); } }); } public void init() { color = getBackground(); punteggio = 0; carattere1 = new Font("MonoSpaced",Font.BOLD,20); carattere2 = new Font("SansSerif",Font.BOLD,10); fine = false; gameRunner = null; immagFuoriSchermo = createImage(size().width, size().height);//double buffering gFuoriSchermo = immagFuoriSchermo.getGraphics();//double buffering livello = 1; try { x = Integer.parseInt(getParameter("X")); } catch (NullPointerException e) {x=120;} try { y = Integer.parseInt(getParameter("Y")); } catch (NullPointerException e) {y=240;} /* * we have to divide the applet size by little square size so we can map an applet * little square to a grid little square which is 1x1 */ maxX = (int) (x / elemBaseX);// x grid size maxY = (int)(y / elemBaseY);// y grid size eseguito1 = false; eseguito2 = false; eseguito3 = false; game = new Tetrisgame(maxX,maxY); // sound inizialization suono_apertura = getAudioClip(getCodeBase(),"Apertura.au"); suono_gameover = getAudioClip(getCodeBase(),"GameOver.au"); suono_riga = getAudioClip(getCodeBase(),"Riga.au"); suono_posiz_pezzo = getAudioClip(getCodeBase(),"PosizPezzo.au"); suono_cambio_liv = getAudioClip(getCodeBase(),"ChangeLiv.au"); suono_sfondo = getAudioClip(getCodeBase(),"tetris1.mid"); } public void start() { if (gameRunner == null) { gameRunner = new Thread(this); gameRunner.start(); } } public void stop() { if (gameRunner.isAlive()) { if(suono_apertura != null) suono_apertura.stop(); if(suono_gameover != null) suono_gameover.stop(); if(suono_riga != null) suono_riga.stop(); if(suono_posiz_pezzo != null) suono_posiz_pezzo.stop(); if(suono_cambio_liv != null) suono_cambio_liv.stop(); if(suono_sfondo != null) suono_sfondo.stop(); gameRunner.stop(); gameRunner = null; } } public void run() { if(suono_apertura != null) suono_apertura.play();// play the "ouverture" sound if(suono_sfondo != null) suono_sfondo.loop(); // play the background song "forever" gameOver = false; while(!gameOver) { if(modifica_livello) { try {Thread.sleep(3000); } catch (InterruptedException e){} modifica_livello = false; } game.makePiece(); /* * zero out Base.cont since Base.fall_down_fast() can be called just one time * per piece. */ Base.cont = 0; if (!gameOver) repaint(); while (!fine && !gameOver) { fine = game.goDown(); if (Tetrisgame.rigaPiena) { if(suono_riga != null) suono_riga.play(); // Tetrisgame.rigaPiena = false; } if(fine) if(suono_posiz_pezzo != null) suono_posiz_pezzo.play(); repaint(); gameOver = game.end(); if (gameOver) { if(suono_gameover != null) suono_gameover.play(); repaint(); break; } if(punteggio >= 0 && punteggio < 200) try {Thread.sleep(300); } catch (InterruptedException e){} if(punteggio>= 200 && punteggio < 500) try {Thread.sleep(200); } catch (InterruptedException e){} if(punteggio >= 500 && punteggio < 1000) try {Thread.sleep(150); } catch (InterruptedException e){} if(punteggio >= 1000) try {Thread.sleep(100); } catch (InterruptedException e){} } fine = false; } } public void update (Graphics g) { paint(g); } public void paint(Graphics g) { if (punteggio >= 170 && !eseguito1) { modifica_livello = true; // game.paint(gFuoriSchermo); if(suono_cambio_liv != null) suono_cambio_liv.play(); livello = 2; for (int i=0;i= 370 && !eseguito2) { modifica_livello = true; // game.paint(gFuoriSchermo); if(suono_cambio_liv != null) suono_cambio_liv.play(); livello = 3; for (int i=0;i= 570 && !eseguito3) { modifica_livello = true; // game.paint(gFuoriSchermo); if(suono_cambio_liv != null) suono_cambio_liv.play(); livello = 4; for (int i=0;i Move right ",appletX,appletY+300); gFuoriSchermo.drawString("Arrow left --> Move left ",appletX,appletY+320); gFuoriSchermo.drawString("Arrow up --> Anticlockwise rotation",appletX,appletY+340); gFuoriSchermo.drawString("Arrow down --> Clockwise rotation",appletX,appletY+360); gFuoriSchermo.drawString("Space --> Go down fast",appletX,appletY+380); if(!Tetrisgame.rigaPiena) { game.paint(gFuoriSchermo); Tetrisgame.rigaPiena = false; } gFuoriSchermo.setColor(Color.black); gFuoriSchermo.drawRect(appletX,appletY,maxX*10,maxY*10); gFuoriSchermo.setFont(carattere2); if(gameOver) { gFuoriSchermo.setColor(Color.red); gFuoriSchermo.setFont(carattere1); gFuoriSchermo.drawString("GAME OVER",106,(maxY/2)*10+appletY); gFuoriSchermo.setColor(Color.black); if(suono_sfondo != null) suono_sfondo.stop(); } g.drawImage(immagFuoriSchermo, 0, 0, this);//double buffering } }