AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Sprachen und Entwicklungsumgebungen Object-Pascal / Delphi-Language Tetris mit Canvas funktioniert nicht wie es soll

Tetris mit Canvas funktioniert nicht wie es soll

Ein Thema von fox67 · begonnen am 13. Feb 2013 · letzter Beitrag vom 21. Mär 2013
Antwort Antwort
Seite 4 von 5   « Erste     234 5   
fox67

Registriert seit: 6. Okt 2010
Ort: 72661 Grafenberg
181 Beiträge
 
Turbo Delphi für Win32
 
#31

AW: Tetris mit Canvas funktioniert nicht wie es soll

  Alt 6. Mär 2013, 14:30
Warum denn nicht irgent wie muss doch auf das Formular gezeichnet werden
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.534 Beiträge
 
Delphi 11 Alexandria
 
#32

AW: Tetris mit Canvas funktioniert nicht wie es soll

  Alt 6. Mär 2013, 14:36
Wie schon gesagt: spendiere THaupt eine Property vom Typ TCanvas, auf den diese dann zeichnet. Dann kannst Du Form2.ImageDings.Canvas oder FormBla.Canvas oder was weiß ich zuweisen und bist nicht an Form2 gebunden.
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#33

AW: Tetris mit Canvas funktioniert nicht wie es soll

  Alt 6. Mär 2013, 14:39
Warum denn nicht irgent wie muss doch auf das Formular gezeichnet werden
Aber stell es dir so vor, da fängt jemand an dich zu füttern.
Kein schöner Gedanke. Eben, du kannst das selber, wenn man dir dazu alles an die Hand gibt.

So ist das auch hier.
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat
fox67

Registriert seit: 6. Okt 2010
Ort: 72661 Grafenberg
181 Beiträge
 
Turbo Delphi für Win32
 
#34

AW: Tetris mit Canvas funktioniert nicht wie es soll

  Alt 6. Mär 2013, 18:21
Ja des mag sein aber mein Problem löst es leider nicht.
  Mit Zitat antworten Zitat
Bjoerk

Registriert seit: 28. Feb 2011
Ort: Mannheim
1.384 Beiträge
 
Delphi 10.4 Sydney
 
#35

AW: Tetris mit Canvas funktioniert nicht wie es soll

  Alt 6. Mär 2013, 18:36
Arni, du machst es dir unnötig schwer. Wie findest du denn das Konzept von #25?

Geändert von Bjoerk ( 6. Mär 2013 um 18:39 Uhr) Grund: Edit: Wie statt was
  Mit Zitat antworten Zitat
fox67

Registriert seit: 6. Okt 2010
Ort: 72661 Grafenberg
181 Beiträge
 
Turbo Delphi für Win32
 
#36

AW: Tetris mit Canvas funktioniert nicht wie es soll

  Alt 6. Mär 2013, 18:47
Das hört sich zwar deutlich einfacher an aber dann müsste einen großteil neumachen
  Mit Zitat antworten Zitat
Bjoerk

Registriert seit: 28. Feb 2011
Ort: Mannheim
1.384 Beiträge
 
Delphi 10.4 Sydney
 
#37

AW: Tetris mit Canvas funktioniert nicht wie es soll

  Alt 6. Mär 2013, 18:59
Übung macht den Meister. Wenn du’s etwas geschickt machst: die GUI 200 Zeilen, die Komponente 300 Zeilen.
  Mit Zitat antworten Zitat
fox67

Registriert seit: 6. Okt 2010
Ort: 72661 Grafenberg
181 Beiträge
 
Turbo Delphi für Win32
 
#38

AW: Tetris mit Canvas funktioniert nicht wie es soll

  Alt 6. Mär 2013, 21:03
Ja aber ich hab trotzdem weiter experimentiert.
Delphi-Quellcode:
unit Unit2;

interface

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

type
  TBlock = class;
  THaupt = class;

  TForm2 = class(TForm)
    Spielfeld: TImage;
    Timer1: TTimer;
    Button1: TSpeedButton;
    Edit1: TEdit;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);


  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  Haupt : THaupt;


  end;

  THaupt = class(TObject)

   Block : array of TBlock;
   Anzahl : integer;
   kannbewegen : boolean;
   IDs : Integer;
   procedure Neu();
   procedure Neufallen();
   procedure verschiebenlinks();
   procedure verschiebenrechts();
   procedure findelinie();
   procedure arraykurzen();
  procedure Drehen();
  function CanMoveLast(const deltaX, deltaY: integer): boolean;
  function findemax(): integer;
  // procedure Prufen();
  // procedure Entfernen();
  private

  public
  zufall: integer;
  gedreht : integer;
  temp : array[1..16] of Integer;
  end;


  TBlock = class(TObject)

    private
    fFarbe : TColor;
    fPosition : TPoint;
    fID : Integer;
    public
    procedure zeichen;
    property Farbe : TColor read fFarbe write fFarbe;
    property Position : Tpoint read fPosition write fPosition;
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

//Code Block
procedure TBlock.zeichen;
begin
form2.Spielfeld.Canvas.Brush.Color := fFarbe;
form2.Spielfeld.Canvas.Brush.Style := bssolid;
form2.Spielfeld.Canvas.Rectangle(fPosition.X*24, fPosition.Y*24, fPosition.X*24+24, fPosition.Y*24+24 );
end;

//Code Haupt
procedure THaupt.Neu;


begin
 gedreht := 0;
 form2.Timer1.Enabled := false;
  if (form2.Edit1.text = '') then
   begin
  repeat
  zufall := random(5) +1;
  until (zufall <> 0) ;

   end
   else
   begin
   try
   zufall := strtoint(form2.Edit1.Text);
    except
    showmessage('keine Zahl');
    repeat
  zufall := random(5) +1;
  until (zufall <> 0) ;
   end;
   end;
   findelinie();
  case zufall of
  1: begin // Quadrat
      IDs := IDs +4;
      setlength(Block, Length(Block)+4);
      Block[high(Block)-3] := TBlock.Create;
      Block[high(Block)-3].Farbe := clred ;
      Block[high(Block)-3].fPosition.X:= 4 ;
      Block[high(Block)-3].fPosition.Y:= -2 ;
      Block[high(Block)-3].fID := IDS-3;
      Block[high(Block)-2] := TBlock.Create;
      Block[high(Block)-2].Farbe := clred ;
      Block[high(Block)-2].fPosition.X:= 5 ;
      Block[high(Block)-2].fPosition.Y:= -2 ;
      Block[high(Block)-2].fID := IDs-2;
      Block[high(Block)-1] := TBlock.Create;
      Block[high(Block)-1].Farbe := clred ;
      Block[high(Block)-1].fPosition.X:= 4 ;
      Block[high(Block)-1].fPosition.Y:= -1 ;
      Block[high(Block)-1].fID := IDs-1;
      Block[high(Block)] := TBlock.Create;
      Block[high(Block)].Farbe := clred ;
      Block[high(Block)].fPosition.X:= 5 ;
      Block[high(Block)].fPosition.Y:= -1 ;
      Block[high(Block)].fID := IDs;
      //showmessage('Quadrat');
     form2.Timer1.Enabled := true;

     end;

  2: begin // Winkellinks
      IDs := IDs +4 ;
      setlength(Block, Length(Block)+4);
      Block[high(Block)-3] := TBlock.Create;
      Block[high(Block)-3].Farbe := clyellow ;
      Block[high(Block)-3].fPosition.X:= 4 ;
      Block[high(Block)-3].fPosition.Y:= -2 ;
      Block[high(Block)-3].fID := IDs-3;
      Block[high(Block)-2] := TBlock.Create;
      Block[high(Block)-2].Farbe := clyellow ;
      Block[high(Block)-2].fPosition.X:= 4 ;
      Block[high(Block)-2].fPosition.Y:= -1 ;
      Block[high(Block)-2].fID := IDs-2;
      Block[high(Block)-1] := TBlock.Create;
      Block[high(Block)-1].Farbe := clyellow ;
      Block[high(Block)-1].fPosition.X:= 5 ;
      Block[high(Block)-1].fPosition.Y:= -1 ;
      Block[high(Block)-1].fID := IDs-1;
      Block[high(Block)] := TBlock.Create;
      Block[high(Block)].Farbe := clyellow ;
      Block[high(Block)].fPosition.X:= 6 ;
      Block[high(Block)].fPosition.Y:= -1 ;
      Block[high(Block)].fID := IDs;
       //showmessage('Winkel');
     form2.Timer1.Enabled := true;

     end;

  3: begin //Winkelrechts
      IDS := IDs +4;
      setlength(Block, Length(Block)+4);
      Block[high(Block)-3] := TBlock.Create;
      Block[high(Block)-3].Farbe := clblue ;
      Block[high(Block)-3].fPosition.X:= 6 ;
      Block[high(Block)-3].fPosition.Y:= -2 ;
      Block[high(Block)-3].fID := IDs-3;
      Block[high(Block)-2] := TBlock.Create;
      Block[high(Block)-2].Farbe := clblue ;
      Block[high(Block)-2].fPosition.X:= 4 ;
      Block[high(Block)-2].fPosition.Y:= -1 ;
      Block[high(Block)-2].fID := IDs-2;
      Block[high(Block)-1] := TBlock.Create;
      Block[high(Block)-1].Farbe := clblue ;
      Block[high(Block)-1].fPosition.X:= 5 ;
      Block[high(Block)-1].fPosition.Y:= -1 ;
      Block[high(Block)-1].fID := IDs-1;
      Block[high(Block)] := TBlock.Create;
      Block[high(Block)].Farbe := clblue ;
      Block[high(Block)].fPosition.X:= 6 ;
      Block[high(Block)].fPosition.Y:= -1 ;
      Block[high(Block)].fID := IDs;
       //showmessage('Winkel');
      form2.Timer1.Enabled := true;

     end;

  4: begin //T
      IDS := IDs +4;
      setlength(Block, Length(Block)+4);
      Block[high(Block)-3] := TBlock.Create;
      Block[high(Block)-3].Farbe := cllime ;
      Block[high(Block)-3].fPosition.X:= 5 ;
      Block[high(Block)-3].fPosition.Y:= -2 ;
      Block[high(Block)-3].fID := IDs-3;
      Block[high(Block)-2] := TBlock.Create;
      Block[high(Block)-2].Farbe := cllime ;
      Block[high(Block)-2].fPosition.X:= 4 ;
      Block[high(Block)-2].fPosition.Y:= -1 ;
      Block[high(Block)-2].fID := IDs-2;
      Block[high(Block)-1] := TBlock.Create;
      Block[high(Block)-1].Farbe := cllime ;
      Block[high(Block)-1].fPosition.X:= 5 ;
      Block[high(Block)-1].fPosition.Y:= -1 ;
      Block[high(Block)-1].fID := IDs-1;
      Block[high(Block)] := TBlock.Create;
      Block[high(Block)].Farbe := cllime ;
      Block[high(Block)].fPosition.X:= 6 ;
      Block[high(Block)].fPosition.Y:= -1 ;
       //showmessage('T');
      form2.Timer1.Enabled := true;

     end;

  5: begin // Rechteck
      IDs := IDs +4;
      setlength(Block, Length(Block)+4);
      Block[high(Block)-3] := TBlock.Create;
      Block[high(Block)-3].Farbe := clpurple ;
      Block[high(Block)-3].fPosition.X:= 5 ;
      Block[high(Block)-3].fPosition.Y:= -4 ;
      Block[high(Block)-3].fID := IDs-3;
      Block[high(Block)-2] := TBlock.Create;
      Block[high(Block)-2].Farbe := clpurple ;
      Block[high(Block)-2].fPosition.X:= 5 ;
      Block[high(Block)-2].fPosition.Y:= -3 ;
      Block[high(Block)-2].fID := IDs-2;
      Block[high(Block)-1] := TBlock.Create;
      Block[high(Block)-1].Farbe := clpurple ;
      Block[high(Block)-1].fPosition.X:= 5 ;
      Block[high(Block)-1].fPosition.Y:= -2 ;
      Block[high(Block)-1].fID := IDs-1;
      Block[high(Block)] := TBlock.Create;
      Block[high(Block)].Farbe := clpurple ;
      Block[high(Block)].fPosition.X:= 5 ;
      Block[high(Block)].fPosition.Y:= -1 ;
      Block[high(Block)].fID := IDs;
       //showmessage('REchteck');
       form2.Timer1.Enabled := true;

     end;
  end;



