Task 1
void setup() {
size(1000,1000); } void draw() { background(255); fill(255); line(0,height/2,width,height/2); line(width/2,0,width/2,height); if(mouseX <= width/2 && mouseY <= height/2){ fill(0); } rect(0,0,width/2,height/2); fill(255); if(mouseX >= width/2 && mouseY <= height/2){ fill(0); } rect(width/2,0,width/2,height/2); fill(255); if(mouseX <= width/2 && mouseY >= height/2){ fill(0); } rect(0,height/2,width/2,height/2); fill(255); if(mouseX >= width/2 && mouseY >= height/2){ fill(0); } rect(width/2,height/2,width/2,height/2); } Task 2 |
boolean button = false;
int x = 50; int y = 50; int w = 100; int h = 75; void setup() { size(200,200); } void draw() { if (button) { background(255); stroke(0); } else { background(0); stroke(255); } fill(175); rect(x,y,w,h); } void mousePressed() { if (mouseX > x && mouseX < x + w && mouseY > y && mouseY < y + h && mousePressed && button == false) { button = true; } else if (mouseX > x && mouseX < x + w && mouseY > y && mouseY < y + h && mousePressed && button == true) { button = false; } } Task 3
|
float x = 0;
float y = height/2; float xSpeed = 1; float ySpeed = 1; float colour1 = 0; float colour2 = 0; float colour3 = 0; float size = 32; void setup() { size(200,200); smooth(); } void draw() { background(255); colour1 = xSpeed * 100; colour2 = x; colour3 = y; size = y/5; y = y + ySpeed; x = x + xSpeed; if ((x > width) || (x < 0)) { xSpeed = xSpeed * -1; } if ((y > height) || (y < 0)) { ySpeed = ySpeed * -1; } // Display circle at x location stroke(0); fill(colour1,colour2,colour3); ellipse(x,y,size,size); } void mousePressed() { ySpeed += 1; xSpeed += 1; } Task 3
|
float x = 0;
float x2 = 400; float xSpeed = 1; float x2Speed = -1; float colour1 = 0; float colour2 = 255; void setup() { size(400,400); } void draw() { background(255,0,0); colour1 = 0; colour2 = 255; x2 = x2 + x2Speed; x = x + xSpeed; if ((x > width) || (x < 0)) { xSpeed = xSpeed * -1; } if ((x2 > width) || (x2 < 0)) { x2Speed = x2Speed * -1; } fill(colour1); rect(400,0,-x,height); fill(colour2); rect(0,0,x2,height); } Task 3
|
float x = 0;
float y = 0; float xSpeed = 5; float ySpeed =0; float accelleration = 5; int size = 20; void setup() { size(500,500); } void draw() { background(255); x = constrain(x, 0, width-size); y = constrain(y, 0, height-size); y = y + ySpeed; x = x + xSpeed; if(x >= width-size && y <= 0) { ySpeed = accelleration; xSpeed = 0; } if(y <= 0 && x < width-size) { ySpeed = 0; xSpeed = accelleration; } if(x <= 0 && y >= height-size) { ySpeed = -accelleration; xSpeed = 0; } if(y >= height-size && x >= width-size) { ySpeed = 0; xSpeed = -accelleration; } if( x <= 0 && y <= 0){ accelleration += 4; } fill(255,0,0); rect(x,y,size,size); } Assignment |
boolean light = false;
int level = 0; int size = 10; float colourOn = 255; float colourOff = 0; void setup() { size(1000,1000); } void draw() { background(0); fill(255); text("Click the red square to turn the light on",width/1.5,100); text("Click the white square to increase the intensity of the light",width/2.5,height/1.3); if(light == true){ fill(255,255,0,level+50); ellipse(width/2,height/2,50,50); fill(255,255,0,level+25); ellipse(width/2,height/2,75,75); fill(255,255,0,level); ellipse(width/2,height/2,100,100); fill(255,255,0,level-25); ellipse(width/2,height/2,125,125); fill(255,255,0,level-50); ellipse(width/2,height/2,150,150); fill(255,255,0,level-75); ellipse(width/2,height/2,175,175); fill(255,255,0,level-100); ellipse(width/2,height/2,200,200); fill(255,255,0,level-125); ellipse(width/2,height/2,225,225); fill(255,255,0,level-150); ellipse(width/2,height/2,250,250); fill(255,255,0,level-175); ellipse(width/2,height/2,275,275); fill(255,255,0,level-200); ellipse(width/2,height/2,300,300); fill(255,255,0,level-225); ellipse(width/2,height/2,325,325); fill(255,255,0,level-250); ellipse(width/2,height/2,350,350); fill(255,255,0,level-275); ellipse(width/2,height/2,375,375); } fill(255); rectMode(CENTER); rect(height/2 ,width/2,size,size); fill(colourOn,colourOff,0); rect(width-10,10,20,20); } void mousePressed() { if(mouseX >= width/2-size/2 && mouseX <= width/2+size/2 && mouseY >= height/2 -size/2 && mouseY <= height/2+size/2 && light == true){ level += 25;} if(mouseX >= width-20 && mouseX <= width && mouseY >= 0 && mouseY <= 20){ light = !light; if(colourOff >= 255){ colourOn =255; colourOff = 0;} else{ colourOff =255; colourOn = 0; level = 0;} } } |