Permalink
Browse files

Make square bounce against walls

  • Loading branch information...
1 parent 967dc62 commit 626311c8a78ba4f66362cae9f2868289045716f5 @jtpalmer committed Aug 21, 2011
Showing with 20 additions and 2 deletions.
  1. +20 −2 examples/test.pl
View
@@ -19,6 +19,8 @@
y => 0,
w => 25,
h => 25,
+ vx => 1,
+ vy => 1,
};
$controller->add_event_handler(
@@ -47,8 +49,24 @@
$controller->add_move_handler(
sub {
- $rect->{x} += 1;
- $rect->{y} += 1;
+ $rect->{x} += $rect->{vx};
+ $rect->{y} += $rect->{vy};
+
+ if ($rect->{vx} < 0 && $rect->{x} < 0) {
+ $rect->{vx} *= -1;
+ }
+
+ if ($rect->{vy} < 0 && $rect->{y} < 0) {
+ $rect->{vy} *= -1;
+ }
+
+ if ($rect->{vx} > 0 && $rect->{x} > $app->w - $rect->{w}) {
+ $rect->{vx} *= -1;
+ }
+
+ if ($rect->{vy} > 0 && $rect->{y} > $app->h - $rect->{h}) {
+ $rect->{vy} *= -1;
+ }
}
);

0 comments on commit 626311c

Please sign in to comment.