How can I synchronise void update with void keypress/keyrelease

here are the variables in the main tab

public float bellyXloc, bellyYloc;
public float bellyWidth, bellyHeight;
public float accelerationX,accelerationY;
public float decelerationX, decelerationY;
public PVector location, acceleration, deceleration;
public float max_speedx,max_speedy ;
public float gravity;
public color characterColor;
public Boolean   isLeft=false ,  isRight=false , isUp= false ;

constructor

 bellyXloc=width/2;
  bellyYloc=height/2; //-(1.6*bellyHeight);
  bellyWidth=width/20;
  bellyHeight= height*0.1;
  characterColor = color(152,223,234);
  location = new PVector(bellyXloc,bellyYloc);
  accelerationX=1.3;
  accelerationY=0;
  max_speedx = 5;
  max_speedy = 3;
  acceleration = new PVector(accelerationX,accelerationY);
  decerelationX = 0.75;
  decerelationY = 0.75;
  deceleration = new PVector(decelerationX, decelerationY);
  gravity = .3;


  a = new AlienCharacter(location,bellyWidth,bellyHeight, color(152,223,234),
                        acceleration, max_speedx, max_speedy, decerelation ,gravity);

in the class void update

void update() {
if ( isRight ) {
  location.add(acceleration);
  if ( acceleration.x > max_speedx ) {
    max_speedx = acceleration.x;
  }
}
else if ( isLeft ) {
  location.sub(acceleration);
  if ( acceleration.x < -max_speedx ) {
    max_speedx = acceleration.x;
  }
}
else { //neither right or left pressed, decelerate
  if ( acceleration.x > 0 ) {
    acceleration.x -= deceleration.x;
    if ( acceleration.x < 0 ) {
      acceleration.x = 0;
    }
  }
  else if ( acceleration.x < 0 ) {
    acceleration.x += deceleration.x;
    if ( acceleration.x > 0 ) {
      acceleration.x = 0;
    }
  }
  }

  if ( isUp ) {
  location.sub(acceleration);
    if ( acceleration.y-0.5 < -max_speedy ) {
    max_speedy = acceleration.y*deceleration.y;
  }
  }

and in the main tab

  void keyPressed() {
  switch(keyCode) {
  case RIGHT: isRight  = true; break;
  case LEFT: isLeft = true; break;
  case UP: isUp = true; break;
  }
  }
  void keyReleased() {
  switch(keyCode) {
  case RIGHT: isRight = false; break;
  case LEFT: isLeft = false; break;
  case UP: isUp = false; break;

  }
  }

The problem that character doesn't react to keyboard I don't where might be the problem

Tagged:

Answers

  • edited March 2017

    Here's some basic movement.

    boolean isRight, isLeft, isUp;
    
    float x, y;
    
    void setup(){
      size(400,400);
      x = 200;
      y = 200;
    }
    
    void draw(){
      if( isRight ){
        x++;
      }
     if( isLeft ){
       x--;
     }
     if( isUp ){
       y--;
     } else if (y<200){
       y++;
     }
     background(0);
     fill(255,0,0);
     rect(x-5,y-5,10,10);
    }
    
    void keyPressed() {
      switch(keyCode) {
      case RIGHT: 
        isRight  = true; 
        break;
      case LEFT: 
        isLeft = true; 
        break;
      case UP: 
        isUp = true; 
        break;
      }
    }
    void keyReleased() {
      switch(keyCode) {
      case RIGHT: 
        isRight = false; 
        break;
      case LEFT: 
        isLeft = false; 
        break;
      case UP: 
        isUp = false; 
        break;
      }
    }
    

    It is hard to say what's wrong with your object not moving unless you post a ready-to-go snippet of code that we can copy, paste, and run to see you issue.

  • edited March 2017

    Here is the full one

    int gameScreen = 0;
    final int displayWidth = 800;
    final int displayHeight = 600;
    
    Alien a;
    
    int FPS = 30;
    
    //Player
    public float bellyXloc, bellyYloc;
    public float bellyWidth, bellyHeight;
    public float accelerationX,accelerationY;
    public float decelerationX, decelerationY;  
    public PVector location, acceleration, deceleration;
    public float max_speedx,max_speedy ;
    public float gravity;
    public color characterColor;
    public Boolean   isLeft=false ,  isRight=false , isUp= false ;
    
    void setup() {
    
    size(800, 600, JAVA2D);
    smooth(4);
    frameRate(FPS); 
    bellyXloc=width/2;
    bellyYloc=height/2; //-(1.6*bellyHeight)
    bellyWidth=width/20;
    bellyHeight= height*0.1;
    characterColor = color(152,223,234);
    location = new PVector(bellyXloc,bellyYloc);
    accelerationX=1.3;
    accelerationY=0;
    max_speedx = 5;
    max_speedy = 3;
    acceleration = new PVector(accelerationX,accelerationY);
    decelerationX = 0.75;
    decelerationY = 0.75;
    deceleration = new PVector(decelerationX, decelerationY);
    gravity = .3;
    
    //Constructor
    a = new Alien(location,bellyWidth,bellyHeight, color(152,223,234),
    acceleration, max_speedx, max_speedy, deceleration ,gravity);
    
    }
    
    /********* SCREEN CONTENTS *********/
    void initScreen() {
    background(0);
    textAlign(CENTER);
    text("Click to start", width/2, height/2);
    }
    
    //initialize game screen
    void gameScreen() {
    background(255);
    }
    
    void gameOverScreen() {
    background(0);
    textAlign(CENTER);
    fill(255);
    textSize(30);
    text("Game Over", height/2, width/2 - 20);
    textSize(15);
    text("Click to Restart", height/2, width/2 + 10); 
    }
    
    void draw() {
    // Display the contents of the current screen
    if (gameScreen == 0) {
    initScreen();
    } else if (gameScreen == 1) {
    gameScreen();
    a.update();
    background(0);
    fill(characterColor);
    a.displayAlien();
    
    
    } else if (gameScreen == 2) {
    gameOverScreen();
    }
    }
    
    
    /********* INPUTS *********/
    
    public void mousePressed() {
    // PUBLIC provide other classes access the fields and methods within a class. 
    // The public keyword is used before a field or method that you want to make        
    // if we are on the initial screen when clicked, start the game
    if (gameScreen==0) {
    startGame();
    }
    }
    
    void keyPressed() {
    switch(keyCode) {
    case RIGHT: isRight  = true; break;
    case LEFT: isLeft = true; break;
    case UP: isUp = true; break; 
    }
    }
    
    void keyReleased() {
    switch(keyCode) {
    case RIGHT: isRight = false; break;
    case LEFT: isLeft = false; break;
    case UP: isUp = false; break;
    
    }
    }
    
    
    /********* OTHER FUNCTIONS *********/
    
    // This method sets the necessery variables to start the game  
    void startGame() {
    gameScreen=1;
    }
    
    /*********CLASS TAB****************/
    class Alien{
    
    //chest
    float lowerchestXloc;
    float lowerchestYloc;
    float lowerchestWidth;
    float lowerchestHeight;
    float upperchestXloc;
    float upperchestYloc;
    float upperchestWidth;
    float upperchestHeight;
    
    //hand 
    float handfrontXloc;
    float handfrontYloc;
    float handfrontWidth;
    float handfrontHeight;
    
    //head
    float headXloc;
    float headYloc;
    float headWidth;
    float headHeight;
    
    //feets
    //front
    float tightfrontXloc;
    float tightfrontYloc;
    float tightfrontWidth;
    float thightfrontHeight;
    float legfrontXloc;
    float legfrontYloc;
    float legfrontWidth;
    float legfrontHeight;
    float toefrontX1;
    float toefrontY1;
    float toefrontX2;
    float toefrontY2;
    float toefrontX3;
    float toefrontY3;
    
    //back feet
    float tightbackXloc;
    float tightbackYloc;
    float tightbackWidth;
    float tightbackHeight;
    float legbackXloc;
    float legbackYloc;
    float legbackWidth;
    float legbackHeight;
    float toebackX1;
    float toebackY1;
    float toebackX2;
    float toebackY2;
    float toebackX3;
    float toebackY3;
    
    Alien (PVector L, float bWidth, float bHeight, color cColor, PVector A, float msx,     
    float  msy, PVector D, float G ) { 
    
    location = new PVector(bellyXloc, bellyYloc);
    location = L;
    bellyWidth = bWidth;
    bellyHeight = bHeight;
    characterColor = cColor;
    acceleration = A;
    max_speedx = msx;
    max_speedy = msy;
    deceleration = D;
    gravity = G;
    }
    
    void displayAlien() {
    
    bellyXloc=width/2;
    bellyYloc=height-(1.6*bellyHeight);
    bellyWidth=width/20;
    bellyHeight= height*0.1;
    
    background(255);
    noStroke();
    fill(152, 223, 234);
    //BODY
    //belly
    ellipse(location.x, location.y, bellyWidth, bellyHeight);
    
    //lowerchest
    lowerchestXloc = location.x;
    lowerchestYloc = location.y - (bellyHeight*0.2);
    lowerchestWidth = bellyWidth*0.95;
    lowerchestHeight = bellyHeight*0.85;
    upperchestXloc = location.x;
    upperchestYloc = lowerchestYloc - (lowerchestHeight*0.2);
    upperchestWidth = lowerchestWidth * 0.98;
    upperchestHeight = lowerchestHeight * 0.9;
    
    ellipse(lowerchestXloc, lowerchestYloc, lowerchestWidth, lowerchestHeight);
    ellipse(upperchestXloc, upperchestYloc, upperchestWidth, upperchestHeight);
    
    //HEAD
    
    headXloc = location.x;
    headYloc = upperchestYloc - (upperchestHeight * 0.79);
    headWidth = bellyWidth*1.1;
    headHeight = headWidth;
    
    ellipse(headXloc, headYloc, headWidth, headHeight);
    
    //face
    
    
    //feets
    //front feet
    tightfrontXloc = location.x - 7;
    tightfrontYloc = location.y + ((bellyHeight/2)-5);
    tightfrontWidth = 0.2*bellyWidth;
    thightfrontHeight = 0.65*bellyHeight;
    legfrontXloc = tightfrontXloc;
    legfrontYloc = tightfrontYloc+(thightfrontHeight-3);
    legfrontWidth = tightfrontWidth;
    legfrontHeight = 0.45*bellyHeight;
    toefrontX1 = legfrontXloc+legfrontWidth/2;
    toefrontY1 = legfrontYloc+(legfrontHeight-5);
    toefrontX2 = toefrontX1-8;
    toefrontY2 = toefrontY1+14;
    toefrontX3 = toefrontX1+8;
    toefrontY3 = toefrontY2;
    
    if(toefrontY2 > height){
         toefrontY2 = height;
    }
    //left feet
    rect(tightfrontXloc, tightfrontYloc, tightfrontWidth, thightfrontHeight);
    rect(legfrontXloc, legfrontYloc, legfrontWidth, legfrontHeight);
    triangle(toefrontX1, toefrontY1, toefrontX2, toefrontY2, toefrontX3,     
    toefrontY3);
    //back feet
    
    tightbackXloc = location.x - 4;
    tightbackYloc = tightfrontYloc;
    tightbackWidth = tightfrontWidth;
    tightbackHeight = thightfrontHeight;
    legbackXloc = tightbackXloc;
    legbackYloc = legfrontYloc;
    legbackWidth = legfrontWidth;
    legbackHeight = legfrontHeight;
    toebackX1 = legbackXloc+legbackWidth/2;
    toebackY1 = toefrontY1;
    toebackX2 = toebackX1-8;
    toebackY2 = toefrontY2;
    toebackX3 = toebackX1+8;
    toebackY3 = toefrontY3;
    
    rect(tightbackXloc, tightbackYloc, tightbackWidth, tightbackHeight);
    rect(legbackXloc, legbackYloc, legbackWidth, legbackHeight);
    triangle(toebackX1, toebackY1, toebackX2, toebackY2, toebackX3, toebackY3);
    
    //hands
    
    handfrontXloc = tightbackXloc;
    handfrontYloc = upperchestYloc-(upperchestHeight*0.25);
    handfrontWidth = tightfrontWidth;
    handfrontHeight = bellyHeight/2+lowerchestHeight/2;
    stroke(255);
    rect(handfrontXloc, handfrontYloc, handfrontWidth, handfrontHeight);
    
    }//end display
    
    
    void update() {
    if ( isRight ) {
    location.add(acceleration);
    if ( acceleration.x > max_speedx ) {
    max_speedx = acceleration.x;
    }
    }
    else if ( isLeft ) {
    location.sub(acceleration);
    if ( acceleration.x < -max_speedx ) {
    max_speedx = acceleration.x;
    }
    }
    else { //neither right or left pressed, decelerate
    if ( acceleration.x > 0 ) {
    acceleration.x -= deceleration.x;
    if ( acceleration.x < 0 ) {
    acceleration.x = 0;
    }
    }
    else if ( acceleration.x < 0 ) {
    acceleration.x += deceleration.x;
    if ( acceleration.x > 0 ) {
    acceleration.x = 0;
    }
    }
    }
    
    if ( isUp ) {
    location.sub(acceleration);
    if ( acceleration.y-0.5 < -max_speedy ) {
    max_speedy = acceleration.y*deceleration.y;
    }
    }
    
    }// end update
    
    
     }//end class
    
  • ctrl-t in the pde editor will indent the code for you, which aids readability.

  • Lines 307-318 are the problem. If neither key is pressed, acceleration.x rapidly becomes 0 because of the logic there. And then what value added/subtracted from the location? Yep, acceleration!

    Removing the offending lines causes your figure to move.

  • thanks I got it, it solved now, i add two new global variable speedX=0 speedY=0 PVector speed(speedX,speedY) however than again my question would if I jump and due to the gravity I fall back how can I stop my character to stay on the ground?

    int gameScreen = 0;
    final int displayWidth = 800;
    final int displayHeight = 600;
    
    Alien a;
    
    int FPS = 30;
    
    //Player
    public float bellyXloc, bellyYloc;
    public float bellyWidth, bellyHeight;
    public float speedX, speedY;
    public float accelerationX,accelerationY;
    public float decelerationX, decelerationY;  
    public PVector location, speed, acceleration, deceleration;
    public float max_speedx,max_speedy ;
    public float gravity;
    public color characterColor;
    public Boolean   isLeft=false ,  isRight=false , isUp= false ;
    
    void setup() {
    
    size(800, 600, JAVA2D);
    smooth(4);
    frameRate(FPS); 
    bellyXloc=width/2;
    bellyYloc=height/2; //-(1.6*bellyHeight)
    bellyWidth=width/20;
    bellyHeight= height*0.1;
    characterColor = color(152,223,234);
    location = new PVector(bellyXloc,bellyYloc);
    speedX = 0;
    speedY = 0;
    speed = new PVector(speedX,speedY);
    accelerationX=1.3;
    accelerationY=0;
    max_speedx = 5;
    max_speedy = 3;
    acceleration = new PVector(accelerationX,accelerationY);
    decelerationX = 0.75;
    decelerationY = 0.75;
    deceleration = new PVector(decelerationX, decelerationY);
    gravity = 1.1;
    
    //Constructor
    a = new Alien(location,bellyWidth,bellyHeight, color(152,223,234),speed,
    acceleration, max_speedx, max_speedy, deceleration ,gravity);
    
    }
    
    /********* SCREEN CONTENTS *********/
    void initScreen() {
    background(0);
    textAlign(CENTER);
    text("Click to start", width/2, height/2);
    }
    
    //initialize game screen
    void gameScreen() {
    background(255);
    }
    
    void gameOverScreen() {
    background(0);
    textAlign(CENTER);
    fill(255);
    textSize(30);
    text("Game Over", height/2, width/2 - 20);
    textSize(15);
    text("Click to Restart", height/2, width/2 + 10); 
    }
    
    void draw() {
    // Display the contents of the current screen
    if (gameScreen == 0) {
    initScreen();
    } else if (gameScreen == 1) {
    gameScreen();
    a.update();
    background(0);
    fill(characterColor);
    a.displayAlien();
    
    
    } else if (gameScreen == 2) {
    gameOverScreen();
    }
    }
    
    
    /********* INPUTS *********/
    
    public void mousePressed() {
    // PUBLIC provide other classes access the fields and methods within a class. 
    // The public keyword is used before a field or method that you want to make        
    // if we are on the initial screen when clicked, start the game
    if (gameScreen==0) {
    startGame();
    }
    }
    
    void keyPressed() {
    switch(keyCode) {
    case RIGHT: isRight  = true; break;
    case LEFT: isLeft = true; break;
    case UP: isUp = true; break; 
    }
    }
    
    void keyReleased() {
    switch(keyCode) {
    case RIGHT: isRight = false; break;
    case LEFT: isLeft = false; break;
    case UP: isUp = false; break;
    
    }
    }
    
    
    /********* OTHER FUNCTIONS *********/
    
    // This method sets the necessery variables to start the game  
    void startGame() {
    gameScreen=1;
    }
    
    /*********CLASS TAB****************/
    class Alien{
    
    //chest
    float lowerchestXloc;
    float lowerchestYloc;
    float lowerchestWidth;
    float lowerchestHeight;
    float upperchestXloc;
    float upperchestYloc;
    float upperchestWidth;
    float upperchestHeight;
    
    //hand 
    float handfrontXloc;
    float handfrontYloc;
    float handfrontWidth;
    float handfrontHeight;
    
    //head
    float headXloc;
    float headYloc;
    float headWidth;
    float headHeight;
    
    //feets
    //front
    float tightfrontXloc;
    float tightfrontYloc;
    float tightfrontWidth;
    float thightfrontHeight;
    float legfrontXloc;
    float legfrontYloc;
    float legfrontWidth;
    float legfrontHeight;
    float toefrontX1;
    float toefrontY1;
    float toefrontX2;
    float toefrontY2;
    float toefrontX3;
    float toefrontY3;
    
    //back feet
    float tightbackXloc;
    float tightbackYloc;
    float tightbackWidth;
    float tightbackHeight;
    float legbackXloc;
    float legbackYloc;
    float legbackWidth;
    float legbackHeight;
    float toebackX1;
    float toebackY1;
    float toebackX2;
    float toebackY2;
    float toebackX3;
    float toebackY3;
    
    Alien (PVector L, float bWidth, float bHeight, color cColor, PVector S,,PVector A, float msx,     
    float  msy, PVector D, float G ) { 
    
    location = new PVector(bellyXloc, bellyYloc);
    location = L;
    bellyWidth = bWidth;
    bellyHeight = bHeight;
    characterColor = colour;
    speed = S;
    acceleration = A;
    max_speedx = msx;
    max_speedy = msy;
    deceleration = D;
    gravity = G;
    }
    
    void displayAlien() {
    
    bellyXloc=width/2;
    bellyYloc=height-(1.6*bellyHeight);
    bellyWidth=width/20;
    bellyHeight= height*0.1;
    
    background(255);
    noStroke();
    fill(152, 223, 234);
    //BODY
    //belly
    ellipse(location.x, location.y, bellyWidth, bellyHeight);
    
    //lowerchest
    lowerchestXloc = location.x;
    lowerchestYloc = location.y - (bellyHeight*0.2);
    lowerchestWidth = bellyWidth*0.95;
    lowerchestHeight = bellyHeight*0.85;
    upperchestXloc = location.x;
    upperchestYloc = lowerchestYloc - (lowerchestHeight*0.2);
    upperchestWidth = lowerchestWidth * 0.98;
    upperchestHeight = lowerchestHeight * 0.9;
    
    ellipse(lowerchestXloc, lowerchestYloc, lowerchestWidth, lowerchestHeight);
    ellipse(upperchestXloc, upperchestYloc, upperchestWidth, upperchestHeight);
    
    //HEAD
    
    headXloc = location.x;
    headYloc = upperchestYloc - (upperchestHeight * 0.79);
    headWidth = bellyWidth*1.1;
    headHeight = headWidth;
    
    ellipse(headXloc, headYloc, headWidth, headHeight);
    
    //face
    
    
    //feets
    //front feet
    tightfrontXloc = location.x - 7;
    tightfrontYloc = location.y + ((bellyHeight/2)-5);
    tightfrontWidth = 0.2*bellyWidth;
    thightfrontHeight = 0.65*bellyHeight;
    legfrontXloc = tightfrontXloc;
    legfrontYloc = tightfrontYloc+(thightfrontHeight-3);
    legfrontWidth = tightfrontWidth;
    legfrontHeight = 0.45*bellyHeight;
    toefrontX1 = legfrontXloc+legfrontWidth/2;
    toefrontY1 = legfrontYloc+(legfrontHeight-5);
    toefrontX2 = toefrontX1-8;
    toefrontY2 = toefrontY1+14;
    toefrontX3 = toefrontX1+8;
    toefrontY3 = toefrontY2;
    
    if(toefrontY2 > height){
         toefrontY2 = height;
    }
    //left feet
    rect(tightfrontXloc, tightfrontYloc, tightfrontWidth, thightfrontHeight);
    rect(legfrontXloc, legfrontYloc, legfrontWidth, legfrontHeight);
    triangle(toefrontX1, toefrontY1, toefrontX2, toefrontY2, toefrontX3,     
    toefrontY3);
    //back feet
    
    tightbackXloc = location.x - 4;
    tightbackYloc = tightfrontYloc;
    tightbackWidth = tightfrontWidth;
    tightbackHeight = thightfrontHeight;
    legbackXloc = tightbackXloc;
    legbackYloc = legfrontYloc;
    legbackWidth = legfrontWidth;
    legbackHeight = legfrontHeight;
    toebackX1 = legbackXloc+legbackWidth/2;
    toebackY1 = toefrontY1;
    toebackX2 = toebackX1-8;
    toebackY2 = toefrontY2;
    toebackX3 = toebackX1+8;
    toebackY3 = toefrontY3;
    
    rect(tightbackXloc, tightbackYloc, tightbackWidth, tightbackHeight);
    rect(legbackXloc, legbackYloc, legbackWidth, legbackHeight);
    triangle(toebackX1, toebackY1, toebackX2, toebackY2, toebackX3, toebackY3);
    
    //hands
    
    handfrontXloc = tightbackXloc;
    handfrontYloc = upperchestYloc-(upperchestHeight*0.25);
    handfrontWidth = tightfrontWidth;
    handfrontHeight = bellyHeight/2+lowerchestHeight/2;
    stroke(255);
    rect(handfrontXloc, handfrontYloc, handfrontWidth, handfrontHeight);
    
    }//end display
    
    
    void update() {
      if ( isRight ) {//if is going to right going to width direction
      speed.x+=acceleration.x;
      location.x+=speed.x;
      if ( acceleration.x > max_speedx ) {
      speed.x = max_speedx;
      }
      }
      else if ( isLeft ) {//if is going to left going to 0 direction
    
      speed.x+=acceleration.x;
      location.x-=speed.x;
      if ( acceleration.x < -max_speedx ) {
      speed.x = max_speedx;
      }
      }
      else { //neither right or left pressed, decelerate
      if ( speed.x > 0 ) {//if key is released 
      speed.x -= deceleration.x;// the acceleration decreasing with deceraltion
      if ( speed.x < 0 ) {// this stop that deceleration turn the character to opposite      
      direction
      speed.x = 0;
      }
      }
      else if ( speed.x < 0 ) {
      speed.x += deceleration.x;
      if ( speed.x > 0 ) {
      speed.x = 0;
      }
      }
      }
    
       if ( isUp ) {
      location.y -= speed.y;
      speed.y += max_speedy*deceleration.y;
    
      }
      else if(max_speedy>0){
      location.y+=speed.y;
      speed.y=((max_speedy*deceleration.y)*gravity);
      }
    
      }// end update
    
      }//end class
    
  • edited March 2017

    You would need to add a conditional statement to check to see it your character is on the ground. Your character is on the ground when its y location reaches or exceeds a certain value. At that point, you will want to reassign that limit to the y position, and probably 0 out the y velocity.

  • edited March 2017

    I added isOnGround as public boolean in class constructor I declared as false

    int gameScreen = 0;
    final int displayWidth = 800;
    final int displayHeight = 600;
    
    Alien a;
    
    int FPS = 30;
    
    //Player
    public float bellyXloc, bellyYloc;
    public float bellyWidth, bellyHeight;
    public float accelerationX,accelerationY;
    public float decelerationX, decelerationY;  
    public PVector location, speed, acceleration, deceleration;
    public float max_speedx,max_speedy ;
    public float gravity;
    public color characterColor;
    public Boolean isOnGround;//added 
    public Boolean   isLeft=false ,  isRight=false , isUp= false ;
    
    void setup() {
    
    size(800, 600, JAVA2D);
    smooth(4);
    frameRate(FPS); 
    bellyXloc=width/2;
    bellyYloc=height/2; //-(1.6*bellyHeight)
    bellyWidth=width/20;
    bellyHeight= height*0.1;
    characterColor = color(152,223,234);
    location = new PVector(bellyXloc,bellyYloc);
    speed = new PVector();
    accelerationX=1.3;
    accelerationY=0;
    max_speedx = 5;
    max_speedy = 3;
    acceleration = new PVector(accelerationX,accelerationY);
    decelerationX = 0.75;
    decelerationY = 0.75;
    deceleration = new PVector(decelerationX, decelerationY);
    gravity = 1.1;
    
    //Constructor
    a = new Alien(location,bellyWidth,bellyHeight, color(152,223,234),speed,
    acceleration, max_speedx, max_speedy, deceleration ,gravity);
    
    }
    
    /********* SCREEN CONTENTS *********/
    void initScreen() {
    background(0);
    textAlign(CENTER);
    text("Click to start", width/2, height/2);
    }
    
    //initialize game screen
    void gameScreen() {
    background(255);
    }
    
    void gameOverScreen() {
    background(0);
    textAlign(CENTER);
    fill(255);
    textSize(30);
    text("Game Over", height/2, width/2 - 20);
    textSize(15);
    text("Click to Restart", height/2, width/2 + 10); 
    }
    
    void draw() {
    // Display the contents of the current screen
    if (gameScreen == 0) {
    initScreen();
    } else if (gameScreen == 1) {
    gameScreen();
    a.update();
    background(0);
    fill(characterColor);
    a.displayAlien();
    
    
    } else if (gameScreen == 2) {
    gameOverScreen();
    }
    }
    
    
    /********* INPUTS *********/
    
    public void mousePressed() {
    // PUBLIC provide other classes access the fields and methods within a class. 
    // The public keyword is used before a field or method that you want to make        
    // if we are on the initial screen when clicked, start the game
    if (gameScreen==0) {
    startGame();
    }
    }
    
    void keyPressed() {
    switch(keyCode) {
    case RIGHT: isRight  = true; break;
    case LEFT: isLeft = true; break;
    case UP: isUp = true; break; 
    }
    }
    
    void keyReleased() {
    switch(keyCode) {
    case RIGHT: isRight = false; break;
    case LEFT: isLeft = false; break;
    case UP: isUp = false; break;
    
    }
    }
    
    
    /********* OTHER FUNCTIONS *********/
    
    // This method sets the necessery variables to start the game  
    void startGame() {
    gameScreen=1;
    }
    
    /*********CLASS TAB****************/
    class Alien{
    
    //chest
    float lowerchestXloc;
    float lowerchestYloc;
    float lowerchestWidth;
    float lowerchestHeight;
    float upperchestXloc;
    float upperchestYloc;
    float upperchestWidth;
    float upperchestHeight;
    
    //hand 
    float handfrontXloc;
    float handfrontYloc;
    float handfrontWidth;
    float handfrontHeight;
    
    //head
    float headXloc;
    float headYloc;
    float headWidth;
    float headHeight;
    
    //feets
    //front
    float tightfrontXloc;
    float tightfrontYloc;
    float tightfrontWidth;
    float thightfrontHeight;
    float legfrontXloc;
    float legfrontYloc;
    float legfrontWidth;
    float legfrontHeight;
    float toefrontX1;
    float toefrontY1;
    float toefrontX2;
    float toefrontY2;
    float toefrontX3;
    float toefrontY3;
    
    //back feet
    float tightbackXloc;
    float tightbackYloc;
    float tightbackWidth;
    float tightbackHeight;
    float legbackXloc;
    float legbackYloc;
    float legbackWidth;
    float legbackHeight;
    float toebackX1;
    float toebackY1;
    float toebackX2;
    float toebackY2;
    float toebackX3;
    float toebackY3;
    
    Alien (PVector L, float bWidth, float bHeight, color cColor, PVector S,,PVector A, float msx,     
    float  msy, PVector D, float G ) { 
    
    location = new PVector(bellyXloc, bellyYloc);
    location = L;
    bellyWidth = bWidth;
    bellyHeight = bHeight;
    characterColor = colour;
    speed = S;
    acceleration = A;
    max_speedx = msx;
    max_speedy = msy;
    deceleration = D;
    gravity = G;
    isOnGround = false;
    }
    
    void displayAlien() {
    
    bellyXloc=width/2;
    bellyYloc=height-(1.6*bellyHeight);
    bellyWidth=width/20;
    bellyHeight= height*0.1;
    
    background(255);
    noStroke();
    fill(152, 223, 234);
    //BODY
    //belly
    ellipse(location.x, location.y, bellyWidth, bellyHeight);
    
    //lowerchest
    lowerchestXloc = location.x;
    lowerchestYloc = location.y - (bellyHeight*0.2);
    lowerchestWidth = bellyWidth*0.95;
    lowerchestHeight = bellyHeight*0.85;
    upperchestXloc = location.x;
    upperchestYloc = lowerchestYloc - (lowerchestHeight*0.2);
    upperchestWidth = lowerchestWidth * 0.98;
    upperchestHeight = lowerchestHeight * 0.9;
    
    ellipse(lowerchestXloc, lowerchestYloc, lowerchestWidth, lowerchestHeight);
    ellipse(upperchestXloc, upperchestYloc, upperchestWidth, upperchestHeight);
    
    //HEAD
    
    headXloc = location.x;
    headYloc = upperchestYloc - (upperchestHeight * 0.79);
    headWidth = bellyWidth*1.1;
    headHeight = headWidth;
    
    ellipse(headXloc, headYloc, headWidth, headHeight);
    
    //face
    
    
    //feets
    //front feet
    tightfrontXloc = location.x - 7;
    tightfrontYloc = location.y + ((bellyHeight/2)-5);
    tightfrontWidth = 0.2*bellyWidth;
    thightfrontHeight = 0.65*bellyHeight;
    legfrontXloc = tightfrontXloc;
    legfrontYloc = tightfrontYloc+(thightfrontHeight-3);
    legfrontWidth = tightfrontWidth;
    legfrontHeight = 0.45*bellyHeight;
    toefrontX1 = legfrontXloc+legfrontWidth/2;
    toefrontY1 = legfrontYloc+(legfrontHeight-5);
    toefrontX2 = toefrontX1-8;
    toefrontY2 = toefrontY1+14;
    toefrontX3 = toefrontX1+8;
    toefrontY3 = toefrontY2;
    
    if(toefrontY2 > height){
         toefrontY2 = height;
    }
    //left feet
    rect(tightfrontXloc, tightfrontYloc, tightfrontWidth, thightfrontHeight);
    rect(legfrontXloc, legfrontYloc, legfrontWidth, legfrontHeight);
    triangle(toefrontX1, toefrontY1, toefrontX2, toefrontY2, toefrontX3,     
    toefrontY3);
    //back feet
    
    tightbackXloc = location.x - 4;
    tightbackYloc = tightfrontYloc;
    tightbackWidth = tightfrontWidth;
    tightbackHeight = thightfrontHeight;
    legbackXloc = tightbackXloc;
    legbackYloc = legfrontYloc;
    legbackWidth = legfrontWidth;
    legbackHeight = legfrontHeight;
    toebackX1 = legbackXloc+legbackWidth/2;
    toebackY1 = toefrontY1;
    toebackX2 = toebackX1-8;
    toebackY2 = toefrontY2;
    toebackX3 = toebackX1+8;
    toebackY3 = toefrontY3;
    
    rect(tightbackXloc, tightbackYloc, tightbackWidth, tightbackHeight);
    rect(legbackXloc, legbackYloc, legbackWidth, legbackHeight);
    triangle(toebackX1, toebackY1, toebackX2, toebackY2, toebackX3, toebackY3);
    
    //hands
    
    handfrontXloc = tightbackXloc;
    handfrontYloc = upperchestYloc-(upperchestHeight*0.25);
    handfrontWidth = tightfrontWidth;
    handfrontHeight = bellyHeight/2+lowerchestHeight/2;
    stroke(255);
    rect(handfrontXloc, handfrontYloc, handfrontWidth, handfrontHeight);
    
    }//end display
    
    
    void update() {
    
     location.y += gravity;
    // 
    
     if  (toefrontY2 >= (displayHeight-2)){
     location.y += speed.y; 
    

    I try to stop the character to go above the screen but it doesn't work and neither the pervious statement above maybe because speed is PVector so speed.y is doesn't have a value or is it automatically 0?

     gravity=0;
     }
    
      if ( isRight ) {//if is going to right going to width direction
      speed.x+=acceleration.x;
      location.x+=speed.x;
      if ( acceleration.x > max_speedx ) {
      speed.x = max_speedx;
      }
      }
      else if ( isLeft ) {//if is going to left going to 0 direction
    
      speed.x+=acceleration.x;
      location.x-=speed.x;
      if ( acceleration.x < -max_speedx ) {
      speed.x = max_speedx;
      }
      }
      else { //neither right or left pressed, decelerate
      if ( speed.x > 0 ) {//if key is released 
      speed.x -= deceleration.x;// the acceleration decreasing with deceraltion
      if ( speed.x < 0 ) {// this stop that deceleration turn the character to opposite      
      direction
      speed.x = 0;
      }
      }
      else if ( speed.x < 0 ) {
      speed.x += deceleration.x;
      if ( speed.x > 0 ) {
      speed.x = 0;
      }
      }
      }
      if(isOnGround) { 
      if ( isUp ) {
      location.y -= speed.y;
      speed.y += (max_speedy*deceleration.y);
      isOnGround=false;
    
      }
      else if(max_speedy>0){
      location.y+=speed.y;
      speed.y=((max_speedy*deceleration.y)*gravity);
      }
    
      }// end update
    
      }//end class
    

    How can fix first the this that the character will stop at the moment on the ground since at the moment i don't have a game grid, and 2nd how can I stop the character go above the screen ? Thanks

Sign In or Register to comment.