Recitation10 Object Oriented Programming by Hangkai Qian

To be honest,  I didn’t very understand OOP when I attended the lecture class though I have worked a long time on the example of Ball in the class, I still didn’t figure it out. So I attended the OOP class. In the class, I knew that the class has variables, constructor and functions. So I used them in my final processing game part: there is a helicopter flying up, and there are pillars worked as obstacles. I had very difficulties in building these blocks, because I couldn’t come up with a way to generate infinite blocks. Finally, referring to the example in the lecture class, I finally worked it out using ArrayList and add new  items every time and use for loop and i<p.size to generate different pillars.

Here is my code:

ArrayList<pillar> p= new ArrayList<pillar>();

void setup(){
size(350,800);

}

void draw(){

int m=millis();
stroke(40);

h.drawheli();
h.moveX();

for (int i = 0; i<p.size(); i++) {
p.get(i).drawPillar();
p.get(i).movePillar();

if (p.get(i).yPos==h.y) {
if (h.x>p.get(i).xPos && h.x<p.get(i).xPos+p.get(i).xh) {
end=true;
} else {
end=false;
}
}
}

if (m%1000<15) {
p.add(new pillar());
}
}

}

class pillar {
float xPos=0;
float yPos=0;
float xh;
pillar() {
xh=round(random(400));
yPos=0;
xPos=random(1400/3.7);
}
void drawPillar() {
stroke(252,0,0);
strokeWeight(15);
line(xPos, yPos, xPos+xh, yPos);

}
void movePillar(){
yPos=yPos+10;
}

}

Here is the video.

P.S. Above is only the code of the pillar. However in my video I included not only pillars

Leave a Reply