Guest User

scroll

a guest
Feb 7th, 2017
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.67 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3.  
  4. ####################################################
  5. # Images File to download
  6. # http://i.imgur.com/pKC0it6.jpg - bg.jpg
  7. # http://i.imgur.com/QBDWhIl.png - standing.png
  8. # http://i.imgur.com/rku5CFE.png - skill1.png
  9. # http://i.imgur.com/AzuQc5a.png - running.png
  10. # http://i.imgur.com/wHnWfln.png - back.png
  11. ####################################################
  12.  
  13.  
  14. use strict;
  15. use warnings;
  16. use Carp;
  17. use SDL;
  18. use SDLx::App;
  19. use SDL::Event;
  20. use SDL::Events;
  21. use SDL::Image;
  22. use SDL::Audio;
  23. use SDL::Mixer;
  24. use SDLx::Sprite;
  25. use SDLx::Sprite::Animated;
  26. use SDL::Mixer::Samples;
  27. use SDL::Mixer::Channels;
  28. use SDL::Mixer::Music;
  29. SDL::init(SDL_INIT_AUDIO);
  30. my $app=SDLx::App->new(
  31.     w=>500,
  32.     h=>500,
  33.     title=>'Sonic',
  34.     dt=>0.02,
  35. );
  36. my $map_pos=0;
  37. my $background=SDL::Image::load('rect.jpg');
  38. my $background_rect; = SDL::Rect->new(0,0,
  39.     $background->w,
  40.     $background->h,
  41. );
  42. my $quit=0;
  43. my $sprite = SDLx::Sprite::Animated->new(
  44.     image => "standing.png",
  45.     step_x => 56.33333333333333,
  46.     step_y => 1,
  47.     rect => SDL::Rect->new(40,410,56.33333333333333,55),
  48.     ticks_per_frame => 10,
  49.     type => "circular",
  50. );
  51. my $skill1=SDLx::Sprite::Animated->new(
  52.     image => "skill1.png",
  53.     step_x => 56.23529411764706,
  54.     step_y => 1,
  55.     rect => SDL::Rect->new(40,410,56.23529411764706,70),
  56.     ticks_per_frame => 6,
  57.     type => "circular",
  58. );
  59. my $running = SDLx::Sprite::Animated->new(
  60.     image => "running.png",
  61.     step_x => 54.81818181818182,
  62.     step_y => 1,
  63.     rect => SDL::Rect->new(40,410,54.81818181818182,57),
  64.     ticks_per_frame => 10,
  65.     type => "circular",
  66. );
  67. my $back = SDLx::Sprite::Animated->new(
  68.     image => "back.png",
  69.     step_x => 54.81818181818182,
  70.     step_y => 1,
  71.     rect => SDL::Rect->new(40,410,54.81818181818182,57),
  72.     ticks_per_frame => 10,
  73.     type => "circular",
  74. );
  75. my $bg=SDLx::Sprite::Animated->new(
  76.     image => "bg.jpg",
  77.     step_x => 500,
  78.     step_y => 1,
  79.     rect => SDL::Rect->new(0,0,500,500),
  80.     ticks_per_frame => 6,
  81.     type => "circular",
  82. );
  83. SDL::Events::enable_key_repeat( 1, 5 );
  84. $skill1->step_y(0);
  85. $running->step_y(0);
  86. $bg->step_y(0);
  87. $sprite->step_y(0);
  88. $sprite->start;
  89. $back->clip->w(0);$back->clip->h(0);
  90. $running->clip->w(0);$running->clip->h(0);
  91. $skill1->clip->w(0);$skill1->clip->h(0);
  92. $back->type('reverse');
  93. my $mov_x=10;
  94. #############
  95. my $skill=0;
  96. my $duration=0;
  97. #############
  98. sub get_events{
  99.     my $event=SDL::Event->new();
  100.     #$event->set_key_repeat(10,10);
  101.     SDL::Events::pump_events();
  102.     while(SDL::Events::poll_event($event)){
  103.         $quit=1 if $event->type==SDL_QUIT;
  104.         if($event->type==SDL_KEYDOWN){
  105.             if($event->key_sym==SDLK_d){
  106.                 &walking;
  107.             }
  108.             if($event->key_sym==SDLK_a){
  109.                 &backWalk;
  110.             }
  111.             if($event->key_sym==SDLK_j){
  112.                 $skill=1
  113.             }
  114.         }
  115.         if($event->type==SDL_KEYUP){
  116.             &idle;
  117.         }
  118.     }
  119. }
  120.  
  121. sub check{
  122.     if($skill==1){
  123.         $duration++;
  124.         if($duration!=100){
  125.             $skill1->clip->w(56.23529411764706);$skill1->clip->h(70);
  126.             $running->clip->w(0);$running->clip->h(0); 
  127.             $sprite->clip->w(0);$sprite->clip->h(0);
  128.             $skill1->start;
  129.         }
  130.         if($duration==100){
  131.             $skill1->clip->w(0);$skill1->clip->h(0);
  132.             $skill=0;$duration=0;
  133.             $skill1->reset;
  134.             &idle;
  135.         }
  136.     }
  137.     sub walking{
  138.         $running->start;
  139.        
  140.         if($mov_x<=222){
  141.             $mov_x++;
  142.         }
  143.         if($mov_x>=222 and $map_pos!=302){
  144.             $map_pos+=2;
  145.         }
  146.         if($map_pos==302){
  147.             $mov_x++;
  148.         }
  149.         #print "$mov_x\n";
  150.         print "$map_pos\n";
  151.         $bg->clip->x($map_pos);
  152.         $running->clip->w(54.81818181818182);$running->clip->h(57);
  153.         $sprite->clip->w(0);$sprite->clip->h(0);
  154.     }
  155.     sub backWalk{
  156.         $back->start;
  157.        
  158.         if($mov_x<=222){
  159.             $mov_x--;
  160.         }
  161.         if($mov_x>=222 and $map_pos==-2){
  162.             $map_pos-=2;
  163.         }
  164.         if($map_pos==302){
  165.             $mov_x--;
  166.         }
  167.         #print "$mov_x\n";
  168.         print "$map_pos\n";
  169.         $bg->clip->x($map_pos);
  170.         $back->clip->w(54.81818181818182);$back->clip->h(57);
  171.         $sprite->clip->w(0);$sprite->clip->h(0);
  172.         $running->clip->w(0);$running->clip->h(0);
  173.     }
  174.     sub idle{
  175.         $sprite->clip->w(56.33333333333333);$sprite->clip->h(55);
  176.         $running->clip->w(0);$running->clip->h(0);
  177.        
  178.         #$fight->clip->w(0);$fight->clip->h(0);    
  179.     }
  180. }
  181.  
  182. sub render{
  183.     SDL::Video::blit_surface($background, $background_rect, $app, $background_rect );
  184.     $bg->draw($app);
  185.     $sprite->draw($app);
  186.     $running->draw($app);
  187.     $skill1->draw($app);
  188.     $back->draw($app);
  189.     $running->rect(SDL::Rect->new($mov_x,410,192,162));
  190.     $skill1->rect(SDL::Rect->new($mov_x,410,192,162));
  191.     $sprite->rect(SDL::Rect->new($mov_x,410,192,162));
  192.     $back->rect(SDL::Rect->new($mov_x,410,192,162));
  193.     SDL::Video::update_rects($app, $background);
  194.     $app->delay(2);
  195.     $app->update();
  196. }
  197.  
  198. while(!$quit){
  199.     get_events();
  200.     check();
  201.     render();
  202. }
Advertisement
Add Comment
Please, Sign In to add comment