end;

procedure THaupt.Neufallen;
var
i :integer;
begin


  Form2.Spielfeld.Canvas.Brush.Color := clwhite;
  Form2.Spielfeld.Canvas.Brush.Style := bssolid;
  Form2.Spielfeld.Canvas.Rectangle(0,0,form2.Spielfeld.Width, form2.Spielfeld.Height);



  kannbewegen :=false;




 if CanMoveLast(0,1) then kannbewegen:= true;


  if kannbewegen then
  begin
  Block[high(Block)-3].fPosition.Y := Block[high(Block)-3].fPosition.Y +1 ;
  Block[high(Block)-2].fPosition.Y := Block[high(Block)-2].fPosition.Y +1 ;
  Block[high(Block)-1].fPosition.Y := Block[high(Block)-1].fPosition.Y +1 ;
  Block[high(Block)].fPosition.Y := Block[high(Block)].fPosition.Y +1 ;
  end;

  if (Block[high(Block)-3].fPosition.y = 27) or (Block[high(Block)-2].fposition.y = 27) or (Block[high(Block)-1].fposition.y = 27) or (Block[high(Block)-0].fposition.y = 27) then kannbewegen:= false;

  for i := 0 to high(Block) do
    begin
      Block[i].zeichen;
    end;
  if not kannbewegen then neu();

