Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

More Pong in SDL

by drose2211 (Novice)
on Dec 03, 2017 at 19:19 UTC ( #1204815=perlquestion: print w/replies, xml ) Need Help??
drose2211 has asked for the wisdom of the Perl Monks concerning the following question:

Been following the tutorial on how to make pong in SDL from the SDL manual. Have run into a few issues and have run into another. In the block that allows for paddle movement I have gotten Errors saying that I have not declared $event. even though It was declared a few lines prior. Just wondering what I am doing wrong. UPDATE......Nevermind I figured it out.

#!/usr/bin/perl use strict; use warnings; # These will allow us to use all the modules necessary to create a GUI +. # They are all parts of the Perl SDL module. # The :: are use to seperate the name of the module and its correspond +ing member. use SDL; use SDL::Events; use SDLx::App; use SDLX::Rect; # First is to set up the screen. # The => operator is similiar to a comma. # but it treates the word to the left as a quoted word. # Even though there isn't any quotes. my $app = SDLx::App->new( width => 1000, height => 1000, title => 'PONG', dt => 0.02, exit_on_quit => 1, ); # The next step is to set up the paddles for each player. # This is done in a hash reference. # This is done so its possible to add information later. # The numbers may need to be changed later to fit the paddles to the s +creen. # I am not 100% sure what -> actually does. # Other than the fact its a deference operator. # h is a shortened reference to height my $player1 = { paddle => SDLx::Rect->new( 10, $app->h / 2, 10, 40), }; my $player2 = { paddel => SDLx::Rect->new( $app->w - 20, $app->h / 2, 10, 40) }; # This is to create the ball my $ball = { rect => SDLx::Rect->new( $app->w / 2, $app->h / 2, 10, 10 ), }; # This is a show handler. # This will render the objects to the screen. $app->add_show_handler ( sub { $app->draw_rect( [ 0, 0, $app->w, $app->h ], 0x000000FF ); $app->draw_rect( $ball->{rect}, 0xFF0000FF ); $app->draw_rect( $player1->{paddle}, 0xFF0000FF ); $app->draw_rect( $player2->{paddle}, 0xFF0000FF ); $app->update; } ); # This section will allow the paddles to be moved. $player1 = { paddle => SDLx::Rect->new( 10, $app->h / 2, 10, 40 ), v_y => 0, }; $app->add_move_handler( sub { my ( $step, $app ) = @_; my $paddle = $player1->{paddle}; my $v_y = $player1->{v_y}; $paddle->( $paddle->y($v_y * $step )); $player2 = { paddle => SDLx::Rect->new( $app->w - 20, $app->h / 2, 10, 40), v_y => 0, }; $app->add_move_handler( sub { my ( $step, $app ) = @_; my $paddle = $player2->{paddle}; my $v_y = $player2->{v_y}; $paddle->y( $paddle->y ( $v_y * $step ) ); }); $app->add_event_handler( sub { my ( $event, $app ) = @_; if ( $event->type == SDL_KEYDOWN ) { if ( $event->key_sym == SDLK_UP) { $player1->{v_y} = -2; } $player1->{v_y} = 2; } } elsif ( $event->type == SDL_KEYUP) { if ( $event->key_sym == SDLK_UP or $event->key_sym == SDLK_DOWN ) { $player1->{v_y} = 0; } } ); $app->run;

Replies are listed 'Best First'.
Re: More Pong in SDL
by NetWallah (Canon) on Dec 03, 2017 at 19:41 UTC
    You should be getting this error:
    syntax error at YourCode.pl line xx, near "elsif"
    because your sub{} encloses the declaration of "my $event".

    Most likely, you have a misplaced "}".

                    All power corrupts, but we need electricity.

Re: More Pong in SDL
by tybalt89 (Curate) on Dec 03, 2017 at 19:39 UTC

    The brace above "elsif" should be farther down.

Re: More Pong in SDL
by poj (Monsignor) on Dec 03, 2017 at 21:31 UTC

    I think there is an missing + in the SDL_manual, in the move handlers it should be

    $paddle->( $paddle->y + ($v_y * $step ) );

    If you get stuck, here is working code up to the point you are at now

    #!/usr/bin/perl use strict; use warnings; # These will allow us to use all the modules necessary to create a GUI +. # They are all parts of the Perl SDL module. # The :: are use to seperate the name of the module and its correspond +ing member. use SDL; use SDL::Events; use SDLx::App; use SDLX::Rect; # First is to set up the screen. # The => operator is similiar to a comma. # but it treates the word to the left as a quoted word. # Even though there isn't any quotes. my $app = SDLx::App->new( width => 500, height => 500, title => 'PONG', dt => 0.02, exit_on_quit => 1, ); # The next step is to set up the paddles for each player. # This is done in a hash reference. # This is done so its possible to add information later. # The numbers may need to be changed later to fit the paddles to the s +creen. # I am not 100% sure what -> actually does. # Other than the fact its a deference operator. # h is a shortened reference to height my $player1 = { paddle => SDLx::Rect->new( 10, $app->h / 2, 10, 40), v_y => 0, score => 0, }; my $player2 = { paddle => SDLx::Rect->new( $app->w - 20, $app->h / 2, 10, 40), v_y => 0, score => 0, }; # This is to create the ball my $ball = { rect => SDLx::Rect->new( $app->w / 2, $app->h / 2, 10, 10 ), v_x => -2.7, v_y => 1.8, }; # This is a show handler. # This will render the objects to the screen. $app->add_show_handler ( sub { $app->draw_rect( [ 0, 0, $app->w, $app->h ], 0x000000FF ); $app->draw_rect( $ball->{rect}, 0xFF0000FF ); $app->draw_rect( $player1->{paddle}, 0xFF0000FF ); $app->draw_rect( $player2->{paddle}, 0xFF0000FF ); $app->update; } ); $app->add_move_handler( sub { my ( $step, $app ) = @_; my $paddle = $player1->{paddle}; my $v_y = $player1->{v_y}; $paddle->y( $paddle->y + ($v_y * $step) ); }); $app->add_move_handler( sub { my ( $step, $app ) = @_; my $paddle = $player2->{paddle}; my $v_y = $player2->{v_y}; $paddle->y( $paddle->y + ($v_y * $step) ); }); $app->add_move_handler( sub { my ( $step, $app ) = @_; my $ball_rect = $ball->{rect}; $ball_rect->x( $ball_rect->x + ($ball->{v_x} * $step) ); $ball_rect->y( $ball_rect->y + ($ball->{v_y} * $step) ); if ( $ball_rect->bottom >= $app->h ) { $ball_rect->bottom( $app->h ); $ball->{v_y} *= -1; } elsif ( $ball_rect->top <= 0 ) { $ball_rect->top( 0 ); $ball->{v_y} *= -1; } elsif ( $ball_rect->right >= $app->w ) { $player1->{score}++; $ball->{v_x} *= -1; #reset_game(); #return; } elsif ( $ball_rect->left <= 0 ) { $player2->{score}++; $ball->{v_x} *= -1; #reset_game(); #return; } }); $app->add_event_handler( sub { my ( $event, $app ) = @_; if ( $event->type == SDL_KEYDOWN ) { if ( $event->key_sym == SDLK_UP) { $player1->{v_y} = -2; } elsif ( $event->key_sym == SDLK_DOWN ){ $player1->{v_y} = 2; } } elsif ( $event->type == SDL_KEYUP) { if ( $event->key_sym == SDLK_UP or $event->key_sym == SDLK_DOWN ) { $player1->{v_y} = 0; } } } ); $app->run;
    poj

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1204815]
Approved by davies
Front-paged by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2018-01-23 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How did you see in the new year?










    Results (240 votes). Check out past polls.

    Notices?