We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Array help! (Read 939 times)
Array help!
Apr 24th, 2010, 9:06am
 
Hi there,
I am very new to processing, only been playing around with it for a few weeks but I am trying to make a very simple snake like interaction, whereby the user can press 4 keys to move an image in a direction and it leave a trail of the said images behind it. Here is my code at present:

Code:
Snake[] mySnake = new Snake[50];
PImage b;

void setup() {
size(400,400);
smooth();
b = loadImage("2.png");
for (int i = 0; i < mySnake.length; i++) {
mySnake[i] = new Snake(width/2, width/2, 1);
}
}
void draw() {
background(255);

for (int i = 0; i < mySnake.length-1; i++) {
mySnake[i] = mySnake[i+1];
mySnake[i].display();

if(keyPressed) {
if (key == 's' || key == 'S') {
mySnake[i].moveleft();
} else if (key == 'a' || key == 'A') {
mySnake[i].moveright();
} else if (key == 'z' || key == 'Z') {
mySnake[i].movedown();
} else if (key == 'w' || key == 'W') {
mySnake[i].moveup();
}
}}
}
//Snake variables
class Snake {
float xpos, ypos, xspeed;

Snake(float tempxpos, float tempypos, float tempxspeed) {
xpos = tempxpos;
ypos = tempypos;
xspeed = tempxspeed;
}

//Then add a function to it to draw the snake
void display() {
image(b, xpos, ypos);
}

void moveleft() {
xpos = xpos + xspeed;
xpos = constrain(xpos, 0, width);
}
void moveright() {
xpos = xpos - xspeed;
xpos = constrain(xpos, 0, width);

}
void moveup() {
ypos = ypos - xspeed;
ypos = constrain(ypos, 0, height);

}
void movedown() {
ypos = ypos + xspeed;
ypos = constrain(ypos, 0, height);

}
}


I'm pretty sure this isn't the right way of thinking but I'm a bit stuck. Atm it just had a very weird sonic like effect and moves too fast, I think this is due to this:
for (int i = 0; i < mySnake.length-1; i++) {  
mySnake[i] = mySnake[i+1];

The reason I used an array was because I thought I'd need one to duplicate the snake object each time and have it duplicate behind it but I'm just not quite sure how to make it do this...

Any help is appreciated at all as I'm very newto all of this but really want to get to grips it.

Thankyou!
Re: Array help!
Reply #1 - Apr 24th, 2010, 12:12pm
 
Indeed you should check keys only once per frame, otherwise you get that weird effect.

What you need to do in your draw() loop :
- check which key has been pressed
- shift your array ( snake[1] = snake[0], etc... )
- put the new position into first item ( snake[0] )
- display each item of the array

Try a search on "snake", there are some other posts that might help.
Re: Array help!
Reply #2 - Apr 24th, 2010, 1:20pm
 
Thankyou! I will have a look around the forum and take into account what you have said and see what I can do.

Is the best way of checking which key has been pressed using some kind of if statement I imagine? Would I need a boolean with that? like if a = true? type thing

Code:
boolean left = false;
boolean right = false;
boolean up = false;
boolean down = false;

if(keyPressed) {
   if (key == 's' || key == 'S') {
mySnake[i].moveleft();
left = true;
} else if (key == 'a' || key == 'A') {
mySnake[i].moveright();
right = true;
 } else if (key == 'z' || key == 'Z') {
mySnake[i].movedown();
 down = true;
  } else if (key == 'w' || key == 'W') {
mySnake[i].moveup();
 up = true;
  }
}}
}

if (left) {
 shift to the left
} else if (right) {
 shift to the right
} else if (down) {
 shift down
} else if (up) {
 shift up
}


I'm not sure what would go under the second if's and else if's I'm imagining it will contain something to do with an array but I'm not sure how to do it as I'm only just learning about arrays the last couple of days. Am I on the right track and how would I go about doing the next bit?

How to use the array in conjunction with a loop and everything is boggling my brain a little.  Shocked Very much a begginer here, hopefully this isn't too advanced for me to do.

Thanks!
Re: Array help!
Reply #3 - Apr 25th, 2010, 11:20am
 
Question to get the looping effect of it showing a trail of the movement does it need to be

for (int i = 0; i < mySnake.length; i++) {
?
or should I be using xpos and ypos instead. and where abouts should be in my code, will it be in the:

void movedown() {
 ypos = ypos + xspeed;
 ypos = constrain(ypos, 0, 380);

}

I tried

void movedown() {
for (int n = 0; n < ypos2.length-1; n++) {
 ypos2[n] = ypos2[n+1] + xspeed;
 ypos2[n] = constrain(ypos2[n], 0, 380);

}

and without the xspeed, and it just stopped it moving in that direction in general so I'm not sure.

I would like to know if I use xpos and ypos, and if so how? if it's in the main draw bit I'm not sure how to do it as just putting snake[i+1] just results in the sonic effect. Sorry for asking again, these troublesome arrays!
Page Index Toggle Pages: 1