Java - Necesito un poco de ayuda

 
Vista:

Necesito un poco de ayuda

Publicado por Adrian Sa (2 intervenciones) el 19/04/2009 12:36:01
Hola estoy comenzando en esto de java y he ralizado ya algunos programas de gestión;
pero cuendo empiezo a hacer un juego se me presenta el problema de que no se como hacer para que el malo se mueva a su aire; ya que lo máximo k konsigo eske kuando yo pulse una tecla el malo se mueva :S:S ...

Me gustaría que alguién me ayudara . Gracias.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Necesito un poco de ayuda

Publicado por wSilverWolfw (15 intervenciones) el 19/04/2009 22:00:43
No seas vago.
jejeje esta bien todos pasamos por eso, siempre nesecitamos un poco de ayuda te mando el siguiente codigo (que espero te sirva de algo) con la condicion de que repases Hilos o Thread y multi Thread que luego te ayudaran mucho.

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package elmalo;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.*;
/**
*
* @author wilder
*/
public class Main extends JFrame implements Runnable,KeyListener{

private Thread hilo;
private JLabel imagenL;
private JLabel jugador;
private ImageIcon imagenI;
public Main(){
super("MOVIMIENTO DEL MALO");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
imagenI=new ImageIcon("imagen.png");
imagenL=new JLabel(imagenI);
imagenL.setBounds(350,180,48,48);
jugador=new JLabel(imagenI);
jugador.setBounds(50,180,48,48);
add(imagenL);
add(jugador);
addKeyListener(this);
setBounds(200,50,800,450);
setVisible(true);
start();
}
public void start(){
if(hilo==null){
hilo=new Thread(this);
hilo.start();
}
}
public void run(){
int ram;
try{
while(true){
ram=(int)(Math.random()*4)+1;
if(ram==1)
imagenL.setLocation(imagenL.getX()-20,imagenL.getY());
if(ram==2)
imagenL.setLocation(imagenL.getX()+20,imagenL.getY());
if(ram==3)
imagenL.setLocation(imagenL.getX(),imagenL.getY()-20);
if(ram==4)
imagenL.setLocation(imagenL.getX(),imagenL.getY()+20);
Thread.sleep(100);
}
}
catch(Exception e){
System.out.println(e.toString());
}
}
public void keyPressed(KeyEvent e){
try{
if(e.getKeyCode()==38)
jugador.setLocation(jugador.getX(),jugador.getY()-20);
if(e.getKeyCode()==39)
jugador.setLocation(jugador.getX()+20,jugador.getY());
if(e.getKeyCode()==40)
jugador.setLocation(jugador.getX(),jugador.getY()+20);
if(e.getKeyCode()==37)
jugador.setLocation(jugador.getX()-20,jugador.getY());
}
catch(Exception er){
System.out.println(er.toString());
}
}
public void keyTyped(KeyEvent e){

}
public void keyReleased(KeyEvent e){

}
public static void main(String[] args) {
new Main();
}

}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar