/* Demo23d.java AdC. Lab2. VideoJuego. StandAlone version. 13Jul2007 */ import hardware.jnpout32.*; import java.awt.*; //import java.awt.Toolkit; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.applet.*; import java.net.*; import java.io.*; //import java.lang.Thread; import java.lang.*; public class Demo23d extends JComponent implements Runnable { private final static int ANCHO=640, ALTO=480; private final static int DIAMETRO=20, RADIO=10; private final static int NUMCOMIDAS=6; private final static int THEq=81, THEa=65; /*private float x, y; private float vx, vy;*/ private int dog1x,dog1y,dog2x,dog2y,dog3x,dog3y,velxdog1,velydog1,velxdog2,velydog2,velxdog3,velydog3; private int jerryx,jerryy; private float tomx,tomy,velxtom,velytom; private boolean arriba, abajo, izquierda, derecha; private double catx,caty,distancia; private int contjerrys; private boolean tomatrapado,activo; //private int x2,y2,vx2,vy2; Random rnd; Image dog1img,dog2img,dog3img,tomimg,jerryimg,bgimage; FileInputStream fin; BufferedReader myInput; private String thisline,thedouble,theexpo,THEPATH; private double expo,mydouble,thefactor,lastdouble; private int thevalue; private short CIRCUITVALUE; pPort lpt; public Demo23d() { setPreferredSize(new Dimension(ANCHO,ALTO)); rnd = new Random(); dog1x = rnd.nextInt(ANCHO); dog1y = rnd.nextInt(ALTO); dog2x = rnd.nextInt(ANCHO); dog2y = rnd.nextInt(ALTO); dog3x = rnd.nextInt(ANCHO); dog3y = rnd.nextInt(ALTO); tomx = rnd.nextInt(ANCHO-RADIO); tomy = rnd.nextInt(ALTO-RADIO); jerryx = rnd.nextInt(ANCHO-RADIO); jerryy = rnd.nextInt(ALTO-RADIO); velxdog1 = 5; velydog1 = 5; velxdog2 = 5; velydog2 = 5; velxdog3 = 5; velydog3 = 5; catx = 100; caty = 100; distancia = 100; dog1img = Toolkit.getDefaultToolkit().createImage("dog.gif"); dog2img = Toolkit.getDefaultToolkit().createImage("dog.gif"); dog3img = Toolkit.getDefaultToolkit().createImage("dog.gif"); tomimg = Toolkit.getDefaultToolkit().createImage("tom.gif"); jerryimg = Toolkit.getDefaultToolkit().createImage("jerry.gif"); bgimage = Toolkit.getDefaultToolkit().createImage("fondo.gif"); contjerrys=0; tomatrapado=false; activo=false; THEPATH = "C:\\MATLAB6p5\\work\\circuit.dat"; thisline = new String(); thevalue = 0; expo=0; mydouble=0; thefactor=0; lastdouble=0; thedouble = new String(); theexpo = new String(); arriba=false; derecha=false; abajo=false; izquierda=false; CIRCUITVALUE = 0; //lpt = new pPort(); addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { actualiza(e.getKeyCode(), true); } public void keyReleased(KeyEvent e) { actualiza(e.getKeyCode(), false); } /*public void keyTyped(KeyEvent e) { actualiza(e.getKeyCode(), true); }*/ private void actualiza(int keyCode, boolean pressed) { if (keyCode==THEq) { activo=true; } if (keyCode==THEa) { reacomodartomyjerry(); } switch (keyCode) { case KeyEvent.VK_UP: arriba = pressed; break; case KeyEvent.VK_DOWN: abajo = pressed; break; case KeyEvent.VK_LEFT: izquierda = pressed; break; case KeyEvent.VK_RIGHT: derecha = pressed; break; } } }); setFocusable(true); } private float clamp(float valor, float min, float max) { if (valor > max) return max; if (valor < min) return min; return valor; } /*private void fisica(float dt) { vx = 0; vy = 0; if (arriba) vy = -300; if (abajo) vy = 300; if (izquierda) vx = -300; if (derecha) vx = 300; x = clamp(x + vx * dt, 0, ANCHO - DIAMETRO); y = clamp(y + vy * dt, 0, ALTO - DIAMETRO); }*/ private void fisica(float dt) { velxtom = 0; velytom = 0; if (arriba) { velytom = -300; } if (abajo) { velytom = 300; } if (izquierda) { velxtom = -300; } if (derecha) { velxtom = 300; } tomx = clamp(tomx + velxtom*dt, 0, ANCHO-DIAMETRO); tomy = clamp(tomy + velytom*dt, 0, ALTO-DIAMETRO); } /*public void moverbala() { if (x2 > ANCHO-RADIO) { vx2 = -5; } if (x2 < RADIO) { vx2 = 5; } if (y2 > ALTO-RADIO) { vy2 = -5; } if (y2 < RADIO) { vy2 = 5; } x2 += vx2; y2 += vy2; }*/ public void moverdogs() { if (dog1x > ANCHO-RADIO) { velxdog1 = -5; } if (dog1x < RADIO) { velxdog1 = 5; } if (dog1y > ALTO-RADIO) { velydog1 = -5; } if (dog1y < RADIO) { velydog1 = 5; } if (dog2x > ANCHO-RADIO) { velxdog2 = -5; } if (dog2x < RADIO) { velxdog2 = 5; } if (dog2y > ALTO-RADIO) { velydog2 = -5; } if (dog2y < RADIO) { velydog2 = 5; } if (dog3x > ANCHO-RADIO) { velxdog3 = -5; } if (dog3x < RADIO) { velxdog3 = 5; } if (dog3y > ALTO-RADIO) { velydog3 = -5; } if (dog3y < RADIO) { velydog3 = 5; } dog1x += velxdog1; dog1y += velydog1; dog2x += velxdog2; dog2y += velydog2; dog3x += velxdog3; dog3y += velydog3; } public boolean comio() { catx = tomx-jerryx; caty = tomy-jerryy; distancia = Math.sqrt((catx*catx) + (caty*caty)); if (distancia < RADIO) { contjerrys++; return true; } else {return false;} } public void reacomodartomyjerry() { tomx = rnd.nextInt(ANCHO-RADIO); tomy = rnd.nextInt(ALTO-RADIO); jerryx = rnd.nextInt(ANCHO-RADIO); jerryy = rnd.nextInt(ALTO-RADIO); } public void obtenervalordelarchivo() { try { fin = new FileInputStream(THEPATH); myInput = new BufferedReader(new InputStreamReader(fin)); thisline = myInput.readLine(); //System.out.println(thisline); } catch (Exception ex) { ex.printStackTrace(); } } public void parsearvalorobtenido() { thedouble = thisline.substring(2,5); theexpo = thisline.substring(thisline.length()-3,thisline.length()); expo = Double.valueOf(theexpo).doubleValue(); mydouble = Double.valueOf(thedouble).doubleValue(); thefactor = Math.pow(10,expo); lastdouble = mydouble*thefactor; thevalue = (int)lastdouble; //System.out.println(thisline+"\n"+mydouble+"\n"+expo+"\n"+thevalue); } public void checkandsetbuttons() { /*obtenervalordelarchivo(); parsearvalorobtenido();*/ switch (CIRCUITVALUE) { case 143: arriba=true; break; case 151: derecha=true; break; case 167: abajo=true; break; case 199: izquierda=true; break; case 7: reacomodartomyjerry(); break; default: arriba=false; derecha=false; abajo=false; izquierda=false; break; } } public void run() { Thread.currentThread().setPriority(5); try { while (true) { Thread.sleep(10); this.CIRCUITVALUE = lpt.input((short)0x379); // checkandsetbuttons(); // System.out.println("CIRCUITVALUE = "+CIRCUITVALUE); } } catch (InterruptedException ex) { ex.printStackTrace(); } } public void paint(Graphics g) { if (activo && contjerrys