Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Varios
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 02-12-2016
Guazna Guazna is offline
Registrado
NULL
 
Registrado: dic 2016
Posts: 9
Poder: 0
Guazna Va por buen camino
Post Alguien podria identificar si este codigo tiene arrays soy nuevo en delphi

Código Delphi [-]
unit CazaSubmarino;
{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  StdCtrls, MMSystem; // MMSystem, para poder oír en Windows

type
  { TForm1 }
  TForm1 = class(TForm)
    Button1: TButton;
    CheckBox1: TCheckBox;
    Image1: TImage;
    Image2: TImage;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Shape1: TShape;
    Shape2: TShape;
    Timer1: TTimer;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Timer1Timer(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  RatonX, RatonY: Integer; // Donde esta el ratón sobre Image1
  RatonX2, RatonY2: Integer; // Donde se dibujara la animación
  Animacion, // Para iniciar la animación
  detectado: Boolean;
  Contador: Integer; // Para saber si esta en Fila 1 ó 10,
  Columna1: Integer; // ó columna 1 o 10 - animación reducida
  Fila1: Integer; // a 4 cuadros ó 6 cuadros
  LugarPulsado, Submarino: Integer; // Donde esta el submarino colocado

implementation

{$R *.lfm}

{ TForm1 }
Procedure ColocaSubmarino();
Begin
  Randomize; // Preparando la "semilla" aleatoria
  Submarino := Random(100) + 1; // Colocando aleatoriamente el submarino
  // como es de 0 a 99 se le agrega uno para
  // que "este" en el tablero (1-100)
End;

Procedure MoverSubmarino(); // Como se moverá el submarino
Var
  Moviendo: Integer;
  // Localtexto1 : string; // Ayuda para el programador
Begin
  Form1.Label2.Caption := '¡El Submarino se mueve!';
  Form1.Label2.Visible := True;
  Moviendo := Random(10); // Generando un número aleatorio del 0 al 9
  Case Submarino of
    1, 10, 91, 100: Begin // Si "esta" en una esquina
        Case Submarino of
          1: Begin // Esquina superior izquierda 4 movimientos disponibles
              Case Moviendo of
                0, 1, 3:
                  Submarino := Submarino + 1; // A la derecha
                4, 5, 6:
                  Submarino := Submarino + 10; // Hacia abajo
                2, 7, 8:
                  Submarino := Submarino + 11; // Diagonal abajo
                // Si es 9 no se moverá
              end;
            end;
          10: Begin // Esquina superior derecha 4 movimientos disponibles
              Case Moviendo of
                0, 1, 3:
                  Submarino := Submarino - 1; // A la izquierda
                4, 5, 6:
                  Submarino := Submarino + 10; // Hacia abajo
                2, 7, 8:
                  Submarino := Submarino + 9; // Diagonal abajo
                // Si es 9 no se moverá
              end;
            end;
          91: Begin // Esquina inferior izquierda 4 movimientos disponibles
              Case Moviendo of
                0, 1, 3:
                  Submarino := Submarino + 1; // A la derecha
                4, 5, 6:
                  Submarino := Submarino - 10; // Hacia arriba
                2, 7, 8:
                  Submarino := Submarino - 9; // Diagonal arriba
                // Si es 9 no se moverá
              end;
            end;
          100: Begin // Esquina inferior derecha 4 movimientos disponibles
              Case Moviendo of
                0, 1, 3:
                  Submarino := Submarino - 1; // A la izquierda
                4, 5, 6:
                  Submarino := Submarino - 10; // Hacia arriba
                7, 8:
                  Submarino := Submarino - 11; // Diagonal arriba
                // Si es 9 o 2 no se moverá
              end;
            end;
        End;
      End; // Fin de esquinas
    2, 3, 4, 5, 6, 7, 8, 9: Begin
      // Si "esta" en la fila superior 6 movimientos posibles
        Case Moviendo of
          0, 1:
            Submarino := Submarino - 1; // A la izquierda
          2, 3:
            Submarino := Submarino + 1; // Hacia la derecha
          4:
            Submarino := Submarino + 9; // Diagonal izquierda abajo
          5, 8:
            Submarino := Submarino + 11; // Diagonal derecha abajo
          6, 7:
            Submarino := Submarino + 10; // Hacia abajo
          // Si es 9 no se movera
        end;
      End;
    92, 93, 94, 95, 96, 97, 98, 99: Begin // Si "esta" en la fila inferior
        Case Moviendo of
          0, 1:
            Submarino := Submarino - 1; // A la izquierda
          2, 3:
            Submarino := Submarino + 1; // Hacia la derecha
          4:
            Submarino := Submarino - 11; // Diagonal izquierda abajo
          5, 8:
            Submarino := Submarino - 9; // Diagonal derecha abajo
          6, 7:
            Submarino := Submarino - 10; // Hacia arriba
          // Si es 9 no se moverá
        end;
      End;
    11, 21, 31, 41, 51, 61, 71, 81: Begin // Si esta en la Primera columna
        Case Moviendo of
          0, 1:
            Submarino := Submarino + 1; // A la derecha
          2, 3:
            Submarino := Submarino - 10; // Arriba
          4:
            Submarino := Submarino - 9; // Diagonal derecha arriba
          5, 8:
            Submarino := Submarino + 11; // Diagonal derecha abajo
          6, 7:
            Submarino := Submarino + 10; // Hacia abajo
          // Si es 9 no se movera
        end;
      End;
    20, 30, 40, 50, 60, 70, 80, 90: Begin // Si esta en la ultima columna
        Case Moviendo of
          0, 1:
            Submarino := Submarino - 1; // a la izquierda
          2, 3:
            Submarino := Submarino - 10; // arriba
          4:
            Submarino := Submarino - 11; // diagonal izquierda arriba
          5, 8:
            Submarino := Submarino + 9; // diagonal izquierda abajo
          6, 7:
            Submarino := Submarino + 10; // Hacia abajo
          // Si es 9 no se moverá
        end;
      End;
  Else Begin
      Case Moviendo of
        0:
          Submarino := Submarino - 11;
        1:
          Submarino := Submarino - 10;
        2:
          Submarino := Submarino - 9;
        3:
          Submarino := Submarino - 1;
        4:
          Submarino := Submarino + 1;
        5:
          Submarino := Submarino + 9;
        6:
          Submarino := Submarino + 10;
        7, 8:
          Submarino := Submarino + 11;
        // Si es 9 no se movera
      end;
    End;
  end;
  // Ayuda para el programador – Nos mostrara la posición del submarino =P
  // Str(Submarino,Localtexto1);
  // Form1.Label3.Caption:=Localtexto1;
  // Form1.Label3.Visible:=true;
End;

Procedure Ubicacion(); // Para saber donde "dibujar" la animación
Begin
  detectado := False;
  Case RatonX Of // Para saber en que columna esta y se asigna para su animación
    0 .. 29: Begin
        RatonX2 := 0;
        Columna1 := 1;
        Form1.Shape2.Width := 60;
        // Si se detecta el submarino en esquina es mas chico
        Form1.Shape2.Left := 0;
      End;
    30 .. 58: Begin
        RatonX2 := 30;
        Columna1 := 2;
        Form1.Shape2.Width := 90; // Si se detecta el submarino el tamaño cambia
        Form1.Shape2.Left := 0; // Y donde pondremos el cuadro verde (shape2)
      End;
    59 .. 87: Begin
        RatonX2 := 59;
        Columna1 := 3;
        Form1.Shape2.Width := 90;
        Form1.Shape2.Left := 30;
      End;
    88 .. 116: Begin
        RatonX2 := 88;
        Columna1 := 4;
        Form1.Shape2.Width := 90;
        Form1.Shape2.Left := 59;
      End;
    117 .. 145: Begin
        RatonX2 := 117;
        Columna1 := 5;
        Form1.Shape2.Width := 90;
        Form1.Shape2.Left := 88;
      End;
    146 .. 174: Begin
        RatonX2 := 146;
        Columna1 := 6;
        Form1.Shape2.Width := 90;
        Form1.Shape2.Left := 117;
      End;
    175 .. 203: Begin
        RatonX2 := 175;
        Columna1 := 7;
        Form1.Shape2.Width := 90;
        Form1.Shape2.Left := 146;
      End;
    204 .. 232: Begin
        RatonX2 := 204;
        Columna1 := 8;
        Form1.Shape2.Width := 90;
        Form1.Shape2.Left := 175;
      End;
    233 .. 261: Begin
        RatonX2 := 233;
        Columna1 := 9;
        Form1.Shape2.Width := 90;
        Form1.Shape2.Left := 204;
      End;
    262 .. 289: Begin
        RatonX2 := 262;
        Columna1 := 10;
        Form1.Shape2.Width := 60;
        Form1.Shape2.Left := 233;
      End;
  End;
  Case RatonY Of // Para saber en que Fila esta
    0 .. 29: Begin // y se asigna para su animación
        RatonY2 := 0; // Se inicia desde fila 0 para
        Fila1 := 0; // poder después saber en que "cuadro" se pulso.
        Form1.Shape2.Height := 60; // Si se detecta el submarino en esquina
        Form1.Shape2.Top := 0; // Recuerda que solo son 4 cuadros
      End;
    30 .. 58: Begin
        RatonY2 := 30;
        Fila1 := 1;
        Form1.Shape2.Height := 90; // Si se detecta el submarino
        Form1.Shape2.Top := 0; // Ya no es esquina pueden ser 9 cuadros
      End;
    59 .. 87: Begin
        RatonY2 := 59;
        Fila1 := 2;
        Form1.Shape2.Height := 90;
        Form1.Shape2.Top := 30;
      End;
    88 .. 116: Begin
        RatonY2 := 88;
        Fila1 := 3;
        Form1.Shape2.Height := 90;
        Form1.Shape2.Top := 59;
      End;
    117 .. 145: Begin
        RatonY2 := 117;
        Fila1 := 4;
        Form1.Shape2.Height := 90;
        Form1.Shape2.Top := 88;
      End;
    146 .. 174: Begin
        RatonY2 := 146;
        Fila1 := 5;
        Form1.Shape2.Height := 90;
        Form1.Shape2.Top := 117;
      End;
    175 .. 203: Begin
        RatonY2 := 175;
        Fila1 := 6;
        Form1.Shape2.Height := 90;
        Form1.Shape2.Top := 146;
      End;
    204 .. 232: Begin
        RatonY2 := 204;
        Fila1 := 7;
        Form1.Shape2.Height := 90;
        Form1.Shape2.Top := 175;
      End;
    233 .. 261: Begin
        RatonY2 := 233;
        Fila1 := 8;
        Form1.Shape2.Height := 90;
        Form1.Shape2.Top := 204;
      End;
    262 .. 289: Begin
        RatonY2 := 262;
        Fila1 := 9;
        Form1.Shape2.Height := 60;
        Form1.Shape2.Top := 233;
      End;
  End;
  LugarPulsado := Fila1 * 10 + Columna1; // Buscando donde pulso del 1 al 100
  Case LugarPulsado of
    1: Begin // Revisando las esquinas
        Case Submarino of
          1, 2, 11, 12E tectado := True;
          End;
          End;
          10:
            Begin
              Case Submarino of
                9, 10, 19, 20E tectado := True;
                End;
                End;
                91:
                  Begin
                    Case Submarino of
                      81, 82, 91, 92E tectado := True;
                      End;
                      End;
                      100:
                        Begin
                          Case Submarino of
                            89, 90, 99, 100E tectado := True;
                            End;
                            End;
                            2, 3, 4, 5, 6, 7, 8, 9:
                              Begin // Si esta en la 1ª fila
                                if LugarPulsado - 1 = Submarino then
                                  detectado := True;
                                if LugarPulsado = Submarino then
                                  detectado := True;
                                if LugarPulsado + 1 = Submarino then
                                  detectado := True;
                                if LugarPulsado + 9 = Submarino then
                                  detectado := True;
                                if LugarPulsado + 10 = Submarino then
                                  detectado := True;
                                if LugarPulsado + 11 = Submarino then
                                  detectado := True;
                              End;
                            92, 93, 94, 95, 96, 97, 98, 99: Begin
                              // Si esta en la ultima fila
                                if (LugarPulsado - 11) = Submarino then
                                  detectado := True;
                                if (LugarPulsado - 10) = Submarino then
                                  detectado := True;
                                if LugarPulsado - 9 = Submarino then
                                  detectado := True;
                                if LugarPulsado - 1 = Submarino then
                                  detectado := True;
                                if LugarPulsado = Submarino then
                                  detectado := True;
                                if LugarPulsado + 1 = Submarino then
                                  detectado := True;
                              End;
                            11, 21, 31, 41, 51, 61, 71, 81: Begin
                              // Primera Columna
                                if (LugarPulsado - 10) = Submarino then
                                  detectado := True;
                                if LugarPulsado - 9 = Submarino then
                                  detectado := True;
                                if LugarPulsado = Submarino then
                                  detectado := True;
                                if LugarPulsado + 1 = Submarino then
                                  detectado := True;
                                if LugarPulsado + 10 = Submarino then
                                  detectado := True;
                                if LugarPulsado + 11 = Submarino then
                                  detectado := True;
                              end;
                            20, 30, 40, 50, 60, 70, 80, 90: Begin
                              // La ultima fila
                                if (LugarPulsado - 11) = Submarino then
                                  detectado := True;
                                if (LugarPulsado - 10) = Submarino then
                                  detectado := True;
                                if LugarPulsado - 9 = Submarino then
                                  detectado := True;
                                if LugarPulsado - 1 = Submarino then
                                  detectado := True;
                                if LugarPulsado = Submarino then
                                  detectado := True;
                                if LugarPulsado + 1 = Submarino then
                                  detectado := True;
                              End;
                          Else // Si esta en otro lugar
                            Begin
                              if (LugarPulsado - 11) = Submarino then
                                detectado := True;
                              if (LugarPulsado - 10) = Submarino then
                                detectado := True;
                              if LugarPulsado - 9 = Submarino then
                                detectado := True;
                              if LugarPulsado - 1 = Submarino then
                                detectado := True;
                              if LugarPulsado = Submarino then
                                detectado := True;
                              if LugarPulsado + 1 = Submarino then
                                detectado := True;
                              if LugarPulsado + 9 = Submarino then
                                detectado := True;
                              if LugarPulsado + 10 = Submarino then
                                detectado := True;
                              if LugarPulsado + 11 = Submarino then
                                detectado := True;
                            End;
                          End;
                        End;

                      procedure TForm1.FormCreate(Sender: TObject);
                      begin
                        Form1.Image2.Visible := True;
                        // Pantalla de presentación
                        Form1.Image1.Visible := False; // Pantalla de juego
                        Form1.Timer1.interval := 5;
                        // Estableciendo velocidad de la animación
                        ColocaSubmarino;
                        // Colocando el submarino en el "tablero"
                        Form1.Label1.Caption := 'El submarino no se mueve';
                        Form1.CheckBox1.Checked := False;
                        Form1.Button1.Caption := 'Iniciar Juego';
                        Form1.Label2.Visible := False;
                        // Para mensajes para el programador =P
                        Form1.Shape1.Brush.Style := bsClear;
                        // Transparente, solo nos interesa el "borde"
                      end;

                      procedure TForm1.Image1MouseDown(Sender: TObject;
                        Button: TMouseButton; Shift: TShiftState;
                        X, Y: Integer);
                      Var
                        Localtexto1, Localtexto2: string;
                        // Variable de uso temporal usado para mostrar donde esta el ratón
                      begin
                        Shape2.Visible := False;
                        RatonX := X;
                        // Capturando la posición de donde se pulso el Ratón
                        RatonY := Y;
                        // Str(X,Localtexto1); // Para mostrar los valores de X e Y - Ayudando al programador =P
                        // Str(X,Localtexto2);
                        // Label2.Caption:= 'X = ' + Localtexto1 + ' Y = ' + Localtexto2;
                        // Label2.Visible:=True;
                        if (Button = mbLeft) then // Si pulsa el izquierdo
                        begin
                          Contador := 0;
                          Ubicacion;
                          Shape2.Brush.Color := clLime;
                          // Preparando el cuadro si se "detecta"
                          If detectado = True then Begin
                            Shape2.Visible := True;
                            Label1.Caption := 'Submarino detectado ';
                          End
                          Else
                            Label1.Caption := 'Sin detección ';
                          // Si deseas saber donde esta solo agrega
                          // +IntToStr(submarino)
                          // Ayuda para el programador =P
                          Animacion := True;
                          PlaySound('Sonar.wav', 0, SND_ASYNC);
                          // Sonido de Sonar (Si capitán obvios)
                          If Form1.CheckBox1.Checked = True Then
                            MoverSubmarino
                          Else
                            Form1.Label2.Visible := False;
                        end
                        Else Begin // Si pulso el derecho
                          Ubicacion;
                          PlaySound('Bexpl.wav', 0, SND_ASYNC);
                          // Sonido de explosión
                          Shape2.Height := 30; // preparando el cuadro rojo a
                          Shape2.Width := 30; // presentarse en el lugar pulsado
                          Shape2.Brush.Color := clRed;
                          Shape2.Top := RatonY2;
                          Shape2.Left := RatonX2;
                          Shape2.Visible := True;
                          If LugarPulsado = Submarino Then Begin
                            Form1.Label1.Caption := 'Submarino destruido';
                            ShowMessage('Haz destruido al submarino');
                            Form1.Button1.Caption := '¿Otro juego?';
                            Form1.Label1.Caption := ' ';
                            Form1.Image2.Visible := True;
                            // mostrando pantalla de presentación
                            Form1.Shape2.Visible := False;
                            Form1.Image1.Visible := False;
                            Form1.Button1.Visible := True;
                          End
                          Else
                            Form1.Label1.Caption := 'Disparo fallido';
                          If Form1.CheckBox1.Checked = True Then
                            MoverSubmarino
                          Else
                            Form1.Label2.Visible := False;
                        End;
                      end;

                      procedure TForm1.Timer1Timer(Sender: TObject);
                      begin
                        If Animacion = True Then Begin
                          if Contador < 12 Then Begin
                          // Se inicia la "animación"
                            Shape1.Left := RatonX2; // del "Sonar"
                            Shape1.Top := RatonY2;
                            Shape1.Visible := True;
                            Shape1.Height := Shape1.Height + 6;
                            Shape1.Width := Shape1.Width + 6;
                            RatonX2 := RatonX2 - 3;
                            RatonY2 := RatonY2 - 3;
                            Contador := Contador + 1;
                          End
                          Else Begin
                            Animacion := False;
                            Contador := 0;
                            Shape1.Visible := False;
                            Shape2.Visible := False;
                            Shape1.Height := 30;
                            Shape1.Width := 30;
                          End;
                        End;
                      end;

                      procedure TForm1.Button1Click(Sender: TObject);
                      begin
                        ColocaSubmarino;
                        // Colocando el submarino en el "tablero"
                        Form1.Shape1.Visible := False;
                        Form1.Button1.Visible := False;
                        Form1.Shape2.Visible := False;
                        Form1.Image2.Visible := False;
                        // ocultando pantalla de presentación
                        Form1.Image1.Visible := True; // Mostrando área de juego
                      end;

end.

Última edición por Neftali [Germán.Estévez] fecha: 02-12-2016 a las 12:13:45. Razón: Formatear código
Responder Con Cita
  #2  
Antiguo 02-12-2016
Avatar de Neftali [Germán.Estévez]
Neftali [Germán.Estévez] Neftali [Germán.Estévez] is offline
[becario]
 
Registrado: jul 2004
Ubicación: Barcelona - España
Posts: 18.272
Poder: 10
Neftali [Germán.Estévez] Es un diamante en brutoNeftali [Germán.Estévez] Es un diamante en brutoNeftali [Germán.Estévez] Es un diamante en bruto
No tiene.
Si buscas la palabra array, no aparece.
__________________
Germán Estévez => Web/Blog
Guía de estilo, Guía alternativa
Utiliza TAG's en tus mensajes.
Contactar con el Clubdelphi

P.D: Más tiempo dedicado a la pregunta=Mejores respuestas.
Responder Con Cita
  #3  
Antiguo 03-12-2016
Avatar de TiammatMX
TiammatMX TiammatMX is offline
Miembro
 
Registrado: jun 2006
Ubicación: Universo Curvo\Vía Láctea\Sistema Solar\Planeta Tierra\América\México\Puebla\Heróica Puebla de Zaragoza\Jardines de San Manuel\Home
Posts: 746
Poder: 18
TiammatMX Va camino a la fama
Thumbs down

Regla cero de la programación: Conviértete en experto de GREP..., Ctrl+F también puede ayudar...
__________________
Felipe Eduardo Ortiz López. Delphi programmers does it recursively...

"Un programador, es un creador de universos en donde sólo él es responsable. Universos de complejidad prácticamente ilimitada que se puede crear en forma de programas de ordenador." - Joseph Weizenbaum.

Témele a los profetas... y a aquellos que están listos para morir por "la verdad", ya que como regla general hacen morir a muchos otros con ellos, frecuentemente antes que ellos, y a veces en lugar de ellos. — Umberto Eco
Responder Con Cita
  #4  
Antiguo 03-12-2016
Guazna Guazna is offline
Registrado
NULL
 
Registrado: dic 2016
Posts: 9
Poder: 0
Guazna Va por buen camino
ajajaja gracias por lo del ctrl + f no sabia esa
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Alguien tiene este componente.... Jose Roman OOP 2 25-11-2010 17:51:21
Qué tiene de malo este código para cargar una imagen a access con ado ? Master23 Varios 8 17-07-2010 03:05:24
Alguien tiene idea de que sea este error.... Alfredo Varios 2 04-02-2005 13:59:15
hola alguien podria ayduarme con asm de delphi mauriciofox OOP 2 24-11-2004 03:29:08
Ehm, alguien me puede facilitar éste código? gatsu PHP 3 09-07-2004 13:35:01


La franja horaria es GMT +2. Ahora son las 09:38:58.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi