根据站主“色块消除游戏”的帖子改编的2048游戏。
By
o3o_o3o_o3o
at 2022-01-25 • 0人收藏 • 847人看过
根据“色块消除游戏”改编的2048。虽然没有那种原版2048流利的动画,但是也是非常用心了。代码有些啰嗦,希望一些大佬可以帮我改一下,谢谢。
import win.ui; import win.ui.menu; /*DSG{{*/ var winform = win.form(text="2048";right=759;bottom=469;max=false) winform.add( p11={cls="plus";left=228;top=54;right=288;bottom=114;dl=1;dt=1;font=LOGFONT(h=-18);forecolor=15780518;notify=1;z=1}; p12={cls="plus";left=295;top=54;right=355;bottom=114;dl=1;dt=1;font=LOGFONT(h=-18);forecolor=15780518;notify=1;z=2}; p13={cls="plus";left=362;top=54;right=422;bottom=114;dl=1;dr=1;dt=1;font=LOGFONT(h=-18);forecolor=15780518;notify=1;z=3}; p14={cls="plus";left=429;top=54;right=489;bottom=114;dr=1;dt=1;font=LOGFONT(h=-18);forecolor=15780518;notify=1;z=4}; p21={cls="plus";left=228;top=120;right=288;bottom=178;dl=1;dt=1;font=LOGFONT(h=-18);forecolor=15780518;notify=1;z=5}; p22={cls="plus";left=295;top=120;right=355;bottom=178;dl=1;dt=1;font=LOGFONT(h=-18);forecolor=15780518;notify=1;z=6}; p23={cls="plus";left=362;top=120;right=422;bottom=178;dl=1;dr=1;dt=1;font=LOGFONT(h=-18);forecolor=15780518;notify=1;z=7}; p24={cls="plus";left=429;top=120;right=489;bottom=178;dr=1;dt=1;font=LOGFONT(h=-18);forecolor=15780518;notify=1;z=8}; p31={cls="plus";left=228;top=182;right=288;bottom=249;db=1;dl=1;dt=1;font=LOGFONT(h=-18);forecolor=15780518;notify=1;z=9}; p32={cls="plus";left=295;top=182;right=355;bottom=249;db=1;dl=1;dt=1;font=LOGFONT(h=-18);forecolor=15780518;notify=1;z=10}; p33={cls="plus";left=362;top=182;right=422;bottom=249;db=1;dl=1;dr=1;dt=1;font=LOGFONT(h=-18);forecolor=15780518;notify=1;z=11}; p34={cls="plus";left=429;top=182;right=489;bottom=249;db=1;dr=1;dt=1;font=LOGFONT(h=-18);forecolor=15780518;notify=1;z=12}; p41={cls="plus";left=228;top=254;right=288;bottom=314;db=1;dl=1;font=LOGFONT(h=-18);forecolor=15780518;notify=1;z=13}; p42={cls="plus";left=295;top=254;right=355;bottom=314;db=1;dl=1;font=LOGFONT(h=-18);forecolor=15780518;notify=1;z=14}; p43={cls="plus";left=362;top=254;right=422;bottom=314;db=1;dl=1;dr=1;font=LOGFONT(h=-18);forecolor=15780518;notify=1;z=15}; p44={cls="plus";left=429;top=254;right=489;bottom=314;db=1;dr=1;font=LOGFONT(h=-18);forecolor=15780518;notify=1;z=16}; plus={cls="plus";text="重新开始";left=228;top=369;right=489;bottom=425;color=16777215;db=1;dl=1;dr=1;forecolor=15780518;notify=1;z=17} ) /*}}*/ import key; import key.hotkey; import process; var g=1; ShowColor = function(){}//先声明一下 var menu = win.ui.menu(winform); menu.add("规则",function(){ var h; h = win.msgboxTest("详见网址:"++'\n'++"https://www.ak-ioi.com/apps/oi-2048/wiki/"++'\n'++"(只不过我这里没有倍增方块)"++'\n'++"点击“确定”打开此链接,否则返回游戏界面。","2048") if(h==1){ process.execute("https://www.ak-ioi.com/apps/oi-2048/wiki/"); } }) hotkey = key.hotkey(winform); a ={ {0;0;0;0}; {0;0;0;0}; {0;0;0;0}; {0;0;0;0}; } create = function(){ var num=0,de,x,y,z,o=0; for(i=1;4;1){ for(j=1;4;1){ if(a[i][j]==0){ o=1; } } } if(o==1){ do{ x = math.random(1,4); y = math.random(1,4); }while(a[x][y]!=0)//随机生成 a[x][y] = math.random(1,2)*2; o=0; } } IsEnd = function(){ var p=0,o=0; for(i=1;4;1){ for(j=1;4;1){ if(a[i][j]==0){ o=1; } } } if(o!=1){ for(i=1;4;1){ for(j=1;3;1){ if((a[j][i]!=0)&&(a[j+1][i]==a[j][i])){ p=1; } } } for(i=1;4;1){ for(j=4;2;-1){ if((a[i][j]!=0)&&(a[i][j-1]==a[i][j])){ p=1; } } } for(i=1;4;1){ for(j=1;3;1){ if((a[i][j]!=0)&&(a[i][j+1]==a[i][j])){ p=1; } } } for(i=1;4;1){ for(j=4;2;-1){ if((a[j][i]!=0)&&(a[j-1][i]==a[j][i])){ p=1; } } } if(p==0){ g = 0; g = win.msgboxTest("游戏失败,点击“确定”重新开始游戏,否则退出游戏。","2048") if(g==1){ for(i=1;4;1){ for(j=1;4;1){ a[i][j] = 0; } } create(); create(); ShowColor(); } else{ hotkey.close(); winform.close(); } } else{ p=0; } } } right = function(){//思路详见链接 var O3O = 0; for(i=1;4;1){ for(j=1;3;1){ if((a[i][j+1]==0)&&(a[i][j]!=0)){ a[i][j+1] = a[i][j]; a[i][j] = 0; j=0; O3O=1; } } } for(i=1;4;1){//再来一遍,防止某块没有遍历到。 for(j=1;3;1){ if((a[i][j+1]==0)&&(a[i][j]!=0)){ a[i][j+1] = a[i][j]; a[i][j] = 0; j=0; O3O=1; } } } for(i=1;4;1){ for(j=4;2;-1){ if((a[i][j]!=0)&&(a[i][j-1]==a[i][j])){ a[i][j] *= 2; a[i][j-1] = 0; O3O=1; } } } for(i=1;4;1){ for(j=1;3;1){ if((a[i][j+1]==0)&&(a[i][j]!=0)){ a[i][j+1] = a[i][j]; a[i][j] = 0; j=0; O3O=1; } } } if(O3O==1){ O3O = 0; return 1; } else{ return 0; } } left = function(){ var O3O = 0; for(i=1;4;1){ for(j=4;2;-1){ if((a[i][j-1]==0)&&(a[i][j]!=0)){ a[i][j-1] = a[i][j]; a[i][j] = 0; j=4; O3O=1; } } } for(i=1;4;1){ for(j=4;2;-1){ if((a[i][j-1]==0)&&(a[i][j]!=0)){ a[i][j-1] = a[i][j]; a[i][j] = 0; j=4; O3O=1; } } } for(i=1;4;1){ for(j=1;3;1){ if((a[i][j]!=0)&&(a[i][j+1]==a[i][j])){ a[i][j] *= 2; a[i][j+1] = 0; O3O=1; } } } for(i=1;4;1){ for(j=4;2;-1){ if((a[i][j-1]==0)&&(a[i][j]!=0)){ a[i][j-1] = a[i][j]; a[i][j] = 0; j=4; O3O=1; } } } if(O3O==1){ O3O = 0; return 1; } else{ return 0; } } up = function(){ var O3O = 0; for(i=1;4;1){ for(j=4;2;-1){ if((a[j-1][i]==0)&&(a[j][i]!=0)){ a[j-1][i] = a[j][i]; a[j][i] = 0; j=4; O3O=1; } } } for(i=1;4;1){ for(j=4;2;-1){ if((a[j-1][i]==0)&&(a[j][i]!=0)){ a[j-1][i] = a[j][i]; a[j][i] = 0; j=4; O3O=1; } } } for(i=1;4;1){ for(j=1;3;1){ if((a[j][i]!=0)&&(a[j+1][i]==a[j][i])){ a[j][i] *= 2; a[j+1][i] = 0; O3O=1; } } } for(i=1;4;1){ for(j=4;2;-1){ if((a[j-1][i]==0)&&(a[j][i]!=0)){ a[j-1][i] = a[j][i]; a[j][i] = 0; j=4; O3O=1; } } } if(O3O==1){ O3O = 0; return 1; } else{ return 0; } } down = function(){ var O3O = 0; for(i=1;4;1){ for(j=1;3;1){ if((a[j+1][i]==0)&&(a[j][i]!=0)){ a[j+1][i] = a[j][i]; a[j][i] = 0; j=0; O3O=1; } } } for(i=1;4;1){ for(j=1;3;1){ if((a[j+1][i]==0)&&(a[j][i]!=0)){ a[j+1][i] = a[j][i]; a[j][i] = 0; j=0; O3O=1; } } } for(i=1;4;1){ for(j=4;2;-1){ if((a[j][i]!=0)&&(a[j-1][i]==a[j][i])){ a[j][i] *= 2; a[j-1][i] = 0; O3O=1; } } } for(i=1;4;1){ for(j=1;3;1){ if((a[j+1][i]==0)&&(a[j][i]!=0)){ a[j+1][i] = a[j][i]; a[j][i] = 0; j=0; O3O=1; } } } if(O3O==1){ O3O = 0; return 1; } else{ return 0; } } ShowColor = function(){//显示颜色 for(i=1;4;1){ for(j=1;4;1){ if(a[i][j]==0){ winform["p"++i++j].foreground = 0xFFC9F8FF; winform["p"++i++j].text = ""; } elseif(a[i][j]==2){ winform["p"++i++j].foreground = 0xFFF1C40F; winform["p"++i++j].text = "2"; } elseif(a[i][j]==4){ winform["p"++i++j].foreground = 0xFF3498DB; winform["p"++i++j].text = "4"; } elseif(a[i][j]==8){ winform["p"++i++j].foreground = 0xFF8E44AD; winform["p"++i++j].text = "8"; } elseif(a[i][j]==16){ winform["p"++i++j].foreground = 0xFF2E468C; winform["p"++i++j].text = "16"; } elseif(a[i][j]==32){ winform["p"++i++j].foreground = 0xFF38495D; winform["p"++i++j].text = "32"; } elseif(a[i][j]==64){ winform["p"++i++j].foreground = 0xFFE74E3B; winform["p"++i++j].text = "64"; } elseif(a[i][j]==128){ winform["p"++i++j].foreground = 0xFFD4862A; winform["p"++i++j].text = "128"; } elseif(a[i][j]==256){ winform["p"++i++j].foreground = 0xFF67B762; winform["p"++i++j].text = "256"; } elseif(a[i][j]==512){ winform["p"++i++j].foreground = 0xFFE9908D; winform["p"++i++j].text = "512"; } elseif(a[i][j]==1024){ winform["p"++i++j].foreground = 0xFFC0C0C0; winform["p"++i++j].text = "1024"; } elseif(a[i][j]==2048){ winform["p"++i++j].foreground = 0xFF66C6FF; winform["p"++i++j].text = "2048"; g = 0; g = win.msgboxTest("游戏通过,点击“确定”重新开始游戏,否则退出游戏。","2048") if(g==1){ for(i=1;4;1){ for(j=1;4;1){ a[i][j] = 0; } } create(); create(); ShowColor(); } else{ hotkey.close(); winform.close(); } } } } } import console; create(); create(); ShowColor(); hotkey.reg(//热键 "RIGHT", function(hFocusWnd,...){ if(g!=0){ if(right()){ create(); } ShowColor(); IsEnd(); } } ) hotkey.reg( "LEFT", function(hFocusWnd,...){ if(g!=0){ if(left()){ create(); } ShowColor(); IsEnd(); } } ) hotkey.reg( "UP", function(hFocusWnd,...){ if(g!=0){ if(up()){ create(); } ShowColor(); IsEnd(); } } ) hotkey.reg( "DOWN", function(hFocusWnd,...){ if(g!=0){ if(down()){ create(); } ShowColor(); IsEnd(); } } ) winform.plus.oncommand = function(id,event){//重新开始按钮 for(i=1;4;1){ for(j=1;4;1){ a[i][j] = 0; } } create(); create(); ShowColor(); } winform.show(); win.loopMessage();
4 个回复 | 最后更新于 2022-01-26
2022-01-26
#2
我把代码改了一下,把数组a中的函数挪到了外面,并且把create函数拆开,变成了create和IsEnd,一个用来生成,另一个用来检测是否游戏结束。这样就不会出现“玩家已经发现游戏结束但是还需要再按一下方向键才能弹出msgbox窗口”的bug了。
代码有些长,我用代码上传工具把代码上传到了这个链接里:
https://paste.rs/aFW.js
登录后方可回帖
代码工整清晰,明天试试,先赞一个