PruebaMotorJuegos1.java

   1 /**
   2  * PruebaMotorDeJuegos1.java
   3  * 
   4  * @author Luis Alejandro Bernal Romero
   5  * 
   6  * Utilizando el motor de juegos dibuja un circulo rojo que se puede mover con el teclado y un cuadrado verde que no hace nada. Este código debe estar en su directorio de trabajo.
   7  */
   8 
   9 import motorJuegos1.*;
  10 import java.awt.*;
  11 import java.awt.event.*;
  12 
  13 class CirculoRojo extends ObjetoMovil{
  14 
  15         public CirculoRojo(int x, int y) {
  16                 super(x, y);
  17         }
  18         
  19         public void paint(Graphics g){
  20                 g.setColor(Color.red);
  21                 g.fillOval(obtX(), obtY(), 50, 50);
  22         }
  23 }
  24 
  25 class CuadradoVerde extends ObjetoMovil{
  26 
  27         public CuadradoVerde(int x, int y) {
  28                 super(x, y);
  29         }
  30 
  31         public void paint(Graphics g) {
  32                 g.setColor(Color.green);
  33                 g.fillRect(obtX(), obtY(), 60, 60);
  34         }
  35 }
  36 
  37 class EscuchaTeclas implements KeyListener{
  38         ObjetoMovil circuloRojo;
  39         private Frame marco;
  40         
  41         public EscuchaTeclas(Frame l, ObjetoMovil c) {
  42                 marco = l;
  43                 circuloRojo = c;
  44         }
  45         
  46         public void keyPressed(KeyEvent e) {
  47                 int tecla = e.getKeyCode();
  48                 switch (tecla) {
  49                 case KeyEvent.VK_UP:{
  50                                 int y = circuloRojo.obtY();
  51                                 y -= 10;
  52                                 if(y < 0){
  53                                         y = marco.getHeight();
  54                                 }
  55                                 circuloRojo.modY(y);
  56                                 marco.repaint();
  57                 }
  58                 break;
  59                 case KeyEvent.VK_DOWN:{
  60                         int y = circuloRojo.obtY();
  61                         y += 10;
  62                         if(y > marco.getHeight()){
  63                                 y = 0;
  64                         }
  65                         circuloRojo.modY(y);
  66                         marco.repaint();
  67                 }
  68                 break;
  69                 case KeyEvent.VK_LEFT:{
  70                         int x = circuloRojo.obtX();
  71                         x -=10;
  72                         if(x < 0){
  73                                 x = marco.getWidth();
  74                         }
  75                         circuloRojo.modX(x);
  76                         marco.repaint();
  77                 }
  78                 break;
  79                 case KeyEvent.VK_RIGHT:{
  80                         int x = circuloRojo.obtX();
  81                         x += 10;
  82                         if(x > marco.getWidth()){
  83                                 x = 0;
  84                         }
  85                         circuloRojo.modX(x);
  86                         marco.repaint();
  87                 }
  88                 break;
  89                 }
  90         }
  91 
  92         public void keyReleased(KeyEvent e) {}
  93 
  94         public void keyTyped(KeyEvent e) {}
  95         
  96 }
  97 
  98 public class PruebaMotorJuegos1 {
  99 
 100         public static void main(String[] args) {
 101                 MotorJuegos motor = new MotorJuegos("Prueba motor grafico");
 102                 CirculoRojo cr = new CirculoRojo(10, 50);
 103                 motor.add(cr);
 104                 CuadradoVerde cv = new CuadradoVerde(100,200);
 105                 motor.add(cv);
 106                 EscuchaTeclas escucha = new EscuchaTeclas(motor, cr);
 107                 motor.addKeyListener(escucha);
 108                 motor.setSize(480, 640);
 109                 motor.setVisible(true);
 110         }
 111         
 112 }

CategoryJava | CategoryProgramacion

Java/Programas/MotorJuegos/PruebaMotorJuegos1.java (last edited 2008-04-20 14:38:16 by localhost)