end;

procedure THaupt.Drehen;
begin
 kannbewegen := false;
  case Zufall of
  1: begin
      //passiert nichts

     end;
  2: begin
       case gedreht of
       0: begin
       Block[high(Block)-3].fPosition.X:= Block[high(Block)-3].fPosition.x +0;
       Block[high(Block)-3].fPosition.Y:= Block[high(Block)-3].fPosition.Y +2;
       Block[high(Block)-2].fPosition.X:= Block[high(Block)-2].fPosition.x+1;
       Block[high(Block)-2].fPosition.Y:= Block[high(Block)-2].fPosition.Y+1;
       Block[high(Block)-1].fPosition.X:= Block[high(Block)-1].fPosition.x -0 ;
       Block[high(Block)-1].fPosition.Y:= Block[high(Block)-1].fPosition.Y - 0;
       Block[high(Block)].fPosition.X:= Block[high(Block)].fPosition.x-1;
       Block[high(Block)].fPosition.Y:= Block[high(Block)].fPosition.Y -1;
       gedreht := 1;

          end;
       1: begin
       Block[high(Block)-3].fPosition.X:= Block[high(Block)-3].fPosition.x +2;
       Block[high(Block)-3].fPosition.Y:= Block[high(Block)-3].fPosition.Y +0;
       Block[high(Block)-2].fPosition.X:= Block[high(Block)-2].fPosition.x +1;
       Block[high(Block)-2].fPosition.Y:= Block[high(Block)-2].fPosition.Y -1;
       Block[high(Block)-1].fPosition.X:= Block[high(Block)-1].fPosition.x -0;
       Block[high(Block)-1].fPosition.Y:= Block[high(Block)-1].fPosition.Y - 0;
       Block[high(Block)].fPosition.X:= Block[high(Block)].fPosition.x - 1 ;
       Block[high(Block)].fPosition.Y:= Block[high(Block)].fPosition.Y +1;
       gedreht := 2;

          end;
       2 : begin
       Block[high(Block)-3].fPosition.X:= Block[high(Block)-3].fPosition.x -0;
       Block[high(Block)-3].fPosition.Y:= Block[high(Block)-3].fPosition.Y -2;
       Block[high(Block)-2].fPosition.X:= Block[high(Block)-2].fPosition.x -1;
       Block[high(Block)-2].fPosition.Y:= Block[high(Block)-2].fPosition.Y -1;
       Block[high(Block)-1].fPosition.X:= Block[high(Block)-1].fPosition.x -0;
       Block[high(Block)-1].fPosition.Y:= Block[high(Block)-1].fPosition.Y -0;
       Block[high(Block)].fPosition.X:= Block[high(Block)].fPosition.x +1;
       Block[high(Block)].fPosition.Y:= Block[high(Block)].fPosition.Y +1;
       gedreht := 3;

           end;
       3 : begin
       Block[high(Block)-3].fPosition.X:= Block[high(Block)-3].fPosition.x -2 ;
       Block[high(Block)-3].fPosition.Y:= Block[high(Block)-3].fPosition.Y +0;
       Block[high(Block)-2].fPosition.X:= Block[high(Block)-2].fPosition.x -1;
       Block[high(Block)-2].fPosition.Y:= Block[high(Block)-2].fPosition.Y +1;
       Block[high(Block)-1].fPosition.X:= Block[high(Block)-1].fPosition.x +0;
       Block[high(Block)-1].fPosition.Y:= Block[high(Block)-1].fPosition.Y +0;
       Block[high(Block)].fPosition.X:= Block[high(Block)].fPosition.x +1;
       Block[high(Block)].fPosition.Y:= Block[high(Block)].fPosition.Y -1;
           gedreht := 0;
           end;

        end;
     end;
  3: begin
     case gedreht of
       0: begin
       Block[high(Block)-3].fPosition.X:= Block[high(Block)-3].fPosition.x -2;
       Block[high(Block)-3].fPosition.Y:= Block[high(Block)-3].fPosition.Y -0;
       Block[high(Block)-2].fPosition.X:= Block[high(Block)-2].fPosition.x+1;
       Block[high(Block)-2].fPosition.Y:= Block[high(Block)-2].fPosition.Y +1;
       Block[high(Block)-1].fPosition.X:= Block[high(Block)-1].fPosition.x -0 ;
       Block[high(Block)-1].fPosition.Y:= Block[high(Block)-1].fPosition.Y - 0;
       Block[high(Block)].fPosition.X:= Block[high(Block)].fPosition.x-1;
       Block[high(Block)].fPosition.Y:= Block[high(Block)].fPosition.Y -1;
       gedreht := 1;

          end;
       1: begin
       Block[high(Block)-3].fPosition.X:= Block[high(Block)-3].fPosition.x -0;
       Block[high(Block)-3].fPosition.Y:= Block[high(Block)-3].fPosition.Y +2;
       Block[high(Block)-2].fPosition.X:= Block[high(Block)-2].fPosition.x +1;
       Block[high(Block)-2].fPosition.Y:= Block[high(Block)-2].fPosition.Y -1;
       Block[high(Block)-1].fPosition.X:= Block[high(Block)-1].fPosition.x -0;
       Block[high(Block)-1].fPosition.Y:= Block[high(Block)-1].fPosition.Y - 0;
       Block[high(Block)].fPosition.X:= Block[high(Block)].fPosition.x - 1;
       Block[high(Block)].fPosition.Y:= Block[high(Block)].fPosition.Y + 1;
       gedreht := 2;

          end;
       2 : begin
       Block[high(Block)-3].fPosition.X:= Block[high(Block)-3].fPosition.x +2;
       Block[high(Block)-3].fPosition.Y:= Block[high(Block)-3].fPosition.Y +0;
       Block[high(Block)-2].fPosition.X:= Block[high(Block)-2].fPosition.x -1;
       Block[high(Block)-2].fPosition.Y:= Block[high(Block)-2].fPosition.Y -1;
       Block[high(Block)-1].fPosition.X:= Block[high(Block)-1].fPosition.x -0;
       Block[high(Block)-1].fPosition.Y:= Block[high(Block)-1].fPosition.Y - 0;
       Block[high(Block)].fPosition.X:= Block[high(Block)].fPosition.x + 1 ;
       Block[high(Block)].fPosition.Y:= Block[high(Block)].fPosition.Y + 1;
       gedreht := 3;

           end;
       3 : begin
       Block[high(Block)-3].fPosition.X:= Block[high(Block)-3].fPosition.x -0 ;
       Block[high(Block)-3].fPosition.Y:= Block[high(Block)-3].fPosition.Y -2;
       Block[high(Block)-2].fPosition.X:= Block[high(Block)-2].fPosition.x -1;
       Block[high(Block)-2].fPosition.Y:= Block[high(Block)-2].fPosition.Y +1;
       Block[high(Block)-1].fPosition.X:= Block[high(Block)-1].fPosition.x -0;
       Block[high(Block)-1].fPosition.Y:= Block[high(Block)-1].fPosition.Y +0;
       Block[high(Block)].fPosition.X:= Block[high(Block)].fPosition.x +1;
       Block[high(Block)].fPosition.Y:= Block[high(Block)].fPosition.Y -1;
           gedreht := 0;
           end;

        end;
     end;
  4: begin
     case gedreht of
       0: begin
       Block[high(Block)-3].fPosition.X:= Block[high(Block)-3].fPosition.x -1;
       Block[high(Block)-3].fPosition.Y:= Block[high(Block)-3].fPosition.Y +1;
       Block[high(Block)-2].fPosition.X:= Block[high(Block)-2].fPosition.x+1;
       Block[high(Block)-2].fPosition.Y:= Block[high(Block)-2].fPosition.Y +1;
       Block[high(Block)-1].fPosition.X:= Block[high(Block)-1].fPosition.x -0;
       Block[high(Block)-1].fPosition.Y:= Block[high(Block)-1].fPosition.Y - 0;
       Block[high(Block)].fPosition.X:= Block[high(Block)].fPosition.x-1;
       Block[high(Block)].fPosition.Y:= Block[high(Block)].fPosition.Y -1;
       gedreht := 1;

          end;
       1: begin
       Block[high(Block)-3].fPosition.X:= Block[high(Block)-3].fPosition.x +1;
       Block[high(Block)-3].fPosition.Y:= Block[high(Block)-3].fPosition.Y +1;
       Block[high(Block)-2].fPosition.X:= Block[high(Block)-2].fPosition.x +1;
       Block[high(Block)-2].fPosition.Y:= Block[high(Block)-2].fPosition.Y -1;
       Block[high(Block)-1].fPosition.X:= Block[high(Block)-1].fPosition.x -0;
       Block[high(Block)-1].fPosition.Y:= Block[high(Block)-1].fPosition.Y - 0;
       Block[high(Block)].fPosition.X:= Block[high(Block)].fPosition.x - 1;
       Block[high(Block)].fPosition.Y:= Block[high(Block)].fPosition.Y + 1;
       gedreht := 2;

          end;
       2 : begin
       Block[high(Block)-3].fPosition.X:= Block[high(Block)-3].fPosition.x +1;
       Block[high(Block)-3].fPosition.Y:= Block[high(Block)-3].fPosition.Y -1;
       Block[high(Block)-2].fPosition.X:= Block[high(Block)-2].fPosition.x -1;
       Block[high(Block)-2].fPosition.Y:= Block[high(Block)-2].fPosition.Y -1;
       Block[high(Block)-1].fPosition.X:= Block[high(Block)-1].fPosition.x -0;
       Block[high(Block)-1].fPosition.Y:= Block[high(Block)-1].fPosition.Y - 0;
       Block[high(Block)].fPosition.X:= Block[high(Block)].fPosition.x + 1 ;
       Block[high(Block)].fPosition.Y:= Block[high(Block)].fPosition.Y + 1;
       gedreht := 3;

           end;
       3 : begin
       Block[high(Block)-3].fPosition.X:= Block[high(Block)-3].fPosition.x -1;
       Block[high(Block)-3].fPosition.Y:= Block[high(Block)-3].fPosition.Y -1;
       Block[high(Block)-2].fPosition.X:= Block[high(Block)-2].fPosition.x -1;
       Block[high(Block)-2].fPosition.Y:= Block[high(Block)-2].fPosition.Y +1;
       Block[high(Block)-1].fPosition.X:= Block[high(Block)-1].fPosition.x -0;
       Block[high(Block)-1].fPosition.Y:= Block[high(Block)-1].fPosition.Y +0;
       Block[high(Block)].fPosition.X:= Block[high(Block)].fPosition.x +1;
       Block[high(Block)].fPosition.Y:= Block[high(Block)].fPosition.Y -1;
           gedreht := 0;
           end;

        end;
     end;
  5: begin
     case gedreht of
       0: begin
       Block[high(Block)-3].fPosition.X:= Block[high(Block)-3].fPosition.x -2;
       Block[high(Block)-3].fPosition.Y:= Block[high(Block)-3].fPosition.Y +2;
       Block[high(Block)-2].fPosition.X:= Block[high(Block)-2].fPosition.x-1;
       Block[high(Block)-2].fPosition.Y:= Block[high(Block)-2].fPosition.Y +1;
       Block[high(Block)-1].fPosition.X:= Block[high(Block)-1].fPosition.x -0;
       Block[high(Block)-1].fPosition.Y:= Block[high(Block)-1].fPosition.Y - 0;
       Block[high(Block)].fPosition.X:= Block[high(Block)].fPosition.x+1;
       Block[high(Block)].fPosition.Y:= Block[high(Block)].fPosition.Y -1;
       gedreht := 1;

          end;
       1: begin
       Block[high(Block)-3].fPosition.X:= Block[high(Block)-3].fPosition.x +2;
       Block[high(Block)-3].fPosition.Y:= Block[high(Block)-3].fPosition.Y -2;
       Block[high(Block)-2].fPosition.X:= Block[high(Block)-2].fPosition.x +1;
       Block[high(Block)-2].fPosition.Y:= Block[high(Block)-2].fPosition.Y -1;
       Block[high(Block)-1].fPosition.X:= Block[high(Block)-1].fPosition.x -0;
       Block[high(Block)-1].fPosition.Y:= Block[high(Block)-1].fPosition.Y -0;
       Block[high(Block)].fPosition.X:= Block[high(Block)].fPosition.x - 1;
       Block[high(Block)].fPosition.Y:= Block[high(Block)].fPosition.Y + 1;
       gedreht := 0;

          end;

     end;
  end;
  end;
