# Collision Detection in Tetris

• ### Question

• Hello,

I am trying to create a small basic version of Tetris. I am aware of the small basic Tetris at the small basic home page, but as I tried to observe and dissect the sample, I only ended up more confused. I mainly want to focus on collision detection. How does the program know when to stop a moving block when it lands on another block? How does it work logically? Any prototypes, explanations, or guidance would be much appreciated.

• Edited by Tuesday, November 18, 2014 11:52 PM
Tuesday, November 18, 2014 11:51 PM

• Sure.  I named "Monoris". :>

A piece for Tetris is tromino - combined with four squares.  So, a piece for Monoris is monomino - just a square.

I rewrote my sample program and publish as TLP645.

Nonki Takahashi

• Marked as answer by Tuesday, November 25, 2014 1:34 AM
Saturday, November 22, 2014 10:44 AM

### All replies

• moving block that is going down

b1y would be the starting y coordinate for the moving block

ex. b1y = b1y - 5

==========================================

top of a block on the floor

ex. b2y = 50

==========================================

if b1y = b2y then

'you make the moving block stop depending on your code

endif

so when moving block y = 50 it will stop

• Edited by Wednesday, November 19, 2014 3:18 AM
Wednesday, November 19, 2014 12:26 AM
• Ezra94, how about to have game board array?  Following code doesn't have collision detection but I think you can do it.

```gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.BackgroundColor = "gray"
InitPiece()
ClearBoard()
DrawBoard()
Sub InitPiece
piece[1] = "width=3;height=2;angle=0;"
piece[1]["shape"]["1"] = "1=1;2=1;3=1;"
piece[1]["shape"]["2"] = "1=0;2=1;3=0;"
color[1] = "red"
piece[2] = "width=3;height=2;angle=0;"
piece[2]["shape"]["1"] = "1=1;2=1;3=1;"
piece[2]["shape"]["2"] = "1=1;2=0;3=0;"
color[2] = "green"
EndSub
Sub ClearBoard
maxRow = 10
maxCol = 5
color[0] = "white"
For row = 1 To maxRow
For col = 1 To maxCol
board[row][col] = 0
EndFor
EndFor
sizePiece = 40
x0 = (gw - sizePiece * maxCol) / 2
y0 = (gh - sizePiece * maxRow) / 2
EndSub
Sub DrawBoard
For row = 1 To maxRow
y = y0 + (row - 1) * sizePiece
For col = 1 To maxCol
x = x0 + (col - 1) * sizePiece
GraphicsWindow.BrushColor = color[board[row][col]]
GraphicsWindow.FillRectangle(x, y, sizePiece, sizePiece)
EndFor
EndFor
EndSub```

Nonki Takahashi

Friday, November 21, 2014 11:17 PM
• Does having a game board array allow me to predict the potential collision detection between different pieces? I guess I should start off more simple. Maybe instead of stacking terminos, I can start off with simple blocks? You're right though I should try to figure it out on my own first. . . . .
• Edited by Friday, November 21, 2014 11:45 PM
Friday, November 21, 2014 11:26 PM
• Most simple game will be Monoris.

```Sub InitPiece
piece[1] = "width=1;height=1;angle=0;"
piece[1]["shape"]["1"] = "1=1;"
color[1] = "red"
piece[2] = "width=1;height=1;angle=0;"
piece[2]["shape"]["1"] = "1=1;"
color[2] = "green"
EndSub```

Nonki Takahashi

Saturday, November 22, 2014 12:02 AM
• If I drop two blocks next to each other, I can't drop the third block on both blocks below so I have to choose between stacking it on one of the two or a completely new spot. How can I predict the potential collision of all these spots?
• Edited by Saturday, November 22, 2014 12:30 AM
Saturday, November 22, 2014 12:19 AM
• Monoris. Never heard of it. I will look it up. By the way, is piece[1] and piece[2] part of a larger shape? Also what does the value "1=1" mean in
`piece[1]["shape"]["1"] = "1=1;"`
Does that allow me to predict it's collision detection?

• Edited by Saturday, November 22, 2014 1:06 AM
Saturday, November 22, 2014 12:21 AM
• Sure.  I named "Monoris". :>

A piece for Tetris is tromino - combined with four squares.  So, a piece for Monoris is monomino - just a square.

I rewrote my sample program and publish as TLP645.

Nonki Takahashi

• Marked as answer by Tuesday, November 25, 2014 1:34 AM
Saturday, November 22, 2014 10:44 AM
• Thank you for the sample. I'll see if I can figure out the collision detection. I've got a couple of ideas I would like to test out.
• Edited by Saturday, November 22, 2014 4:11 PM
Saturday, November 22, 2014 4:10 PM
• Hello, Nonki (or anyone reading)

I am altering the code for the sample program, TLP645,  provided so I could make a full game from it (more details on that project will be discussed at a later post). However, I confused about how collision detection works. I've tried everything from adding text windows to breaking the program into smaller parts but I'm still confused. I have three questions.

First question: when I add text windows for row in the subroutine drawBoard, the max value of row for each piece decreases by one. For example, when the first piece reaches the bottom of the screen the value of row is 11 then when the second piece lands on top of the first piece the value of row is 10 and so on. At what part of the program does the value of row decrease? How did you get the falling pieces to stack on top of each other?

Another question I have is the nature of the collision detection. It appears that the value of color determines the collision of each piece. When board[rowBoard][colBoard] <> 0,  or if the current piece is not the color "white", then collision will be "True" and the current piece will stop. That would mean that the color value has to be 1 or 2 to stop and collide but in the subroutine movePiece the color value of the current piece is already 1 or 2 (red or green) far before reaching the bottom of the screen or colliding with another piece. What does the value of color have to do with collision? Why didn't you have the collision become "True" when the current piece reaches the bottom? Am I misreading this part?

Lastly, when collision is "True" then "Floating" is "False" which means the program will stop adding 1 to row = dropped[iDropped]["row"] and as a result stop the piece from falling. In other words, if the current piece stops moving down each row then collision will occur. I know it's an odd question but is that correct?

If you need any further explanation let me know. Any help would be much welcomed. Thank you.

Thursday, January 8, 2015 4:47 AM