#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Using arrow keys to move to "players" around.

edited May 2015

What I am trying to do is create the simplest form of a video game. I am trying to move mario left and right with the Z/X keys and Luigi with the Right/Left arrow keys. Would I use an if statement or rather do for example KeyCode?

So far my code looks like:

(mod: see comment)

Thank you so much. I have tried a few things and cannot seem to get it right.

Tagged:

## Answers

• You should start by editing your post and formatting your code for this forum properly. Hint: Select code and press Ctrl + o

• ``````void setup() {
size (600, 600);
smooth();
}

void draw(){
background(41, 194, 252);

fill(54, 252, 41);
stroke(54, 252, 41);
rect(0,500, 600, 100);

fill(254, 255, 36);
stroke(245, 255, 36);
ellipse(475, 100, 120, 120);

mario(100, 425, 1.5);
luigiLeft(400, 425, 1.5);

}

void mario(float x, float y, float s) {
pushMatrix();
translate(x-s*.75*50, y-s*50);
scale(s*100/16);
strokeWeight(.1);
stroke(159, 89, 39); // Light brown color
fill(159, 89, 39); // Light brown color
rect(2, 7, 6, 1);
rect(1, 8, 10, 1);
rect(0, 9, 12, 1);
rect(2, 10, 1, 1);
rect(9, 10, 1, 1);
rect(1, 14, 3, 1);
rect(8, 14, 3, 1);
rect(0, 15, 4, 1);
rect(8, 15, 4, 1);
stroke(244, 164, 96); // Peach color
fill(244, 164, 96); // Peach color
rect(5, 2, 4, 1);
rect(2, 3, 9, 1);
rect(2, 4, 10, 1);
rect(3, 5, 4, 1);
rect(3, 6, 7, 1);
rect(0, 10, 2, 1);
rect(10, 10, 2, 1);
rect(0, 11, 3, 1);
rect(9, 11, 3, 1);
rect(0, 12, 2, 1);
rect(10, 12, 2, 1);
stroke(220, 0, 0); // Red color
fill(220, 0, 0); // Red color
rect(4, 7, 1, 1);
rect(4, 8, 1, 1);
rect(7, 8, 1, 1);
rect(4, 9, 4, 1);
rect(3, 10, 1, 1);
rect(5, 10, 2, 1);
rect(8, 10, 1, 1);
rect(3, 11, 6, 1);
rect(2, 12, 8, 1);
rect(2, 13, 3, 1);
rect(7, 13, 3, 1);
stroke(139, 69, 19); // Dark brown color
fill(139, 69, 19); // Dark brown color
rect(2, 2, 3, 1);
rect(7, 2, 1, 1);
rect(1, 3, 1, 1);
rect(3, 3, 1, 1);
rect(7, 3, 1, 1);
rect(1, 4, 1, 1);
rect(3, 4, 2, 1);
rect(8, 4, 1, 1);
rect(1, 5, 2, 1);
rect(7, 5, 4, 1);
stroke(255, 215, 0); // Gold color
fill(255, 215, 0); // Gold color
rect(4, 10, 1, 1);
rect(7, 10, 1, 1);
stroke(220, 0, 0); // Red color
fill(220, 0, 0); // Red color
rect(3, 0, 5, 1);
rect(2, 1, 9, 1);
popMatrix();
}

void luigiLeft(float x, float y, float s) {
pushMatrix();
translate(x+s*.75*50, y-s*50);
scale(-s*100/16,s*100/16);
strokeWeight(.1);
stroke(46, 139, 87); // Green shirt
fill(46, 139, 87); // Green shirt
rect(2, 7, 6, 1);
rect(1, 8, 10, 1);
rect(0, 9, 12, 1);
rect(2, 10, 1, 1);
rect(9, 10, 1, 1);
stroke(109, 49, 9); // Dark brown shoes
fill(109, 49, 9); // Dark brown shoes
rect(1, 14, 3, 1);
rect(8, 14, 3, 1);
rect(0, 15, 4, 1);
rect(8, 15, 4, 1);
stroke(244, 164, 96); // Peach color
fill(244, 164, 96); // Peach color
rect(5, 2, 4, 1);
rect(2, 3, 9, 1);
rect(2, 4, 10, 1);
rect(3, 5, 4, 1);
rect(3, 6, 7, 1);
rect(0, 10, 2, 1);
rect(10, 10, 2, 1);
rect(0, 11, 3, 1);
rect(9, 11, 3, 1);
rect(0, 12, 2, 1);
rect(10, 12, 2, 1);
stroke(45, 40, 240); // Blue overalls
fill(45, 40, 240); // Blue overalls
rect(4, 7, 1, 1);
rect(4, 8, 1, 1);
rect(7, 8, 1, 1);
rect(4, 9, 4, 1);
rect(3, 10, 1, 1);
rect(5, 10, 2, 1);
rect(8, 10, 1, 1);
rect(3, 11, 6, 1);
rect(2, 12, 8, 1);
rect(2, 13, 3, 1);
rect(7, 13, 3, 1);
stroke(109, 49, 9); // Dark brown facial features
fill(109, 49, 9); // Dark brown facial features
rect(2, 2, 3, 1);
rect(7, 2, 1, 1);
rect(1, 3, 1, 1);
rect(3, 3, 1, 1);
rect(7, 3, 1, 1);
rect(1, 4, 1, 1);
rect(3, 4, 2, 1);
rect(8, 4, 1, 1);
rect(1, 5, 2, 1);
rect(7, 5, 4, 1);
stroke(255, 215, 0); // Gold color
fill(255, 215, 0); // Gold color
rect(4, 10, 1, 1);
rect(7, 10, 1, 1);
stroke(46, 139, 87); // Green hat
fill(46, 139, 87); // Green hat
rect(3, 0, 5, 1);
rect(2, 1, 9, 1);
popMatrix();
}
``````
• (duplicate thread removed)

• thank you

• ``````float marioX = 100;
float marioY = 425;
float greenMarioX = 400;
float greenMarioY = 425;

void setup() {
size (600, 600);
smooth();
}

void draw(){
background(41, 194, 252);

fill(54, 252, 41);
stroke(54, 252, 41);
rect(0,500, 600, 100);

fill(254, 255, 36);
stroke(245, 255, 36);
ellipse(475, 100, 120, 120);

mario(marioX,marioY, 1.5);
luigiLeft(greenMarioX,greenMarioY, 1.5);

}

void keyPressed(){
if(key == ' '){
marioX++;
greenMarioX--;
}
}

// snip two more long methods
``````
• You can test for the left and right arrow keys using keyCode, compare it to LEFT and RIGHT. Normal keys are 'z' and 'x', of course.

• I'll give this a try! Thank you!

• Post your try at getting it working for more help if you need it!

• ``````float marioX = 100;
float marioY = 425;
float luigiX = 400;
float luigiY = 425;

void setup() {
size (600, 600);
smooth();
}

void draw(){
background(41, 194, 252);

fill(54, 252, 41);
stroke(54, 252, 41);
rect(0,500, 600, 100);

fill(254, 255, 36);
stroke(245, 255, 36);
ellipse(475, 100, 120, 120);

mario(marioX, marioY, 1.5);
luigiLeft(luigiX, luigiY, 1.5);

}

