View difference between Paste ID: FgiGiepX and DVRq31jL
SHOW: | | - or go back to the newest paste.
1
w,h=term.getSize() -- 51  19
2
3
function monSign()
4
	monitor=peripheral.wrap("top")
5
	monitor.setTextScale(0.5)
6
	monW,monH=monitor.getSize()  --w 15  h 10
7
	monitor.clear()
8
	monitor.setCursorPos(6,4)
9
	monitor.write("SPACE")
10
	monitor.setCursorPos(4,6)
11
	monitor.write("INVADERS!")
12
end
13
14
function initVariables()
15
	keyPress= 0
16
	curPos=w/2
17
	bullet=false
18
	hPos=h-4
19
	bulletCurPos=0
20
	gameTimer=0
21
	alienW = 5
22
	alienH = 2
23
	alienLeftStop=2
24
	alienRightStop=8
25
	lives=3
26
	killed= false
27
	killCounter=0
28
	noBombs=true
29
	bombTimer=0
30
	win=false
31
	score=0
32
33
	alien1 = true
34
	alien2 = true
35
	alien3 = true
36
	alien4 = true
37
	alien5 = true
38
	alien6 = true
39
	
40
41
	bomb1=false
42
	bomb2=false
43
	bomb3=false
44
45
46
	alienCount=6
47
end
48
49
function clear()
50
	  term.clear()
51
	  term.setCursorPos(1,1)
52
	  term.setBackgroundColour(colours.black)
53
	  term.setTextColour(colours.white)
54
end
55
56
function gameController()
57
	local alienMove=1
58
	local gameTimer = os.startTimer(0.1)
59
	repeat
60
		
61
		local event,p1,p2,p3 = os.pullEvent()
62
		if event=="key" and killed == false then
63
			keyPress=p1
64
			keyAction()
65
		elseif event=="timer" then
66
			
67
			if noBombs==true then 
68
				bombTimer = bombTimer+1
69
				if bombTimer==30 then
70
					noBombs=false
71
					bombTimer=0
72
				end
73
			end
74
			bombs()
75
			
76
			if killed==true then 
77
				killCounter=killCounter+1
78
					if killCounter==10 then
79
						killed=false
80
						curPos=w/2
81
						killCounter=0
82
						drawShip(curPos)
83
					end
84
			end
85
			
86
			term.setTextColour(colours.white)
87
			term.setCursorPos(1,1)
88
			--print("p1:        "..p1.."  ")
89
			term.setCursorPos(1,2)
90
			--print("gameTimer: "..gameTimer.."  ")
91
			term.setCursorPos(1,3)
92
			--print("hPos: "..hPos.."  ")
93
			if p1== gameTimer then
94
			gameTimer=os.startTimer(0.1)
95
			
96
				if bullet==true then
97
					delBullet()
98
					hPos=hPos-1
99
					if hPos==alienH or HPos==alienH+1 then
100
						--checkBulletCollision
101
						if alien1 == true and bulletCurPos >= alienW and bulletCurPos <= alienW+3 then
102
							delAlien(alienW,alienH)
103
							alien1=false
104
							alienCount=alienCount-1
105
							score=score+50
106
							bullet=false
107
						elseif alien2 == true and bulletCurPos >= alienW+8 and bulletCurPos <= alienW+11 then
108
							delAlien(alienW+8,alienH)
109
							alien2=false
110
							alienCount=alienCount-1
111
							score=score+50
112
							bullet=false
113
						elseif alien3 == true and bulletCurPos >= alienW+16 and bulletCurPos <= alienW+19 then
114
							delAlien(alienW+16,alienH)
115
							alien3=false
116
							alienCount=alienCount-1
117
							score=score+50
118
							bullet=false
119
						elseif alien4 == true and bulletCurPos >= alienW+24 and bulletCurPos <= alienW+27 then
120
							delAlien(alienW+24,alienH)
121
							alien4=false
122
							alienCount=alienCount-1
123
							score=score+50
124
							bullet=false
125
						elseif alien5 == true and bulletCurPos >= alienW+32 and bulletCurPos <= alienW+35 then
126
							delAlien(alienW+32,alienH)
127
							alien5=false
128
							alienCount=alienCount-1
129
							score=score+50
130
							bullet=false
131
						elseif alien6 == true and bulletCurPos >= alienW+40 and bulletCurPos <= alienW+43 then
132
							delAlien(alienW+40,alienH)
133
							alien6=false
134
							alienCount=alienCount-1
135
							score=score+50
136
							bullet=false
137
						end
138
						
139
						
140
						if alienCount==0 then 
141
							win=true
142
							gameOver()
143
						end
144
					end
145
					if hPos<=1 then 
146
						bullet=false
147
					else
148
						drawBullet()
149
					end
150
				end
151
				
152
				if  score <0 then score = 0 end
153
				term.setCursorPos(w-10,h)
154
				term.setTextColour(colours.white)
155
				write("Score: "..score)
156
				
157
				delAlienFormation()
158
				alienW=alienW+alienMove
159
				if alien1==false then alienLeftStop = -6 end
160
				if alien1==false and alien2==false then alienLeftStop = -14 end
161
				if alien1==false and alien2==false and alien3==false then alienLeftStop = -22 end
162
				if alien1==false and alien2==false and alien3==false and alien4==false then alienLeftStop = -30 end
163
				if alien1==false and alien2==false and alien3==false and alien4==false and alien5 ==false then alienLeftStop = -38 end
164
				if alien6==false then alienRightStop = 16 end
165
				if alien6==false and alien5==false then alienRightStop = 24 end
166
				if alien6==false and alien5==false and alien4==false then alienRightStop = 32 end
167
				if alien6==false and alien5==false and alien4==false and alien3==false then alienRightStop = 40 end
168
				if alien6==false and alien5==false and alien4==false and alien3==false and alien2 ==false then alienRightStop = 48 end
169
				
170
				if alienW> alienRightStop and alienMove ==1 then alienMove= -1 end -- 8
171
				if alienW < alienLeftStop and alienMove == -1 then alienMove= 1 end -- 2
172
				drawAlienFormation()
173
				
174
				
175
			end
176
		end
177
		
178
		
179
	until event=="char" and p1==("x")  
180
end
181
182
function getKey()
183
	input=read()
184
	print(input.." = "..keys.getName(input))
185
end
186
187
function keyAction()
188
	if keyPress == 30 or keyPress == 203 then
189
		--left - a or left arrow
190
		delShip(curPos)
191
		if curPos>5 then 
192
			curPos=curPos-1
193
		end
194
		drawShip(curPos)
195
	elseif keyPress == 32 or keyPress == 205 then
196
		--right - d or right arrow
197
		delShip(curPos)
198
		if curPos < w-3 then 
199
			curPos=curPos+1
200
		end
201
		drawShip(curPos)
202
	elseif keyPress == 57 then
203
		-- fire - space bar
204
		if bullet==false then
205
			hPos=h-4
206
			bullet=true
207
			bulletCurPos = curPos
208
			drawBullet()
209
		end
210
	elseif keyPress == 16 then
211
		quit()
212
	end
213
end
214
215
function quit()
216
	os.shutdown()
217
end
218
219
function drawShip(wPos)
220
	term.setTextColour(colours.white)
221
	term.setBackgroundColour(colours.white)
222
	term.setCursorPos(wPos,h-3)
223
	print("X")
224
	term.setCursorPos(wPos-1,h-2)
225
	print("XXX")
226
	term.setCursorPos(wPos-2,h-1)
227
	print("XXXXX")
228
	term.setBackgroundColour(colours.black)
229
end
230
231
function delShip(wPos)
232
	term.setTextColour(colours.red)
233
	term.setBackgroundColour(colours.black)
234
	term.setCursorPos(wPos,h-3)
235
	print(" ")
236
	term.setCursorPos(wPos-1,h-2)
237
	print("   ")
238
	term.setCursorPos(wPos-2,h-1)
239
	print("     ")
240
	--term.setBackgroundColour(colours.black)
241
end
242
243
function drawBullet()
244
	if bullet==true then
