Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import controlP5.*;
- import processing.serial.*;
- //definicion de variables
- PImage img;
- int iW2, iH2;
- int posX, posY;
- int velocity, angle;
- Serial serial;
- String direccion;
- void setup() {
- serial = new Serial(this, Serial.list()[1], 9600);
- // tamaño de la ventana
- size(800, 600);
- // carga la imagen en la variable
- img = loadImage("spaceship03.gif");
- iW2 = img.width/2;
- iH2 = img.height/2;
- posX = (width/2)-iW2;
- posY = (height/2)-iH2;
- velocity = -5;
- angle = 0;
- }
- void draw() {
- // limpia la ventana
- background(0);
- posX += velocity * sin(radians(angle));
- if (posX-iW2 < 0) posX = width;
- if (posX-iW2 > width) posX = iW2;
- posY += velocity * cos(radians(angle));
- if (posY-iH2 < 0) posY = height;
- if (posY-iH2 > height) posY = iH2;
- // dibuja la imagen
- pushMatrix();
- translate(posX-iW2, posY-iH2);
- rotate(radians(-angle));
- image(img, -iW2, -iH2);
- popMatrix();
- mover();
- }
- /*void keyPressed() {
- if (key == CODED) {
- if (keyCode == UP)
- velocity -= 5;
- if (keyCode == DOWN)
- velocity += 5;
- if (keyCode == LEFT)
- angle += 5;
- if (keyCode == RIGHT)
- angle -= 5;
- }
- }*/
- void mover(){
- if ( serial.available() > 0){ // If data is available,
- direccion = serial.readString()+""; //lee lo del puerto serial y se lo asigna a la variable direccion
- }
- println(direccion); //imprime en consola la varible val
- if(direccion == null){
- direccion ="0";
- }
- if(direccion.indexOf("up")>=0){
- velocity -= 5;
- direccion="";
- }
- if(direccion.indexOf("down")>=0){
- velocity += 5;
- direccion="";
- }
- if(direccion.indexOf("right")>=0){
- angle += 5;
- direccion="";
- }
- if(direccion.indexOf("left")>=0){
- angle -= 5;
- direccion="";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement