perl-sdl处理事件

use SDL;
use SDLx::App;
use SDLx::Sprite;
use SDL::Event;
use SDL::Events;
my $app=SDLx::App->new(w=>400,h=>400,d=>32,t=>"sky");
#画大地
$app->draw_rect([0,0,400,400],[0,100,0,255]);
#随机生成大地的麻点
for (my $i = 1; $i < 500; $i++){
    $app->[int(rand(400))][200+int(rand(200))]=[10,10,10,255];
}
#画夜空
my $sky=SDLx::Sprite->new(width=>400,height=>200);
$sky->surface->draw_rect([0,0,400,200],[0,0,0,255]);
#画树
my $tree=SDLx::Surface->new(width=>400,height=>150);
for (my $i = 1; $i < 30; $i++){
    $x=int(20+rand(375));
    $y=int(15+rand(100));
    $tree->draw_circle_filled([$x,$y],15,[0,150,0,255]);
	$tree->draw_rect([$x,$y+15,2,15],[0,0,15,255]);
}
#画月亮
$sky->surface->draw_circle_filled([150,50],25,[255,255,0,255]);
#画星星
for (my $i = 1; $i < 50; $i++)
{
   my $x=int(rand(400));
   my $y=int(rand(200));
   if (not ($x>125 and $x<175  and $y >25 and $y<75))
   {
      $sky->surface->draw_circle_filled([$x,$y],2,[0,0,200,255]);      
   }  
}
$sky->draw_xy($app);
$tree->blit($app,[0,0,400,150],[0,200,400,150]);
$app->update();
my $event=SDL::Event->new();
my $quit=0;
while (!$quit){
     SDL::Events::pump_events();
	 while (SDL::Events::poll_event($event))
	 {
	     $quit=1 if $event->type==SDL_KEYDOWN;
	 }
}

 上面和下面的代码都完成退出窗口,上面的窗口,读取一个键盘信号就退出,下面的代码读到退出信号就退出

use SDL;
use SDLx::App;
use SDL::Event;

my $app=SDLx::App->new(w=>400,h=>400,d=>32,t=>"sky");
$app->add_event_handler(\&quit_handle);
$app->run();


sub quit_handle
{
    my $event=shift;
	my $controller=shift;
	$controller->stop if $event->type==SDL_QUIT;
}

 

use SDL;
use SDLx::App;
use SDL::Event;

my $app=SDLx::App->new(w=>400,h=>400,d=>32,t=>"sky",exit_on_quit=>1);
$app->run();

 

你可能感兴趣的:(perl)