245
		term.setCursorPos(bulletCurPos,hPos)
246
		term.setTextColour(colours.orange)
247
		term.setBackgroundColour(colours.orange)
248
		print("I")
249
		term.setTextColour(colours.white)
250
		term.setBackgroundColour(colours.black)
251
	end
252
end
253
254
function delBullet()
255
	term.setCursorPos(bulletCurPos,hPos)
256
	term.setTextColour(colours.black)
257
	term.setBackgroundColour(colours.black)
258
	print(" ")
259
	term.setTextColour(colours.white)
260
	term.setBackgroundColour(colours.black)
261
end
262
263
function drawAlien(aW,aH,num)
264
	term.setCursorPos(aW,aH)
265
	term.setTextColour(colours.lime)
266
	term.setBackgroundColour(colours.lime)
267
	print(string.rep(num,3))
268
	term.setCursorPos(aW,aH+1)
269
	print(string.rep(num,1))
270
	term.setCursorPos(aW+2,aH+1)
271
	print(string.rep(num,1))
272
	term.setTextColour(colours.white)
273
	term.setBackgroundColour(colours.black)
274
end
275
276
function delAlien(aW,aH)
277
	term.setCursorPos(aW,aH)
278
	term.setTextColour(colours.black)
279
	term.setBackgroundColour(colours.black)
280
	print("   ")
281
	term.setCursorPos(aW,aH+1)
282
	print(" ")
283
	term.setCursorPos(aW+2,aH+1)
284
	print(" ")
285
	term.setTextColour(colours.white)
286
	term.setBackgroundColour(colours.black)
287
end
288
289
function drawAlienFormation()
290
	if alien1==true then drawAlien(alienW,alienH,1) end
291
	if alien2==true then drawAlien(alienW+8,alienH,2) end
292
	if alien3==true then drawAlien(alienW+16,alienH,3) end
293
	if alien4==true then drawAlien(alienW+24,alienH,4) end
294
	if alien5==true then drawAlien(alienW+32,alienH,5) end
295
	if alien6==true then drawAlien(alienW+40,alienH,6) end
296
end
297
298
function delAlienFormation()
299
	if alien1==true then delAlien(alienW,alienH) end
300
	if alien2==true then delAlien(alienW+8,alienH) end
301
	if alien3==true then delAlien(alienW+16,alienH) end
302
	if alien4==true then delAlien(alienW+24,alienH) end
303
	if alien5==true then delAlien(alienW+32,alienH) end
304
	if alien6==true then delAlien(alienW+40,alienH) end
305
end
306
307
function drawBomb(bombW,bombH)
308
	term.setCursorPos(bombW+1,bombH+2)
309
	term.setBackgroundColour(colours.yellow)
310
	term.setTextColour(colours.yellow)
311
	print("*")
312
	term.setBackgroundColour(colours.black)
313
	term.setTextColour(colours.white)
314
end
315
316
function delBomb(bombW,bombH)
317
	term.setCursorPos(bombW+1,bombH+2)
318
	term.setBackgroundColour(colours.black)
319
	term.setTextColour(colours.black)
320
	print(" ")
321
	term.setBackgroundColour(colours.black)
322
	term.setTextColour(colours.white)
323
end
324
325
function createBomb()
326
	chance = math.random(5)
327
	if chance==1 then 
328
		return true
329
	else 
330
		return false
331
	end
332
end
333
334
function selectAlien()
335
	selAlien= math.random(6)
336
	--term.setCursorPos(1,8)
337
	--term.setTextColour(colours.white)
338
	--print("aliens left "..alienCount)
339
	--term.setCursorPos(1,9)
340
	if selAlien==1 and alien1== true then bombW=alienW
341
	elseif selAlien==2 and alien2== true then bombW=alienW+8
342
	elseif selAlien==3 and alien3== true then bombW=alienW+16
343
	elseif selAlien==4 and alien4== true then bombW=alienW+24
344
	elseif selAlien==5 and alien5== true then bombW=alienW+32
345
	elseif selAlien==6 and alien6== true then bombW=alienW+40
346
	end
347
end
348
349
function bombs()
350
	if bomb1==false and createBomb() == true and noBombs == false then
351
		selectAlien()
352
		drawBomb(bombW,alienH)
353
		bomb1=true
354
		bomb1W= bombW
355
		bomb1H=alienH
356
	elseif bomb1 ==true then 
357
		delBomb(bomb1W,bomb1H)
358
		bomb1H = bomb1H+1
359
		if bomb1H ==14 and bomb1W == curPos then
360
			bomb1=false
361
			shipHit()
362
		elseif bomb1H == 15 and bomb1W >= curPos-2 and bomb1W <= curPos then
363
			bomb1=false
364
			shipHit()
365
		elseif bomb1H == 16 and bomb1W >= curPos-4 and bomb1W <= curPos+1 then
366
			bomb1=false
367
			shipHit()
368
		elseif bomb1H > 16 then
369
			bomb1=false
370
			term.setCursorPos(1,10)
371
	print("   ")
372
		else
373
		    drawBomb(bomb1W,bomb1H)
374
		end
375
	end	
376
	if bomb2==false and createBomb() == true and noBombs == false then
377
		selectAlien()
378
		drawBomb(bombW,alienH)
379
		bomb2=true
380
		bomb2W= bombW
381
		bomb2H=alienH
382
	elseif bomb2 ==true then 
383
		delBomb(bomb2W,bomb2H)
384
		bomb2H = bomb2H+1
385
		if bomb2H ==14 and bomb2W == curPos then
386
			bomb2=false
387
			shipHit()
388
		elseif bomb2H == 15 and bomb2W >= curPos-2 and bomb2W <= curPos then
389
			bomb2=false
390
			shipHit()
391
		elseif bomb2H == 16 and bomb2W >= curPos-4 and bomb2W <= curPos+1 then
392
			bomb2=false
393
			shipHit()
394
		elseif bomb2H > 16 then
395
			bomb2=false
396
			term.setCursorPos(1,10)
397
	print("   ")
398
		else
399
		    drawBomb(bomb2W,bomb2H)
400
		end
401
	end	
402
	if bomb3==false and createBomb() == true and noBombs == false then
403
		selectAlien()
404
		drawBomb(bombW,alienH)
405
		bomb3=true
406
		bomb3W= bombW
407
		bomb3H=alienH
408
	elseif bomb3 ==true then 
409
		delBomb(bomb3W,bomb3H)
410
		bomb3H = bomb3H+1
411
		if bomb3H ==14 and bomb3W == curPos then
412
			bomb3=false
413
			shipHit()
414
		elseif bomb3H == 15 and bomb3W >= curPos-2 and bomb3W <= curPos then
415
			bomb2=false
416
			shipHit()
417
		elseif bomb3H == 16 and bomb3W >= curPos-4 and bomb3W <= curPos+1 then
418
			bomb3=false
419
			shipHit()
420
		elseif bomb3H > 16 then
421
			bomb3=false
422
			term.setCursorPos(1,10)
423
	print("   ")
424
		else
425
		    drawBomb(bomb3W,bomb3H)
426
		end
427
	end
428
end
429
430
function shipHit()
431
	lives=lives-1
432
	delShip(curPos)
433
	killed=true
434
	noBombs=true
435
	score=score-10
436
	if score<0 then score =0 end
437
	if lives==0 then
438
		gameOver()
439
	else
440
		term.setCursorPos(2,h)
441
		term.setTextColour(colours.white)
442
		write("Lives: "..lives)
443
		term.setCursorPos(w-10,h)
444
		term.setTextColour(colours.white)
445
		write("Score: "..score)
446
	end
447
end
448
449
function gameOver()
450
	clear()
451
	if win==true then
452
		term.setCursorPos((w/2)-3, h/2)
453
		print("YOU WIN")
454
		win=false
455
	else
456
		term.setCursorPos((w/2)-4, h/2)
457
		print("GAME OVER")
458
	end
459
	local scoreStr= tostring(score)
460
	term.setCursorPos((w/2)-((7+#scoreStr))/2, (h/2)+2)
461
	print("Score: "..scoreStr)
462
	sleep(1)
463
	term.setCursorPos((w/2)-9, (h/2)+5)
464
	print("Play again (Y or N)")
465
	
466
467
	while true do
468
		local event,p1,p2,p3 = os.pullEvent()
469
		if event=="char" then
470
			if string.lower(p1)=="y" then 
471
				runGame()
472
			elseif string.lower(p1)=="n" then 
473
				os.shutdown()
474
			end
475
		end
476
	end
477
478
end
479
480
481
482
function runGame()
483
	clear()
484
	
485
	initVariables()
486
	drawShip(curPos)
487
	drawAlienFormation()
488
489
	term.setCursorPos(2,h)
490
	write("Lives: "..lives)
491
	term.setCursorPos(w-10,h)
492
	write("Score: "..score)
493
494
	gameController()
495
end
496
497
function line1(h)
498
	term.setCursorPos(12,h) write("    ")
499
	term.setCursorPos(18,h) write("    ")
500
	term.setCursorPos(24,h) write("    ")
501
	term.setCursorPos(30,h) write("    ")
502
	term.setCursorPos(36,h) write("    ")
503
end
504
505
506
function line2(h)
507
	term.setCursorPos(12,h) write(" ")
508
	term.setCursorPos(18,h) write(" ")
509
	term.setCursorPos(21,h) write(" ")
510
	term.setCursorPos(24,h) write(" ")
511
	term.setCursorPos(27,h) write(" ")
512
	term.setCursorPos(30,h) write(" ")
513
	term.setCursorPos(36,h) write(" ")
514
end	
515
	
516
function line3(h)
517
	term.setCursorPos(12,h) write("    ")
518
	term.setCursorPos(18,h) write("    ")
519
	term.setCursorPos(24,h) write("    ")
520
	term.setCursorPos(30,h) write(" ")
521
	term.setCursorPos(36,h) write("   ")
522
end
523
524
function line4(h)
525
	term.setCursorPos(15,h) write(" ")
526
	term.setCursorPos(18,h) write(" ")
527
	term.setCursorPos(24,h) write(" ")
528
	term.setCursorPos(27,h) write(" ")
529
	term.setCursorPos(30,h) write(" ")
530
	term.setCursorPos(36,h) write(" ")
531
end
532
	
533
function line5(h)
534
	term.setCursorPos(12,h) write("    ")
535
	term.setCursorPos(18,h) write(" ")
536
	term.setCursorPos(24,h) write(" ")
537
	term.setCursorPos(27,h) write(" ")
538
	term.setCursorPos(30,h) write("    ")
539
	term.setCursorPos(36,h) write("    ")
540
end
541
	
542
	
543
function line6(h)
544
	term.setCursorPos(4,h) write("   ")
545
	term.setCursorPos(9,h) write(" ")
546
	term.setCursorPos(12,h) write(" ")
547
	term.setCursorPos(15,h) write(" ")
548
	term.setCursorPos(18,h) write(" ")
549
	term.setCursorPos(21,h) write("    ")
550
	term.setCursorPos(27,h) write("   ")
551
	term.setCursorPos(33,h) write("    ")
552
	term.setCursorPos(39,h) write("   ")
553
	term.setCursorPos(45,h) write("    ")
554
end
555
556
function line7(h)
557
	term.setCursorPos(5,h) write(" ")
558
	term.setCursorPos(9,h) write("  ")
559
	term.setCursorPos(12,h) write(" ")
560
	term.setCursorPos(15,h) write(" ")
561
	term.setCursorPos(18,h) write(" ")
562
	term.setCursorPos(21,h) write(" ")
563
	term.setCursorPos(24,h) write(" ")
564
	term.setCursorPos(27,h) write(" ")
565
	term.setCursorPos(30,h) write(" ")
566
	term.setCursorPos(33,h) write(" ")
567
	term.setCursorPos(39,h) write(" ")
568
	term.setCursorPos(42,h) write(" ")
569
	term.setCursorPos(45,h) write(" ")
570
end
571
	
572
function line8(h)
573
	term.setCursorPos(5,h) write(" ")
574
	term.setCursorPos(9,h) write("    ")
575
	term.setCursorPos(15,h) write(" ")
576
	term.setCursorPos(18,h) write(" ")
577
	term.setCursorPos(21,h) write("    ")
578
	term.setCursorPos(27,h) write(" ")
579
	term.setCursorPos(30,h) write(" ")
580
	term.setCursorPos(33,h) write("   ")
581
	term.setCursorPos(39,h) write("   ")
582
	term.setCursorPos(45,h) write("    ")
583
end
584
585
586
function line9(h)
587
	term.setCursorPos(5,h) write(" ")
588
	term.setCursorPos(9,h) write(" ")
589
	term.setCursorPos(11,h) write("  ")
590
	term.setCursorPos(15,h) write(" ")
591
	term.setCursorPos(18,h) write(" ")
592
	term.setCursorPos(21,h) write(" ")
593
	term.setCursorPos(24,h) write(" ")
594
	term.setCursorPos(27,h) write(" ")
595
	term.setCursorPos(30,h) write(" ")
596
	term.setCursorPos(33,h) write(" ")
597
	term.setCursorPos(39,h) write(" ")
598
	term.setCursorPos(42,h) write(" ")
599
	term.setCursorPos(48,h) write(" ")
600
end	
601
	
602
function line10(h)
603
	term.setCursorPos(4,h) write("   ")
604
	term.setCursorPos(9,h) write(" ")
605
	term.setCursorPos(12,h) write(" ")
606
	term.setCursorPos(16,h) write("  ")
607
	term.setCursorPos(21,h) write(" ")
608
	term.setCursorPos(24,h) write(" ")
609
	term.setCursorPos(27,h) write("   ")
610
	term.setCursorPos(33,h) write("    ")
611
	term.setCursorPos(39,h) write(" ")
612
	term.setCursorPos(42,h) write(" ")
613
	term.setCursorPos(45,h) write("    ")
614
end
615
616
function whiteScreen()
617
	term.setBackgroundColour(colours.white)
618
	term.clear()
619
	term.setBackgroundColour(colours.black)
620
end
621
	
622
function splashScreen()
623
	whiteScreen()
624
	sleep(1)
625
626
	local scrollH = h+2
627
	while scrollH > 1 do
628
		whiteScreen()
629
		line1(scrollH)
630
		line2(scrollH+1)
631
		line3(scrollH+2)
632
		line4(scrollH+3)
633
		line5(scrollH+4)
634
		scrollH=scrollH-1
635
		sleep(0.1)
636
	end
637
	local scrollH = h+2
638
	while scrollH > 1 do
639
		whiteScreen()
640
		line1(2)
641
		line2(3)
642
		line3(4)
643
		line4(5)
644
		line5(6)
645
		line6(scrollH+6)
646
		line7(scrollH+7)
647
		line8(scrollH+8)
648
		line9(scrollH+9)
649
		line10(scrollH+10)
650
		scrollH=scrollH-1
651
		sleep(0.1)
652
	end
653
	term.setBackgroundColour(colours.white)
654
	term.setTextColour(colours.black)
655
	
656
	term.setCursorPos((w/2)-6,15)
657
	print("by Jcdwalel and PumpkinheadAbbey")
658-
	print("by fredTHead")
658+
659
	term.setCursorPos((w/2)-15,17)
660
	print("Left and Right Arrows to move")
661
	term.setCursorPos((w/2)-6,18)
662
	print("Space to fire")
663
	
664
	term.setCursorPos((w/2)-10,19)
665
	write("Press fire when ready")
666
	
667
	
668
	
669
	while true do
670
		local event,p1,p2,p3 = os.pullEvent()
671
		if event=="char" then
672
			term.setBackgroundColour(colours.black)
673
			term.clear()
674
			runGame()
675
		end  
676
	end
677
end
678
679
680
681
monSign()
682
clear()
683
684
term.setCursorPos(w/2-6 ,h/2)
685
print("Press any key")
686
while true do
687
	local event,p1,p2,p3 = os.pullEvent()
688
	if event=="char" then
689
		splashScreen()
690
	end  
691
end