sigh.看来大家对tk不敢兴趣?
上面那个扫雷写的很烂,
我再来贴一个吧,是贪吃蛇 , 一共 3,482 个字节
总想不通,tk那么好的东西,为什么不用呢?
配合上perl,无敌啊....;)
#!/usr/bin/perl
# writen by NetKen.
use Tk;
use strict;
my $max_width = 32;
my $max_height = 24;
my $snake_size=15;
my $speed = 200;
my($new_snake_x,$new_snake_y);
my @snake_pos = ( [$snake_size,$snake_size] , [$snake_size,$snake_size*2] , [$snake_size,$snake_size*3] , [$snake_size,$snake_size*4] );
my $last="d";
my $dir="d";
my $win = MainWindow->new( '-title'=>'Perl/Tk Snake!' );
my $canvas = $win->Canvas( '-width'=>$max_width*$snake_size,'-height'=>$max_height*$snake_size,'-relief'=>'ridge','-border'=>1)->pack();
$canvas->create('text',$max_width*$snake_size/2,$max_height*$snake_size/2,'-anchor'=>'center','-text'=>'Double Press Mouse Button 1 --> START or RESTART','-fill'=>'white');
foreach my $tmp (@snake_pos) {
my $box = $canvas->create('rectangle',$$tmp[0],$$tmp[1],$$tmp[0]+$snake_size-2,$$tmp[1]+$snake_size-2,'-fill'=>'black','-tag'=>'box');
}
my $new_x = $snake_pos[@snake_pos-1][0]+$x;
my $new_y = $snake_pos[@snake_pos-1][1]+$y;
if ($new_x < 1 or $new_x > $max_width*$snake_size-1 or $new_y < 1 or $new_y > $max_height*$snake_size-1) {gameover();return;}
foreach my $tmp (@snake_pos) {
if ($new_x == $$tmp[0] and $new_y == $$tmp[1]) {gameover();return;}
}
return if $last eq $nodir;
$last = $dir;
push(@snake_pos,[$new_x,$new_y]);
#->
if ( $new_x == $new_snake_x and $new_y == $new_snake_y ) {
unshift(@snake_pos,[$snake_pos[0][0],$snake_pos[0][1]]);
$canvas->delete('new_snake');
rand_new_snake();
new_snake($new_snake_x,$new_snake_y);
}
#-<
$canvas->delete('box');
foreach my $tmp (@snake_pos) {
my $box = $canvas->create('rectangle',$$tmp[0],$$tmp[1],$$tmp[0]+$snake_size-2,$$tmp[1]+$snake_size-2,'-fill'=>'black','-tag'=>'box');
}
shift(@snake_pos);
$win->after($speed,\&snake );
};
sub gameover {
$canvas->create('text',$max_width*$snake_size/2,$max_height*$snake_size/2,'-anchor'=>'center','-text'=>'GAME OVER!','-fill'=>'red','-font'=>'-adobe-verdana-bold-r-narrow--36-360-90-90-90-p-90-iso8859','-tag'=>'gameover');
};
sub new_snake {
my($x,$y)=@_;
$canvas->create('rectangle',$x,$y,$x+$snake_size-2,$y+$snake_size-2,'-fill'=>'black','-tag'=>'new_snake');
$speed-=5 if $speed > 10;
};
sub rand_new_snake {
$new_snake_x=int rand($max_width-2);
$new_snake_y=int rand($max_height-2);
$new_snake_x++;$new_snake_y++;
$new_snake_x*=$snake_size;$new_snake_y*=$snake_size;
};
sub game_win {
$canvas->create('text',$max_width*$snake_size/2,$max_height*$snake_size/2,'-anchor'=>'center','-text'=>'YOU ARE VICTOROR!','-fill'=>'red','-font'=>'-adobe-verdana-bold-r-narrow--24-360-90-90-90-p-90-iso8859','-tag'=>'gameover');
};
sub start {
$start_button->destroy();
format_array();
level();
};
sub restart {
for (my $i=0;$i<$max ;$i++)
{
my $n_i = "$i";$n_i = "0$i" if $i<10;
my $button_room_name = "button_room"."$i";
${$button_room_name}->destroy();
for (my $j=0;$j<$max ;$j++) {
my $n_j = "$j";$n_j = "0$j" if $j<10;
my $button_name = "button".$n_i.$n_j;
undef ${$button_name};
}
}
sub format_array {
undef @array;
for (my $k=0;$k<$bomb+1 ;$k++) {
srand(rand($max*100));
my $number = rand($max*100);
$number=int($number);
$number+=$max if $number<$max;
my $i = $number % $max;
my $j = int(( $number - $j ) / $max);
$array[$i][$j] = "0";
}
for (my $i=0;$i<$max ;$i++ ) {
for (my $j=0;$j<$max ;$j++ ) {
if ($array[$i][$j] eq "0") {
$array[$i-1][$j]++ if $i>0 and $array[$i-1][$j] ne "0";
$array[$i+1][$j]++ if $i<$max-1 and $array[$i+1][$j] ne "0";
$array[$i][$j-1]++ if $j>0 and $array[$i][$j-1] ne "0";
$array[$i][$j+1]++ if $j<$max-1 and $array[$i][$j+1] ne "0";
$array[$i-1][$j-1]++ if $i>0 and $j>0 and $array[$i-1][$j-1] ne "0";
$array[$i+1][$j+1]++ if $i<$max-1 and $j<$max-1 and $array[$i+1][$j+1] ne "0";
$array[$i-1][$j+1]++ if $i>0 and $j<$max-1 and $array[$i-1][$j+1] ne "0";
$array[$i+1][$j-1]++ if $i<$max-1 and $j>0 and $array[$i+1][$j-1] ne "0";
}
}
}
};
sub level {
for (my $i=0;$i<$max ;$i++)
{
my $n_i = "$i";$n_i = "0$i" if $i<10;
my $button_room_name = "button_room"."$i";
$eval .= '$'."$button_room_name".'=$win->Frame()->pack("-side"=>"top");';
for (my $j=0;$j<$max ;$j++) {
my $n_j = "$j";$n_j = "0$j" if $j<10;
my $button_name = "button".$n_i.$n_j;
$eval .= '$'."$button_name".'=$'."$button_room_name".'->Button("-text"=>"?","-width"=>1,"-height"=>1,"-command"=>[\&change_button,'."$i,$j".'])->pack("-side"=>"left");';
}
}
eval $eval;
undef $eval;
};
# 如果为 null ,则 flat ,如果有数字 则 ridge
sub change_button {
my $i = shift;
my $j = shift;
my $n_i = "$i";$n_i = "0$i" if $i<10;
my $n_j = "$j";$n_j = "0$j" if $j<10;
if ($array[$i][$j] eq "0") {
gameover();
} else {
unless ($array[$i][$j]) {
disp_blank($i,$j);
} else {
${"button".$n_i.$n_j}->configure ( "-text"=>"$array[$i][$j]" , "-relief"=>"ridge" );
}
}
};
sub disp_blank {
my $i = shift;
my $j = shift;
my $m_i = "$i";$m_i = "0$i" if $i<10;
my $m_j = "$j";$m_j = "0$j" if $j<10;