end;


procedure THaupt.verschiebenlinks;
var
i : Integer;
begin
if (Block[high(Block)-3].fPosition.x = 0) or (Block[high(Block)-2].fPosition.x = 0) or (Block[high(Block)-1].fPosition.x = 0) or (Block[high(Block)-0].fPosition.x = 0) then
begin

end
else
begin


if canmovelast(-1,0) then
begin

Block[high(Block)-3].fPosition.X:= Block[high(Block)-3].fPosition.X -1;
Block[high(Block)-2].fPosition.X:= Block[high(Block)-2].fPosition.X -1;
Block[high(Block)-1].fPosition.X:= Block[high(Block)-1].fPosition.X -1;
Block[high(Block)-0].fPosition.X:= Block[high(Block)-0].fPosition.X -1;
Form2.Spielfeld.Canvas.Brush.Color := clwhite;
Form2.Spielfeld.Canvas.Brush.Style := bssolid;
Form2.Spielfeld.Canvas.Rectangle(0,0,form2.Spielfeld.Width, form2.Spielfeld.Height);
 for i := 0 to high(Block) do
    begin
      Block[i].zeichen;
    end;
end;
end;
end;

procedure Thaupt.verschiebenrechts;
var
i : Integer;
begin
if (Block[high(Block)-3].fPosition.x = 15) or (Block[high(Block)-2].fPosition.x = 15) or (Block[high(Block)-10].fPosition.x = 15) or (Block[high(Block)-0].fPosition.x = 15) then
begin

