How to control image with arrow keys?

Hello! I'm trying to make a piece where a small icon is moved by using the arrow keys. I've been trying for a while to find out how to do it, but I can't find help as to what code is needed and how to do it! I can make my images move but not with the arrow keys. I'd greatly appreciate any help!

This is what I'm working with so far:

import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
//17000 hz mosquito frq
AudioPlayer player;
Minim minim;
//Minim mysound;


float y;
float x;
PImage photo;
PImage photo1;
int xpos= 200;
int ypos= 200;
void setup() {
  size(768,567);
  y = height;
  photo = loadImage("ash.png");
  photo1 = loadImage("lavender_town_remake__night_time__by_creepypasta81691-d4wxz46.png");
 //minim = new Minim(this);
  //player = minim.loadFile("Awesome Video Game Music #12!- Pokemon- RedBlue - Lavender Town Theme");
 // player.play();
}
void draw() {
  background(0);
   image(photo1, 0, 0);
  image(photo, 0, y);
  image(photo,xpos,y);
  xpos=xpos+1;
//if (ypos>height+20)
//{
  //xpos=-20;

  if(y > 0) {
    y = y - 2;
  }
}

//void stop()
//{
//  player.close();
  //minim.stop();
  //super.stop();
//}
Tagged:

Answers

  • got it to work thank you!

  • edited December 2013

    Or something like this with the myCar function:

    http://www.openprocessing.org/sketch/103475

    Car myCar;
    Grape newGrape;
    
    void setup() {
      size(500, 500);
      myCar = new Car();
      newGrape = new Grape();
    }
    void draw() {
      background(255);
      //
      newGrape.display();
      myCar.display();
      myCar.move();
      //
      if (rectangle_collision (myCar.xpos, myCar.ypos, myCar.sizeW, myCar.sizeH,
      newGrape.xpos, newGrape.ypos, newGrape.sizeW, newGrape.sizeH)) { 
        // println ("hit");
        newGrape = new Grape();
      }
    }
    //
    boolean rectangle_collision(float carX, float carY, float carW, float carH,
    float grapeX, float grapeY, float grapeW, float grapeH)
    // from PhiLho
    {
      return (grapeX + grapeW/2 < carX + carW/2 &&
              grapeX - grapeW/2 > carX - carW/2 &&
              grapeY + grapeW/2 < carY + carW/2 &&
              grapeY - grapeW/2 > carY - carW/2);
    }
    // =========================================================
    void keyPressed() {
      int k = keyCode;
      if (k == ENTER | k == RETURN)
        myCar.speed = myCar.speed + 1;
      else if (k == ' ') 
        myCar.speed = myCar.speed - 1;
      else if (k == UP)     myCar.up    = true;
      else if (k == DOWN)   myCar.down  = true;
      else if (k == LEFT)   myCar.left  = true;
      else if (k == RIGHT)  myCar.right = true;
    }
    void keyReleased() {
      int k = keyCode;
      if      (k == UP)     myCar.up    = false;
      else if (k == DOWN)   myCar.down  = false;
      else if (k == LEFT)   myCar.left  = false;
      else if (k == RIGHT)  myCar.right = false;
    }
    class Car {
      color c;
      float xpos;
      float ypos;
      float speed=7;
      int sizeW = 50;
      int sizeH = 50;
      boolean up, down, left, right;
      float topBorderC;
      float bottomBorderC;
      float rightBorderC;
      float leftBorderC;
      Car() {
        c = color(175);
        xpos = width/2;
        ypos = width/2;
        setBordersC() ;
      }
      void setBordersC() {
        topBorderC = ypos - sizeW/2;
        bottomBorderC = ypos + sizeW/2;
        rightBorderC = xpos + sizeH/2;
        leftBorderC = xpos - sizeH/2;
      }
      void display() {
        rectMode(CENTER);
        stroke(0);
        fill(c);
        rect(xpos, ypos, sizeW, sizeH);
      }
      void move() {
        if (up)    ypos -= speed;
        if (down)  ypos += speed;
        if (left)  xpos -= speed;
        if (right) xpos += speed;
      }
    }
    class Grape {
      color c;
      float xpos;
      float ypos;
      int sizeW = 10;
      int sizeH = 10;
      float topBorderG;
      float bottomBorderG;
      float rightBorderG;
      float leftBorderG;
      //  float speed;
      Grape() {
        c = color(random(180, 255), 44, 208);
        xpos = int(random(width));
        ypos = int(random(height));
        setBordersG();
      }
      void setBordersG() {
        topBorderG = ypos - sizeW/2;
        bottomBorderG = ypos + sizeW/2;
        rightBorderG = xpos + sizeH/2;
        leftBorderG = xpos - sizeH/2;
      }
      void display() {
        rectMode(CENTER);
        stroke(0);
        fill(c);
        setBordersG();
        rect(xpos, ypos, sizeW, sizeH);
      }
      void tele() {
        xpos = int(random(width));
        ypos = int(random(height));
      }
    }
    
  • edited December 2013

    I remember that sketch from an old post: =:)
    http://forum.processing.org/one/topic/how-do-i-make-an-object-disappear-upon-collision

    And seems like I did my own tweaks for it as well. Check it out! :D


    "CarGame.pde":

    /**
     * Car Game {Alternative} (v1.0)
     * by Michael (mtwalworth)
     * modders Chrisir & GoToLoop
     *
     * forum.Processing.org/one/topic
     * /how-do-i-make-an-object-disappear-upon-collision
     * 
     * www.OpenProcessing.org/sketch/103128
     */
    
    final static int CAR_W = 70, CAR_H = 40, CAR_S = 10;
    final static int GRAPE_W = 10, GRAPE_H = 10, GRAPE_S = 0;
    final static int BANANA_W = 10, BANANA_H = 20, BANANA_S = 0;
    
    final static color CAR_C = #0000FF;
    final static color GRAPE_C = #F080F0, BANANA_C = #FFFF00;
    
    final static float FPS = 50, BOLD = 2;
    final static color BG = -1, FG = 0;
    
    Sprite car, grape, banana;
    
    void setup() {
      size(800, 600);
      frameRate(FPS);
      smooth();
    
      stroke(FG);
      strokeWeight(BOLD);
    
      rectMode(CENTER);
      ellipseMode(CENTER);
    
      car    = createCar();
      grape  = createGrape();
      banana = createBanana();
    }
    
    void draw() {
      background(BG);
    
      car.script();
      grape.script();
      banana.script();
    
      if (car.checkImmersion(grape))
        grape.resetSprite((int) random(GRAPE_W, width-GRAPE_W), 
        (int) random(GRAPE_H, height-GRAPE_H));
    
      if (car.checkImmersion(banana))
        banana.resetSprite((int) random(BANANA_W, width-BANANA_W), 
        (int) random(BANANA_H, height-BANANA_H));
    }
    
    void keyPressed() {
      final int k = keyCode;
    
      if (k == ' ' | k == ENTER)            car.setAcceleration( 1);
      else if (k == SHIFT | k == CONTROL)   car.setAcceleration(-1);
    
      else if (k == 'W' | k == UP)          car.setVertical(-1);
      else if (k == 'S' | k == DOWN)        car.setVertical( 1);
    
      else if (k == 'A' | k == LEFT)        car.setHorizontal(-1);
      else if (k == 'D' | k == RIGHT)       car.setHorizontal( 1);
    }
    
    void keyReleased() {
      final int k = keyCode;
    
      if (k == ' ' | k == ENTER   && car.a > 0 || 
        k == SHIFT | k == CONTROL && car.a < 0)
        car.setAcceleration(0);
    
      else if (k == 'W' | k == UP)     car.north = false;
      else if (k == 'S' | k == DOWN)   car.south = false;
    
      else if (k == 'A' | k == LEFT)   car.west  = false;
      else if (k == 'D' | k == RIGHT)  car.east  = false;
    }
    
    Sprite createCar() {
      return new Sprite(width >> 1, height >> 1, 
      CAR_W, CAR_H, CAR_S, CAR_C, false);
    }
    
    Sprite createGrape() {
      return new Sprite((int) random(GRAPE_W, width - GRAPE_W), 
      (int) random(GRAPE_H, height - GRAPE_H), 
      GRAPE_W, GRAPE_H, GRAPE_S, GRAPE_C, true);
    }
    
    Sprite createBanana() {
      return new Sprite((int) random(BANANA_W, width - BANANA_W), 
      (int) random(BANANA_H, height - BANANA_H), 
      BANANA_W, BANANA_H, BANANA_S, BANANA_C, false);
    }
    


    "Sprite.pde":

    class Sprite {               // requires rectMode(CENTER); \\
      final static byte ACCEL_FREQ = 20;
      final static byte SPD_MIN = 0, SPD_MAX = 50;
    
      int x, y;
      byte  w, h, rx, ry, s, a;
      color c;
      boolean isRounded;
    
      int top, bottom, left, right;
      boolean north, south, west, east;
    
      Sprite(int xx, int yy, int ww, int hh, 
      int ss, color cc, boolean r) {
        // invokes initialization method:
        setSprite(xx, yy, ww, hh, ss, cc, r);
      }
    
      void setSprite(int xx, int yy, int ww, int hh, 
      int ss, color cc, boolean r) {
        w = (byte) ww;          // diameter x \\
        h = (byte) hh;          // diameter y \\
    
        rx = (byte) (ww >> 1);  // radius x \\
        ry = (byte) (hh >> 1);  // radius y \\
    
        s = (byte) ss;          // speed \\
        c = cc;                 // color \\
    
        isRounded = r;          // ellipse(true) or rect(false) \\
    
        resetSprite(xx, yy);    // completes initialization \\
      }
    
      void resetSprite(int xx, int yy) {
        x = xx;                 // location x \\
        y = yy;                 // location y \\
    
        setHorizontal(0);
        setVertical(0);
      }
    
      void setHorizontal(int horizontal) {
        if (horizontal < 0)        west = true;
        else if (horizontal > 0)   east = true;
        else                       west = east = false;
      }
    
      void setVertical(int vertical) {
        if (vertical < 0)          north = true;
        else if (vertical > 0)     south = true;
        else                       north = south = false;
      }
    
      void setAcceleration(int accel) {
        a = (byte) (accel == 0? 0 : accel >> 31 | 1);
      }
    
      void updateSpeed() {
        if (frameCount % ACCEL_FREQ != 0)  return;
    
        if ((s += a) == SPD_MIN)   s = SPD_MIN + 1;
        else if (s == SPD_MAX)     s = SPD_MAX - 1;
      }
    
      void moveSprite() {
        if (west)    x -= s;
        if (east)    x += s;
        if (north)   y -= s;
        if (south)   y += s;
      }
    
      void updateBorders() {
        left   = x - rx;
        right  = x + rx;
    
        top    = y - ry;
        bottom = y + ry;
      }
    
      void confineToScreen() {   // invoke updateBorders() 1st! \\
        if (left < 0)               x = rx;
        else if (right >= width)    x = width  - rx - 1;
    
        if (top < 0)                y = ry;
        else if (bottom >= height)  y = height - ry - 1;
      }
    
      void displaySprite() {
        fill(c);
    
        if (isRounded)  ellipse(x, y, w, h);
        else            rect(x, y, w, h);
      }
    
      boolean checkImmersion(Sprite other) {
        return abs(x - other.x) < abs(rx - other.rx) 
          & abs(y - other.y) < abs(ry - other.ry);
      }
    
      void script() {
        if (s != 0) {
          updateSpeed();
          moveSprite();
          updateBorders();
          confineToScreen();
        }
    
        displaySprite();
      }
    }
    

Sign In or Register to comment.