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