Pumpkin Patch

example processing input 🎃

This code takes user input to add a pumpkin to the pumpkin patch whenever the user clicks the mouse.

float previousX;
float previousY;
float previousHeight;

void setup() {
  size(400, 300);

void draw() {
  //needed for mousePressed() to work

void mousePressed() {

  //create next pumpkin
  float nextX = mouseX;
  float nextY = mouseY;
  float nextWidth = random(50, 100);
  float nextHeight = random(25, 75);

  //draw next pumpkin
  fill(random(200, 256), random(75, 125), 0);
  stroke(random(100, 140), random(40, 80), 0);
  ellipse(nextX, nextY, nextWidth, nextHeight);
  ellipse(nextX, nextY, nextWidth*.75, nextHeight);
  ellipse(nextX, nextY, nextWidth*.5, nextHeight);
  ellipse(nextX, nextY, nextWidth*.25, nextHeight);

  //connect next pumpkin to previous pumpkin
  stroke(0, random(50, 200), 0, 100);
  strokeWeight(random(5, 15));

  bezier(previousX, previousY - previousHeight/2, 
    previousX + random(-100, 100), previousY - previousHeight*2, 
    nextX + random(-100, 100), nextY - nextHeight*2, 
    nextX, nextY - nextHeight/2);

  //set the previous pumpkin to the next pumpkin
  previousX = nextX;
  previousY = nextY;
  previousHeight = nextHeight;

pumpkin patch

Code Editor

See the Pen by Happy Coding (@KevinWorkman) on CodePen.

This code uses the bezier() function to draw curves that connect the pumpkins like vines. The bezier() function takes 8 parameters representing 4 points, which form a curve using the pattern in this image:

You can read more about the bezier() function in the reference.

Tweak Ideas

Go back up