This time I struggled a lot with even coming up with what to create for the homework assignment and I just couldn't figure out what to do so I thought of something for which I thought I would be needing at least 3 arrays however I ended up only using one, I tried to alter the project to have more arrays but ended up having even more trouble and thus I was not able to finish in time, regretfully. This is what I could come up with even though it sadly does not meet the requirments. I tried to create multiple different types of ojects that could bounce and f they hit eacother they woud bounce off but I was not succesful.
I wanted to make balls bounce in the screen and I eventually figured out that the best way to do that was by using multiple functions for the balls to create the bouncing effect. I wanted the balls to be at different locations as well so I randomized this.
Video:
Code:
Tab 1:
//Declaring
ball [] bouncingBalls = new ball[20];
square [] bouncingSquares = new square[20];
void setup() {
size(800, 800);
//creating the actual balls
for( int i = 0 ; i < 20; i++){
bouncingBalls[i] = new ball(random(0, width), random(0, 200));
}
for( int i = 0 ; i < 20; i++){
bouncingSquares[i] = new square(random(0, width), random(0, 200));
}
}
void draw() {
background(0);
//The actual bouncing
for( int i = 0 ; i < 20; i++){
bouncingBalls[i].run();
}
for( int i = 0 ; i < 20; i++){
bouncingSquares[i].run();
}
}
Tab 2:
class ball {
float x = 0;
float y = 0;
float speedX = 4;
float speedY = 0.5;
ball(float _x, float _y) {
x = _x;
y = _y;
}
void run() {
display();
move();
bounce();
gravity();
}
void gravity() {
speedY += 0.2;
}
void bounce() {
if(x > width) {
speedX = speedX * -1;
}
if(x < 0) {
speedX = speedX * -1;
}
if(y > height) {
speedY = speedY * -1;
}
if(y < 0) {
speedY = speedY * -1;
}
}
void move() {
x += speedX;
y += speedY;
}
void display() {
ellipse(x, y, 30, 30);
}
}
Tab 3:
class square {
float x = 0;
float y = 0;
float speedX = 4;
float speedY = 0.5;
square(float _x, float _y) {
x = _x;
y = _y;
}
void run() {
display();
move();
bounce();
gravity();
}
void gravity() {
speedY += 0.2;
}
void bounce() {
if(x > width) {
speedX = speedX * -1;
}
if(x < 0) {
speedX = speedX * -1;
}
if(y > height) {
speedY = speedY * -1;
}
if(y < 0) {
speedY = speedY * -1;
}
}
void move() {
x += speedX;
y += speedY;
}
void display() {
rect(x, y, 10, 10);
}
}
I wanted to make balls bounce in the screen and I eventually figured out that the best way to do that was by using multiple functions for the balls to create the bouncing effect. I wanted the balls to be at different locations as well so I randomized this.
Video:
Code:
Tab 1:
//Declaring
ball [] bouncingBalls = new ball[20];
square [] bouncingSquares = new square[20];
void setup() {
size(800, 800);
//creating the actual balls
for( int i = 0 ; i < 20; i++){
bouncingBalls[i] = new ball(random(0, width), random(0, 200));
}
for( int i = 0 ; i < 20; i++){
bouncingSquares[i] = new square(random(0, width), random(0, 200));
}
}
void draw() {
background(0);
//The actual bouncing
for( int i = 0 ; i < 20; i++){
bouncingBalls[i].run();
}
for( int i = 0 ; i < 20; i++){
bouncingSquares[i].run();
}
}
Tab 2:
class ball {
float x = 0;
float y = 0;
float speedX = 4;
float speedY = 0.5;
ball(float _x, float _y) {
x = _x;
y = _y;
}
void run() {
display();
move();
bounce();
gravity();
}
void gravity() {
speedY += 0.2;
}
void bounce() {
if(x > width) {
speedX = speedX * -1;
}
if(x < 0) {
speedX = speedX * -1;
}
if(y > height) {
speedY = speedY * -1;
}
if(y < 0) {
speedY = speedY * -1;
}
}
void move() {
x += speedX;
y += speedY;
}
void display() {
ellipse(x, y, 30, 30);
}
}
Tab 3:
class square {
float x = 0;
float y = 0;
float speedX = 4;
float speedY = 0.5;
square(float _x, float _y) {
x = _x;
y = _y;
}
void run() {
display();
move();
bounce();
gravity();
}
void gravity() {
speedY += 0.2;
}
void bounce() {
if(x > width) {
speedX = speedX * -1;
}
if(x < 0) {
speedX = speedX * -1;
}
if(y > height) {
speedY = speedY * -1;
}
if(y < 0) {
speedY = speedY * -1;
}
}
void move() {
x += speedX;
y += speedY;
}
void display() {
rect(x, y, 10, 10);
}
}
Comments
Post a Comment