View difference between Paste ID: CJxyqRvP and k20ui8aL
SHOW: | | - or go back to the newest paste.
1-
 --yfcgUsLw
1+
 -- Tetris by MKmisfit
2
3
local timerLength = 0.5
4
5
local pieceX
6
local pieceY
7
local pieceR
8
local pieceType
9
local nextPieceType
10
local score
11
local gameOver
12
13
local pieceArray = {
14
{ {0,0},{-1,0},{1,0},{0,1} },
15
{ {0,0},{-1,0},{-2,0},{1,0} },
16
{ {0,0},{-1,0},{0,1},{1,1} },
17
{ {0,0},{-1,1},{0,1},{1,0} },
18
{ {0,0},{1,0},{0,1},{1,1} },
19
{ {0,0},{-1,0},{1,0},{1,1} },
20
{ {0,0},{-1,0},{1,0},{-1,1} }
21
}
22-
local pieceColor = {colors.red, colors.cyan, colors.blue, colors.orange, colors.green, colors.purple}
22+
23
local pieceColor = {colors.purple, colors.cyan, colors.red, colors.green, colors.yellow, colors.blue, colors.orange}
24-
local numPieces = 6
24+
25
local numPieces = 7
26
27
local boardArray
28
local boardWidth = 12
29
local boardHeight = 21
30
local boardX = 8
31
local boardY = -1
32
local edgeColor = colors.white
33
local boardColor = colors.black
34
35
local scoreX = 26
36
local scoreY = 4
37
local nextPieceX = 26
38
local nextPieceY = 8
39
40
local highScore = {}
41
local saveFile = "tetris_scores"
42
43
local function loadHighScore()
44
45
	if fs.exists(saveFile) then
46
		local file = fs.open(saveFile,"r")				
47
		for n = 1, 10 do
48
			highScore[n] = {file.readLine(), tonumber(file.readLine())}
49
		end	
50
		file.close()
51
	else
52
		for n = 1, 10 do
53
			highScore[n] = {"", 0}
54
		end
55
	end
56
end
57
58
local function saveHighScore()
59
	local file = fs.open(saveFile,"w")	
60
			
61
	for n = 1, 10 do
62
		file.write(highScore[n][1].."\n")
63
		file.write(tostring(highScore[n][2]).."\n")
64
	end
65
	file.close()
66
end
67
68
local function testHighScore()
69
	for n = 1, 10 do
70
		if score > highScore[n][2] then
71
			term.setCursorPos(1,1)
72
			term.write("You got a High Score!")
73
			term.setCursorPos(1,2)
74-
			name = string.sub(io.read(), 1, 32)
74+
75
			name = string.sub(io.read(), 1, 16)
76
			name = name .. string.sub("                  ", 1, 18 - string.len(name))
77
			table.insert(highScore, n, {name, score})
78
			return true
79
		end
80
	end
81
	return false
82
end
83
84
local function drawCredits()
85
	term.setCursorPos(1,1)
86
	term.write("Tetris")
87
	term.setCursorPos(1,2)
88
	term.write("Original by Alexey Pajitnov circa 1984")
89
	term.setCursorPos(1,3)
90
	term.write("Lua adaptation by MKmisfit")
91
	term.setCursorPos(1,4)
92
	term.write("arrow keys move, (1, 2) keys rotate, ENTER pauses")
93
94
	if highScore[1][2] > 0 then
95
		term.setCursorPos(1,6)
96
		term.write("High Scores")
97
		for n = 1, 10 do
98-
			term.write(highScore[n][1] .. "  " .. highScore[n][2])
98+
99
			term.setCursorPos(1,n+6)
100
			term.write(highScore[n][1] .. highScore[n][2])
101
		end
102
	end
103
104
end
105
106
local function testPiece(piece, x, y, rotation)
107
108
	if rotation == 0 then
109
		for i = 1, 4 do
110
			if boardArray[x + piece[i][1]][y + piece[i][2]] ~= boardColor then
111
				return true
112
			end
113
		end
114
	end
115
	if rotation == 1 then
116
			for i = 1, 4 do
117
			if boardArray[x - piece[i][2]][y + piece[i][1]] ~= boardColor then
118
				return true
119
			end
120
		end
121
	end
122
	if rotation == 2 then
123
			for i = 1, 4 do
124
			if boardArray[x - piece[i][1]][y - piece[i][2]] ~= boardColor then
125
				return true
126
			end
127
		end
128
	end
129
	if rotation == 3 then
130
			for i = 1, 4 do
131
			if boardArray[x + piece[i][2]][y - piece[i][1]] ~= boardColor then
132
				return true
133
			end
134
		end
135
	end
136
137
	return false
138
end
139
140
local function drawScore()
141
	term.setBackgroundColor(boardColor)
142
	term.setCursorPos(scoreX + 7, scoreY)
143
	term.write(tostring(score) .. "       ")
144
end
145
146
local function setPiece(piece, x, y, rotation, color)
147
148
	if rotation == 0 then
149
		for i = 1, 4 do
150
			boardArray[x + piece[i][1]][y + piece[i][2]] = color 
151
		end
152
	end
153
	if rotation == 1 then
154
		for i = 1, 4 do
155
			boardArray[x - piece[i][2]][y + piece[i][1]] = color 
156
		end
157
	end
158
	if rotation == 2 then
159
		for i = 1, 4 do
160
			boardArray[x - piece[i][1]][y - piece[i][2]] = color
161
		end
162
	end
163
	if rotation == 3 then
164
		for i = 1, 4 do
165
			boardArray[x + piece[i][2]][y - piece[i][1]] = color
166
		end
167
	end
168
	
169
	local minx = 2
170
	local maxx = boardWidth - 1	
171
	local miny = y - 2
172
	local maxy = y + 2
173
174
	local haspixels
175
	local linescleared = 0
176
177
	if miny < 3 then miny = 3 end
178
	if maxy >= boardHeight then maxy = boardHeight - 1 end
179
	
180
	--it would be more efficient to clear all lines at once
181
182
	for by = miny, maxy do
183
	for bx = minx, maxx do
184
		if boardArray[bx][by] == boardColor then break end
185
		if bx == maxx then
186
			linescleared = linescleared + 1
187
			for copyy = by - 1, 2, -1 do			
188
				term.setCursorPos(minx + boardX, copyy + 1 + boardY)
189
				haspixels = false
190
				for copyx = minx, maxx do
191
					term.setBackgroundColor(boardArray[copyx][copyy])
192
					term.write(" ")
193
					haspixels = haspixels or (boardArray[copyx][copyy] ~= boardColor)
194
					boardArray[copyx][copyy + 1] = boardArray[copyx][copyy]	
195
				end
196
				if not haspixels then break end
197
				--clear top line
198
				if copyy == 2 then
199
					term.setCursorPos(minx + boardX, copyy + boardY)
200
					term.setBackgroundColor(boardColor)
201
					for copyx = minx, maxx do
202
						term.write(" ")
203
						boardArray[copyx][copyy] = boardColor
204
					end
205
				end
206
			end
207
			sleep(0.5)
208
		end
209
	end
210
	end
211
212
	if linescleared ~= 0 then
213
		if linescleared == 1 then
214
			score = score + 100
215
		end
216
		if linescleared == 2 then
217
			score = score + 200
218
		end
219
		if linescleared == 3 then
220
			score = score + 400
221
		end
222
		if linescleared == 4 then
223
			score = score + 800
224
		end
225
		drawScore()
226
	end
227
228
end
229
230
local function drawPiece(piece, x, y, rotation, color)
231
232
	if rotation == 0 then
233
		for i = 1, 4 do
234
			term.setCursorPos(x + piece[i][1], y + piece[i][2])
235
			term.setBackgroundColor(color)
236
			term.write(" ")
237
		end
238
	end
239
	if rotation == 1 then
240
		for i = 1, 4 do
241
			term.setCursorPos(x - piece[i][2], y + piece[i][1])
242
			term.setBackgroundColor(color)
243
			term.write(" ")
244
		end
245
	end
246
	if rotation == 2 then
247
		for i = 1, 4 do
248
			term.setCursorPos(x - piece[i][1], y - piece[i][2])
249
			term.setBackgroundColor(color)
250
			term.write(" ")
251
		end
252
	end
253
	if rotation == 3 then
254
		for i = 1, 4 do
255
			term.setCursorPos(x + piece[i][2], y - piece[i][1])
256
			term.setBackgroundColor(color)
257
			term.write(" ")
258
		end
259
	end
260
261
262
end
263
264
local function dropNewPiece()
265
	pieceX = boardWidth / 2
266
	pieceY = 2
267
	pieceType = nextPieceType
268
	nextPieceType = math.floor(math.random(1, numPieces + 0.999))
269
	pieceR = 0
270
271
	if testPiece(pieceArray[pieceType], pieceX, pieceY, pieceR) then
272
		gameOver = true
273
	end
274
	
275
	drawPiece(pieceArray[pieceType], nextPieceX + 2, nextPieceY + 2, 0, boardColor)
276
	drawPiece(pieceArray[nextPieceType], nextPieceX + 2, nextPieceY + 2, 0, pieceColor[nextPieceType])
277
278
end
279
280
local function main()
281
282
	--clear the board	
283
	for x = 1, boardWidth do
284
	for y = 1, boardHeight do