end
else
begin
if canmovelast(1,0) then
begin
Block[high(Block)-3].fPosition.X:= Block[high(Block)-3].fPosition.X +1;
Block[high(Block)-2].fPosition.X:= Block[high(Block)-2].fPosition.X +1;
Block[high(Block)-1].fPosition.X:= Block[high(Block)-1].fPosition.X +1;
Block[high(Block)-0].fPosition.X:= Block[high(Block)-0].fPosition.X +1;
Form2.Spielfeld.Canvas.Brush.Color := clwhite;
Form2.Spielfeld.Canvas.Brush.Style := bssolid;
Form2.Spielfeld.Canvas.Rectangle(0,0,form2.Spielfeld.Width, form2.Spielfeld.Height);
 for i := 0 to high(Block) do
    begin
      Block[i].zeichen;
    end;
end;
end;
end;

function THaupt.CanMoveLast(const deltaX, deltaY: integer): boolean;
var
   N, I, J: integer;
begin
   Result := true;
   N := High(Block);
   for I := 0 to N - 4 do
     for J := 0 to 3 do

      if (Block[N - J].fPosition.X + deltaX = Block[I].fPosition.X)
         and (Block[N - J].fPosition.y + deltaY = Block[I].fPosition.Y) then
           Result := false;

end;

