bonk.erl

来自「OTP是开放电信平台的简称」· ERL 代码 · 共 577 行 · 第 1/2 页

ERL
577
字号
				  {fg, LogoCol},				  {bg, BGCol}]),    gs:create(image, bonkCanvas, [{bitmap,lists:append(bonk_dir(), "bitmaps/erl-e")},				  {coords, [{ErlLogoX, ErlLogoY}]},				  {fg, ErlFgCol},				  {bg, ErlBGCol}]),    gs:create(image, bonkCanvas, [{bitmap,lists:append(bonk_dir(), "bitmaps/erl-text")},				  {coords, [{ErlLogoX, ErlLogoY}]},				  {fg, ErlTxtCol}]),    gs:create(line, bLine, bonkCanvas, [{coords, [{0,BLineY}, {Width,BLineY}]},					{fg, BLineCol},					{width, 2}]),    gs:create(line, bLine, bonkCanvas, [{coords, [{0,SLineY}, {Width, SLineY}]},					{fg, SLineCol},					{width, 2}]),    gs:create(text, scoreText, bonkCanvas, [{coords, [{SX, SY}]},					    {fg, TextCol},					    {text, "Score:"}]),    gs:create(text, scoreOut, bonkCanvas, [{coords, [{SX+TextWidth, SY}]},					   {fg, NrCol},					   {width, DSX-TextWidth},					   {text, ""}]),    gs:create(text, bombText, bonkCanvas, [{coords, [{SX+DSX, SY}]},					   {fg, TextCol},					   {text, "Bombs:"}]),    gs:create(text, bombOut, bonkCanvas, [{coords, [{SX+DSX+TextWidth, SY}]},					  {fg, NrCol},					  {width, DSX-TextWidth},					  {text, ""}]),    gs:create(text, bonusText, bonkCanvas, [{coords, [{SX+2*DSX, SY}]},					    {fg, TextCol},					    {text, "Bonus:"}]),    gs:create(text, bonusOut, bonkCanvas, [{coords, [{SX+2*DSX+TextWidth, SY}]},					   {fg, NrCol},					   {width, DSX-TextWidth},					   {text, ""}]),    gs:create(text, levelText,bonkCanvas, [{coords, [{SX+3*DSX, SY}]},					   {fg, TextCol},					   {text, "Level:"}]),    gs:create(text, levelOut, bonkCanvas, [{coords, [{SX+3*DSX+TextWidth, SY}]},					   {fg, NrCol},					   {width, DSX-TextWidth},					   {text, ""}]),    gs:create(text, hiScoreText, bonkCanvas, [{coords, [{HiX, HiY}]},					      {fg, HiHeadCol},					      {text, "High Scores"}]),    gs:create(text, hiScoreOut, bonkCanvas, [{coords, [{HiX, HiY+DHY}]},					     {fg, HiCol},					     {justify, left},					     {width, HiWidth}]),    gs:create(button, newButton,bonkWin, [{x, BX},{y, BY},					  {enable,false},					  {label, {text, "New Game"}},					  {click, true},					  {fg, BTextCol},					  {bg, BGCol},					  {relief, flat},					  {activefg, BTextCol},					  {activebg, BGCol},					  {align, center}]),    gs:create(button, endButton,bonkWin, [{x, BX+DBX},{y, BY},					  {enable,false},					  {label, {text, "End Game"}},					  {click, true},					  {fg, BTextCol},					  {bg, BGCol},					  {relief, flat},					  {activefg, BTextCol},					  {activebg, BGCol},					  {align, center}]),    gs:create(button, aboutButton,bonkWin, [{x, BX+2*DBX},{y, BY},					    {enable,false},					    {label, {text, "About"}},					    {click, true},					    {fg, BTextCol},					    {bg, BGCol},					    {relief, flat},					    {activefg, BTextCol},					    {activebg, BGCol},					    {align, center}]),    gs:create(button, quitButton, bonkWin, [{x, BX+3*DBX},{y, BY},					    {enable,false},					    {label, {text, "Quit"}},					    {click, true},					    {fg, BTextCol},					    {bg, BGCol},					    {relief, flat},					    {activefg, BTextCol},					    {activebg, BGCol},					    {align, center}]),    {SqrPids, Bmps} =	create_squares(SquareX, SquareY, SquareSize, SquareCol, SquareSpace),    gs:config(bonkWin, [{map, true}]),    {SqrPids, Bmps, Colors}.create_squares(X, Y, Size, Color, Spc) ->    create_squares(X, Y, Size, Color, Spc, 1, 1, [], []).create_squares(_X, _Y, _Size, _Color, _Spc, 4, 5, Pids, Bmps) ->    {Pids, Bmps};create_squares(X, Y, Size, Color, Spc, Row, 5, Pids, Bmps) ->    create_squares(X, Y, Size, Color, Spc, Row+1, 1, Pids, Bmps);create_squares(X, Y, Size, Color, Spc, Row, Col, Pids, Bmps) ->    Xpos = X+Col*Size+(Col-1)*Spc,    Ypos = Y+Row*Size+(Row-1)*Spc,    gs:create(rectangle, bonkCanvas,	      [{coords, [{Xpos,Ypos},{Xpos+Size, Ypos+Size}]},	       {bw, 2},{fg, Color},{buttonpress,true}]),    Bmp = gs:create(image, bonkCanvas,		    [{coords, [{Xpos+1, Ypos+1}]},		     {bitmap,lists:append(bonk_dir(), "bitmaps/bonktom")},		     {buttonpress, true},{fg, Color}]),    Pid = bonk_square:start(Bmp),    gs:config(Bmp, [{data, Pid}]),    create_squares(X, Y, Size, Color, Spc, Row, Col+1, [Pid|Pids], [Bmp|Bmps]).clear_board([]) ->    true;clear_board([Square | Rest]) ->    gs:config(Square, [{bitmap,lists:append(bonk_dir(), "bitmaps/bonktom")}]),    clear_board(Rest).%% Prints the list on the screen.%% The list is on the form [[Score,Name],[Score,Name]..].display_highscore(ScoreList) ->    display_highscore("",ScoreList,0).display_highscore(Scores,[],_N) ->    gs:config(hiScoreOut,[{text,Scores}]);display_highscore(Scores,_ScoreList,10) ->   % This is max number of items.    display_highscore(Scores,[], 10);display_highscore(Scores,[[Score,Name]|Rest],N) ->    NewScores = lists:append(Scores,lists:append(lists:append(Score, [32 | Name]), [10])),    display_highscore(NewScores,Rest,N+1).%% Reads the highscorelist from the file "bonk.score".%% The list should be an sorted erlang-list.get_highscore() ->    case file:consult("bonk.score") of	{ok,[Score_list]} ->	    {Score_list,"./bonk.score"};	{error,_} ->	    {[],"./bonk.score"}    end.%% Prints out the highscorelist and places the new score in the%% list if it is high enough.update_scorelist(SoundPid, Scores) ->    case Scores#scores.points of	0 -> true;	Score ->	    {ScoreL,FileName} = get_highscore(),	    New_scorelist=update_scorelist_2(ScoreL, Score, 0, SoundPid),	    display_highscore(New_scorelist),	    case file:open(FileName, write) of		{error,_} ->		    true;		{ok,FD} ->		    io:format(FD,"~w~s~n",[New_scorelist,"."]),		    file:close(FD)	    end    end.update_scorelist_2([], Score, N, _SoundPid) when N < 10 ->    [[integer_to_list(Score),getuser()]];update_scorelist_2(_, _, N, _SoundPid) when N >= 10 ->    [];update_scorelist_2([[Sc, Name] | Rest], Score, N, SoundPid) ->    case list_to_integer(Sc) of	Sc_int when Sc_int < Score ->	    if		N == 0 -> SoundPid ! best_score;		true   -> SoundPid ! high_score	    end,	    lists:append([[integer_to_list(Score),getuser()]],			 update_scorelist_3([[Sc,Name]|Rest],N+1));	_Other ->	    lists:append([[Sc,Name]],update_scorelist_2(Rest, Score, N+1, SoundPid))    end.update_scorelist_3([],_) ->    [];update_scorelist_3(_,N) when N >= 10 ->    [];update_scorelist_3([Item|Rest],N) ->    lists:append([Item],update_scorelist_3(Rest,N+1)).getuser() ->    case os:type() of	{unix,_} ->	    lists:delete(10,os:cmd("whoami"));	_ ->	    "Unknown"    end.%% Prints out the initial values of scores, bonus, level and bombs.clear_scores(Scores) ->    Score = integer_to_list(Scores#scores.points),    Bombs = integer_to_list(Scores#scores.bombs),    Bonus = integer_to_list(Scores#scores.bonus),    Level = integer_to_list(Scores#scores.level),    gs:config(scoreOut,{text,Score}),    gs:config(bombOut,{text,Bombs}),    gs:config(bonusOut,{text,Bonus}),    gs:config(levelOut,{text,Level}).%% Removes everything that is present in the message-que.flush() ->    receive	_X ->	    flush()	after	    0 ->		true	end.sleep(X) ->    receive after X -> true end.%% Opens a window and shows the contents of the file: "bonk.txt".%% The window will be removed before the function ends.display_about() ->    {BGColor,TextColor,Bfg} =	case get(colormode) of	    bw     -> {black, white, white};	    _Color -> {black, peachpuff1, orange}	end,    Wid = 500, Hei = 635,     GS = gs:start(),    gs:create(window, aboutWin, GS, [{width, Wid}, {height,Hei},				     {map, false},				     {bg, BGColor},				     {title, "About Bonk!"}]),    gs:create(canvas, aboutCan, aboutWin, [{width, Wid},{height, Hei},					   {bg, black}]),    gs:create(button, okButton, aboutWin, [{x, Wid div 2 - 50},{y, Hei - 40},					   {label,{text, "Ok"}}, {click, true},					   {fg, Bfg}, {bg, BGColor},					   {relief, flat},					   {activefg, Bfg},					   {activebg, BGColor}]),    gs:create(text, aboutText, aboutCan, [{width, Wid-30}, {coords, [{15, 0}]},					  {fg, TextColor}, {justify, center}]),    case file:open(lists:append(bonk_dir(),"bonk.txt"), read) of	{ok, Fd} ->	    write_text(Fd, "", io:get_line(Fd, "")),	    file:close(Fd);	{error, _Reason} ->	    gs:config(aboutText, {text, "Error: could not read the about file"})    end,    gs:config(aboutWin, [{map,true}]),    receive	{gs, okButton, click, _, _} ->	    gs:destroy(aboutWin)    end.write_text(_Fd, Text, eof) ->    gs:config(aboutText, {text, Text});write_text(Fd, Text, More) ->    write_text(Fd, lists:append(Text, More), io:get_line(Fd, "")).

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?