Processing Downloads/Documentation:
- Insertion: https://www.youtube.com/watch?v=ROalU379l3U
- Selection: https://www.youtube.com/watch?v=Ns4TPTC8whw
Sample code (Base code for the moving shapes):
int person1x,person1y;
int person2x,person2y;
//Parameters: x and y only exist inside of your stickman function.
//They are local variables, and can only be used inside stickman
void stickman(int x, int y) {
stroke(255);
fill(255);
ellipse(x, y, 200, 200);
fill(255,0,0);
ellipse(x + 50, y - 30, 40, 40);
fill(#0000FF);
ellipse(x - 50, y - 30, 40, 40);
line(x, y + 100, x, y + 250);
line(x-50, y+150, x+50, y+150);
}
void setup(){
//initialize the variables in setup
person1x = 130;
person1y = 140;
person2x = 400;
person2y = 100;
size(800, 600);
stickman(person1x, person1y);
stickman(person2x, person2y);
}
void draw(){
background(0);
stickman(person1x, person1y);
stickman(person2x, person2y);
stickman(mouseX,mouseY);
//change the variables that you use to position the shapes by
//a small amount in each draw and it will move on the screen:
person1x = person1x + 1;
person1y = person1y + 1;
person2y = person2y + 1;
}
Sample code (if , if/else, if/else if/... ):
void setup() {
size(1000, 800);
}
void draw() {
background(0);
solution1(); //solutions 1,2, and 3 are equivalent
}
void solution1() {
textSize(40);
if (mouseX < width/2) {
if (mouseY < height/2) {
text("Top Left", 50, 50);
} else {
text("Bottom Left", 50, 50);
}
} else {
if (mouseY < height/2) {
text("Top Right", 50, 50);
} else {
text("Bottom Right", 50, 50);
}
}
}
void solution2() {
textSize(40);
if (mouseX < width/2 && mouseY < height/2) { // && means AND , || means OR
text("Top Left", 50, 50);
}
if (mouseX < width/2 && mouseY >= height/2) {
text("Bottom Left", 50, 50);
}
if (mouseX >= width/2 && mouseY < height/2) {
text("Top Right", 50, 50);
}
if (mouseX >= width/2 && mouseY > height/2) {
text("Bottom Right", 50, 50);
}
}
void solution3() {
textSize(40);
if (mouseX < width/2 && mouseY < height/2) {
text("Top Left", 50, 50);
}else if (mouseX < width/2) {
text("Bottom Left", 50, 50);
}else if (mouseY < height/2) {
text("Top Right", 50, 50);
}else {
text("Bottom Right", 50, 50);
}
}
Sample code to trace through for loop practice:
//trace through and see if you can figure out
//what will print at the end
int size = 0;
for(int i = 1; i < 2000; i = i *2){
if(i < 300){
size = size + 10;
}else if(size < 700){
size = size + 100;
}else{
size = size + 1000;
}
}
println(i);Sample code (Robot Army):
void android(int x, int y) {color c = color(164, 199, 67);
fill(c);
stroke(c);
rect(x,y,80,60);
}
void androidRow(int x, int y){
for (int j = 0; j < 1000; j=j+100) {
android(x + j, y);
}
}
void androidArmy(int x,int y){
for (int i = 0; i < 500; i=i+100) {
androidRow(x, y+i);
}
}
Sample code (Simple ball bouncing):
float x, y, dx, dy, radius;
float R, G, B;
void setup() {
size(1000, 800);
x = 100.0;
y = 200.0;
radius = random(30, 100);
dx = random(-5, 5);
dy = random(-5, 5);
R = random(255);
G = random(255);
B = random(255);
}
void draw() {
background(255);
display();
bounce();
move();
}
void move() {
x = x + dx; //change x's value
y = y + dy;
}
void display() {
fill(R, G, B);
ellipse(x, y, 2*radius, 2*radius);
}
void bounce() {
// if statements
if (x < radius || x >= width - radius) {
dx = -dx;
}
if (y < radius || y >= height - radius) {
dy = -dy;
}
}
}
To add to the Ball Class:
/*
* return true if the ball running this ball is
* touching the other ball.
*/
boolean isTouching(Ball other){
}
/*
* If the current ball is MOVING, check to see if it is
* touching another ball in the ArrayList.
* If the ball isTouching() a GROWING or SHRINKING ball
* change the state of the ball to GROWING.
*/
void checkCollision(ArrayList<Ball> allBalls){
}
Base Code for Chain Reaction:
//CONSTANTS AND GLOBAL VARIABLES/**************************
*Fill in the functions:
*setup
*processAllBalls
*checkAllCollisions
*startReaction
***************************/
void setup() {
//initialize all variables and the screen here
}
void processAllBalls() {
/**************
*1. draw moving/growing/shrinking only
*2. Make the balls do the action that matches their state:
* GROWING / SHRINKING / MOVING
* (the balls that are in a DEAD state don't have an action)
*****************/
}
void checkAllCollisions() {
/**************
*1. loop through all balls in the ArrayList
*2. have each MOVING Ball run the checkCollision(ArrayList<Ball>)function
*3. if they are colliding, change the state appropriately
*****************/
}
void startReaction() {
/**************
*1. create a ball at mouseX/mouseY
*2. make it have a GROWING state
*3. add it to the ArrayList
*****************/
}
/**************************
*Leave these functions alone:
*draw
*mouseCLicked
*keyPressed
***************************/
void draw() {
processAllBalls();
checkForCollisions();
}
void mouseClicked() {
startReaction();
}
void keyPressed() {
setup();
}
This comment has been removed by a blog administrator.
ReplyDelete