Perl字符貪吃蛇

  一時興起,想試試能不能用perl實現字符貪吃蛇,算法以下:算法

  定義2個數組@bg、@snake,@bg用來顯示整個界面,@snake從蛇頭開始保存蛇的座標點。數組

  蛇每移動一次,新的座標點放到@snake頭部,並去除最後一個元素,再改變@bg對應座標的值。ide

  經過控制檯窗口不斷清屏再打印,使蛇「看起來在移動」。spa

  簡單的速度控制實現:每次移動後sleep 若干秒。scala

 

  感受原來的make_food算法不夠好,修改以下:code

sub make_food{
    if(@snake < $full){
        my @empty_points=();
        foreach(0..$#bg){  
            my $y=$_;
            foreach(0..$#{$bg[0]}){
                push @empty_points,[$y,$_] if($bg[$y][$_] eq '.');
            }
        }  # 找出全部空的座標點,存入@empty_points數組
        until($food){            
            my $num=int( rand( scalar(@empty_points) ) );  # 隨機取出@empty_points下標
            my ($y,$x)=@{ $empty_points[$num] }[0,1];
            $bg[$y][$x]='O';
            $food=1;
        } 
    }
}
View Code

 

  一個要解決的問題:無阻塞獲取按鍵。經過度娘找到模塊Term::ReadKey,新的問題產生了:批量輸入方向,蛇會依次按照輸入移動,暫時想不到好辦法解決。blog

  使用Time::HiRes的sleep,實現毫秒級等待,use 5.01 支持given...when語法。遊戲

  使用 w a s d 控制移動,代碼以下:it

use strict;
use 5.01;
use Time::HiRes qw/sleep/;
use Term::ReadKey;
use constant {WIDTH=>10,HEIGHT=>6,DEBUG=>1};
my @bg=();
my @snake=();
my ($score,$food,$speed,$alive)=(0,0,1,1); # 長度、食物、速度、存活
my $full=WIDTH*HEIGHT;
my %direct=( UP=>[-1,0],
            DOWN=>[1,0],
            LEFT=>[0,-1],
            RIGHT=>[0,1], ); # 移動方向
my $head='RIGHT'; # 初始移動方向

&init;
while($alive){    
    $speed=($speed<9)?(1+int($score/10)):9;  # 速度控制
    if(@snake==$full){
        print "congratulations!\n";  # 蛇佔滿遊戲區時顯示
    }
    else{
        &move;
        &check_head;
        &show if $alive;    
        sleep (1-$speed*0.1);
    }    
}

sub init{
    my $y=int(HEIGHT/2);
    @bg=map{my $x=$_;[map{$bg[$x][$_]='.'}0..WIDTH-1]}0..HEIGHT-1;
    @{$bg[$y]}[1,2]=('#','@'); # 初始蛇身位置
    @snake=( [$y,2],[$y,1], ); # 保存蛇身座標
    &make_food;  # 產生食物
}
sub show{
    system("cls");
    print "your score : $score\n";
    print "current speed : ",$speed,"\n\n";
    print @$_,"\n" foreach(@bg);
}
sub move{
    ReadMode 2;
    my ($key,$cur);
    $key=ReadKey(-1);
    $key=~tr/a-z/A-Z/ if $key;
    given($key){
        # 不容許反向移動
        when('W'){ 
            if($head eq 'DOWN'){
                $cur='DOWN';
            }
            else{
                $cur=$head='UP';
            }
        }
        when('A'){
            if($head eq 'RIGHT'){
                $cur='RIGHT';
            }
            else{
                $cur=$head='LEFT';
            }
        }
        when('S'){ 
            if($head eq 'UP'){
                $cur='UP';
            }
            else{
                $cur=$head='DOWN';
            }
        }
        when('D'){
            if($head eq 'LEFT'){
                $cur='LEFT';
            }
            else{
                $cur=$head='RIGHT';
            }
        }
        default { $cur=$head; }
    }
    unshift @snake,[$snake[0][0]+$direct{$cur}[0],$snake[0][1]+$direct{$cur}[1]];
}
sub make_food{
    if(@snake < $full){
        until($food){
            my ($x,$y)=(int(rand(WIDTH)),int(rand(HEIGHT)));
            if($bg[$y][$x] eq '.'){
                $bg[$y][$x]='O';
                $food=1;
            }
        } 
    }
}
sub check_head{
    # 蛇身超出範圍
    if($snake[0][0] < 0 || $snake[0][0] > HEIGHT-1
        || $snake[0][1] < 0 || $snake[0][1] > WIDTH-1){
            $alive=0;
    }
    # 蛇吃到本身
    if(@snake>3){
        foreach(1..$#snake){
            if($snake[0][0] == $snake[$_][0] &&  $snake[0][1] == $snake[$_][1]){
                $alive=0;
            }
        }
    }
    # 移動
    if($bg[$snake[0][0]][$snake[0][1]] eq '.'){
        $bg[$snake[0][0]][$snake[0][1]]='@';
    }
    # 吃到食物
    if($bg[$snake[0][0]][$snake[0][1]] eq 'O'){
        $bg[$snake[0][0]][$snake[0][1]]='@';
        $score++;
        $food=0;
        &make_food;
        push @snake,[$snake[-1][0],$snake[-1][1]];  # 新的蛇身放在尾部
    }
    $bg[$snake[-1][0]][$snake[-1][1]]='.';  # 先清除尾巴顯示
    pop @snake;  # 去掉尾巴
    map{$bg[$snake[$_][0]][$snake[$_][1]]='#'}1..$#snake;  # 其餘蛇身顯示
}

  遊戲界面:io

  

相關文章
相關標籤/搜索