What does append do within this code?

edited December 2017 in Questions about Code

Hello I am new here and not sure if this is the right place to ask but on line 45 and 47 what does "append" do? I was looking at sketches of different games made in processing and that is the only thing I do not understand. I searched google and found a page explaining it but with my limit knowledge of processing it blew right over my head.

//The arrays
BackDucks[] ducks = new BackDucks[0];
FrontDucks[] ducks2 = new FrontDucks[0];

//Ints
int score = 0;
int lives=3;
boolean GameOver = true;

//The images
PImage duck;
PImage smallduck;
PImage table;

void setup() {
size(1020, 720);

//Load the images
duck = loadImage ("duck.png");
smallduck = loadImage ("smallduck.png");
table = loadImage ("table.png");
}
//End of setup

void draw() {

if (!GameOver) {
background(125);

//If loop to add new ducks
if (frameCount%40==0) {
FrontDucks duck2 = new FrontDucks();
ducks2 = (FrontDucks[]) append(ducks2, duck2);
BackDucks duck = new BackDucks();
ducks = (BackDucks[]) append(ducks, duck);
}


//Moves the two duck rows
for (int i = 0; i<ducks.length; i++) {
ducks[i].update();
ducks2[i].update();
}
} else {
//fill(251,255,26);
fill(255);
textSize(26);
text("Remember to click the targets", width/2-175, height/2-300);
text("and don't let the ducks escape.", width/2-175, height/2-250);
text("Press any key to play", width/2-125, height/2-200);
if (keyPressed == true) {
reset();
}
}

//Check if backduck has left the table
for (int i = 0; i<ducks.length; i++) {
BackDucks duck = ducks[i];
if ((duck.x<-20)&&(duck.x>-23)) {
lives--;
}
}

//Check if frontduck has left the table
for (int i = 0; i<ducks.length; i++) {
FrontDucks duck2 = ducks2[i];
if ((duck2.x>width+duck2.w) && (duck2.x<width+duck2.w+3)) {
lives--;
}
}

/// Game Over
if (lives<0) { //lives get to 0 then there is game over
GameOver =true;
lives=0;
}

image(table, 0, 0);
//The score box
textSize(30);
fill(255);
stroke(0);
rect(440, 559, 150, 80);
fill(0);
text("Score:" + score, 457, 569+20);
text("Lives:" + lives, 457, 600+20);
}
// End of Draw


void mousePressed() {
for (int i = 0; i<ducks.length; i++) {
BackDucks duck = ducks[i];
if ((mouseX>duck.x) && (mouseX<duck.x + duck.w) && (mouseY>duck.y) && (mouseY<duck.y+duck.h)) {
duck.alive = false;
score++;
}
}
for (int i = 0; i<ducks2.length; i++) {
FrontDucks duck2 = ducks2[i];
if ((mouseX>duck2.x) && (mouseX<duck2.x + duck2.w) && (mouseY>duck2.y) && (mouseY<duck2.y+duck2.h)) {
duck2.alive = false;
score++;
}
}
}


class BackDucks {
float x;
float y;
float w;
float h;
float XX;
float YY;
float speed;
boolean alive;
BackDucks() {
y = 200;
w = 50;
x = width+30;
h = 50;
XX = -3;
YY = 0;
alive = true;
speed=1;
}


void update() {
x+=XX;
y+=YY*speed;
if (alive) {
fill(255, 0, 0);
stroke(255);
} else {
fill(0, 0, 0);
noStroke();
YY-=1;
XX=0;
x=-1000;
}
image(smallduck, x-55, y-20);
}
}


class FrontDucks {
float x;
float y;
float w;
float h;
float XX;
float YY;
boolean alive;
FrontDucks() {
y = 400;
w = 80;
x = 0-w-50;
h = 80;
XX = -3;
YY = 0;
alive = true;
}


void update() {
x-=XX;
y+=YY;
if (alive) {
fill(255, 0, 0);
stroke(255);
} else {
fill(0, 0, 0);
noStroke();
YY+=1;
XX=0;
x=-1000;
}
image(duck, x-50, y-40);
}
}


void reset() {
lives =3;
score=0;
GameOver = false;
ducks = (BackDucks[]) new BackDucks[0];
ducks2 = (FrontDucks[]) new FrontDucks[0];
}
Tagged:

Answers

  • Yeah it appends an item on an array, making it longer

    In this case we need to cast its type with the expression in ( ) as explained in the reference

  • append (list, thing)

    append to this list with a thing: list[0], list[1], list[2] ... thing

    append(ducks2, duck2);

    append to these ducks2(list) with this duck2(thing): ducks2[0], ducks2[1]... ducks2[x]

    (FrontDucks[]) ...

    I'm going to cast () the next thing I say to the type "a FrontDucks list" []

    (FrontDucks[]) append(ducks2, duck2);

    I'm going to cast something to a FrontDucks list: the ducks2 list with a duck2 appended to it.

  • //If loop to add new ducks

    i'm guessing that it adds new ducks.

  • edited December 2017

    code vandalised and restored.

    i've added Coder1137 to the list.

This discussion has been closed.