贪吃蛇在线玩玩起心里很烦

一群人坐上转椅玩起了#真人版“贪吃蛇”#,太怀念以前上课拿着诺基亚破纪录的日子了!
秒拍,10秒拍大片
秒拍,10秒拍大片
下次自动登录当前位置:
微信扫一扫分享到朋友圈
动态图稍大,请稍等!喜欢这篇文章?那就分享到朋友圈吧!今日推理题目:在死亡面前,为了求生,即便突破底线,无情无义...但是这样能逃过一劫吗?请问这个女人会死吗?(动态图稍大,请稍微等待一下哦!)实在想不出答案?那就点下面吧!点击阅读原文获取答案,答案太出乎意外了!↓↓↓↓↓↓↓
分享给好友
分享到微信朋友圈:
第一步 打开微信底部扫一扫
第二步 扫下面的文章二维码
第三步 右上角点击转发
相关文章Relevant
■ 点击上面蓝字一键关注 ▲QIBU生活微刊建议在WIFI下观看,土豪请随意~~1、每一次接吻 会消耗体内至少12个卡路里科学家指出:...
我是主播 贝妮~(微信号:Voaoao)每天提供最热门、最火爆、最精彩的视频!口味有点儿重喔~笑死!笑死!笑死!如果觉得这些还...
【最费脑力的14部电影】《盗梦空间》、《记忆裂痕》、《生死停留》、《死亡幻觉》、《禁闭岛》、《穆赫兰道》、《蝴蝶效应》、...
现如今,飞机以舒适、方便与节省时间等原因成为出行首选的交通方式之一.可你是否知道,为何不能喝飞机上的冲泡茶饮,又为何在...
感知CG,感触创意,感受艺术,感悟心灵 在CG世界的一期中我们展示了 Vince Low的一部分作品,今天再次翻看CG网站时发现他的...
因女儿未出世便患肿瘤,柴静离职后首发雾霾调查.雾霾是什么?它从哪儿来?我们怎么办?看完这些,才知道雾霾的真相.震撼!震...贪吃蛇玩起来容易,写起来还真容易@@!
&&&&& 说实话,玩贪吃蛇我绝对是高手~玩多少次都通关多少次。不过写贪吃蛇我照样是高手,写了也不下5、6次了,每次都发现了一两个Boss张精心设计的小bug,虽然最后发现那些都是意外。在这里首先感谢Boss张精心设计的《手机贪吃蛇游戏》,也感谢那些意外出现的小&bug&,不过还是要指出的是Boss张写Controler类是,应该想的是Controller吧!这&直接&导致了我对&断言失败&产生的恐惧感~
& 这次照样把Greedy Snake Beta1.0打包成了安装程序,透明的界面让蛇在桌面上跑,食物是圆形的,和原来的正方形一样都是实现的接口IDrawObj。不过我简单实现了蛇随着进食的多少改变自己的体形,颜色。当然也加大了难度,提高了蛇运动的速度。
注意:蛇是根据像素点一个一个移动的,如果想加快蛇的移动速度,不能让蛇一次移动2个或多个像素点。这样会导致蛇尾无法和蛇身前一个拐点重合,将不能删除拐点,蛇将越来越长。暂时只能该Interval属性。
疑点:不明白为什么把断言测试写成:
//p1和p2的X坐标或Y座标有且只有一个是相等的&&
Debug.Assert(p1.X&==&p1.X&&&&p1.Y&!=&p2.Y&||&p1.Y&==&p2.Y&&&&p1.X&!=&p2.X);&&
//为什么不是&&
Debug.Assert(&(p1.X&==&p2.X&&&&p1.Y&!=&p2.Y)&||&(p1.Y&==&p2.Y&&&&p1.X&!=&p2.X)&);&&
游戏运行和暂停的时候占用CPU使用率50%以上:有什么办法可以减低CPU使用率吗?
哎呀~困了~~~~歇笔了、、、
> 本站内容系网友提交或本网编辑转载,其目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请及时与本网联系,我们将在第一时间删除内容!
这段时间以来,本博陆续介绍了Android系统的开发和调试环境.模拟器,并详细解析了Android的一个实例SoftKeyboard,这里具体列出如下: Android是什么 Android SDK 2.3与Eclipse最新版开发环境搭建(一) Android SDK 2.3与Eclipse最新版开发环境搭建(二) Android SDK 2.3与Ecli ...
最近在写一个浪新微博团购分享的手机客户端(感兴趣的朋友可以到这里下载/ ,是J2ME版本的,以后我基本就不会进行J2ME版本的开发,注意精力放在Android上了),因此博客更新慢了点.不过,我会尽量保证一周至少更新一次. 本次讲讲如何使用Google的SVN来管理我们的Android开源项目. 一.创 ...
大家想必都使用过微博,或者是每天牢骚不断的强迫症用户,或者是随便注册个帐户的酱油党.毋容置疑,从2010年开始到现在,微博在中国又重新焕发出第二春(第一春是饭否.叽歪...2009年被封杀过,不过最近貌似饭否又活过来了...),而浪新微博在其中居功至伟. 其实,中国的微博大同小异,学习的都是鼻祖:Twitter:就是好像中国的SNS都是学习的Facebook ...
Snake也是一个经典游戏了, Nokia蓝屏机的王牌游戏之一. Android SDK 1.5就有了它的身影.我们这里就来详细解析一下 Android SDK Sample中的 Snake工程.本工程基于 SDK 2.3.3版本中的工程,路径为: %Android_SDK_HOME% /samples/android-10/Snake一. Eclipse工 ...
本次会详细讲解将Android的Snake Sample移植到J2ME上,从而比较二者的区别和联系. 在&1.Android SDK Sample-Snake详解 &中,我们已经详细介绍了Android实现的Snake项目结构,现在我们要将这个项目用J2ME实现. 一. J2ME vs. Android Android的UI实用.方便,而且很 ...
四.工程文件下载 为了方便大家阅读,可以到如下地址下载工程源代码: http://download.csdn.net/source/3145349 五.小结及下期预告: 本次详细解析了 Android SDK 自带 Sample—— Snake的结构和功能.下次将会把这个游戏移植到 J2ME平台上,并且比较 Android和 J2ME的区别和相通之处,让从事 ...
为了让大家更好的理解J2ME和Android编程的差别,我用J2ME重新实现了Android的Snake Sample.
下次,我会详细介绍在将Snake从Android移植到J2ME上时,需要特别注意的问题,并对Android和J2ME的区别和联系进行粗略的比较.
本次,暂时把J2ME实现的运行画面列出如下.JAVA的好处就是一次编写多处运行,:- ...
今天整理电脑, 发现自己去年就是大一上学期快结束时写的贪吃蛇, 那时 还没有写blog, 今天拿出来晒晒啊,嘿嘿 多多交流,多多交流......
#include&stdio.h&#include&conio.h&#include&math.h&#include&dos.h&#include&std ...最近很火的贪吃蛇游戏叫什么?贪吃蛇还能这么玩
贪吃蛇作为手机上经典的像素游戏,最初是以极为简单的像素黑点画面出现在诺基亚手机上面,这也成为了诺基亚手机上面的经典游戏,游戏也是不断的被模仿致敬。
找回美好的回忆,经典像素蛇
尽管有几万种游戏,但是对于某些人来说贪吃蛇才是心中永恒的经典,还记得没日没夜的玩这款游戏的日子吗?
来到了触控手机时代,贪吃蛇再次被拿起来,这个游戏的魅力依然不可减,一款slither.io游戏出现在大家眼前,才发现贪吃蛇游戏还可以这样玩。
《蛇蛇大作战 slither.io》是一款结合了Agar.io和贪吃蛇的玩法的休闲游戏。线上对战,但侵略性比Agar.io低。在游戏中玩家会化身为一只小小的蛇,在比赛中通过吃掉别人的蛇来使自己变得更为强大!
后来又出现了很多的贪吃蛇游戏,每个都感觉是那么的熟悉。
虫虫大作战火热上市啦!重新演绎经典的&贪吃蛇&玩法、糅合清心Q萌的界面、海量新颖的皮肤以及多人实时的掠夺作战,将带来全新的休闲对战体验!快来下载《虫虫大作战》吧!
其中最为成功的可能就是要属《贪吃蛇大作战》了。
贪吃蛇大作战 - 这是一个有毒的游戏!
小时候玩的经典游戏《贪吃蛇》,现在华丽升级为《贪吃蛇大作战》,全新玩法等你来挑战!
这是一款超好玩的休闲竞技游戏,不仅比拼手速,更考验你的策略!在贪吃蛇大作战的世界中,每个人在初始都化身为一条小蛇,通过不断努力变得越来越长,终于制霸一方!
【来源:网络】
第一手全民飞机大战资讯攻略尽在助手神器!
手机访问手机扫描二维码访问  某天闲逛时看见一副动图:
  真的是非常贪吃,各种拐弯各种吃,感觉十分有趣。
  用Perl来实现自动吃满,蓄谋已久,之前的字符贪吃蛇、深度优先算法、A*算法,都是为此篇做铺垫。
  那么,怎样让蛇不吃到自己呢?
  1、让蛇按照我们设计好的路线行进,在一个N*M(N、M均为偶数,奇数不讨论)的游戏区域,设计好路线如下:
    当花儿谢了,果子熟透,春夏秋冬一个轮回后,蛇终于吃满了。。。
  2、假设蛇总是追着自己的尾巴跑,那么永远不会死;然而,这没什么鸟用,我们需要的是一条&贪吃&的蛇。
  3、在2上稍稍改进,吃完食物后再追着尾巴,嗯,已经很接近了。但是问题来了:怎么吃到食物?吃到食物后被围死怎么办?
  其实我的想法就是解决3中的问题:
  1、使用A*算法计算到食物的最短路径
  2、模拟吃到食物后的场景,能否回到蛇尾;能--吃食物,不能--继续追着尾巴,重复1、2
  3、如果无法回到尾巴,保持当前移动方向(认命吧,要挂的节奏)
  伪码如下:
