User File #637895214307794367

Upload All User Files

#637895214307794367 - TAStudio Tetris lua script

TAStudioTetris.lua
System: Nintendo Entertainment System
119 downloads
Uploaded 5/30/2022 3:30 PM by TASeditor (see all 188)
You can now play Tetris in TAStudio.
Any NES game with 2 controllers plugged in. Open TAStudio. Playing field height is movie lenght.
Arrow key move the piece, Z and X rotate it.
-- TAStudio Tetris by TASeditor

w = 16
h = movie.length()
field = {}
fulllines = {}

t = {}
t["P1 Up"]		= 1
t["P1 Down"]	= 2
t["P1 Left"]	= 3
t["P1 Right"]	= 4
t["P1 Start"]	= 5
t["P1 Select"]	= 6
t["P1 B"]		= 7
t["P1 A"]		= 8
t["P2 Up"]		= 9
t["P2 Down"]	= 10
t["P2 Left"]	= 11
t["P2 Right"]	= 12
t["P2 Start"]	= 13
t["P2 Select"]	= 14
t["P2 B"]		= 15
t["P2 A"]		= 16

tetromino = {}

tetromino[1] = {0,0,0,0,
				  1,1,1,1,
				  0,0,0,0,
				  0,0,0,0}

tetromino[2] = {0,0,0,0,
				  2,2,2,0,
				  0,0,2,0,
				  0,0,0,0}

tetromino[3] = {0,0,0,0,
				  3,3,3,0,
				  3,0,0,0,
				  0,0,0,0}

tetromino[4] = {0,0,0,0,
				  0,4,4,0,
				  0,4,4,0,
				  0,0,0,0}

tetromino[5] = {0,0,0,0,
				  0,5,5,0,
				  5,5,0,0,
				  0,0,0,0}

tetromino[6] = {0,0,0,0,
				  6,6,6,0,
				  0,6,0,0,
				  0,0,0,0}
				  
tetromino[7] = {0,0,0,0,
				  7,7,0,0,
				  0,7,7,0,
				  0,0,0,0}

Color = {0xFFC4F7C8,--1
		 0xFF0000FF,--2
		 0xFF00FF00,--3
		 0xFF00FFFF,--4
		 0xFFFF0000,--5
		 0xFFFF00FF,--6
		 0xFFFFFF00,--7
		 0xFF0094FF,--8
		 0xFF000000,--9
		 0xFFBBEBBE,--10
		 0xFFFFFFFF}
		 
function TAStudioColor(index, column)

	col_index = t[column]
	if col_index ~= nil and col_index <= w
	then field_index = index*w + col_index
		 d = (field_index % w) + 1
	elseif col_index ~= nil and col_index > w
		then return Color[1]
	else return
	end
	
	if index < h and field[field_index] ~= nil-- and column == c[d]
	then if col_index >= currentX and col_index < currentX+4 and index >= currentY and index < currentY+4
		 then if tetromino[currentPiece][Rotate((col_index-currentX)+1,(index-currentY)+1,currentRotation)] ~= 0
			  then if running
				   then return Color[tetromino[currentPiece][Rotate((col_index-currentX)+1,(index-currentY)+1,currentRotation)]+1]
				   else return Color[field[field_index]+1]
				   end
			  else return Color[field[field_index]+1]
			  end
		 else return Color[field[field_index]+1]
		 end
	elseif index >= h
		then return Color[1]
	end
	
end

function TAStudioText(index, column)

	if not running and not gameOver
	then if index == 2 and column == "P1 Right"
		 then return "P"
		 elseif index == 2 and column == "P1 Start"
		 then return "R"
		 elseif index == 2 and column == "P1 Select"
		 then return "E"
		 elseif index == 2 and column == "P1 B"
		 then return "S"
		 elseif index == 2 and column == "P1 A"
		 then return "S"
		 elseif index == 4 and column == "P1 Right"
		 then return "E"
		 elseif index == 4 and column == "P1 Start"
		 then return "N"
		 elseif index == 4 and column == "P1 Select"
		 then return "T"
		 elseif index == 4 and column == "P1 B"
		 then return "E"
		 elseif index == 4 and column == "P1 A"
		 then return "R"
		 elseif index == 6 and column == "P1 Select"
		 then return "T"
		 elseif index == 6 and column == "P1 B"
		 then return "O"
		 elseif index == 8 and column == "P1 Right"
		 then return "S"
		 elseif index == 8 and column == "P1 Start"
		 then return "T"
		 elseif index == 8 and column == "P1 Select"
		 then return "A"
		 elseif index == 8 and column == "P1 B"
		 then return "R"
		 elseif index == 8 and column == "P1 A"
		 then return "T"
		 end
	elseif not running and gameOver
		then if index == 2 and column == "P1 Start"
			 then return "G"
			 elseif index == 2 and column == "P1 Select"
			 then return "A"
			 elseif index == 2 and column == "P1 B"
			 then return "M"
			 elseif index == 2 and column == "P1 A"
			 then return "E"
			 elseif index == 4 and column == "P1 Start"
			 then return "O"
			 elseif index == 4 and column == "P1 Select"
			 then return "V"
			 elseif index == 4 and column == "P1 B"
			 then return "E"
			 elseif index == 4 and column == "P1 A"
			 then return "R"
			 end
	end
	
