Skip to content

Instantly share code, notes, and snippets.

@kthakore
Created August 5, 2010 19:33
Show Gist options
  • Save kthakore/510260 to your computer and use it in GitHub Desktop.
Save kthakore/510260 to your computer and use it in GitHub Desktop.
package mario;
use Mouse;
use SDL;
use SDL::Event;
use SDL::Events;
use SDLx::App;
use SDLx::Sprite;
extends 'SDLx::Sprite';
has 'v_x' => (is => 'rw', isa => 'Num', default => 0);
has 'v_y' => (is => 'rw', isa => 'Num', default => 0 );
has 'speed' => (is => 'rw', isa => 'Int', default => 10 );
sub BUILD
{
my $self = shift;
$self->surface->draw_rect( [0,0,$self->w, $self->h], 0xFFFFFFFF);
$self->v_x(0.0); #DEFAULT is NOT WORKING .... -.-
$self->v_y(0.0);
$self->speed(1);
}
sub move
{
my ($self, $dt) = @_;
die 'THIS SHOULD NOT BE UNDEF' unless $self->v_x;
$self->x( $self->x + $dt * $self->v_x );
$self->y( $self->y + $dt * $self->v_y );
# if ( $self->v_y > 0 || $self->v_y < $self->h )
# {
# $self->y( $self->y + 0.05 );
# }
}
sub event_move
{
my ($self, $move) = @_;
#Do animation here.
if( $move =~ /^up/ ) { $self->v_y( - $self->speed ); }
if( $move =~ /^down/ ) { $self->v_y( $self->speed ); }
if( $move =~ /^right/ ) { $self->v_x( $self->speed ); }
if( $move =~ /^left/ ) { $self->v_x( -$self->speed ); }
}
package main;
use strict;
use warnings;
use SDL;
use SDL::Event;
use SDL::Events;
use SDLx::App;
use SDLx::Sprite;
my $app = SDLx::App->new( dt => 1 );
my $mario = mario->new(width => 20, height => 20);
$app->add_event_handler(
sub {
my $e = shift;
return 0 if $e->type == SDL_QUIT;
if ( $e->type == SDL_KEYDOWN)
{
my $key = $e->key_sym;
$mario->event_move(SDL::Events::get_key_name($key));
}
return 1;
}
);
$app->add_show_handler( sub { $app->update(); } );
$app->add_move_handler(
sub {
my $dt = shift;
$app->draw_rect( [0,0,$app->w, $app->h], 0 );
$mario->move($dt);
$mario->draw( $app );
}
);
$app->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment