Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import controlP5.*;
- import processing.serial.*;
- PImage img;
- int iW2, iH2;
- int posX, posY;
- int velocity, angle;
- Serial serial;
- String jostick;
- void setup() {
- serial = new Serial (this, serial.list ()[0], 9600);
- // tamaño de la ventana
- size(800, 600);
- // carga la imagen en la variable
- img = loadImage("spaceship02.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();
- CONTROL();
- }
- // teclado
- 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 CONTROL(){
- if ( serial.available() > 0)
- {
- // If data is available,
- jostick = serial.readString()+"";
- println(jostick);
- }
- if(jostick==null){
- jostick="0";
- }
- if(jostick.indexOf("a")>=0){//Arriba
- velocity -= 5;
- jostick ="";
- }
- if(jostick.indexOf("b")>=0){//Abajo
- velocity += 5;
- jostick="";
- }
- if(jostick.indexOf("c")>=0){//izquierda
- angle += 5;
- jostick="";
- }
- if(jostick.indexOf("d")>=0){//Derecha
- angle -= 5;
- jostick="";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement