Recent

Author Topic: only using/filtering true values of a multidimensional boolean array  (Read 1286 times)

waituntilthis

  • New Member
  • *
  • Posts: 24
hi all,

I've searched for an hour now, and no result so I decided to ask you guys.

I'm building a tetris clone for my internship, and I divided my field into a twodimensional array. if a block lands, that coordinate (x,y) gets turned from 0 to 1.
I got tetris so far that it removes rows, but when a row is removed, existing blocks won't drop down anymore, they hang still in the air.

I'm thinking of giving all Boolean 1 blocks a +1 on the vertical coordinate, but I cant get it to work and I can't find relevant info for it (or i'm reading over it, since my experience with free pascal is 2 months.)

the most important pieces of code:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls, LCLType,math,
  9.   StdCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     darkblue: TImage;
  17.     fallingblock: TImage;
  18.     Label1: TLabel;
  19.     lightblue: TImage;
  20.     green: TImage;
  21.     blockfalling: TTimer;
  22.     move: TTimer;
  23.     yellow: TImage;
  24.     orange: TImage;
  25.     red: TImage;
  26.     purple: TImage;
  27.     horizontal: tlabel;
  28.     vertical: tlabel;
  29.     procedure blockfallingTimer(Sender: TObject);
  30.     procedure FormCreate(Sender: TObject);
  31.     procedure FormKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
  32.     procedure moveTimer(Sender: TObject);
  33.     procedure createblock(b: integer);
  34.     procedure pickblock();
  35.   private
  36.     blockwait, bottomcheck, movement, x, y, shapes: integer;
  37.     start,filled: boolean;
  38.     img : Timage;
  39.     location: array[0..10, 0..20] of integer;
  40.     arrfilled: array[0..10, 0..20] of boolean;
  41.     arrfilledimg: array[0..10, 0..20] of timage;
  42.   public
  43.  
  44.   end;
  45.  
  46. //irrelevant code here
  47.  
  48. procedure tform1.createblock(b:integer);
  49. var
  50.   a,drop:integer;
  51.   fullrow:boolean;
  52. begin
  53.   x:=5;
  54.   y:=0;
  55.   img:= timage.create(application);
  56.   img.top:=y*50;
  57.   img.left:=x*50;
  58.   img.visible:=true;
  59.   img.stretch:=true;
  60.   img.width:=50;
  61.   img.height:=50;
  62.   img.enabled:=true;
  63.   img.transparent:=true;
  64.   img.Parent:=fallingblock.parent;
  65.   pickblock();
  66.   fallingblock:=img;
  67.  
  68.   fullrow:=true;
  69.   for a:=0 to 9 do
  70.   begin
  71.   if  arrfilled[a,b]= false then fullrow:=false;
  72.   end;
  73.   if fullrow then
  74.   begin
  75.   for a:=0 to 9 do
  76.     begin
  77.     arrfilled[a,b]:=false;
  78.     arrfilledimg[a,b].visible:=false;
  79.   end;
  80.    {for drop:=0 to 9 do
  81.      begin
  82.       if a=1 then
  83.       begin
  84.       b:=0;
  85.       b+1:=1;
  86.       a:=0;
  87.       end;
  88.       for drop:=0 to (b-1) do
  89.       begin
  90.       //b has to go down 1* (b+1) but how?
  91.       //how do i separate the 1/0's in my entire array?
  92.       //maybe use locate?
  93.       end;
  94.      end;}
  95.   end;
  96. end;
  97. procedure TForm1.blockfallingTimer(Sender: TObject);
  98. begin
  99.  
  100. horizontal.caption:=x.tostring;
  101. vertical.caption:=y.tostring;
  102.   if (y>=19) or (arrfilled[x,(y+1)]=true) then
  103. begin
  104.   arrfilled[x,y] := true;
  105.   arrfilledimg[x,y] := fallingblock;
  106.  
  107.   createblock(y);
  108. end
  109. else
  110. begin
  111. y:=y+1;
  112. fallingblock.top := 50 * y;
  113. end;
  114. end;
  115.  

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: only using/filtering true values of a multidimensional boolean array
« Reply #1 on: November 20, 2019, 09:40:27 am »
I think that if I were doing that I'd take the naive approach of slapping a TDrawGrid onto the form and manipulating rows and columns relative to that.

However I make absolutely no claim to being the most experienced graphics programmer around here.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Handoko

  • Hero Member
  • *****
  • Posts: 5129
  • My goal: build my own game engine using Lazarus
Re: only using/filtering true values of a multidimensional boolean array
« Reply #2 on: November 20, 2019, 10:19:58 am »
I got tetris so far that it removes rows, but when a row is removed, existing blocks won't drop down anymore, they hang still in the air.

Sure, it won't automatically drop. You need to write a loop to do it. The pseudo code can be something like this:

procedure RemoveLine(Index: Integer);
var
  i, j: Integer;
begin

  If Index >= (MaxRow - 1) then Exit;
  If Index < 0 then Exit;

  for i := Index to MaxlRow-1 do
    for j := 0 to MaxColumn do
      GameWorld[i, j] := GameWorld[i+1, j];

end;
« Last Edit: November 20, 2019, 03:09:26 pm by Handoko »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: only using/filtering true values of a multidimensional boolean array
« Reply #3 on: November 20, 2019, 11:45:17 am »
Did you look at the tetris clone that comes with freepascal ?

Handoko

  • Hero Member
  • *****
  • Posts: 5129
  • My goal: build my own game engine using Lazarus
Re: only using/filtering true values of a multidimensional boolean array
« Reply #4 on: November 20, 2019, 11:59:14 am »
I already told him on his previous post. But it seems, he is unable to understand. Just as he said his experience with Free Pascal is only 2 months.

waituntilthis

  • New Member
  • *
  • Posts: 24
Re: only using/filtering true values of a multidimensional boolean array
« Reply #5 on: November 20, 2019, 02:38:35 pm »
I didn't look at the tetris clone, but I knew of it existance. it's petris right? as handoko previously said my knowledge of pascal is little, and tetris is very hard. and that's exactly why i'm determined to work on it since I'm making tetris to understand pascal better, not for the sake of making tetris. (thanks handoko for the code btw, ill try to implement it)! my response was delayed since I had to do a presentation just now, but I thank the both of you for your input. the reason for not looking at petris is that I feel like it would make it to easy for me, and it would tempt me to just remove large pieces of handwritten code and replace it by pieces of petris. I won't learn that way, just copy a program :(

I learned a lot already, stuff about multidimensional arrays, Booleans, collision detection etc, thanks to tetris being too hard for me. so even if I cant finish it, I still would've learned from it! :)

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: only using/filtering true values of a multidimensional boolean array
« Reply #6 on: November 20, 2019, 02:55:46 pm »
I think that your determination to work on your own "clean room" implementation does you a lot of credit.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Handoko

  • Hero Member
  • *****
  • Posts: 5129
  • My goal: build my own game engine using Lazarus
Re: only using/filtering true values of a multidimensional boolean array
« Reply #7 on: November 20, 2019, 03:02:59 pm »
@waituntilthis

Yes, making tetris is very hard but writing your own from zero without studying how others do it is even harder.

You misunderstood me. I didn't and I don't recommend you to copy/paste the code. That is bad. Because writing a tetris clone is relatively not easy, I just suggested you to study how others do it.
« Last Edit: November 20, 2019, 03:09:14 pm by Handoko »

 

TinyPortal © 2005-2018