function THaupt.findemax;
var
i,max :integer;
begin
max := 0;
for i := 1 to high(Block) do
  begin
    if 27-Block[i].fPosition.Y > max then max := 27-Block[i].fPosition.Y;

  end;
result :=max;
end;

procedure THaupt.findelinie;
var
i,j: integer;


begin
Anzahl := 0;
showmessage(inttostr(findemax+1));
for j := 0 to findemax do
 begin
  anzahl:=0;
  for i := 1 to high(Block) do
  begin
   if Block[i].fPosition.Y = 27-J then
      begin
        Anzahl := Anzahl+1;
        temp[Anzahl] := Block[i].fID ;
        form2.Label1.Caption := inttostr(Anzahl)+' ,'+inttostr(j);
        if Anzahl = 16 then
         begin
         showmessage('endlich');
         arraykurzen;
         end;

      end
      else
      begin

      end;
  end;
end;

  end;



procedure THaupt.arraykurzen;
var
i,j,x : integer;
begin
x:= 0;
for j := 1 to 16 do
    begin
    for i := 1 to high(Block) do
     begin
      if block[i].fID = temp[j] then
        begin
          x := i+1;
          repeat
            try
            showmessage(inttostr(X)+' , '+ inttostr(high(Block )));
            Block[x-1].fPosition.X := Block[x].fPosition.x;
            Block[x-1].fPosition.X := Block[x].fPosition.y;
            Block[x-1].fFarbe := Block[x].fFarbe;
            Block[x-1].fID := Block[x].fID;
            x := x+1;
            setlength(Block, length(Block)-1);
            except
            showmessage('hier')
            end;

          until (x+1 > length(Block)) ;
        end;

     end;

  end;

 // findelinie;
end;

//Fenster
procedure TForm2.Button1Click(Sender: TObject);
begin
Haupt.Neu();

end;



procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
var
i : integer;
begin

for i := 1 to high(Haupt.Block) do
begin
Haupt.Block[i].Free;
end;

Haupt.Free;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin

 Haupt := THaupt.Create;
setlength(Haupt.Block, 0);
Haupt.IDs := 0;
end;

procedure TForm2.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if key = VK_UP then Haupt.Drehen;
if key = VK_left then Haupt.verschiebenlinks;
if key = Vk_right then Haupt.verschiebenrechts;
if key = vk_down then Haupt.Neufallen;




end;