while( 存活 && 未吃满){
计算蛇头到食物的路径
if( 有蛇头到食物的路径 ){
模拟吃到食物后的场景
计算是否有路径到达蛇尾
if( 有路径 ){
执行蛇头到食物的路径
# 没有到蛇尾的路径
找出当前可行方向追逐蛇尾
显示:祝贺
显示:wtf ?
  模拟吃到食物后的场景:使用A*计算路径,并创建一条&影子蛇&用来模拟吃到食物后的状态
  计算是否有路径到达蛇尾:和&追逐蛇尾&方法类似,也可使用A*
  找出当前可行方向追逐蛇尾:
    由于移动时尾巴会变动,未尝试A*,使用指定方向深度优先搜索算法
    伪码如下:
sub trace_tail{
@moves=grep{可行};
foreach(@moves){
next if(下一步越界 || 吃到自己)
unshift @feasible,$_ if(远离食物)
其他方向 push @feasible
foreach(@feasible){
next_move=cur_move+$_
指定方向优先探索
next_move的反向上一步设为不可通过
每走一步,从蛇尾开始设未经过;每原路返回一次,当前位置的蛇身设不可通过
if(有路径)return next_move
执行完毕无路径,return cur_move
  运行如下:
  实际上还是会被困死,虽然追逐尾巴时避开食物优先,但唯一路径可能会出现食物,不吃也得吃了,如下:
  代码较乱,准备加好注释后贴上来。
  有很大的优化空间,等有时间再慢慢搞,代码:
use Time::HiRes qw/sleep/;
use Term::ReadK
use constant {WIDTH=&12,HEIGHT=&8,DEBUG=&0};
my @bg=();
my @snake=();
my @head2food=();
my @food_coordinate=();
# 保存食物坐标
my ($score,$food,$speed,$alive,$head2tail,$head_move)=(0,0,1,1,0,4); # 得分、食物、速度、存活、能否回到尾巴的标志、初始移动方向
my $full=(WIDTH-2)*(HEIGHT-2);
my %opposite=( 1=&3,
# 对立的移动方向,禁止反向移动
my @uldr=( 0,[-1,0],[0,-1],[1,0],[0,1], ); # 上、左、下、右
############################################################################################
#my $move=&manual_move(4);
# 开始向右移动
my $move=&smart_move($head_move);$speed=9;
while( $alive && @snake & $full ){
@head2food=&head_to_food($head_move,@snake);
# 计算蛇头到食物的路径
if( $head2food[0] != 0 ){
$head2tail=&head_to_tail(\@snake,\@head2food);
# 判断吃到食物后是否能回到尾巴
if($head2tail){
# 能回到尾巴,吃食物
foreach( @head2food ){
$head_move=$_;
$move-&($head_move);
if($alive){
sleep (1-$speed*0.1);
$head_move=&trace_tail($head_move,@snake);
# 找出当前可行方向追逐蛇尾
$move-&($head_move);
if($alive){
sleep (1-$speed*0.1);
if(@snake==$full){
print "\ncongratulations!\n";
# 蛇占满游戏区时显示
print "\nWTF?\n";
############################################################################################
my $y=int(HEIGHT/2);
for(my $y=0;$y&HEIGHT;$y++){
for( my $x=0 ; $x&WIDTH ; $x++ ){
if( $y == 0 || $y == HEIGHT-1 ||
$x == 0 || $x == WIDTH-1
$bg[$y][$x] = '*';
$bg[$y][$x] = ' ';
@{$bg[$y]}[1,2]=('#','@'); # 初始蛇身位置
@snake=( [$y,2],[$y,1], ); # 保存蛇身坐标
# 产生食物
############################################################################################
system("cls") unless(DEBUG);
print "your score : $score\n";
print "current speed : ",$speed,"\n\n";
print @$_,"\n" foreach(@bg);
############################################################################################
sub smart_move{
my $move_direct=shift;
# 闭包,传入初始移动方向
$move_direct=shift;
unshift @snake,[$snake[0][0]+$uldr[$move_direct][0],$snake[0][1]+$uldr[$move_direct][1]];
############################################################################################
sub head_to_food{
# 蛇头到食物
my $head_shadow=shift;
my @snake_ghost=@_;
my @bg_ghost=@bg;
my @path=&a_star( [ $snake_ghost[0][0],$snake_ghost[0][1] ],
[ $food_coordinate[0],$food_coordinate[1] ],\@bg_ghost ); # A*算法,传递起点、终点、蛇当前坐标指针
return @path;
############################################################################################
# 追逐尾巴,传递当前移动方向、蛇身所有坐标
sub trace_tail{
print "call trace_tail\n" if DEBUG;
my $cur_move=shift;
# 当前移动方向
my @snake_ghost=@_;
my @path=();
my $cur_position=[ $snake_ghost[0][0],$snake_ghost[0][1] ];
cur_position:",$snake_ghost[0][0],",",$snake_ghost[0][1],"\n" if DEBUG;
my $end=[ $snake_ghost[-1][0],$snake_ghost[-1][1] ];
my $tail=$#snake_
my @bg_ghost=map{ [ split('','0'x WIDTH) ] }0..(HEIGHT-1);
# 0--未经过 1--已走 2--不可通过
map{ my $y=$_;map { $bg_ghost[$y][$_] = ( $bg[$y][$_] eq '*' )?2:0 }0..$#{$bg[0]} }0..$#
map{ $bg_ghost[ $snake_ghost[$_][0] ][ $snake_ghost[$_][1] ] = 2 } 1..$#snake_
# 蛇身不可通过
my @feasible=();
my @next_moves=grep{ ! /$opposite{$cur_move}/ }values %opposite;
# 取出除反方向的可行方向
my $weight=0;
foreach(@next_moves){
$weight=1;
my $next_move=$_;
my ($y,$x)=( $snake_ghost[0][0]+$uldr[$next_move][0],$snake_ghost[0][1]+$uldr[$next_move][1] );
# 防止越界
if( $y & 1 || $y & HEIGHT-2 || $x & 1 || $x & WIDTH-2){
$weight=0;
# 防止吃到自己
foreach(0..$#snake_ghost){
if( $y == $snake_ghost[$_][0] && $x == $snake_ghost[$_][1]
$weight=0;
if($weight){
if( abs($y - $food_coordinate[0]) & abs($snake_ghost[0][0] - $food_coordinate[0]) ||
abs($x - $food_coordinate[1]) & abs($snake_ghost[0][1] - $food_coordinate[1]) ){
unshift @feasible,$next_move;
# 远离食物放到数组头
push @feasible,$next_move;
# 其他路径放在末尾
feasible:@feasible\n" if DEBUG;
foreach(@feasible){
$cur_move=$_;
cur_move:$cur_move\n" if DEBUG;
my @one234 = ();
# 将当前移动方向设置为初始方向
given($cur_move){
when(1){ @one234 = @uldr;}
when(2){ @one234 = @uldr[0,2,3,4,1]; }
when(3){ @one234 = @uldr[0,3,4,1,2]; }
when(4){ @one234 = @uldr[0,4,1,2,3]; }
#################################################################
# 指定方向深度搜索
my ($step,$p_step)=(0,'');
if($bg_ghost[ $cur_position-&[0] ][ $cur_position-&[1] ] == 0 ){
$bg_ghost[ $cur_position-&[0] ][ $cur_position-&[1] ]=1;
# 当前移动方向的反方向点不可经过,即禁止回退
$bg_ghost[ $snake_ghost[0][0]+$uldr[$opposite{$cur_move}]-&[0] ][ $snake_ghost[0][1]+$uldr[$opposite{$cur_move}]-&[1] ] = 2;
$bg_ghost[ $snake_ghost[0][0] ][ $snake_ghost[0][1] ] = 2;
# 当前点不可返回
if( $cur_position-&[0] == $food_coordinate[0] && $cur_position-&[1] == $food_coordinate[1] ){
push @snake_ghost,[ $snake_ghost[-1][0],$snake_ghost[-1][0] ];
$path[$step]={step=&$step,
coordinate=&[$cur_position-&[0],$cur_position-&[1]],
direct=&1,
$path[$step]-&{direct}=4 if($step == 0);
# 起点不可再做起点
path[$step]:$path[$step]:",$path[$step]-&{step},"\n" if DEBUG;
if(DEBUG){
print @$_,"\n" foreach(@bg_ghost);
if( $cur_position-&[0]==$end-&[0] && $cur_position-&[1]==$end-&[1]){
my @arr=('A'..'Z','a'..'z');
foreach(0..$#path){
$bg_ghost[ $path[$_]-&{coordinate}-&[0] ][ $path[$_]-&{coordinate}-&[1] ] = $arr[$_];
trace_tail: return $cur_move\n" if DEBUG;
return $cur_move;
# 有可行方向,返回
if($step&1 && $step&=$#snake_ghost){
$bg_ghost[ $snake_ghost[$tail][0] ][ $snake_ghost[$tail][1] ] = 0
if ( $bg_ghost[ $snake_ghost[$tail][0] ][ $snake_ghost[$tail][1] ] == 2 );
# 每移动一次,将蛇尾所在坐标设未经过
$tail=($tail&0)?($tail-1):1;
$cur_position=[ $path[$step-1]-&{coordinate}-&[0]+$one234[1]-&[0],
$path[$step-1]-&{coordinate}-&[1]+$one234[1]-&[1] ];
(y,x):(",$cur_position-&[0],",",$cur_position-&[1],")\n" if DEBUG;
if(@path){
$p_step=pop(@path);
while($p_step-&{direct}==4 && (@path)){
$bg_ghost[ $p_step-&{coordinate}-&[0] ][ $p_step-&{coordinate}-&[1] ] = 2;
$p_step=pop(@path);
$tail=($tail&$#snake_ghost-1)?($tail+1):$#snake_ghost-1;
$bg_ghost[ $snake_ghost[$tail][0] ][ $snake_ghost[$tail][1] ] = 2
if ( $bg_ghost[ $snake_ghost[$tail][0] ][ $snake_ghost[$tail][1] ] == 0 );
# 每回退一次,将蛇尾所在坐标设为不可通过
if($p_step-&{direct}&4){
$p_step-&{direct}++;
",$p_step-&{step},":p_step-&{direct}:",$p_step-&{direct},"\n" if DEBUG;
push @path,$p_step;
my @temp=@{$p_step-&{coordinate}}[0,1];
$cur_position = [ $temp[0]+$one234[$p_step-&{direct}]-&[0],
$temp[1]+$one234[$p_step-&{direct}]-&[1] ];
(y,x):(",$cur_position-&[0],",",$cur_position-&[1],")\n" if DEBUG;
}while(@path);
# 指定方向深度搜索结束
#################################################################
cur_move:$cur_move
trace_tail:return fail\n" if DEBUG;
return $cur_move;
# 没有到尾巴的可行方向了,准备认命
#######################################################################################
# 能否到尾巴位置,能返回1,否则返回0
# 算法大致与 trace_tail 相同
sub head_to_tail{
print "call head_to_tail\n" if DEBUG;
my ($p_snake,$p_path)=@_;
my @snake_ghost=@$p_snake;
my @path=@$p_path;
my $cur_move=$path[-1];
my @arr=();
foreach(0..$#path){
my ($y,$x)=( $snake_ghost[0][0]+$uldr[ $path[$_] ]-&[0],$snake_ghost[0][1]+$uldr[ $path[$_] ]-&[1] );
unshift @snake_ghost,[$y,$x];
pop @snake_ghost if($_ & $#path);
# 影子蛇先行,吃到食物后的状态
my $cur_position=[ $snake_ghost[0][0],$snake_ghost[0][1] ];
cur_position:",$snake_ghost[0][0],",",$snake_ghost[0][1],"\n" if DEBUG;
my $end=[ $snake_ghost[-1][0],$snake_ghost[-1][1] ];
my $tail=$#snake_
my @bg_ghost=map{ [ split('','0'x WIDTH) ] }0..(HEIGHT-1);
# 0--未经过 1--已走 2--不可通过
map{ my $y=$_;map { $bg_ghost[$y][$_] = ( $bg[$y][$_] eq '*' )?2:0 }0..$#{$bg[0]} }0..$#
map{ $bg_ghost[ $snake_ghost[$_][0] ][ $snake_ghost[$_][1] ] = 2 } 1..$#snake_
# 蛇身不可通过
my @feasible=();
my @next_moves=grep{ ! /$opposite{$cur_move}/ }values %opposite;
my $weight=0;
foreach(@next_moves){
$weight=1;
my $next_move=$_;
my ($y,$x)=( $snake_ghost[0][0]+$uldr[$next_move][0],$snake_ghost[0][1]+$uldr[$next_move][1] );
# 防止越界
if( $y & 1 || $y & HEIGHT-2 || $x & 1 || $x & WIDTH-2){
$weight=0;
# 防止吃到自己
foreach(0..$#snake_ghost){
if( $y == $snake_ghost[$_][0] && $x == $snake_ghost[$_][1] ){
$weight=0;
if($weight){
push @feasible,$next_move;
# 路径随意放
feasible:@feasible\n" if DEBUG;
foreach(@feasible){
$cur_move=$_;
my @one234 = ();
# 将当前移动方向设置为初始方向
given($cur_move){
when(1){ @one234 = @uldr;}
when(2){ @one234 = @uldr[0,2,3,4,1]; }
when(3){ @one234 = @uldr[0,3,4,1,2]; }
when(4){ @one234 = @uldr[0,4,1,2,3]; }
#################################################################
# 指定方向深度搜索
my ($step,$p_step)=(0,'');
if($bg_ghost[ $cur_position-&[0] ][ $cur_position-&[1] ] == 0 ){
$bg_ghost[ $cur_position-&[0] ][ $cur_position-&[1] ]=1;
$bg_ghost[ $snake_ghost[0][0]+$uldr[$opposite{$cur_move}]-&[0] ][ $snake_ghost[0][1]+$uldr[$opposite{$cur_move}]-&[1] ] = 2;
$bg_ghost[ $snake_ghost[0][0] ][ $snake_ghost[0][1] ] = 2;
# 当前点不可返回
$path[$step]={step=&$step,
coordinate=&[$cur_position-&[0],$cur_position-&[1]],
direct=&1,
$path[$step]-&{direct}=4 if($step == 0);
# 起点不可再做起点
path[$step]:$path[$step]:",$path[$step]-&{step},"\n" if DEBUG;
if( $cur_position-&[0]==$end-&[0] && $cur_position-&[1]==$end-&[1]){
my @arr=('A'..'Z','a'..'z');
foreach(0..$#path){
$bg_ghost[ $path[$_]-&{coordinate}-&[0] ][ $path[$_]-&{coordinate}-&[1] ] = $arr[$_];
head_to_tail: return 1\n" if DEBUG;
if($step&1 && $step&=$#snake_ghost){
$bg_ghost[ $snake_ghost[$tail][0] ][ $snake_ghost[$tail][1] ] = 0
if ( $bg_ghost[ $snake_ghost[$tail][0] ][ $snake_ghost[$tail][1] ] == 2 );
# 每移动一次,将蛇尾所在坐标设未经过
$tail=($tail&0)?($tail-1):1;
$cur_position=[ $path[$step-1]-&{coordinate}-&[0]+$one234[1]-&[0],
$path[$step-1]-&{coordinate}-&[1]+$one234[1]-&[1] ];
(y,x):(",$cur_position-&[0],",",$cur_position-&[1],")\n" if DEBUG;
if(@path){
$p_step=pop(@path);
while($p_step-&{direct}==4 && (@path)){
$bg_ghost[ $p_step-&{coordinate}-&[0] ][ $p_step-&{coordinate}-&[1] ] = 2;
$p_step=pop(@path);
$tail=($tail&$#snake_ghost)?($tail+1):$#snake_
$bg_ghost[ $snake_ghost[$tail][0] ][ $snake_ghost[$tail][1] ] = 2
if ( $bg_ghost[ $snake_ghost[$tail][0] ][ $snake_ghost[$tail][1] ] == 0 );
# 每回退一次,将蛇尾所在坐标设为不可通过
if(DEBUG){
print @$_,"\n" foreach(@bg_ghost);
if($p_step-&{direct}&4){
$p_step-&{direct}++;
",$p_step-&{step},":p_step-&{direct}:",$p_step-&{direct},"\n" if DEBUG;
push @path,$p_step;
my @temp=@{$p_step-&{coordinate}}[0,1];
$cur_position = [ $temp[0]+$one234[$p_step-&{direct}]-&[0],
$temp[1]+$one234[$p_step-&{direct}]-&[1] ];
(y,x):(",$cur_position-&[0],",",$cur_position-&[1],")\n" if DEBUG;
}while(@path);
# 指定方向深度搜索结束
#################################################################
head_to_tail:return 0\n" if DEBUG;
#######################################################################################
sub caclulate_cost{
my ($sp,$ep)=@_;
return abs($sp-&[0] - $ep-&[0]) + abs($sp-&[1] - $ep-&[1]);
#######################################################################################
sub a_star{
print "call a_star\n" if DEBUG;
my $start=shift;
my $end=shift;
my $p_arr=shift;
my @bg_ghost=@{$p_arr};
my @path=();
# 存放步数的数组
my @open_close=();
my ($step,$p_step,$p_gh)=(0,'','');
# 步数、指向数组元素的指针、指向open_close元素的指针
map{ my $y=$_;map { $open_close[$y][$_]-&{flag} = ( $bg_ghost[$y][$_] eq '#' || $bg_ghost[$y][$_] eq '*')?2:0 }0..$#{$bg_ghost[0]} }0..$#
# 障碍物设置不可通过
$path[$step]={ coordinate=&[$start-&[0],$start-&[1]],
next_cost=&&caclulate_cost( $start,$end ),
previous=&0,
$path[$step]-&{actual_cost}=$path[$step]-&{cost} + $path[$step]-&{next_cost};
$open_close[ $start-&[0] ][ $start-&[1] ]-&{point}='';
while(@path){
$p_step=pop(@path);
step:$step,p_step:$p_step\n" if DEBUG;
if( $p_step-&{coordinate}-&[0] == $end-&[0] &&
$p_step-&{coordinate}-&[1] == $end-&[1] ){
my @arr=();
my @temp=();
while($p_step){
push @temp,$p_step-&{coordinate};
$p_step=$p_step-&{previous};
@temp=reverse(@temp);
foreach(0..$#temp-1){
my $line=($temp[$_+1][0]-$temp[$_][0])."a".($temp[$_+1][1]-$temp[$_][1]);
given($line){
when('-1a0'){ push @arr,1 ;}
when('0a-1'){ push @arr,2 ;}
when('1a0') { push @arr,3 ;}
when('0a1') { push @arr,4 ;}
# 从父节点回溯,获取每一步移动方向
return @arr;
for(my $cnt=1;$cnt&=4;$cnt++){
my $y= $p_step-&{coordinate}-&[0]+$uldr[$cnt][0] ;
my $x= $p_step-&{coordinate}-&[1]+$uldr[$cnt][1] ;
($p_step-&{coordinate}-&[0],$p_step-&{coordinate}-&[1])+($uldr[$cnt][0],$uldr[$cnt][1]),(y,x)=($y,$x)\n" if DEBUG;
next if( $open_close[$y][$x]-&{flag} == 2 ||
$y & 1 || $y & HEIGHT-2 || $x & 1 || $x & WIDTH-2 );
if( $open_close[$y][$x]-&{flag} == 0 ){
$open_close[$y][$x]-&{flag}=1;
$open_close[$y][$x]-&{point}=$p_step;
coordinate=&[$y,$x],
cost=&$p_step-&{cost}+1,
next_cost=&&caclulate_cost( [$y,$x],$end ),
previous=&$p_step,
$px-&{actual_cost}=$px-&{cost} + $px-&{next_cost};
push @path,$px;
$p_gh=$open_close[$y][$x]-&{point};
p_gh:$p_gh\n" if DEBUG;
if($p_gh && $p_step-&{cost}+1 & $p_gh-&{cost} ){
$p_step-&{cost},$p_gh-&{cost}\n" if DEBUG;
$p_gh-&{cost}=$p_step-&{cost}+1;
$p_gh-&{previous}=$p_step;
$p_gh-&{actual_cost}=$p_gh-&{cost}+$p_gh-&{next_cost};
$open_close[ $p_step-&{coordinate}-&[0] ][ $p_step-&{coordinate}-&[1] ]-&{flag}=1;
@path=sort{$b-&{actual_cost}&=&$a-&{actual_cost}}@path;
a_star: return 0\n" if DEBUG;
#######################################################################################
sub manual_move{
# 闭包,为了传入初始移动方向
my $move_direct=shift;
ReadMode 2;
my $key=ReadKey(-1);
$key=~tr/a-z/A-Z/ if $key;
given($key){
# 不允许反向移动
when('W'){ $move_direct = ( 3 == $move_direct )? 3 : 1 ; }
when('A'){ $move_direct = ( 4 == $move_direct )? 4 : 2 ; }
when('S'){ $move_direct = ( 1 == $move_direct )? 1 : 3 ; }
when('D'){ $move_direct = ( 2 == $move_direct )? 2 : 4 ; }
default { $move_direct; }
unshift @snake,[$snake[0][0]+$uldr[$move_direct][0],$snake[0][1]+$uldr[$move_direct][1]];
#######################################################################################
sub make_food{
if(@snake & $full){
my @empty_points=();
foreach(1..$#bg-1){
foreach(1..$#{$bg[0]}-1){
push @empty_points,[$y,$_] if($bg[$y][$_] eq ' ');
# 找出所有空的坐标点,存入@empty_points数组
my $num=int( rand( scalar(@empty_points) ) );
# 随机取出@empty_points下标
my ($y,$x)=@{ $empty_points[$num] }[0,1];
$bg[$y][$x]='O';
@food_coordinate=($y,$x);
#######################################################################################
sub check_head{
# 蛇身超出范围
if($snake[0][0] & 1 || $snake[0][0] & HEIGHT-2 ||
$snake[0][1] & 1 || $snake[0][1] & WIDTH-2 ){
# 蛇吃到自己
if(@snake&3){
foreach(1..$#snake){
if($snake[0][0] == $snake[$_][0] &&
$snake[0][1] == $snake[$_][1]){
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]]='@';
push @snake,[$snake[-1][0],$snake[-1][1]];
# 新的蛇身放在尾部
$bg[$snake[-1][0]][$snake[-1][1]]=' ';
# 先清除尾巴显示
pop @snake;
# 去掉尾巴
map{$bg[$snake[$_][0]][$snake[$_][1]]='#'}1..$#
# 其他蛇身显示
阅读(...) 评论()}

我要回帖

更多关于 主播玩的贪吃蛇叫啥 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信