Ver Mensaje Individual
  #9  
Antiguo 30-05-2015
Avatar de nlsgarcia
[nlsgarcia] nlsgarcia is offline
Miembro Premium
 
Registrado: feb 2007
Ubicación: Caracas, Venezuela
Posts: 2.206
Reputación: 21
nlsgarcia Tiene un aura espectacularnlsgarcia Tiene un aura espectacular
Vilkov,

Cita:
Empezado por Vilkov
...nuestro profesor nos ha encomendado la tarea de realizar un juego...he logrado hacer el diseño y la parte del juego, pero no tengo ni la menor idea de como hacer para que al clicar el botón "iniciar", en un StaticText, me salga el nombre aleatorio de los datos del StringGrid...


Revisa este código:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Buttons, Grids, jpeg, Math, ComCtrls;

type

  TShape = class(ExtCtrls.TShape);

  TForm1 = class(TForm)
    Image1: TImage;
    Image2: TImage;
    Image3: TImage;
    StringGrid1: TStringGrid;
    StaticText1: TStaticText;
    BitBtn1: TBitBtn;
    Shape1: TShape;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    StatusBar1: TStatusBar;
    procedure FormCreate(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure ValidateImage(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  SelectImage : Integer;
  PreviousImage : Integer;
  RndImg : Array of Byte;
  Success, Error : Integer;
  StartGame : Boolean = False;

  Ask : Array[0..2] of String = ('¿Cual es la Imagen del Universo?',
                                 '¿Cual es la Imagen de la Tierra?',
                                 '¿Cual es la Imagen de la Galaxia Espiral?');

implementation

{$R *.dfm}

// Configura los componentes de la aplicación
procedure TForm1.FormCreate(Sender: TObject);
var
   Row, Col : Integer;

begin

   // Configura StaticText1
   StaticText1.Caption := 'Click al Botón con el Bombillo';
   StaticText1.Font.Color := clNavy;
   StaticText1.Font.Name := 'Tahoma';
   StaticText1.Font.Size := 12;

   // Configura Image(x) y Button(x)
   Image1.Center := True;
   Image1.Stretch := True;
   Image1.Picture.LoadFromFile('Image\Universe.jpg');
   Button1.Tag := 0;
   Button1.OnClick := ValidateImage;

   Image2.Center := True;
   Image2.Stretch := True;
   Image2.Picture.LoadFromFile('Image\Earth.jpg');
   Button2.Tag := 1;
   Button2.OnClick := ValidateImage;

   Image3.Center := True;
   Image3.Stretch := True;
   Image3.Picture.LoadFromFile('Image\Spiral Galaxy.jpg');
   Button3.Tag := 2;
   Button3.OnClick := ValidateImage;

   // Configura y carga StringGrid1 en base al arreglo Ask
   StringGrid1.FixedRows := 0;
   StringGrid1.FixedCols := 0;
   StringGrid1.RowCount := 3;
   StringGrid1.ColCount := 1;

   for Row:= 0 to StringGrid1.RowCount - 1 do
      for Col:= 0 to StringGrid1.ColCount - 1  do
      begin
         StringGrid1.Cells[Col,Row]:= Ask[Row];
         StringGrid1.ColWidths[Col] := 300;
      end;

   StringGrid1.Selection := TGridRect(Rect(-1, -1, -1, -1));

   // Configura BitBtn1 
   BitBtn1.Glyph.LoadFromFile('Image/Help.bmp');

   // Configura Shape1
   Shape1.Brush.Color := clSkyBlue;
   Shape1.Brush.Style := bsSolid;
   Shape1.Canvas.Font.Name :='Tahoma';
   Shape1.Canvas.Font.Size := 14;

   // Configura StatusBar1
   StatusBar1.Panels.Add.Text := 'Aciertos';
   StatusBar1.Panels.Items[0].Width := 200;

   StatusBar1.Panels.Add.Text := 'Errorres';
   StatusBar1.Panels.Items[1].Width := 200;

end;

// Valida la imagen seleccionada
procedure TForm1.ValidateImage(Sender: TObject);
begin

   if not StartGame then
      Exit;

   // Verifica y contabiliza las respuestas
   if SelectImage = TImage(Sender).Tag then
   begin

      Shape1.Canvas.Font.Color := clGreen;
      Shape1.Canvas.Font.Style := [fsBold];
      Shape1.Canvas.TextOut(135,1,'¡Correcto!   ');

      Inc(Success);

      StatusBar1.Panels.Items[0].Text := Format('Aciertos = %d',[Success]);

   end
   else
   begin

      Shape1.Canvas.Font.Color := clRed;
      Shape1.Canvas.Font.Style := [fsBold];
      Shape1.Canvas.TextOut(135,1,'¡Incorrecto!');

      Inc(Error);

      StatusBar1.Panels.Items[1].Text := Format('Errorres = %d',[Error]);

   end;

end;

// Selecciona una pregunta al random y configura la interfaz
procedure TForm1.BitBtn1Click(Sender: TObject);
var
   Row : Integer;
   Select : Boolean;
   i : Integer;

begin

   Randomize;

   PreviousImage := SelectImage;

   // Selecciona de forma equiprobable una pregunta sin repetición consecutiva
   repeat

      Select := True;
      repeat
         Row := Random(3);
      until Row <> PreviousImage;

      if Length(RndImg) = 3 then
         SetLength(RndImg,0);

      for i := Low(RndImg) to High(RndImg) do
      begin
         if RndImg[i] = Row then
         begin
            Select := False;
            Break;
         end
      end;

      if Select then
      begin
         SetLength(RndImg,Length(RndImg)+1);
         RndImg[High(RndImg)] := Row;
      end;

   until Select;

   SelectImage := Row;

   StaticText1.Caption := StringGrid1.Cells[0,Row];
   StringGrid1.Selection := TGridRect(Rect(0, Row, 0, Row));
   Shape1.Canvas.TextOut(135,1,StringOfChar(' ',20));

   StartGame := True;

end;

end.
El código anterior en Delphi 7 sobre Windows 7 Professional x32, Implementa un juego básico de preguntas, como se muestra en la siguiente imagen:



Notas:

1- El código propuesto tiene como único objetivo, servir de guía a los requerimientos planteados en el Msg #1.

2- La configuración de los controles, se hace mediante código para facilitar la visualización de la configuración de los mismos.

3- El código propuesto esta disponible en : GameUniverse.rar

Espero sea útil

Nelson.

Última edición por nlsgarcia fecha: 30-05-2015 a las 14:26:45.
Responder Con Cita