procedure TForm2.Timer1Timer(Sender: TObject);
begin
Haupt.Neufallen();

end;




end.
Jetzt klappt das finden aber mit dem arraykürzen da gibt es immer eine EAces violation
  Mit Zitat antworten Zitat
Bjoerk

Registriert seit: 28. Feb 2011
Ort: Mannheim
1.384 Beiträge
 
Delphi 10.4 Sydney
 
#39

AW: Tetris mit Canvas funktioniert nicht wie es soll

  Alt 6. Mär 2013, 22:43
Warum steigst du denn nicht (endlich) auf ne Liste um?

Mit dem Array zur Not halt so (ungetestet). Nach DeleteBlock darf nur ein gültiger Index übergeben werden, sonst knallt's.

Delphi-Quellcode:
    TBlock = class
    private
      FFarbe : TColor;
      FPosition : TPoint;
      FID : Integer;
      function GetX: integer;
      function GetY: integer;
      procedure SetX(const Value: integer);
      procedure SetY(const Value: integer);
    public
      property X: integer read GetX write SetX;
      property Y: integer read GetY write SetY;
      property Farbe: TColor read FFarbe write FFarbe;
      property Position: TPoint read FPosition write FPosition;
      property ID: integer read FID write FID;
      procedure Zeichnen;
      procedure Assign(const Value: TBlock);
    end;

    THaupt = class
    ..
    public
      procedure DeleteBlock(Index: integer);
      procedure Clear;
    end;

function TBlock.GetX: integer;
begin
  Result := FPosition.X;
end;

function TBlock.GetY: integer;
begin
  Result := FPosition.Y;
end;

procedure TBlock.SetX(const Value: integer);
begin
  FPosition.X := Value;
end;

procedure TBlock.SetY(const Value: integer);
begin
  FPosition.Y := Value;
end;

procedure TBlock.Assign(const Value: TBlock);
begin
  FFarbe := Value.Farbe;
  FPosition := Value.Position;
  FID := Value.ID;
end;

procedure THaupt.DeleteBlock(Index: integer);
var
  I, N: integer;
begin
  N := Length(Block);
  for I := Index to N - 2 do
    Block[I].Assign(Block[I + 1]);
  Block[N - 1].Free;
  SetLength(Block, N - 1);
end;

procedure THaupt.Clear; // in FormDestroy aufrufen
begin
  while Length(Block) > 0 do
    DeleteBlock(Length(Block) - 1);
end;
  Mit Zitat antworten Zitat
fox67

Registriert seit: 6. Okt 2010
Ort: 72661 Grafenberg
181 Beiträge
 
Turbo Delphi für Win32
 
#40

AW: Tetris mit Canvas funktioniert nicht wie es soll

  Alt 21. Mär 2013, 15:40
Das mit dem array funktioniert nicht so recht. Jetzt habe ich das mal mit einer TObjectlist probiert. Eigentlich sollte ich den ganzen code nochmal neuschreiben oder zumindest anpassen aber da ich mir nicht sicher bin ob des so klappt probiere ich es erst mal so.
Delphi-Quellcode:
procedure THaupt.findelinie;
var
x,x2 : TObjectlist;
i,j,tmp : integer;
test : TBlock;

weiter,start : Boolean;

begin
x := Tobjectlist.Create;
x2 := Tobjectlist.create;
weiter := true;
start := false;
for i := 1 to high(Block) do
  begin
    x.Add(Block[i]);
  end;
for j := 1 to 27 do
begin
  if weiter then // damit nicht schon die nächste
                  // unterrsucht wird bevor die davor
                  // gelöscht wird
    begin

    x2.clear;
    x2:= Tobjectlist.Create;
    for i := 0 to x.Count-1 do
     begin
     test := TBlock(x.Items[i]) ;
     if test.fPosition.Y = j then
     begin
      showmessage(inttostr(j));
      x2.Add(x.items[i]);

       if x2.Count+1 = 16 then
        begin
          weiter := false;
          start := true;
        end;
      end;
     end;
    end;
end;
if start then
begin
  for i := 0 to x2.Count - 1 do
   begin
     tmp := x.IndexOf(x2.Items[i]);
     x.Delete(tmp);
   end;
setlength(Block,x.Count+1);
for i := 1 to x.Count do
  begin
    Block[i] := TBlock(x.Items[i-1]);
  end;
  weiter := true;
  start := false;
  x.Free;
end;
end;
Das funktioniert nur leider nicht es löscht jedesmal etwas aber nicht das was es soll.
  Mit Zitat antworten Zitat
Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:03 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz