Advertisement
Josueco

lab8_pro

May 28th, 2015
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1. import controlP5.*;
  2. import processing.serial.*;
  3. //definicion de variables
  4. PImage img;
  5. int iW2, iH2;
  6. int posX, posY;
  7. int velocity, angle;
  8. Serial serial;
  9. String direccion;
  10.  
  11. void setup() {
  12.  
  13.   serial = new Serial(this, Serial.list()[1], 9600);
  14.   // tamaño de la ventana
  15.   size(800, 600);
  16.   // carga la imagen en la variable
  17.   img = loadImage("spaceship03.gif");
  18.   iW2 = img.width/2;
  19.   iH2 = img.height/2;
  20.   posX = (width/2)-iW2;
  21.   posY = (height/2)-iH2;
  22.   velocity = -5;
  23.   angle = 0;
  24.  
  25. }
  26.  
  27. void draw() {
  28.   // limpia la ventana
  29.   background(0);
  30.   posX += velocity * sin(radians(angle));
  31.   if (posX-iW2 < 0) posX = width;
  32.   if (posX-iW2 > width) posX = iW2;
  33.   posY += velocity * cos(radians(angle));
  34.   if (posY-iH2 < 0) posY = height;
  35.   if (posY-iH2 > height) posY = iH2;
  36.   // dibuja la imagen
  37.   pushMatrix();
  38.   translate(posX-iW2, posY-iH2);
  39.   rotate(radians(-angle));
  40.   image(img, -iW2, -iH2);
  41.   popMatrix();
  42.   mover();
  43. }
  44.  
  45. /*void keyPressed() {
  46.   if (key == CODED) {
  47.     if (keyCode == UP)
  48.       velocity -= 5;    
  49.     if (keyCode == DOWN)
  50.       velocity += 5;
  51.     if (keyCode == LEFT)
  52.       angle += 5;    
  53.     if (keyCode == RIGHT)
  54.       angle -= 5;
  55.   }
  56. }*/
  57.  
  58. void mover(){
  59.  if ( serial.available() > 0){  // If data is available,
  60.     direccion = serial.readString()+"";  //lee lo del puerto serial y se lo asigna a la variable direccion
  61.   }
  62.  
  63.  println(direccion); //imprime en consola la varible val
  64.  if(direccion == null){
  65.    direccion ="0";
  66.    }
  67.  
  68.  if(direccion.indexOf("up")>=0){
  69.         velocity -= 5;    
  70.         direccion="";
  71.      }
  72.      
  73.  if(direccion.indexOf("down")>=0){
  74.       velocity += 5;
  75.       direccion="";
  76.      }
  77.      
  78.  if(direccion.indexOf("right")>=0){
  79.       angle += 5;    
  80.       direccion="";
  81.  }
  82.  
  83.  if(direccion.indexOf("left")>=0){
  84.       angle -= 5;
  85.      direccion="";
  86.     }
  87.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement