Recitation 10 Workshops
In this week’s recitation, I attended two workshops, which are map() function workshop and OOP workshop
For the map() function, it is pretty straightforward.
map(Variable name, lower bound of domain, upper bound of domain, lower bound of codomain, upper bound of codomain), which accept a variable name and 4 float data as range.
For the OOP part, a definition of class starts with
class (class name){
}
Then we define local variables within the class, and write an initialize funtion within the class, whose name is same as the class itself. It goes like:
class (test){
float x = 0;
float y = 0;
test(parameters){
……
}
}
When creating a new instance of class, we use
test instance = new test(parameters);
In my final project, I wrote a class of bullets in my game since all the bullets are similar objects and can be put together.
class Balls { float x_pos; float y_pos; float vy = 10; PImage bullet; Balls (int n,int m,int p,float x,float y){ x_pos = (x-50*width/height) + ((m+1)*100*width/height)/(n+1); y_pos = y; switch (n){ case 1: switch(p){ case 1: bullet = loadImage("bullet1.png"); break; case 2: bullet = loadImage("bullet3.png"); break; } break; case 2: switch(p){ case 1: bullet = loadImage("bullet2.png"); break; case 2: bullet = loadImage("bullet4.png"); break; } break; case 3: switch(p){ case 1: bullet = loadImage("bullet2.png"); break; case 2: bullet = loadImage("bullet4.png"); break; }; default: bullet = loadImage("bullet4.png"); break; } } void update(){ //fill(255); //ellipse(x_pos,y_pos,8*width/1000,8*width/1000); image(bullet,x_pos,y_pos,25*width/1000,25*width/1000); y_pos -= vy * 0.9; } }