How Do I Code This? (bleu box)

edited September 2015 in How To...

I am currently working on a game. It requires the UP and RIGHT or LEFT buttons to be pressed at the same time in order to jump and move from side-to-side simultaneously. I do not know how to code this correctly. If you would like to see this glitch in detail, visit http://bleubox.altervista.org/productions.html and play the demo game (which is badly in need of fixing). Knowing how to do this would make the full version of my game much more dynamic and fun.

Tagged:

Answers

  • _vk_vk
    edited September 2015 Answer ✓

    I think this code is from Daniel Shiffman's nature of code

    http://natureofcode.com

    You might like it.

    Specially:

    http://natureofcode.com/book/chapter-1-vectors

    Mover m;
    
    void setup() {
      size(400, 400);
      m = new Mover(3, width/2, height - 3);
    }
    
    
    
    void draw() {
      background(255);
    
      PVector wind = new PVector(0.001, 0);
      // We could scale by mass to be more accurate.
      PVector gravity = new PVector(0, 0.1);
    
    
      float c = 0.01; 
      PVector friction = m.velocity.get(); 
      friction.mult(-1); 
      friction.normalize();
      friction.mult(c);
    
      // Apply the friction force vector to the object.
      m.applyForce(friction);
      m.applyForce(wind);
      m.applyForce(gravity);
    
      m.update();
      m.display();
      m.checkEdges();
    }
    
    void keyPressed() {
      if (key == CODED) {
        switch (keyCode) {
    
        case UP:
          PVector u = new PVector(0, -4);
          m.applyForce(u);
          break;
    
        case RIGHT:
          PVector r = new PVector(1, 0);
          m.applyForce(r);
          break;
    
        case LEFT:
          PVector l = new PVector(-1, 0);
          m.applyForce(l);
          break;
    
        case DOWN:
          PVector d = new PVector(0, 2);
          m.applyForce(d);
          break;
    
        }
      }
    
      if (key == ' '){
          PVector j = new PVector(0, -8);
          m.applyForce(j);
      }
    
    }
    
    
    
    
    
    
    class Mover {
    
      PVector location;
      PVector velocity;
      PVector acceleration;
      // The object now has mass!
      float mass;
    
      Mover() {
        // And for now, we’ll just set the mass equal to 1 for simplicity.
        mass = 1;
        location = new PVector(30, 30);
        velocity = new PVector(0, 0);
        acceleration = new PVector(0, 0);
      }
    
      Mover(float m, int x, int y) {
        // And for now, we’ll just set the mass equal to 1 for simplicity.
        mass = m;
        location = new PVector(x, y);
        velocity = new PVector(0, 0);
        acceleration = new PVector(0, 0);
      }
    
    
      // Newton’s second law.
      void applyForce(PVector force) {
        //[full] Receive a force, divide by mass, and add to acceleration.
        PVector f = PVector.div(force, mass);
        acceleration.add(f);
        //[end]
      }
    
      void update() {
        //[full] Motion 101 from Chapter 1
        velocity.add(acceleration);
        location.add(velocity);
        //[end]
        // Now add clearing the acceleration each time!
        acceleration.mult(0);
      }
    
      void display() {
        stroke(0);
        fill(175);
        //[offset-down] Scaling the size according to mass.
        ellipse(location.x, location.y, mass*16, mass*16);
      }
    
      // Somewhat arbitrarily, we are deciding that an object bounces when it hits the edges of a window.
      void checkEdges() {
        if (location.x > width) {
          location.x = width;
          velocity.x *= -1;
        } 
        else if (location.x < 0) {
          velocity.x *= -1;
          location.x = 0;
        }
    
        if (location.y > height - mass*8) {
          // Even though we said we shouldn't touch location and velocity directly, there are some exceptions.
          // Here we are doing so as a quick and easy way to reverse the direction of our object when it reaches the edge.
          velocity.y *= -1;
          location.y = height - mass*8;
        }
      }
    }
    
  • I cut off the parts of the code from http://studio.ProcessingTogether.com/sp/pad/export/ro.9bY07S95k2C2a and am working on fitting it in my game. Thanks!

Sign In or Register to comment.