Advertisement
Guest User

Untitled

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