bonk.erl

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

ERL
577
字号
%% ``The contents of this file are subject to the Erlang Public License,%% Version 1.1, (the "License"); you may not use this file except in%% compliance with the License. You should have received a copy of the%% Erlang Public License along with this software. If not, it can be%% retrieved via the world wide web at http://www.erlang.org/.%% %% Software distributed under the License is distributed on an "AS IS"%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See%% the License for the specific language governing rights and limitations%% under the License.%% %% The Initial Developer of the Original Code is Ericsson Utvecklings AB.%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings%% AB. All Rights Reserved.''%% %%     $Id$%%-module(bonk).-export([run/0, run/1,bonk_dir/0,start/0]).-record(colors, {miss, x, bomb, face}).-record(scores, {points, level, bombs, hits, showed, bonus}).start() ->    spawn(bonk, run, []).run() ->    run(color).run([ColorMode]) ->   % This is for the start script...    run(ColorMode);run(ColorMode) when atom(ColorMode) ->    GS = gs:start(),    SoundPid=spawn_link(bonk_sound,start,[]),    {H,M,S}=time(),    random:seed(H*13,M*7,S*3),    {SqrPids, Bmps, Colors} = create_board(GS, ColorMode),    {ScoreL,_File} = get_highscore(),    display_highscore(ScoreL),    put(colormode, ColorMode),    SoundPid ! music,    sleep(6500),    gs:config(aboutButton, [{enable,true}]),    gs:config(newButton, [{enable,true}]),    gs:config(quitButton, [{enable,true}]),    idle(SoundPid, SqrPids, Bmps, Colors).%% This is not an application so we don't have their way of knowing%% a private data directory where the GIF files are located (this directory).%% We can find GS and makes it relative from there /kgb%%%% Note the silly slash that is added. The rest of the code uses%% append to contruct file names and assumes that the directory ends%% in slash. If filename:join was used and the problem is gone.-define(EbinFromGsPriv,"../contribs/bonk").bonk_dir() ->    GsPrivDir = code:priv_dir(gs),    filename:join(GsPrivDir,?EbinFromGsPriv) ++ "/".idle(SoundPid, SqrPids, Bmps, Colors) ->    receive	{gs, newButton, click, _Data, _Args} ->	    init(SoundPid, SqrPids, Bmps, Colors);	{gs, aboutButton, click, _Data, _Args} ->	    display_about(),	    idle(SoundPid, SqrPids, Bmps, Colors);	{gs, quitButton, click, _Data, _Args} ->	    SoundPid ! quit,	    send_to_all(SqrPids, quit);	_Other ->	    %%io:format("Got ~w in idle~n", [_Other]),	    idle(SoundPid, SqrPids, Bmps, Colors)    end.init(SoundPid, SqrPids, Bmps, Colors) ->    clear_board(Bmps),    SoundPid ! start,    gs:config(newButton, [{enable,false}]),    gs:config(endButton, [{enable,true}]),    gs:config(aboutButton, [{enable,false}]),    Scores = #scores{points=0, level=1, bombs=0, hits=0, showed=0, bonus=10},    clear_scores(Scores),    flush(),    send_to_all(SqrPids, start),    game(SoundPid, SqrPids, Bmps, Colors, Scores).game(SoundPid, SqrPids, Bmps, Colors, Scores) ->    receive  	{gs, _Square, buttonpress, SqrPid, [1 | _Rest]} when pid(SqrPid) ->	    SqrPid ! bonk,	    game(SoundPid, SqrPids, Bmps, Colors, Scores);	{gs, _Id, buttonpress, _Data, [Butt | _Rest]} when Butt =/= 1 ->	    NewScores = bomb(SoundPid, SqrPids, Scores),	    game(SoundPid, SqrPids, Bmps, Colors, NewScores);	{show, Square, Rect} ->	    NewScores = show_face(Square, Rect, Colors, Scores),	    game(SoundPid, SqrPids, Bmps, Colors, NewScores);	{hide, Square, Rect} ->	    NewScores = hide_face(Square, Rect, Colors, Scores),	    game(SoundPid, SqrPids, Bmps, Colors, NewScores);	{missed, Square, Rect} ->	    case miss_face(SoundPid, Square, Rect, Colors, Scores) of		{continue, NewScores} ->		    game(SoundPid, SqrPids, Bmps, Colors, NewScores);		{game_over, NewScores} ->		    game_over(SoundPid, SqrPids, Bmps, Colors, NewScores)	    end;	{bonked, Square, Rect} ->	    NewScores = bonked(SoundPid, SqrPids, Square, Rect, Scores, Colors),	    game(SoundPid, SqrPids, Bmps, Colors, NewScores);	{bombed, Square, Rect} ->	    NewScores = bombed(SoundPid, SqrPids, Square, Rect, Scores, Colors),	    game(SoundPid, SqrPids, Bmps, Colors, NewScores);	{gs, endButton, click, _Data, _Args} ->	    game_over(SoundPid, SqrPids, Bmps, Colors, Scores);	{gs, quitButton, click, _Data, _Args} ->	    quit(SoundPid, SqrPids, Bmps, Colors, Scores);	_Other ->	    game(SoundPid, SqrPids, Bmps, Colors, Scores)    end.	    game_over(SoundPid, SqrPids, Bmps, Colors, Scores) ->    SoundPid ! game_over,    send_to_all(SqrPids, stop),    flush(),    sleep(2000),    update_scorelist(SoundPid, Scores),    gs:config(newButton, [{enable,true}]),    gs:config(endButton, [{enable,false}]),    gs:config(aboutButton, [{enable,true}]),    idle(SoundPid, SqrPids, Bmps, Colors).quit(SoundPid, SqrPids, _Bmps, _Colors, _Scores) ->    SoundPid ! quit,    send_to_all(SqrPids, quit),    true.bomb(SoundPid, SqrPids, Scores) ->    case Scores#scores.bombs of	Bombs when Bombs > 0 ->	    send_to_all(SqrPids, bomb),	    SoundPid ! bomb,	    gs:config(bombOut,[{text,integer_to_list(Bombs-1)}]),	    Scores#scores{bombs=Bombs-1};	_Other ->	    Scores    end.show_face(Square, Rect, Colors, Scores) ->    Showed = Scores#scores.showed,    if	Showed == Scores#scores.level+1 ->	    Square ! sleep,	    Scores;	true ->	    FaceColors = Colors#colors.face,	    FaceColor = lists:nth(random:uniform(length(FaceColors)), FaceColors),	    gs:config(Rect, [{bitmap,lists:append(bonk_dir(),"bitmaps/bonkface")},{fg, FaceColor}]),	    Scores#scores{showed=Showed+1}    end.    hide_face(_Square, Rect, _Colors, Scores) ->    Showed = Scores#scores.showed,    gs:config(Rect, [{bitmap,lists:append(bonk_dir(),"bitmaps/bonktom")}]),    Scores#scores{showed=Showed-1}.miss_face(SoundPid, _Square, Rect, Colors, Scores) ->    SoundPid ! missed,    gs:config(Rect, [{bitmap,lists:append(bonk_dir(),"bitmaps/bonkmiss")}, {fg, Colors#colors.miss}]),    Bonus = Scores#scores.bonus,    if	Bonus > 1 ->	    gs:config(bonusOut, [{text,integer_to_list(Bonus-1)}]),	    {continue, Scores#scores{bonus=Bonus-1}};	true ->	    gs:config(bonusOut, [{text,"0"}]),	    {game_over, Scores}    end.bonked(SoundPid, SqrPids, _Square, Rect, Scores, Colors) ->    gs:config(Rect, [{bitmap,lists:append(bonk_dir(),"bitmaps/bonkx")}, {fg, Colors#colors.x}]),    SoundPid ! bonk,    update_score(SoundPid, SqrPids, Scores).bombed(SoundPid, SqrPids, _Square, Rect, Scores, Colors) ->    gs:config(Rect, [{bitmap,lists:append(bonk_dir(),"bitmaps/bonkbomb")}, {fg, Colors#colors.bomb}]),    update_score(SoundPid, SqrPids, Scores).update_score(SoundPid, SqrPids, Scores) ->    Points = Scores#scores.points,    Level = Scores#scores.level,    NewPoints = Points+Level,    gs:config(scoreOut,[{text,integer_to_list(NewPoints)}]),    case Scores#scores.hits of	24 ->	    SoundPid ! new_level,	    NewLevel = Level+1,	    NewBombs = Scores#scores.bombs+1,	    send_to_all(SqrPids, {new_level, NewLevel}),	    gs:config(levelOut,[{text,integer_to_list(NewLevel)}]),	    gs:config(bombOut,[{text,integer_to_list(NewBombs)}]),	    Scores#scores{points=NewPoints, level=NewLevel, hits=0, bombs=NewBombs};	Hits ->	    Scores#scores{points=NewPoints, hits=Hits+1}    end.	    send_to_all([], _Msg) ->    true;send_to_all([Pid|Rest],Msg) when pid(Pid) ->    Pid ! Msg,    send_to_all(Rest,Msg);send_to_all([_Else|Rest],Msg) ->    send_to_all(Rest,Msg).create_board(GS, ColorMode) ->    Colors =	case ColorMode of	    bw     -> #colors{miss=white, x=white, bomb=white, face=[white]};	    _Color -> #colors{miss=red, x=green, bomb=white,			      face=[lightblue, orange, magenta, peachpuff, pink]}	end,    BGCol     = if ColorMode==bw -> black; true -> black  end,	% background color    TextCol   = if ColorMode==bw -> white; true -> pink   end,	% status texts    NrCol     = if ColorMode==bw -> white; true -> purple end,	% status figures    LogoCol   = if ColorMode==bw -> white; true -> green  end,	% bonk logo    BLineCol  = if ColorMode==bw -> white; true -> grey   end,	% button line    SLineCol  = if ColorMode==bw -> white; true -> red    end,	% status line    BTextCol  = if ColorMode==bw -> white; true -> orange end,	% button text    HiHeadCol = if ColorMode==bw -> white; true -> red    end,	% high score label    HiCol     = if ColorMode==bw -> white; true -> cyan   end,	% high scores    SquareCol = if ColorMode==bw -> white; true -> yellow end,	% game squares    ErlFgCol  = if ColorMode==bw -> white; true -> red    end,	%    ErlBGCol  = if ColorMode==bw -> black; true -> white  end,	%    ErlTxtCol = if ColorMode==bw -> white; true -> black  end,	%    Width = 550,      	       % width of bonk window    Height = 550,     	       % Height of bonk window        BX = 0,           	       % x-pos for first button    DBX = 100,        	       % space between buttons    BY = 0,           	       % y-pos for buttons    BLineY = 30,      	       % y-pos of button line    LogoX = (Width-320) div 2, % x-pos of bonk logo (logo is 320 pix wide)    LogoY = BLineY+2, 	       % y-pos of bonk logo    ErlLogoX = LogoX + 200,    % x-pos of Erlang e    ErlLogoY = LogoY + 10,     % y-pos of Erlang e    SLineY = Height-22,        % status line position    TextWidth = 50,   	       % text width of status items    SX = 2,          	       % x-pos for first status item    DSX = TextWidth+94,	       % pixels between status items    SY = SLineY+2,   	       % y-pos status items    HiWidth = 100,             % width of high score field    _HiHeight = 180,            % height of the same    HiX = Width-HiWidth,       % high score text position    HiY = BLineY+10,    DHY = 20,                  % space between title & scores    SquareSize = 76,           % size of each game square    SquareSpace = 1,           % space between game squares    SquareX = 40,    SquareY = 65,        gs:create(window, bonkWin, GS, [{width, Width}, {height, Height}, 				    {bg, BGCol},				    {title, "Bonk the game"},				    {iconname, "Bonk!"},				    {map, false}]),    gs:create(canvas, bonkCanvas, bonkWin, [{width, Width},					    {height, Height},					    {bg, BGCol}]),    gs:create(image, bonkCanvas, [{bitmap,lists:append(bonk_dir(), "bitmaps/bonklogo")},				  {coords, [{LogoX, LogoY}]},

⌨️ 快捷键说明

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