void keyPressed() {
if (key == Z) {
marioX++;

}
if (key == X) {
marioX--;
}
if (keyPressed && (key == CODED)) {
if (keyCode == LEFT) {
luigiX--;
}
else if (keyCode == RIGHT) {
luigiX++;
}

}
``````

Okay so I did this, but I do not think it's correct.

• Answer ✓

When Processing sees X or Z, it think it's a variable, but it doesn't know about that variable, so you get an error.

You need to compare it to the character 'z' or 'x'. A character is a single letter enclosed by single quotes.

You might also try pressing Ctrl + t to format your code's indenting. If you had indented your code properly, you might have noticed you were missing a closing brace (}).

Checking if keyPressed is true inside keyPressed() is a little redundant.

Other than that, you're very close:

`````` void keyPressed() {
if (key == 'z') {
marioX++;
} else if (key == 'x') {
marioX--;
}
if (key == CODED) {
if (keyCode == LEFT) {
luigiX--;
} else if (keyCode == RIGHT) {
luigiX++;
}
}
}
``````
• I have another question for this same project.

If I am trying to make luigi flee from mario once mario get to close to him how would I do that? I am trying to make him "flee" to the right.

My code right now is: float marioX = 100; float marioY = 425; float luigiX = 400; float luigiY = 425;

``````void setup() {
size (600, 600);
smooth();
}

void draw(){
background(41, 194, 252);

fill(54, 252, 41);
stroke(54, 252, 41);
rect(0,500, 600, 100);

fill(254, 255, 36);
stroke(245, 255, 36);
ellipse(475, 100, 120, 120);

mario(marioX, marioY, 1.5);
luigiLeft(luigiX, luigiY, 1.5);

}

void keyPressed() {
if (key == 'z') {
marioX-=5;
} else if (key == 'x') {
marioX+=5;
}

if (key == CODED) {
if (keyCode == LEFT) {
luigiX-=5;
} else if (keyCode == RIGHT) {
luigiX+=5;
}
}
}

void mario(float x, float y, float s) {
pushMatrix();
translate(x-s*.75*50, y-s*50);
scale(s*100/16);
strokeWeight(.1);
stroke(159, 89, 39); // Light brown color
fill(159, 89, 39); // Light brown color
rect(2, 7, 6, 1);
rect(1, 8, 10, 1);
rect(0, 9, 12, 1);
rect(2, 10, 1, 1);
rect(9, 10, 1, 1);
rect(1, 14, 3, 1);
rect(8, 14, 3, 1);
rect(0, 15, 4, 1);
rect(8, 15, 4, 1);
stroke(244, 164, 96); // Peach color
fill(244, 164, 96); // Peach color
rect(5, 2, 4, 1);
rect(2, 3, 9, 1);
rect(2, 4, 10, 1);
rect(3, 5, 4, 1);
rect(3, 6, 7, 1);
rect(0, 10, 2, 1);
rect(10, 10, 2, 1);
rect(0, 11, 3, 1);
rect(9, 11, 3, 1);
rect(0, 12, 2, 1);
rect(10, 12, 2, 1);
stroke(220, 0, 0); // Red color
fill(220, 0, 0); // Red color
rect(4, 7, 1, 1);
rect(4, 8, 1, 1);
rect(7, 8, 1, 1);
rect(4, 9, 4, 1);
rect(3, 10, 1, 1);
rect(5, 10, 2, 1);
rect(8, 10, 1, 1);
rect(3, 11, 6, 1);
rect(2, 12, 8, 1);
rect(2, 13, 3, 1);
rect(7, 13, 3, 1);
stroke(139, 69, 19); // Dark brown color
fill(139, 69, 19); // Dark brown color
rect(2, 2, 3, 1);
rect(7, 2, 1, 1);
rect(1, 3, 1, 1);
rect(3, 3, 1, 1);
rect(7, 3, 1, 1);
rect(1, 4, 1, 1);
rect(3, 4, 2, 1);
rect(8, 4, 1, 1);
rect(1, 5, 2, 1);
rect(7, 5, 4, 1);
stroke(255, 215, 0); // Gold color
fill(255, 215, 0); // Gold color
rect(4, 10, 1, 1);
rect(7, 10, 1, 1);
stroke(220, 0, 0); // Red color
fill(220, 0, 0); // Red color
rect(3, 0, 5, 1);
rect(2, 1, 9, 1);
popMatrix();
}

void luigiLeft(float x, float y, float s) {
pushMatrix();
translate(x+s*.75*50, y-s*50);
scale(-s*100/16,s*100/16);
strokeWeight(.1);
stroke(46, 139, 87); // Green shirt
fill(46, 139, 87); // Green shirt
rect(2, 7, 6, 1);
rect(1, 8, 10, 1);
rect(0, 9, 12, 1);
rect(2, 10, 1, 1);
rect(9, 10, 1, 1);
stroke(109, 49, 9); // Dark brown shoes
fill(109, 49, 9); // Dark brown shoes
rect(1, 14, 3, 1);
rect(8, 14, 3, 1);
rect(0, 15, 4, 1);
rect(8, 15, 4, 1);
stroke(244, 164, 96); // Peach color
fill(244, 164, 96); // Peach color
rect(5, 2, 4, 1);
rect(2, 3, 9, 1);
rect(2, 4, 10, 1);
rect(3, 5, 4, 1);
rect(3, 6, 7, 1);
rect(0, 10, 2, 1);
rect(10, 10, 2, 1);
rect(0, 11, 3, 1);
rect(9, 11, 3, 1);
rect(0, 12, 2, 1);
rect(10, 12, 2, 1);
stroke(45, 40, 240); // Blue overalls
fill(45, 40, 240); // Blue overalls
rect(4, 7, 1, 1);
rect(4, 8, 1, 1);
rect(7, 8, 1, 1);
rect(4, 9, 4, 1);
rect(3, 10, 1, 1);
rect(5, 10, 2, 1);
rect(8, 10, 1, 1);
rect(3, 11, 6, 1);
rect(2, 12, 8, 1);
rect(2, 13, 3, 1);
rect(7, 13, 3, 1);
stroke(109, 49, 9); // Dark brown facial features
fill(109, 49, 9); // Dark brown facial features
rect(2, 2, 3, 1);
rect(7, 2, 1, 1);
rect(1, 3, 1, 1);
rect(3, 3, 1, 1);
rect(7, 3, 1, 1);
rect(1, 4, 1, 1);
rect(3, 4, 2, 1);
rect(8, 4, 1, 1);
rect(1, 5, 2, 1);
rect(7, 5, 4, 1);
stroke(255, 215, 0); // Gold color
fill(255, 215, 0); // Gold color
rect(4, 10, 1, 1);
rect(7, 10, 1, 1);
stroke(46, 139, 87); // Green hat
fill(46, 139, 87); // Green hat
rect(3, 0, 5, 1);
rect(2, 1, 9, 1);
popMatrix();
}
``````
• DON'T FEATURE CREEP. Tell us exactly what your end goal is NOW.

DON'T START NEW THREADS ON THE SAME TOPIC. Too late. Use the other thread instead.

Someone lock this, mmk?

Sign In or Register to comment.