[GAME] Block movement with keyPressed NEED HELP !!

edited December 2013 in Questions about Code

Hello everyone,

me and my group are working on a game for first year Game Development. We have a game where you are in a maze and you have to push blocks to get out of it. Every block has an arrow which points to the way it moves.

Now we are having trouble with the movement of the blocks. When we press the key that needs to be pressed it stays in place. Please help us!

CODE:

class Block {
  float x, y;
  float vy;
  float bWidth, bHeight;
  color fillColor;
  PImage blockImage;

  Block(float newX, float newY, float tW, float tH) {
    init();

    bWidth = tW;
    bHeight = tH;
    x = newX;
    y = newY;
  }

  void init() {
    fillColor = color(255, 140, 0);
    blockImage = loadImage("blockup.png");
  }

  void update() {
    playerCollisionResponse();
  }

  void draw() {
    fill(0);
    image(blockImage, x, y, bWidth, bHeight);
  }

  void blockMovement() {
    for (int iBlock=0; iBlock<blockups.size(); iBlock++) {
      if (keysPressed[UP] && keysPressed[' '] && thePlayer.x == x && thePlayer.y-y == 64) {
        y = y - 64;
      }
      else {
        y = y;
      }
    }
  }

  void playerCollisionResponse() {
    Player otherObject = thePlayer;

    float xOverlap = calculate1DOverlap(otherObject.x - otherObject.diameter/2, x, otherObject.diameter, bWidth), 
    yOverlap = calculate1DOverlap(otherObject.y - otherObject.diameter/2, y, otherObject.diameter, bHeight);

    if (abs(xOverlap)>0 && (abs(yOverlap)>0)) 
      if (abs(xOverlap) < abs(yOverlap)) {
        otherObject.x += xOverlap;
      }
      else {
        otherObject.y += yOverlap;
      }
  }
}
Tagged:

Answers

  • When posting code, plz highlight it and hit CTRL+K to format it! :-S

  • "When we press the key that needs to be pressed it stays in place."
    Could be useful to see the code managing these keys...

Sign In or Register to comment.