Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Processing 3 - Python mode Create an apple tree with 4 – 5 apples spread around

ID: 3876804 • Letter: P

Question

Processing 3 - Python mode

Create an apple tree with 4 – 5 apples spread around the tree

. Use conditional statements and the mousePressed function to make the apples fall to the ground when the tree trunk is clicked.

Tree and apples appear in the foreground (in front of the horizon made by the ground). use a rect to make the ground.

Clicking the mouse makes the apples fall.

ONLY clicking the trunk (no other section of the drawing) makes the apples fall.

Clicking the trunk again makes the apples reappear in the tree.

The apples stop falling once they reach the ground (they do not fall all the way off the sketch window

Here is how it should look:

Explanation / Answer

void setup()

{
size(500, 500);

listOfTrees = new ArrayList(0);
}

void draw()
{
background(color(0, 0, 0));
int i;

for (i = 0; i < listOfTrees.size(); i++)
{
tree temp = (tree) listOfTrees.get(i);
temp.drawTree();
}
}

void mousePressed()
{
float k = round(random(100,400));
tree newTree = new tree(k,k,k);
listOfTrees.add(newTree);
}

void keyPressed()
{
int i;
boolean found = false;

switch(key)
{
case 'r':
listOfTrees.remove(listOfTrees.size() - 1);
break;
case 'h':
for (i = listOfTrees.size()-1; i >= 0 && !found; i--)
{
tree temp = (tree) listOfTrees.get(i);
if (temp.amIvisible)
{
tree temp2 = new tree(temp.x, temp.y, temp.x2);
temp2.amIvisible = false;
listOfTrees.set(i, temp2 );
found = true;
}
}
break;
case 's':
for (i = listOfTrees.size()-1; i >= 0 && !found; i--)
{
tree temp = (tree) listOfTrees.get(i);
if (!temp.amIvisible) {
tree temp2 = new tree(temp.x, temp.y, temp.x2);
temp2.amIvisible = true;
listOfTrees.set(i, temp2 );
found = true;
}
}
break;
}
}

class tree
{
boolean amIvisible;
float x, y, x2;

tree(float _x, float _y, float _x2) {
x = _x;
y = _y;
x2 = _x2;
amIvisible = true;
}

void drawTree()
{
if (amIvisible)
{
strokeWeight(20);
stroke(39,7,7);
line(x,y, x2,height);

noStroke();
fill(0,255,0);
ellipse(x, y, 200,200);
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote