# Move the character by pressing up key

**URL:** https://discourse.processing.org/t/move-the-character-by-pressing-up-key/2475
**Category:** Coding Questions
**Created:** [August 8, 2018, 11:04am UTC](https://discourse.processing.org/t/move-the-character-by-pressing-up-key/2475 "2018-08-08T11:04:16Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![suptoabha](https://avatars.discourse-cdn.com/v4/letter/s/a587f6/32.png) [@suptoabha](https://discourse.processing.org/u/suptoabha)
#### Post date: [August 8, 2018, 11:04am UTC](https://discourse.processing.org/t/move-the-character-by-pressing-up-key/2475/1 "2018-08-08T11:04:17Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![iamtomodea](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/iamtomodea/32/1054_2.png) [@iamtomodea](https://discourse.processing.org/u/iamtomodea)
#### Post date: [August 8, 2018, 11:35am UTC](https://discourse.processing.org/t/move-the-character-by-pressing-up-key/2475/2 "2018-08-08T11:35:58Z")

</div>

Use the function keyPressed to take keyboard input

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

```

---

<div class="post-metadata">

### Author: ![iamtomodea](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/iamtomodea/32/1054_2.png) [@iamtomodea](https://discourse.processing.org/u/iamtomodea)
#### Post date: [August 8, 2018, 11:40am UTC](https://discourse.processing.org/t/move-the-character-by-pressing-up-key/2475/3 "2018-08-08T11:40:17Z")

</div>

Look at [PShape](https://processing.org/reference/PShape.html) as a way of drawing your character so that you can position it with one set of x,y variables.

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [August 8, 2018, 11:49am UTC](https://discourse.processing.org/t/move-the-character-by-pressing-up-key/2475/4 "2018-08-08T11:49:28Z")

</div>

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 🙂

---

<div class="post-metadata">

### Author: ![suptoabha](https://avatars.discourse-cdn.com/v4/letter/s/a587f6/32.png) [@suptoabha](https://discourse.processing.org/u/suptoabha)
#### Post date: [August 8, 2018, 12:31pm UTC](https://discourse.processing.org/t/move-the-character-by-pressing-up-key/2475/5 "2018-08-08T12:31:23Z")

</div>

would u plz complete the code ☹

---

<div class="post-metadata">

### Author: ![EnhancedLoop7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/enhancedloop7/32/1040_2.png) [@EnhancedLoop7](https://discourse.processing.org/u/EnhancedLoop7)
#### Post date: [August 8, 2018, 5:39pm UTC](https://discourse.processing.org/t/move-the-character-by-pressing-up-key/2475/6 "2018-08-08T17:39:11Z")

</div>

So here’s what I came up with:

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

```auto
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:

```auto
  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:

```auto
  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:

```auto
    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:

```auto
//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:

```auto
//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:

```auto
 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 😃

```auto
 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

---

<div class="post-metadata">

### Author: ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)
#### Post date: [August 8, 2018, 9:43pm UTC](https://discourse.processing.org/t/move-the-character-by-pressing-up-key/2475/7 "2018-08-08T21:43:52Z")

</div>

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

> [@Guidelines—Asking Questions](https://discourse.processing.org/t/guidelines-tips-on-asking-questions/2147):
>
> Summary (TL;DR) Ask complete questions to get better answers! Be specific. For example: I want to load an image. I tried using createImg() , and I expected the image to be on canvas, but what happened instead was the image showing up below the canvas. Isolate your problem and work in small steps. Share only the code directly related to your problem if it is part of a bigger project, and if something isn’t working, try the smallest code that could do the thing you want. Can we run your code to …

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

> [@Guidelines—Answering Questions](https://discourse.processing.org/t/guidelines-how-to-answer-questions/2145):
>
> If you’re here, it means you’re thinking about contributing to the Processing community by answering questions on the forum. That’s great! This guide is meant to help you answer questions in a way that’s most helpful. Be nice. Remember that you’re talking to a person! Follow the guidelines in the [FAQ](https://discourse.processing.org/faq). Also read through the [P5.js community guidelines](https://p5js.org/community/). Specifically, avoid making judgments like “this code is bad” or “that’s a dumb way to do this”. Be mindful of how your tone comes across over the…

---

<div class="post-metadata">

### Author: ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)
#### Post date: [August 8, 2018, 9:45pm UTC](https://discourse.processing.org/t/move-the-character-by-pressing-up-key/2475/8 "2018-08-08T21:45:03Z")

</div>

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

> **[Getting User Input](https://happycoding.io/tutorials/processing/input)**
>
> Learn how to detect user input to make interactive programs in Processing.

---

<div class="post-metadata">

### Author: ![EnhancedLoop7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/enhancedloop7/32/1040_2.png) [@EnhancedLoop7](https://discourse.processing.org/u/EnhancedLoop7)
#### Post date: [August 8, 2018, 11:57pm UTC](https://discourse.processing.org/t/move-the-character-by-pressing-up-key/2475/9 "2018-08-08T23:57:03Z")

</div>

@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! 😃 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

---

<div class="post-metadata">

### Author: ![suptoabha](https://avatars.discourse-cdn.com/v4/letter/s/a587f6/32.png) [@suptoabha](https://discourse.processing.org/u/suptoabha)
#### Post date: [August 9, 2018, 5:38am UTC](https://discourse.processing.org/t/move-the-character-by-pressing-up-key/2475/10 "2018-08-09T05:38:15Z")

</div>

thnaks for ur great help

---

<div class="post-metadata">

### Author: ![DongKingKong0](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/dongkingkong0/32/960_2.png) [@DongKingKong0](https://discourse.processing.org/u/DongKingKong0)
#### Post date: [August 25, 2018, 6:42pm UTC](https://discourse.processing.org/t/move-the-character-by-pressing-up-key/2475/11 "2018-08-25T18:42:53Z")

</div>

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.

```auto
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](https://github.com/processing/processing/wiki/Troubleshooting#key-repeat-on-macos-sierra) for a workaround.
