<commit_before>

extends Control

# Simple Tetris-like demo, (c) 2012 <NAME>
# Implemented by using a regular Control and drawing on it during the _draw() callback.
# The drawing surface is updated only when changes happen (by calling update())


var score = 0
var score_label=null

const MAX_SHAPES = 7

var block = preload("block.png")

var block_colors=[
	Color(1,0.5,0.5),
	Color(0.5,1,0.5),
	Color(0.5,0.5,1),
	Color(0.8,0.4,0.8),
	Color(0.8,0.8,0.4),
	Color(0.4,0.8,0.8),
	Color(0.7,0.7,0.7)]

var	block_shapes=[
	[ Vector2(0,-1),Vector2(0,0),Vector2(0,1),Vector2(0,2) ], # I
	[ Vector2(0,0),Vector2(1,0),Vector2(1,1),Vector2(0,1) ], # O
	[ Vector2(-1,1),Vector2(0,1),Vector2(0,0),Vector2(1,0) ], # S
	[ Vector2(1,1),Vector2(0,1),Vector2(0,0),Vector2(-1,0) ], # Z
	[ Vector2(-1,1),Vector2(-1,0),Vector2(0,0),Vector2(1,0) ], # L
	[ Vector2(1,1),Vector2(1,0),Vector2(0,0),Vector2(-1,0) ], # J
	[ Vector2(0,1),Vector2(1,0),Vector2(0,0),Vector2(-1,0) ]] # T
	

var block_rotations=[
	Matrix32( Vector2(1,0),Vector2(0,1), Vector2() ),
	Matrix32( Vector2(0,1),Vector2(-1,0), Vector2() ),
	Matrix32( Vector2(-1,0),Vector2(0,-1), Vector2() ),
	Matrix32( Vector2(0,-1),Vector2(1,0), Vector2() )
]
	

var width=0
var height=0

var cells={}

var piece_active=false
var piece_shape=0
var piece_pos=Vector2()
var piece_rot=0


func piece_cell_xform(p,er=0):
	var r = (4+er+piece_rot)%4
	return piece_pos+block_rotations[r].xform(p)

func _draw():

	var sb = get_stylebox("bg","Tree") # use line edit bg
	draw_style_box(sb,Rect2(Vector2(),get_size()).grow(3))
	
	var bs = block.get_size()
	for y in range(height):
		for x in range(width):
			if (Vector2(x,y) in cells):
				draw_texture_rect(block,Rect2(Vector2(x,y)*bs,bs),false,block_colors[cells[Vector2(x,y)]])
				
	if (piece_active):
		
		for c in block_shapes[piece_shape]:
			draw_texture_rect(block,Rect2(piece_cell_xform(c)*bs,bs),false,block_colors[piece_shape])
			

func piece_check_fit(ofs,er=0):

	for c in block_shapes[piece_shape]:
		var pos = piece_cell_xform(c,er)+ofs
		if (pos.x < 0):
			return false
		if (pos.y < 0):
			return false
		if (pos.x >= width):
			return false
		if (pos.y >= height):
			return false
		if (pos in cells):
			return false
	
	return true	

func new_piece():

	piece_shape = randi() % MAX_SHAPES	
	piece_pos = Vector2(width/2,0)
	piece_active=true
	piece_rot=0
	if (piece_shape==0):
		piece_pos.y+=1
		
	if (not piece_check_fit(Vector2())):
		#game over
		#print("GAME OVER!")
		game_over()
		
	update()
		
	
func test_collapse_rows():
	var accum_down=0
	for i in range(height):
		var y = height - i - 1
		var collapse = true
		for x in range(width):
			if (Vector2(x,y) in cells):
				if (accum_down):
					cells[ Vector2(x,y+accum_down) ] = cells[Vector2(x,y)]
			else:
				collapse=false
				if (accum_down):
					cells.erase( Vector2(x,y+accum_down) )
						
		if (collapse):
			accum_down+=1
		
			
	score+=accum_down*100
	score_label.set_text(str(score))
			
		
func game_over():

		piece_active=false
		get_node("gameover").set_text("Game Over")		
		update()
				
		
func restart_pressed():

		score=0
		score_label.set_text("0")
		cells.clear()
		get_node("gameover").set_text("")		
		piece_active=true
		update()
		
		
<commit_msg>Remove focus from restart button, fixes #1850<commit_after>

extends Control

# Simple Tetris-like demo, (c) 2012 <NAME>
# Implemented by using a regular Control and drawing on it during the _draw() callback.
# The drawing surface is updated only when changes happen (by calling update())


var score = 0
var score_label=null

const MAX_SHAPES = 7

var block = preload("block.png")

var block_colors=[
	Color(1,0.5,0.5),
	Color(0.5,1,0.5),
	Color(0.5,0.5,1),
	Color(0.8,0.4,0.8),
	Color(0.8,0.8,0.4),
	Color(0.4,0.8,0.8),
	Color(0.7,0.7,0.7)]

var	block_shapes=[
	[ Vector2(0,-1),Vector2(0,0),Vector2(0,1),Vector2(0,2) ], # I
	[ Vector2(0,0),Vector2(1,0),Vector2(1,1),Vector2(0,1) ], # O
	[ Vector2(-1,1),Vector2(0,1),Vector2(0,0),Vector2(1,0) ], # S
	[ Vector2(1,1),Vector2(0,1),Vector2(0,0),Vector2(-1,0) ], # Z
	[ Vector2(-1,1),Vector2(-1,0),Vector2(0,0),Vector2(1,0) ], # L
	[ Vector2(1,1),Vector2(1,0),Vector2(0,0),Vector2(-1,0) ], # J
	[ Vector2(0,1),Vector2(1,0),Vector2(0,0),Vector2(-1,0) ]] # T
	

var block_rotations=[
	Matrix32( Vector2(1,0),Vector2(0,1), Vector2() ),
	Matrix32( Vector2(0,1),Vector2(-1,0), Vector2() ),
	Matrix32( Vector2(-1,0),Vector2(0,-1), Vector2() ),
	Matrix32( Vector2(0,-1),Vector2(1,0), Vector2() )
]
	

var width=0
var height=0

var cells={}

var piece_active=false
var piece_shape=0
var piece_pos=Vector2()
var piece_rot=0


func piece_cell_xform(p,er=0):
	var r = (4+er+piece_rot)%4
	return piece_pos+block_rotations[r].xform(p)

func _draw():

	var sb = get_stylebox("bg","Tree") # use line edit bg
	draw_style_box(sb,Rect2(Vector2(),get_size()).grow(3))
	
	var bs = block.get_size()
	for y in range(height):
		for x in range(width):
			if (Vector2(x,y) in cells):
				draw_texture_rect(block,Rect2(Vector2(x,y)*bs,bs),false,block_colors[cells[Vector2(x,y)]])
				
	if (piece_active):
		
		for c in block_shapes[piece_shape]:
			draw_texture_rect(block,Rect2(piece_cell_xform(c)*bs,bs),false,block_colors[piece_shape])
			

func piece_check_fit(ofs,er=0):

	for c in block_shapes[piece_shape]:
		var pos = piece_cell_xform(c,er)+ofs
		if (pos.x < 0):
			return false
		if (pos.y < 0):
			return false
		if (pos.x >= width):
			return false
		if (pos.y >= height):
			return false
		if (pos in cells):
			return false
	
	return true	

func new_piece():

	piece_shape = randi() % MAX_SHAPES	
	piece_pos = Vector2(width/2,0)
	piece_active=true
	piece_rot=0
	if (piece_shape==0):
		piece_pos.y+=1
		
	if (not piece_check_fit(Vector2())):
		#game over
		#print("GAME OVER!")
		game_over()
		
	update()
		
	
func test_collapse_rows():
	var accum_down=0
	for i in range(height):
		var y = height - i - 1
		var collapse = true
		for x in range(width):
			if (Vector2(x,y) in cells):
				if (accum_down):
					cells[ Vector2(x,y+accum_down) ] = cells[Vector2(x,y)]
			else:
				collapse=false
				if (accum_down):
					cells.erase( Vector2(x,y+accum_down) )
						
		if (collapse):
			accum_down+=1
		
			
	score+=accum_down*100
	score_label.set_text(str(score))
			
		
func game_over():

		piece_active=false
		get_node("gameover").set_text("Game Over")		
		update()
				
		
func restart_pressed():

		score=0
		score_label.set_text("0")
		cells.clear()
		get_node("gameover").set_text("")		
		piece_active=true
		get_node("../restart").release_focus()
		update()
		
		

{'fraction_non_alphanumeric': 0.17541711311801622, 'fraction_numerical': 0.0627583039951018, 'mean_word_length': 4.481543624161074, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4846169', 'n_tokens_mistral': 3048, 'n_tokens_neox': 2737, 'n_words': 509}