Skip to content
Newer
Older
100644 59 lines (48 sloc) 1.25 KB
fd407cf @kthakore Example for game loop chapter
kthakore authored Sep 29, 2010
1 use strict;
2 use warnings;
3
4 use SDL;
5 use SDL::Video;
6 use SDL::Event;
7 use SDL::Events;
8
9 use SDLx::App;
10 use SDLx::Controller::Interface;
11 my $app = SDLx::App->new( w => 512, h => 512, dt => 0.02, flags => SDL_HWSURFACE|SDL_DOUBLEBUF );
12 my $ball =
13 SDLx::Controller::Interface->new( x => 10, h => 10, v_x => 150, v_y => 150 );
14
15 $app->add_event_handler( sub { return 0 if $_[0]->type == SDL_QUIT; return 1 }
16 );
17
18 $ball->set_acceleration(
19 sub {
20 my ( $time, $s ) = @_;
21 if ( $s->x >= $app->w - 10 ) {
22 $s->x( $app->w - 11 );
23 $s->v_x( -1 * $s->v_x );
24 }
25 elsif ( $s->x <= 0 ) {
26 $s->x(11);
27 $s->v_x( -1 * $s->v_x );
28 }
29
30 if ( $s->y >= $app->h - 10 ) {
31 $s->y( $app->h - 11 );
32 $s->v_y( $s->v_y * -0.9 );
33
34 }
35 elsif ( $s->y <= 0 ) {
36 $s->y(11);
37 $s->v_y( $s->v_y * -0.9 );
38
39 }
40 return ( 0, 0, 0 );
41 }
42 );
43
44 my $previous = [0,0,0,0];
45 $ball->attach(
46 $app,
47 sub {
48 $app->draw_rect([0,0,$app->w, $app->h], 0 );
49 my $current = [ $_[0]->x, $_[0]->y, 10, 10 ];
50 $app->draw_rect( $current, 0xFF0000FF );
51 $app->update( $current );
52 $app->update( $previous );
53 $previous = $current;
54 }
55 );
56
57 $app->run();
58
Something went wrong with that request. Please try again.