Проект "Вгадай мелодію"

Пропоную на уроці інформатики виготовити проект "Guess a melody" (Вгадай мелодію). 

До якої теми його можна застосувати? До будь-якої теми з програмування: тут і рядки і масиви і величини, робота з файлами. Раджу виконувати проект поступово (протягом кількох уроків). 

Талановиті учні зможуть розвинути основну ідею. Але найціннішим є те, що він має абсолютгно практичне спрямування - використовуйте його на своїх уроках для релаксації, а вчителям музичного мистецтва він знадобиться на кожному уроці.

Мій варіант для Borland Delphi 7.0   












Лістинг проекту з коментарями:

unit melody;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, MPlayer, jpeg;

type
  TForm1 = class(TForm)
    MediaPlayer1: TMediaPlayer;
    Image1: TImage;
    Image2: TImage;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Image3: TImage;
    Image4: TImage;
    procedure FormCreate(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure Label1Click(Sender: TObject);
    procedure Label2Click(Sender: TObject);
    procedure Label3Click(Sender: TObject);
    procedure Label4Click(Sender: TObject);
   
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;   list: textfile; song: array [1..100] of string;
                                   mp: array [1..100] of string;

  k,i,z,f,j,p,a,b,c,d: integer; s: tSearchRec;  n1,n2,n3,n4:string;
  namel, numl: string;  songg:pansichar;
implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
        // формування масиву назв пісень
k:=0; f:=0;
 if FindFirst('MELODY\*.mp3',faAnyFile,s) = 0 then
    repeat
      k:=k+1;
      song[k]:=s.Name;
    until FindNext(s) <> 0;
  FindClose(s);

 image2.left:=form1.Width div 2 - image2.Width div 2;
 image2.top:=form1.height - form1.height div 6 - image2.height div 2;

 label1.top:=form1.Height div 10;
 label2.top:=label1.top+50;
 label3.top:=label2.top+50;
 label4.top:=label3.top+50;

 

end;

procedure TForm1.Image2Click(Sender: TObject);
begin
label1.Enabled:=true;
label2.Enabled:=true;
label3.Enabled:=true;
label4.Enabled:=true;
label1.font.Color:=clblack;
label2.font.Color:=clblack;
label3.font.Color:=clblack;
label4.font.Color:=clblack;
image3.Visible:=false;
image4.Visible:=false;

// вибір 1 пісні
repeat
p:=1;
randomize; // включення генератора випадкових чисел
 j:=random(k+1);
 if j=0 then j:=j+1;
 n1:=song[j];
           // занесення пісні в базу даних - вона вже грала
 for z:=1 to f do
 if mp[z]=n1 then p:=0;

 if f=k then begin p:=1; f:=0; end;
 until p=1;

 f:=f+1;
 mp[f]:=n1;

 repeat
 j:=random(k+1);
 if j=0 then j:=j+1;
 n2:=song[j];

 j:=random(k+1);
 if j=0 then j:=j+1;
 n3:=song[j];

 j:=random(k+1);
 if j=0 then j:=j+1;
 n4:=song[j];

 until (n1<>n2) and (n1<>n3) and (n1<>n4) and
           (n2<>n3) and (n2<>n4) and
             (n3<>n4);



 repeat
a:=random(4)+1; b:=random(4)+1; c:=random(4)+1; d:=random(4)+1;
until (a<>b) and (a<>c) and (a<>d) and
           (b<>c) and (b<>d) and
             (c<>d);


numl:=inttostr(a);
namel:='Label'+numl;
TLabel(FindComponent(namel)).caption:=n1;

numl:=inttostr(b);
namel:='Label'+numl;
TLabel(FindComponent(namel)).caption:=n2;

numl:=inttostr(c);
namel:='Label'+numl;
TLabel(FindComponent(namel)).caption:=n3;

numl:=inttostr(d);
namel:='Label'+numl;
TLabel(FindComponent(namel)).caption:=n4;

label1.Visible:=true;
label2.Visible:=true;
label3.Visible:=true;
label4.Visible:=true;

 songg:=pansichar('MELODY\'+n1);
 mediaplayer1.FileName:=songg;
 mediaplayer1.Open;
 mediaplayer1.Play;

 end;

procedure TForm1.Label1Click(Sender: TObject);
begin
mediaplayer1.Stop;
 numl:=inttostr(a);
 namel:='Label'+numl;

 if namel='Label1' then begin
                        image3.Width:=100;
                        image3.Height:=100;
                        image3.left:=form1.Width div 2 - image3.Width div 2;
                        image3.top:=form1.Height div 2 -image3.Height div 2;
                        image3.Visible:=true;
                        label1.font.Color:=clgreen;
                        end
                        else
                        begin
                        image4.Width:=100;
                        image4.Height:=100;
                        image4.left:=form1.Width div 2 -image4.Width div 2;
                        image4.top:=form1.Height div 2 -image4.Height div 2;
                        image4.Visible:=true;
                        label1.font.Color:=clred;
                        end;
label2.Enabled:=false;
label3.Enabled:=false;
label4.Enabled:=false;

end;




procedure TForm1.Label2Click(Sender: TObject);
begin
mediaplayer1.Stop;
 numl:=inttostr(a);
 namel:='Label'+numl;

 if namel='Label2' then begin
                        image3.Width:=100;
                        image3.Height:=100;
                        image3.left:=form1.Width div 2 - image3.Width div 2;
                        image3.top:=form1.Height div 2 -image3.Height div 2;
                        image3.Visible:=true;
                        label2.font.Color:=clgreen;
                        end
                        else
                        begin
                        image4.Width:=100;
                        image4.Height:=100;
                        image4.left:=form1.Width div 2 -image4.Width div 2;
                        image4.top:=form1.Height div 2 -image4.Height div 2;
                        image4.Visible:=true;
                        label2.font.Color:=clred;
                        end;
label1.Enabled:=false;
label3.Enabled:=false;
label4.Enabled:=false;

end;

procedure TForm1.Label3Click(Sender: TObject);
begin
mediaplayer1.Stop;
 numl:=inttostr(a);
 namel:='Label'+numl;

 if namel='Label3' then begin
                        image3.Width:=100;
                        image3.Height:=100;
                        image3.left:=form1.Width div 2 - image4.Width div 2;
                        image3.top:=form1.Height div 2 -image4.Height div 2;
                        image3.Visible:=true;
                        label3.font.Color:=clgreen;
                        end
                        else
                        begin
                        image4.Width:=100;
                        image4.Height:=100;
                        image4.left:=form1.Width div 2 -image4.Width div 2;
                        image4.top:=form1.Height div 2 -image4.Height div 2;
                        image4.Visible:=true;
                        label3.font.Color:=clred;
                        end;
label1.Enabled:=false;
label2.Enabled:=false;
label4.Enabled:=false;
end;

procedure TForm1.Label4Click(Sender: TObject);
begin
mediaplayer1.Stop;
 numl:=inttostr(a);
 namel:='Label'+numl;

 if namel='Label4' then begin
                        image3.Width:=100;
                        image3.Height:=100;
                        image3.left:=form1.Width div 2 - image3.Width div 2;
                        image3.top:=form1.Height div 2 -image3.Height div 2;
                        image3.Visible:=true;
                        label4.font.Color:=clgreen;
                        end
                        else
                        begin
                        image4.Width:=100;
                        image4.Height:=100;
                        image4.left:=form1.Width div 2 -image4.Width div 2;
                        image4.top:=form1.Height div 2 -image4.Height div 2;
                        image4.Visible:=true;
                        label4.font.Color:=clred;
                        end;
label1.Enabled:=false;
label2.Enabled:=false;
label3.Enabled:=false;
end;

end.


Немає коментарів:

Дописати коментар