285
		if x == 1 or y == 1 or x == boardWidth or y == boardHeight then		
286
			boardArray[x][y] = edgeColor
287
			if y > 1 and y < boardHeight then 
288
				term.setCursorPos(x + boardX, y + boardY)
289
				term.setBackgroundColor(edgeColor)
290
				term.write(" ")
291
			end
292
		else	
293
			boardArray[x][y] = boardColor
294
			term.setCursorPos(x + boardX, y + boardY)
295
			term.setBackgroundColor(boardColor)
296
			term.write(" ")
297
		end
298
	end
299
	end
300
301
	
302
	term.setBackgroundColor(boardColor)
303
	term.setCursorPos(scoreX, scoreY)
304
	term.write("Score: 0       ")
305
	term.setCursorPos(nextPieceX, nextPieceY)
306
	term.write("Next:")
307
308
	gameOver = false
309
	score = 0
310
311
	nextPieceType = math.floor(math.random(1, numPieces + 0.999))
312
	dropNewPiece()
313
314
	timer1 = os.startTimer(timerLength)
315
316
	local event, par
317
318
	while true do		
319
		event, par = os.pullEvent()
320
321
		if event == "timer" and par == timer1 then 
322
			if testPiece(pieceArray[pieceType], pieceX, pieceY + 1, pieceR) then
323
				setPiece(pieceArray[pieceType], pieceX, pieceY, pieceR, pieceColor[pieceType])
324
				dropNewPiece()
325
				drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, pieceColor[pieceType])
326
			else
327
				drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, boardColor)
328
				pieceY = pieceY + 1
329
				drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, pieceColor[pieceType])
330
			end
331
		
332
			if gameOver then
333
				break
334
			end
335
336
			timer1 = os.startTimer(timerLength)
337
		end
338
339
		if event == "key" then
340
			if par == 203 then
341
				if not testPiece(pieceArray[pieceType], pieceX - 1, pieceY, pieceR) then
342
					drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, boardColor)
343
					pieceX = pieceX - 1
344
					drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, pieceColor[pieceType])
345
				end
346
			elseif par == 205 then
347
				if not testPiece(pieceArray[pieceType], pieceX + 1, pieceY, pieceR) then
348
					drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, boardColor)
349
					pieceX = pieceX + 1
350
					drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, pieceColor[pieceType])
351
				end
352
			elseif par == 208 then
353
				if not testPiece(pieceArray[pieceType], pieceX, pieceY + 1, pieceR) then
354
					drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, boardColor)
355
					pieceY = pieceY + 1
356
					drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, pieceColor[pieceType])
357
					score = score + 2
358
					drawScore()
359
				end
360
			elseif par == 2 then
361
				if not testPiece(pieceArray[pieceType], pieceX, pieceY, (pieceR + 1) % 4) then
362
					drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, boardColor)
363
					pieceR = (pieceR + 1) % 4
364
					drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, pieceColor[pieceType])
365
				end
366
			elseif par == 3 then
367
				if not testPiece(pieceArray[pieceType], pieceX, pieceY, (pieceR + 3) % 4) then
368
					drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, boardColor)
369
					pieceR = (pieceR + 3) % 4
370
					drawPiece(pieceArray[pieceType], pieceX + boardX, pieceY + boardY, pieceR, pieceColor[pieceType])
371
				end
372
			elseif par == 28 then
373
				term.setBackgroundColor(boardColor)
374
				term.setCursorPos(scoreX, scoreY + 1)
375
				term.write("Paused")
376
				
377
				repeat
378
					event, par = os.pullEvent()
379
				until event == "key" and par == 28
380
381
				term.setCursorPos(scoreX, scoreY + 1)
382
				term.write("      ")
383
				timer1 = os.startTimer(timerLength)
384
			end
385
		end
386
387
	end
388
389
390
end
391
392
boardArray = {}
393
for x = 1, boardWidth do
394
	boardArray[x] = {}
395
end
396
397
term.setBackgroundColor(boardColor)
398
term.clear()
399
400
loadHighScore()
401
drawCredits()
402
403
term.setCursorPos(1,19)
404
term.write("Press ENTER to continue")
405
repeat
406
	event, par = os.pullEvent()
407
until event == "key" and par == 28
408
term.clear()
409
410
main()
411
412
term.setBackgroundColor(colors.black)
413
term.clear()
414
415
if testHighScore() then
416
	saveHighScore()
417
	term.clear()
418
end
419
drawCredits()
420
421
term.setCursorPos(1,17)
422
term.write("Your final score was " .. tostring(score))
423
term.setCursorPos(1,18)
424
term.write("Thank You for Playing!")
425
term.setCursorPos(1,19)