Need help t to move the character by pressing up key


#1

When keyboard arrow key UP is press, character should move up to the next upper section.
Each time character is more up on level, this level will be coloured differently. Please somebody help me to code this.


#2

Use the function keyPressed to take keyboard input

void keyPressed() {
  if (key == CODED) {
    if (keyCode == UP) {
// do something
}
}
}

#3

Look at PShape as a way of drawing your character so that you can position it with one set of x,y variables.


#4

Can you please format you code by selecting the code and clicking the following icon: </>
You can edit your post by clicking the pen icon in the bottom right of your post.

Thank you :slight_smile:


#5

would u plz complete the code :frowning:


#6

So here’s what I came up with:

Like usual, below this code I include an explanation if you or anyone else needs it :grin:

Player p; 

void setup() {

size(700,500);

p = new Player(); 

}

void draw() {
background(255);

p.display(); 

} //end of void draw


class Player
{

  int e; // for the x-value
  int eY; //for the y-value
  int r1, r2, r3, r4; //just initialize them here 

  Player()
  {
    e = 378; 
    eY = 450; 

  }//the default constructor
  
  
  void display()
   { 
    line(0,200,700,200);
    line(0,300,700,300);
    line(0,400,700,400);

    fill(0);
    ellipse(e,eY,80,50);
    
    fill(#F3F156);
    rect(r1,eY,58,58);
    
    fill(#90F614);
    rect(r2,eY+10,7,7);
    rect(r3,eY+10,7,7);
    
    
    fill(#EF1A1A);
    rect(r4,eY+30,16,9); 
    
    move(); 
     
   } //end of display
   
   void move()
   {
 
    //only move the e variable  
    e = e + 1; 
    
    //make all the rest of the variables follow the variable e
    r1 = e-28; 
    r2 = e-20;
    r3 = e+14;
    r4 = e-7;

    if(e>width+80)
    {
      e = -40;   //Just to make it look a little smoother 
    }
    
    if(keyPressed)
    {
      if(key==CODED)
      {
        if(keyCode==UP)
         {
          eY-=5; 
         }
         else if(keyCode == DOWN) 
         {
          eY+=5;  
         }
      } 
    }//end of if key pressed 
    

   } //end of the move 
   
   
}//end of player class

Now for some explanation:

I don’t know how much of a game you want to make, but I got you started with making your character an object. You can also make the lines part of their own class, but that’s if you want. So I first declared the object like this:

Player p;

And then in void setup() I initialized the player like this:

p = new Player();

Then I made the player class. In it, I moved all of your variables there. I have two main variables for the player:

  int e; // for the x-value
  int eY; //for the y-value

And then, I just declared every other variable, because later on I want to base each variable off of e, and eY.

Then I made the default constructor for the player class, meaning that every time you create a new player, by default, certain values will be set to its variables. I made e = 378, like it was in your original code, and I made eY = 450, like I believe your ellipse was. You can see that default constructor here:

  Player()
  {

    e = 378; 
    eY = 450;

  }//the default constructor

Then I made the display function, so that you can actually see the player. I literally just copied and pasted your code in, but for the shapes that had to do with your player, I made their x values either equal to r1, r2, r3, r4, or some addition to e and eY. Hopefully that makes sense?

You can see that here:

    line(0,200,700,200);
    line(0,300,700,300);
    line(0,400,700,400);

    fill(0);
    ellipse(e,eY,80,50); 
    
    fill(#F3F156);
    rect(r1,eY,58,58);
    
    fill(#90F614);
    rect(r2,eY+10,7,7);
    rect(r3,eY+10,7,7);
    
    
    fill(#EF1A1A);
    rect(r4,eY+30,16,9);

Then afterwards I have a move() function, so that your player can move. In this part, you will see why I made all your variables be based off of e or eY. That way, you only have to move one variable, and check for the position of one variable.

First, I have your e variable constantly increasing, just like you had it:

//only move the e variable because all the other variables take e into account
    e = e + 1;

Then, I initialized r1, r2, r3, and r4 to follow the variable e:

//make all the rest of the variables follow the variable e
    r1 = e-28; 
    r2 = e-20;
    r3 = e+14;
    r4 = e-7;

Then, I had a position check. If the e variable was greater than the width+80, reset the variable e to -40. I did that, just so the carousel effect you were making could look a bit smoother. Your player would just flash from reaching the end, back to the beginning of the screen, so I have it go out of frame for a bit, again for the sake of it looking a bit better. You can edit it to your liking:

 if(e>width+80)
    {
      e = -40;   //Just to make it look a little smoother 
    }

And again, you only have to check e, and not every single variable like you did before.

Finally, I have the key checks. You said you wanted to move the character up, so what you do is first check if a key is pressed. If that is true? Then check if the key is CODED which would be keys such as UP and DOWN and LEFT and RIGHT and such. If the keyCode is UP? Decrease your eY value. And if it’s DOWN? Increase your eY. I know you didn’t really ask about moving the character down, but I assume that’s the path you were going on :smiley:

 if(keyPressed)
    {
      if(key==CODED)
      {
        if(keyCode==UP)
         {
          eY-=5; 
         }
         else if(keyCode == DOWN) 
         {
          eY+=5;  
         }
      } 
    }//end of if key pressed

And that’s all for now? If you have any more questions, or any need of clarification I have no problem helping out with that.

Note:
Like @iamtomodea said, PShape is another way that you can make your character. I didn’t want to completely rewrite your code, but instead keep your own familiar lines. But yes, PShape would be the most compact, I believe.

Hope this helps,
EnhancedLoop7


#7

@suptoabha Please don’t vandalize your own posts. Also, please see this guide:

@EnhancedLoop7 It’s great that you want to help, but please do not post full code solutions. Please see this guide:


#8

Shameless self-promotion: Here is a guide that discusses user input in Processing:


#9

@Kevin I understand what you mean about posting full answers, and I too would love to gently coax the person asking for help in the right direction, not just drop a whole program on them! :smiley: I did like posting whole sets of code though because I wanted the person to see how everything ended up coming together, and hence why I include a detailed explanation underneath. Thank you though for providing me with that link so I can see how to better support this community!

EnhancedLoop7


#10

thnaks for ur great help


#11

The variable keyCode only stores one key at once.
So if you want the user to be able to press multiple keys at once, you can make a boolean array.
Then check for the keys you want to have in your keyPressed and keyReleased function.

void keyPressed() {
  if(keyCode == UP) {
    upKeyPressed = true;
  }
}

void keyReleased() {
  if(keyCode == UP) {
    upKeyPressed = false;
  }
}

Note: This doesn’t really work with macOS High Sierra, since Apple added this popup window when you hold a key down. This will cause Processing to behave strangely. See the Processing GitHub for a workaround.