Skip to content

Instantly share code, notes, and snippets.

@joseph-montanez
Created May 28, 2011 22:28
Show Gist options
  • Save joseph-montanez/997293 to your computer and use it in GitHub Desktop.
Save joseph-montanez/997293 to your computer and use it in GitHub Desktop.
Sample Bouncing Ball
public class Ball : Darkcore.Sprite {
public double velocity_x;
public double velocity_y;
public double acceleration {
get; set; default = 0.010;
}
public Ball (ref Darkcore.Engine engine) {
base.from_file (engine, "resources/panda-ball.png");
this.width = 64.00;
this.height = 64.00;
this.x = 100.00;
this.y = 100.00;
this.velocity_x = 2.00;
this.velocity_y = 2.00;
this.on_render = (engine, ball) => {
var half_height = height / 2.00;
var half_width = width / 2.00;
if (y + half_height + velocity_y >= engine.height) {
velocity_y = (Math.fabs(velocity_y) + acceleration) * -1.00;
}
if (y - half_height - velocity_y <= 0) {
velocity_y = (Math.fabs(velocity_y) + acceleration);
}
if (x + (width / 2) + velocity_x >= engine.width) {
velocity_x = (Math.fabs(velocity_x) + acceleration) * -1.00;
}
if (x - (width / 2) - velocity_x <= 0) {
velocity_x = (Math.fabs(velocity_x) + acceleration);
}
b
x += velocity_x;
y += velocity_y;
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment