Here I used the code from the class where we made lots of colourful spheres coming out, and I replaced the spheres to alphabets. The result looks like below.
The codings for this are below.
ArrayList<Alphabet> alphabets;
void setup() {
size(800, 600);
//noStroke();
alphabets = new ArrayList<Alphabet>();
alphabets.add(new Alphabet());
background(255);
}
void draw() {
background(255);
alphabets.add(new Alphabet());
for (Alphabet a : alphabets) {
a.update();
a.render();
a.bounds();
}
if(alphabets.size() > 100 ){
alphabets.remove(0);
}
}
class Alphabet {
//variables for the object
float xPos;
float yPos;
float xSpeed;
float ySpeed;
float diam;
color c;
String[] alphas = {"a", "b", "c", "d", "e", "f", "g" , "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
int index = int(random(alphas.length));
Alphabet() {
//
c = color(random(0, 255), random(0, 255), random(0, 255));
textSize(24);
xPos = mouseX;
yPos = mouseY;
xSpeed = random(-3., 5.);
ySpeed = random(-3., 5.);
diam = 100;
}
Alphabet(float x_, float y_, float d_) {
// this is the constructor
c = color(random(0, 255), random(0, 255), random(0, 255));
xPos = x_;
yPos = y_;
xSpeed = random(-3., 5.);
ySpeed = random(-3., 5.);
diam = d_;
}
//declare all my functionsa
void render() {
fill(c);
text(alphas[index], xPos, yPos);
}
void update() {
xPos += xSpeed;
yPos += ySpeed;
}
void bounds() {
if (xPos >= width || xPos <= 0) {
xSpeed = xSpeed *-1 ;
}
if (yPos >= height || yPos <= 0) {
ySpeed = ySpeed *-1 ;
}
}
}
The problem I faced making this homework is that at first I didn’t know how to make the alphabets coming out randomly. However, I figured out quickly when I searched it online and it was actually pretty easy (just have to remember how to do it).
