viewport as collider like in TETRIS .

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By THE HELIX

I need to make the viewport as a barrier / a wall to hold the blocks like in TETRIS where the blocks are prevented from leaving the viewport . The blocks are falling along the y-axis from above.I put four staticbody 2D with collission shape around the viewport to create a barrier and attached the code to the player as below

extends KinematicBody2D

var extents
var screensize
var pos
var velocity = Vector2()
var gravity = 50

func _ready():
      screensize = get_viewport_rect().size
      extents = $Sprite.get_texture().get_size() / 8
      pos = screensize / 8

func _physics_process(delta):
      velocity.y += gravity * delta
      position.y += velocity.y * delta
      move_and_collide(velocity * delta)
      pos += velocity * delta
      if pos.x >= screensize.x - extents.x:
	       pos.x = screensize.x - extents.x
	       velocity.x *= 0
      if pos.x <= extents.x:
	       pos.x = extents.x
	       velocity.x *= 0
      if pos.y >= screensize.y - extents.y:
	       pos.y = screensize.y - extents.y
	       velocity.y *= 0
      if pos.y <= extents.y:
	       pos.y = extents.y
	       velocity.y *= 0

When I execute it the viewport acts as a wall/barrier.The spawned blocks are held by the viewport wall.After some time more blocks fall . When the lower blocks are cleared the upper blocks do not come in the place of the cleared blocks but stay where they where and comes down only when more blocks fall on them.When I make velocity.y *= 1 and velocity.x *= 1 ; the blocks come down if the lower blocks are cleared.But when more blocks fall on them they pass through the collission body and fail to detect collissions .I even put staticbody around the viewport but they fail to detect collission.
Can you please help me out ?