# Enemy Movement Match 3

**URL:** <https://discourse.processing.org/t/enemy-movement-match-3/9558>\
**Category:** Coding Questions\
**Created:** [March 23, 2019, 7:20pm UTC](https://discourse.processing.org/t/enemy-movement-match-3/9558 "2019-03-23T19:20:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [March 23, 2019, 7:20pm UTC](https://discourse.processing.org/t/enemy-movement-match-3/9558/1 "2019-03-23T19:20:05Z")

</div>

Hey I’m trying to create a match 3 game with blocks that dont just drop downwards. I’ve tried several ways to get my blocks to do as i want but my booleans ain’t working correctly. The enemy should check below till it reaches the bottom or another block then if on top of another block it checks left or right, but one block below to see if it should drop down.  
Here a picture of the game so far

 ![match3](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4c8c63c6db29c5d4f466126cc22b269a4bbba517.png)

this one shows the booleans as you can see left and right are false but should be true

 ![match3](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4f8d663e4769c0cede329b44357b2da718ede757.png)

that top block should have left and right being true

here the main code:

```auto
ArrayList<Block> block = new ArrayList<Block>();
Control con = new Control();
int s = 25;
int sx = 50;
void setup(){
  size(600,600);
  block.add(new Block(sx,height-(s*2),0));
}

void draw(){
  background(0);
  textSize(20);
  text("Size: " + block.size(), 5,20);
  for (Block b: block){
    b.show();
    b.check();
  }
  //con.show();
}

void keyPressed(){
  if (key == 'n'){
    con.update();
  }
  if (key == ' '){
    for (Block b: block){
      b.move();
      b.timer = 5;
    }
  }// n
}// keyPressed

```

here the block code:

```auto
class Block{
  int x,y,type;
  int timer = 0;
  int yspd = 0;
  int xspd = 0;
  boolean left, right, down,moved;
  Block(int x, int y, int type){
    this.x = x;
    this.y = y;
    this.type = type;
  }
  
  void show(){
    if (type == 0){fill(255,0,0);}
    if (type == 1){fill(0,255,0);}
    rect(x,y,s,s);
    textSize(20);
    /*text("L: " + left, x + 25, y);
    text("R: " + right, x + 125, y);
    text("D: " + down, x + 225, y);
    text("M: " + moved, x + 325, y);
    text("Xspd: " + xspd, 5, 60);
    text("Yspd: " + yspd, 5, 80);*/
  }// show
  
  void res(){
    if (timer > -1){timer -= 1;}
    down = true;
    left = true;
    right = true;
    xspd = 0;
    yspd = 0;
  }
  
  void check(){
    res();
    for (int i = 0; i < block.size(); i++){
      Block b = block.get(i);
      if (y + s == height || b.y - s == y && b.x == x){down = false;}
      if (x - s == 0 || b.x - s == x && b.y + s == y || b.y + s == height){left = false;}
      if (x + s == width || b.x + s == x && b.y + s == y || b.y + s == height){right = false;}
    }
    if (down == true){yspd = 1;}
    if (right == true && left == true && down == false){
      float r = random(1);
      r = round(r);
      if (r == 0){xspd = 1;}
      if (r == 1){xspd = -1;}
    }
    if (left == false && right == false && down == false){moved = true;}
  }// check
  
  void move(){
    if (timer > -2 && timer <= 0){
      y += yspd * s;
      x += xspd * s;
      timer = -3;
    }// timer
  }// move  
}

```

i made the timer reset when press space, so i can control it better & instead of using my controller i press n for next for more control.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [March 24, 2019, 2:52am UTC](https://discourse.processing.org/t/enemy-movement-match-3/9558/2 "2019-03-24T02:52:39Z")

</div>

-a- your code can not run as there seems to be a  
Control class missing

-b- i try follow your timer concept,  
what actually is a counter ( counted down at every draw loop ( 60 times per sec ))  
by printing the timer value ( in a code copy )

```auto
// program start
sub 1 
timer: -1
timer: -1
// ...
 timer: -1
key []: set timer: 5
sub 1 
timer: 4
sub 1 
timer: 3
sub 1 
timer: 2
sub 1 
timer: 1
sub 1 
timer: 0
sub 1 
timer: -1
timer: -1
//...                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 

```

so my understanding is that you move UP by one pix  
if you press key [space]  
but the timer = -3 i never see ??

ok, i might got it wrong,  
anyhow i not understand your concept,  
but you can work on it by print variable “timer”  
every time it is used.

if you wanted to build a real timer tell us.

---

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [March 24, 2019, 3:14am UTC](https://discourse.processing.org/t/enemy-movement-match-3/9558/3 "2019-03-24T03:14:48Z")

</div>

if it ran on its own then the timer = -3 wouldnt be there. It would just loop around letting the enemy move into the space i want them, but sadly my boolean aint working correct and you can delete the control class it has no use till i get everything working as it should.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [March 24, 2019, 3:52am UTC](https://discourse.processing.org/t/enemy-movement-match-3/9558/4 "2019-03-24T03:52:45Z")

</div>

the 3 false are set by your collision check,  
if you check on yourself:

```auto
// for (int i = 0; i < block.size(); i++){
    for (int i = 1; i < block.size(); i++){

```

by the way, this checks should not use “==”  
as it only works in integer and with step = 1 pix.  
better use a  
`>= <=`

---

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [March 24, 2019, 4:56am UTC](https://discourse.processing.org/t/enemy-movement-match-3/9558/5 "2019-03-24T04:56:45Z")

</div>

Why would I use 2 for loops to check collision? I just the block to loop through every other block not have them all loop through every block. & I know where they are set just wondering why they not getting a positive reading.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [March 24, 2019, 5:47am UTC](https://discourse.processing.org/t/enemy-movement-match-3/9558/6 "2019-03-24T05:47:31Z")

</div>

it is your code, or not?

---

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [March 24, 2019, 5:59am UTC](https://discourse.processing.org/t/enemy-movement-match-3/9558/7 "2019-03-24T05:59:59Z")

</div>

I wrote the code. I know where the checks are. IDK why the checks aint working

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [March 24, 2019, 7:08am UTC](https://discourse.processing.org/t/enemy-movement-match-3/9558/8 "2019-03-24T07:08:30Z")

</div>

possibly they work but movement code not like what you expected:  
use diag printing:

```auto
colcheck end: down: true left true right true
L: true R: true D: true M: false Xspd: 0 Yspd: 1 x 50 y 550
res timer -1
colcheck end: down: true left true right true
L: true R: true D: true M: false Xspd: 0 Yspd: 1 x 50 y 550
res timer -1
colcheck end: down: true left true right true
key [] call move
move start: x50 y 550 timer -1
move end: x 50 y 575 timer -3
key [] set timer 5
L: true R: true D: true M: false Xspd: 0 Yspd: 1 x 50 y 575
res timer 4
colcheck end: down: false left false right false
L: false R: false D: false M: true Xspd: 0 Yspd: 0 x 50 y 575
res timer 3
colcheck end: down: false left false right false
L: false R: false D: false M: true Xspd: 0 Yspd: 0 x 50 y 575
res timer 2
colcheck end: down: false left false right false
L: false R: false D: false M: true Xspd: 0 Yspd: 0 x 50 y 575
res timer 1
colcheck end: down: false left false right false
L: false R: false D: false M: true Xspd: 0 Yspd: 0 x 50 y 575
res timer 0
colcheck end: down: false left false right false
L: false R: false D: false M: true Xspd: 0 Yspd: 0 x 50 y 575
res timer -1
colcheck end: down: false left false right false
L: false R: false D: false M: true Xspd: 0 Yspd: 0 x 50 y 575
res timer -1
colcheck end: down: false left false right false
L: false R: false D: false M: true Xspd: 0 Yspd: 0 x 50 y 575

```

```auto
//https://discourse.processing.org/t/enemy-movement-match-3/9558
// check KLL
boolean diagp = true;

ArrayList<Block> block = new ArrayList<Block>();
//Control con = new Control();
int s = 25;
int sx = 50;
void setup(){
  size(600,600);
  block.add(new Block(sx,height-(s*2),0));
}

void draw(){
  background(0);
  textSize(20);
  text("Size: " + block.size(), 5,20);
  for (Block b: block){
    b.show();
    b.check();
  }
  //con.show();
}

void keyPressed(){
  if (key == 'n'){
// con.update();
  }
  if (key == ' '){
    for (Block b: block){
      if (diagp) println("key [] call move");
      b.move();
      b.timer = 5;
      if (diagp) println("key [] set timer "+b.timer);
    }
  }// n
}// keyPressed

class Block{
  int x,y,type;
  int timer = 0;
  int yspd = 0;
  int xspd = 0;
  boolean left, right, down,moved;
  Block(int x, int y, int type){
    this.x = x;
    this.y = y;
    this.type = type;
  }
  
  void show(){
    if (type == 0){fill(255,0,0);}
    if (type == 1){fill(0,255,0);}
    rect(x,y,s,s);
    if (diagp) println("L: "+left+" R: "+right+" D: "+down+" M: "+moved+" Xspd: "+xspd+" Yspd: "+yspd+" x "+x+" y "+y);
  }// show
  
  void res(){
    if (timer > -1){timer -= 1;}
    down = true;
    left = true;
    right = true;
    xspd = 0;
    yspd = 0;
    if (diagp) println("res timer "+timer);
  }
  
  void check(){
    res();
    for (int i = 0; i < block.size(); i++){
      Block b = block.get(i);
      if (y + s == height || b.y - s == y && b.x == x){down = false;}
      if (x - s == 0 || b.x - s == x && b.y + s == y || b.y + s == height){left = false;}
      if (x + s == width || b.x + s == x && b.y + s == y || b.y + s == height){right = false;}
      if (diagp) println("colcheck end: down: "+down+" left "+left+" right "+right);
    }
    if (down == true){yspd = 1;}
    if (right == true && left == true && down == false){
      float r = random(1);
      r = round(r);
      if (r == 0){xspd = 1;}
      if (r == 1){xspd = -1;}
    }
    if (left == false && right == false && down == false){moved = true;}
  }// check
  
  void move(){
    if (diagp) println("move start: x"+x+" y "+y+" timer "+timer);
    if (timer > -2 && timer <= 0){
      y += yspd * s;
      x += xspd * s;
      timer = -3;
      if (diagp) println("move end: x "+x+" y "+y+" timer "+timer);
    }// timer
  }// move  
}

```

the timer counter in res() counts,  
but does not do anything?  
and the move ( after key [space] ) does one jump down to the limit  
was this the idea?

i know that println(""); thing might be a oldstyle,  
i like it because i can follow what happens in every loop  
( possibly combine with frameRate(1); )  
but the Processing IDE 3(.5.3)  
also allow to use DEBUG, play with it.

 ![SNAG-0020](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/bdd90fca0debde33cbaafbaf64d4462f2afa80c9.jpeg)

---

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [March 24, 2019, 11:46am UTC](https://discourse.processing.org/t/enemy-movement-match-3/9558/9 "2019-03-24T11:46:24Z")

</div>

They don’t work. I placed up a picture showing the boolean doesn’t work. & I use n & space as i explained to see if it works. It like you didnt even read the 2 times i already said this.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [March 24, 2019, 2:50pm UTC](https://discourse.processing.org/t/enemy-movement-match-3/9558/10 "2019-03-24T14:50:48Z")

</div>

i only wanted to show you about tools to check on your logic…

right, but i could not check it until i added a second BLOCK to above code  
and see what happens when you do a collision check over all block from inside the class.

and i found out that i could see and understand nothing using

- your text()
- or my println()

until i changed again and use a ID parameter for the block.  
now i can identify what happens in your logic.

- with 2 blocks you make each
- 
  - reset

- 
  - 2 limit/collision checks

and that looks also 2 times for  
b.y + s == height  
and sets left and right to false.

in words: ! your logic is:  
if one block is down at the screen end  
both can not move left right.

here a print snap AFTER i pressed [space]  
and both blocks jumped down s ???

> **log**
>
> ```auto
> D: false L: false R: false M: true Xspd: 0 Yspd: 0 x 50 y 575
> res timer -1
> block 0 ID 1 at x 50 y 575
> check down: IF y + s == height true || b.y - s == y false && b.x == x true THEN down to false: false
> check left: IF x - s == 0 false || b.x - s == x false && b.y + s == y false || b.y + s == height true THEN left to false: false
> check right: IF x + s == width false || b.x + s == x false && b.y + s == y false || b.y + s == height true THEN right to false: false
> colcheck end: down: false left: false right: false
> block 1 ID 1 at x 50 y 575
> check down: IF y + s == height true || b.y - s == y false && b.x == x true THEN down to false: false
> check left: IF x - s == 0 false || b.x - s == x false && b.y + s == y false || b.y + s == height false THEN left to false: false
> check right: IF x + s == width false || b.x + s == x false && b.y + s == y false || b.y + s == height false THEN right to false: false
> colcheck end: down: false left: false right: false
> D: true L: false R: false M: false Xspd: 0 Yspd: 1 x 50 y 125
> res timer -1
> block 0 ID 2 at x 50 y 125
> check down: IF y + s == height false || b.y - s == y false && b.x == x true THEN down to false: true
> check left: IF x - s == 0 false || b.x - s == x false && b.y + s == y false || b.y + s == height true THEN left to false: false
> check right: IF x + s == width false || b.x + s == x false && b.y + s == y false || b.y + s == height true THEN right to false: false
> colcheck end: down: true left: false right: false
> block 1 ID 2 at x 50 y 125
> check down: IF y + s == height false || b.y - s == y false && b.x == x true THEN down to false: true
> check left: IF x - s == 0 false || b.x - s == x false && b.y + s == y false || b.y + s == height false THEN left to false: false
> check right: IF x + s == width false || b.x + s == x false && b.y + s == y false || b.y + s == height false THEN right to false: false
> colcheck end: down: true left: false right: false
> 
> ```

---

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [March 24, 2019, 3:04pm UTC](https://discourse.processing.org/t/enemy-movement-match-3/9558/11 "2019-03-24T15:04:52Z")

</div>

ok, so that’s the problem? Thanks for telling me. I thought since i just looped through on the block and didnt use the b.left it would only effect the block that is looping not the block that it checking against. & they are both meant to fall when hit space. When i place the auto move in there could be 3 or 4 blocks moving at once.
