image.png
// angry birds
class Bird {
float x, y, xSpeed, ySpeed, rotation;
boolean stopped = false;
Bird(float xx, float yy) {
x = xx;
y =yy;
xSpeed = (mouseX - x) /50.0;
ySpeed = (mouseY -y) /50.0;
}
void update() {
x += xSpeed;
y += ySpeed;
ySpeed += GRAV;
if(y > 300) {
xSpeed = ySpeed = 0;
stopped = true;
} else {
rotation += ROTATE;
}
}
void display() {
translate(x,y);
image(redImg, 0, 0, 64, 64);
resetMatrix();
}
}
String redUrl = "https://vignette.wikia.nocookie.net/p__/images/e/e2/Red_bird.png/revision/latest?cb=20150530184407&path-prefix=protagonist";
PImage redImg = loadImage(redUrl);
Bird red;
float GRAV = .05;
float ROTATE = 0.1;
void setup() {
size(600,400);
imageMode(CENTER);
red = new Bird(width/2, 300);
}
void draw() {
background(128, 196, 255);
fill(0,128,0);
noStroke();
rect(0, 300, width, 200);
red.update();
red.display();
drawTrajectory();
}
void mousePressed() {
red = new Bird(width/2, 300);
}
void drawTrajectory() {
Bird tb = new Bird(width/2, 300);
stroke(0, 128);
while(!tb.stopped) {
float oldX = tb.x;
float oldY = tb.y;
tb.update();
line(oldX, oldY, tb.x, tb.y);
}
noStroke();
fill(0, 64);
ellipse(tb.x, tb.y, 30, 30);
}
网友评论