end

function Rotate(x, y, r)

	r = r % 4;
	
	if r == 0
	then return (y-1)*4+x
	elseif r == 1
	then return 13+(y-1)-((x-1)*4)
	elseif r == 2
	then return 16-((y-1)*4)-(x-1)
	elseif r == 3
	then return 4-(y-1)+((x-1)*4)
	end
	return 0

end

--returns true if no collision
function CheckCollision(piece, rotation, posX, posY)

	for y = 1, 4, 1 do
		for x = 1, 4, 1 do
			local piece_i = Rotate(x,y,rotation)
			local field_i = (posY + (y-1)) * w + (posX + (x-1))
			if tetromino[piece][piece_i] ~= 0 and posX + x <= 1 
			then return false
			end
			if tetromino[piece][piece_i] ~= 0 and posX + x-1 > w 
			then return false
			end
			if tetromino[piece][piece_i] ~= 0 and field[field_i] ~= 0
			then return false
			end
		end
	end

	return true

end

function PieceToField(piece, rotation, posX, posY)

	for y = 1, 4, 1 do
		for x = 1, 4, 1 do
			local piece_i = Rotate(x,y,rotation)
			local field_i = (posY + (y-1)) * w + (posX + (x-1))
			if tetromino[piece][piece_i] ~= 0 and field[field_i] == 0
			then field[field_i] = tetromino[piece][piece_i] 
			end
		end
	end

end

function Inputs()

	if not running
	then return
	end
	--
	if inp["Left"] and wasinp["Left"] == nil and CheckCollision(currentPiece, currentRotation, currentX-1, currentY)
	then currentX = currentX-1
	end
	if inp["Right"] and wasinp["Right"] == nil and CheckCollision(currentPiece, currentRotation, currentX+1, currentY)
	then currentX = currentX+1
	end
	if inp["Down"] and CheckCollision(currentPiece, currentRotation, currentX, currentY+1)
	then currentY = currentY+1
	elseif inp["Down"] and wasinp["Down"] == nil
		then PieceToField(currentPiece, currentRotation, currentX, currentY)
			  NewPiece()
	end
	if inp["X"] and wasinp["X"] == nil and CheckCollision(currentPiece, currentRotation+1, currentX,currentY)
	then currentRotation = currentRotation+1
	end
	if inp["Z"] and wasinp["Z"] == nil and CheckCollision(currentPiece, currentRotation-1, currentX,currentY)
	then currentRotation = currentRotation-1
	end

end

function NewPiece()
	
	currentPiece = nextPiece
	nextPiece = math.random(1,7)
	currentRotation = 0
	currentX = 5
	currentY = -1

end

function InitializeField()
	for y = 1, h, 1 do
		for x = 1, w, 1 do
			if x == 1 or x == w or y == h
			then field[(y-1)*w+x] = 0--9
			else field[(y-1)*w+x] = 0
			end
		end
	end
end

function CheckLines()

	for y = 1, 4, 1 do
		if currentY + (y-1) < h 
		then line = true
			 for x = 1, w, 1 do
				if field[(currentY + (y-1)) * w + ((x))] == 0
				then line = false
				end
			 end
			 
			 if line
			 then for x = 1, w, 1 do 
					field[(currentY + (y-1)) * w + ((x))] = 10
				  end
				  table.insert(fulllines, currentY + (y-1))
			 end
		end
	end
	
end

function MoveLinesDown()

	for k,v in pairs(fulllines) do
		
		for y = v, 1, -1 do		
			for x = 1, w, 1 do
				field[((y))*w+x] = field[((y-1))*w+x]
			end
		end
		for x = 1, w, 1 do
			field[x] = 0
		end
		
		fulllines[k] = nil
	end
	
end

function Exit()

	client.pause()

end

tastudio.onqueryitembg(TAStudioColor)
tastudio.onqueryitemtext(TAStudioText)
event.onexit(Exit)

running = false
gameOver = false
nextPiece = math.random(1,7)
NewPiece()
timer=0
nextt = 10
InitializeField()
client.unpause()
while true do

	tastudio.setplayback(currentY+1)
	timer=timer+1
	
	inp = input.get()
	Inputs()
	
	if running and timer >= nextt
	then timer=0
		 	 
		 if CheckCollision(currentPiece, currentRotation, currentX, currentY+1)
		 then currentY = currentY+1
		 else PieceToField(currentPiece, currentRotation, currentX, currentY)
			  CheckLines()
			  
			  if #fulllines > 0
			  then MoveLinesDown()
			  end
		 
			  NewPiece()
			  
			  if not CheckCollision(currentPiece, currentRotation, currentX, currentY)
			  then running = false
				   gameOver = true
			  end
		 end
	elseif inp["Enter"] and not running 
		then InitializeField()
			 running = true
			 gameOver = false
	end
	
	wasinp = inp

	emu.frameadvance()

end