Advertisement
Chaos_Cash

Mini games for Computer Craft 1.5 (New Game "Snake", Added Highscore and Settings)

Oct 29th, 2023 (edited)
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.22 KB | Gaming | 0 0
  1. --If control is not not working rebind your settings
  2. --For new Ideas Please leave a comment on Pastebin
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10. if not fs.exists("settings") then
  11. game_settings=fs.open("settings","w")
  12. game_settings.writeLine("key")
  13. game_settings.writeLine("17")
  14. game_settings.writeLine("key")
  15. game_settings.writeLine("31")
  16. game_settings.writeLine("key")
  17. game_settings.writeLine("200")
  18. game_settings.writeLine("key")
  19. game_settings.writeLine("208")
  20. game_settings.writeLine("key")
  21. game_settings.writeLine("17")
  22. game_settings.writeLine("key")
  23. game_settings.writeLine("31")
  24. game_settings.writeLine("key")
  25. game_settings.writeLine("30")
  26. game_settings.writeLine("key")
  27. game_settings.writeLine("32")
  28. game_settings.flush()
  29. game_settings.close()
  30. end
  31.  
  32. if not fs.exists("highscore") then
  33. highscore=fs.open("highscore","w")
  34. for i=1,30 do
  35. highscore.writeLine("0")
  36. end
  37. highscore.flush()
  38. highscore.close()
  39. end
  40.  
  41.  
  42. --
  43.  
  44.  
  45. function assign_highscore()
  46. highscore=fs.open("highscore","r")
  47. pong_easy_highscore=tonumber(highscore.readLine())
  48. pong_normal_highscore=tonumber(highscore.readLine())
  49. pong_hard_highscore=tonumber(highscore.readLine())
  50. pong_extreme_highscore=tonumber(highscore.readLine())
  51. pong_insane_highscore=tonumber(highscore.readLine())
  52. click_me_easy_highscore=tonumber(highscore.readLine())
  53. click_me_normal_highscore=tonumber(highscore.readLine())
  54. click_me_hard_highscore=tonumber(highscore.readLine())
  55. click_me_extreme_highscore=tonumber(highscore.readLine())
  56. click_me_insane_highscore=tonumber(highscore.readLine())
  57. snake_easy_highscore=tonumber(highscore.readLine())
  58. snake_normal_highscore=tonumber(highscore.readLine())
  59. snake_hard_highscore=tonumber(highscore.readLine())
  60. snake_extreme_highscore=tonumber(highscore.readLine())
  61. snake_insane_highscore=tonumber(highscore.readLine())
  62. highscore.close()
  63. end
  64.  
  65.  
  66.  
  67. function assign_settings()
  68. game_settings=fs.open("settings","r")
  69. pong_p1_up_kind=game_settings.readLine()
  70. pong_p1_up_num=tonumber(game_settings.readLine())
  71. pong_p1_down_kind=game_settings.readLine()
  72. pong_p1_down_num=tonumber(game_settings.readLine())
  73. pong_p2_up_kind=game_settings.readLine()
  74. pong_p2_up_num=tonumber(game_settings.readLine())
  75. pong_p2_down_kind=game_settings.readLine()
  76. pong_p2_down_num=tonumber(game_settings.readLine())
  77. snake_up_kind=game_settings.readLine()
  78. snake_up_num=tonumber(game_settings.readLine())
  79. snake_down_kind=game_settings.readLine()
  80. snake_down_num=tonumber(game_settings.readLine())
  81. snake_left_kind=game_settings.readLine()
  82. snake_left_num=tonumber(game_settings.readLine())
  83. snake_right_kind=game_settings.readLine()
  84. snake_right_num=tonumber(game_settings.readLine())
  85. end
  86.  
  87.  
  88.  
  89. function startup_menu()
  90. term.setBackgroundColor(colors.black)
  91. term.clear()
  92. headline("Start menu")
  93. button(" Pong",16,6,25,8)
  94. button(" Exit",44,16,51,18)
  95. button(" Update",15,10,26,13)
  96. button("Click me",27,6,37,8)
  97. button("Sounds:",28,10,37,13)
  98. button("Snake",28,14,37,16)
  99. button(" Settings",15,14,26,16)
  100. term.setCursorPos(17,12)
  101. term.write("History")
  102. term.setCursorPos(31,12)
  103. term.write(sound_status.." ")
  104. b_check=false
  105. while true do
  106. pull()
  107.  
  108. if button_check(28,10,37,13) then
  109. if sound_status=="on" then
  110. sound_status="off"
  111. volume=0
  112. else
  113. sound_status="on"
  114. volume=1
  115. end
  116. term.setCursorPos(31,12)
  117. term.write(sound_status.." ")
  118. end
  119.  
  120. if button_check(15,14,26,16) then
  121. settings_pong()
  122. end
  123.  
  124. if button_check(28,14,37,16) then
  125. difficulty_menu("Snake")
  126. end
  127.  
  128. if button_check(16,6,25,8) then
  129. info_1=0
  130. player_menu()
  131. end
  132.  
  133. if button_check(44,16,51,18) then
  134. term.clear()
  135. term.setCursorPos(-1,1)
  136. error()
  137. end
  138.  
  139. if button_check(15,10,26,13) then
  140. update_history()
  141. end
  142.  
  143. if button_check(27,6,37,8) then
  144. difficulty_menu("Click me")
  145. end
  146. end
  147. end
  148.  
  149.  
  150.  
  151. function update_history()
  152. term.clear()
  153. headline("Update History")
  154. term.setCursorPos(1,4)
  155. print("1.0: Completed basic game")
  156. print("1.1: Difficulties, Better Interface, start menu")
  157. print("1.2: Singleplayer mode, Better Interface, bug fixes")
  158. print("1.3: Pong Vs bot mode, Update History, added Score")
  159. print("1.4: Added Click me, some sounds, major bug fixes")
  160. button("Home",38,16,50,18)
  161. while 1==1 do
  162. pull()
  163. if button_check(38,16,50,18) then
  164. info_1=0
  165. startup_menu()
  166. end
  167. end
  168. end
  169.  
  170.  
  171.  
  172. function settings_pong()
  173. term.clear()
  174. headline("Settings")
  175. button(" Home",44,16,51,18)
  176. term.setCursorPos(1,3)
  177. term.setTextColor(colors.red)
  178. term.write("-Pong:")
  179. button(" Up P1",2,5,12,7)
  180. button(" Down P1",14,5,26,7)
  181. button(" Up P2",2,9,12,11)
  182. button(" Down P2",14,9,26,11)
  183. button(" Next",14,17,26,19)
  184. while true do
  185. pull()
  186.  
  187. if button_check(44,16,51,18) then
  188. startup_menu()
  189. end
  190.  
  191. if button_check(2,5,12,7) then
  192. change_settings(1)
  193. end
  194.  
  195. if button_check(14,5,26,7) then
  196. change_settings(3)
  197. end
  198.  
  199. if button_check(2,9,12,11) then
  200. change_settings(5)
  201. end
  202.  
  203. if button_check(14,9,26,11) then
  204. change_settings(7)
  205. end
  206.  
  207. if button_check(14,17,26,19) then
  208. settings_snake()
  209. end
  210. end
  211. end
  212.  
  213.  
  214.  
  215.  
  216. function settings_snake()
  217. term.clear()
  218. headline("Settings")
  219. button(" Home",44,16,51,18)
  220. term.setCursorPos(1,3)
  221. term.setTextColor(colors.red)
  222. term.write("-Snake:")
  223. button(" Up",2,5,12,7)
  224. button(" Down",14,5,26,7)
  225. button(" Left",2,9,12,11)
  226. button(" Right ",14,9,26,11)
  227. button(" Last",1,17,13,19)
  228. while true do
  229. pull()
  230.  
  231. if button_check(44,16,51,18) then
  232. startup_menu()
  233. end
  234.  
  235. if button_check(2,5,12,7) then
  236. change_settings(9)
  237. end
  238.  
  239. if button_check(14,5,26,7) then
  240. change_settings(11)
  241. end
  242.  
  243. if button_check(2,9,12,11) then
  244. change_settings(13)
  245. end
  246.  
  247. if button_check(14,9,26,11) then
  248. change_settings(15)
  249. end
  250.  
  251. if button_check(1,17,13,19) then
  252. settings_pong()
  253. end
  254. end
  255. end
  256.  
  257.  
  258.  
  259. function change_settings(change_line)
  260. os.sleep(0.5)
  261. term.setTextColor(colors.red)
  262. term.setCursorPos(10,2)
  263. term.write("Please press a key ")
  264. pull()
  265. old_settings={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
  266. settings_len=(fs.attributes("settings")["size"]-6)/2
  267. cur_line=1
  268. game_settings=fs.open("settings","r")
  269. for i=1,settings_len do
  270. old_settings[cur_line]=game_settings.readLine()
  271. cur_line=cur_line+1
  272. end
  273. game_settings=fs.open("settings","w")
  274. cur_line=1
  275. for i=1,settings_len do
  276. if cur_line~=change_line then
  277. game_settings.writeLine(old_settings[cur_line])
  278. cur_line=cur_line+1
  279. else
  280. game_settings.writeLine(info_1)
  281. game_settings.writeLine(info_2)
  282. cur_line=cur_line+2
  283. end
  284. end
  285. game_settings.flush()
  286. term.setCursorPos(10,2)
  287. term.write("The new Keybind is: ")
  288. term.setTextColor(colors.green)
  289. term.write(info_1.." "..info_2)
  290. assign_settings()
  291. end
  292.  
  293.  
  294. function player_menu()
  295. is_bot=false
  296. term.clear()
  297. headline("Pong")
  298. button(" 1 Player",5,6,17,10)
  299. button(" 2 Players",19,6,31,10)
  300. button("Vs Bot",33,6,45,10)
  301. b_check=false
  302. while true do
  303. pull()
  304. if button_check(5,6,17,10) then
  305. is_bot=false
  306. info_1=0
  307. player_count=1
  308. difficulty_menu("Pong")
  309. end
  310.  
  311. if button_check(19,6,31,10) then
  312. is_bot=false
  313. info_1=0
  314. player_count=2
  315. difficulty_menu("Pong")
  316. end
  317.  
  318. if button_check(33,6,45,10) then
  319. info_1=0
  320. player_count=2
  321. is_bot=true
  322. difficulty_menu("Pong")
  323. end
  324. end
  325. end
  326.  
  327.  
  328.  
  329. function minecraft_or_craftos_pc()
  330. term.clear()
  331. term.setCursorPos(7,3)
  332. term.setTextColor(colors.white)
  333. term.write("Are you using this program in ")
  334. term.setTextColor(colors.red)
  335. term.write("minecraft")
  336. term.setTextColor(colors.white)
  337. term.setCursorPos(18,4)
  338. term.write("or ")
  339. term.setTextColor(colors.red)
  340. term.write("CraftOS-PC?")
  341.  
  342. button(" Minecraft",8,8,22,10)
  343. button(" CraftOS-PC",30,8,44,10)
  344.  
  345. while minecraft_or_pc==nil do
  346. pull()
  347.  
  348. if button_check(8,8,22,10) then
  349. minecraft_or_pc="minecraft"
  350. term.clear()
  351. term.setCursorPos(3,3)
  352. term.write("Please place a Speaker on top of the Computer")
  353. sleep(3)
  354. end
  355.  
  356. if button_check(30,8,44,10) then
  357. minecraft_or_pc="pc"
  358. end
  359. end
  360.  
  361. if minecraft_or_pc=="pc" then
  362. periphemu.create("top","speaker",1)
  363. end
  364. speaker = peripheral.wrap("top")
  365. sound_status="off"
  366. volume=0
  367. end
  368.  
  369.  
  370.  
  371. function difficulty_menu(what_game)
  372. term.clear()
  373. headline(what_game)
  374. button("Easy",8,7,18,11)
  375. button("Normal",22,7,32,11)
  376. button("Hard",36,7,46,11)
  377. button(" Extreme",22,13,32,17)
  378. button("Insane",36,13,46,17)
  379. b_check=false
  380. while true do
  381. pull()
  382.  
  383. if button_check(8,7,18,11) then
  384. game_dif="easy"
  385. if what_game=="Pong" then
  386. info_1=0
  387. bot_dif=6
  388. pong(0.13)
  389. end
  390. if what_game=="Click me" then
  391. click_me(1)
  392. end
  393. if what_game=="Snake" then
  394. snake(0.15)
  395. end
  396. end
  397.  
  398. if button_check(22,7,32,11) then
  399. game_dif="normal"
  400. if what_game=="Pong" then
  401. info_1=0
  402. bot_dif=7
  403. pong(0.1)
  404. end
  405. if what_game=="Click me" then
  406. click_me(0.8)
  407. end
  408. if what_game=="Snake" then
  409. snake(0.125)
  410. end
  411. end
  412.  
  413. if button_check(36,7,46,11) then
  414. game_dif="hard"
  415. if what_game=="Pong" then
  416. info_1=0
  417. bot_dif=8
  418. pong(0.07)
  419. end
  420. if what_game=="Click me" then
  421. click_me(0.6)
  422. end
  423. if what_game=="Snake" then
  424. snake(0.1)
  425. end
  426. end
  427.  
  428. if button_check(22,13,32,17) then
  429. game_dif="extreme"
  430. if what_game=="Pong" then
  431. info_1=0
  432. bot_dif=9
  433. pong(0.05)
  434. end
  435. if what_game=="Click me" then
  436. click_me(0.5)
  437. end
  438. if what_game=="Snake" then
  439. snake(0.075)
  440. end
  441. end
  442.  
  443. if button_check(36,13,46,17) then
  444. game_dif="insane"
  445. if what_game=="Pong" then
  446. info_1=0
  447. bot_dif=9
  448. pong(0.04)
  449. end
  450. if what_game=="Click me" then
  451. click_me(0.45)
  452. end
  453. if what_game=="Snake" then
  454. snake(0.05)
  455. end
  456. end
  457. end
  458. end
  459.  
  460.  
  461. function snake(game_speed)
  462. score=0
  463. level=1
  464. do_set_level_2=true
  465. game_running=true
  466. check_change_snake_color=0
  467. snake_len=2
  468. new_dir="up"
  469. snake_head_x=18
  470. snake_head_y=9
  471. snake_field_x1=1
  472. snake_field_y1=1
  473. snake_field_x2=36
  474. snake_field_y2=18
  475. snake_x={0,0,0,0}
  476. snake_y={0,0,0,0}
  477. do_gen_point=true
  478. term.clear()
  479. square(snake_field_x1,snake_field_y1,snake_field_x2,snake_field_y2,colors.cyan," "," ")
  480. button(" Home",44,16,51,18)
  481. print_snake()
  482. write_score()
  483. os.sleep(1)
  484. while game_running do
  485. if do_gen_point then
  486. gen_point()
  487. end
  488. print_snake()
  489. info_1=0
  490. check_snake()
  491. if do_set_level_2 and score==150 then
  492. level=2
  493. reset_snake()
  494. do_set_level_2=false
  495. line_y(12,5,14,colors.gray)
  496. line_y(24,5,14,colors.gray)
  497. new_dir="up"
  498. speaker.playNote("bit",volume,20)
  499. os.sleep(0.20)
  500. speaker.playNote("bit",volume,22)
  501. os.sleep(0.20)
  502. speaker.playNote("bit",volume,24)
  503. os.sleep(0.20)
  504. end
  505.  
  506. i=snake_len+1
  507. while i>0 do
  508. snake_x[i] = snake_x[i-1]
  509. snake_y[i] = snake_y[i-1]
  510. i=i-1
  511. end
  512. snake_x[1] = snake_head_x
  513. snake_y[1] = snake_head_y
  514. write_score()
  515. clear_snake()
  516. os.startTimer(game_speed)
  517. while info_1~="timer" do
  518. pull()
  519.  
  520. if button_check(44,16,51,18) then
  521. startup_menu()
  522. end
  523.  
  524. if info_1==snake_up_kind and info_2==snake_up_num and snake_dir~="down" then
  525. new_dir="up"
  526. end
  527.  
  528. if info_1==snake_down_kind and info_2==snake_down_num and snake_dir~="up" then
  529. new_dir="down"
  530. end
  531.  
  532. if info_1==snake_left_kind and info_2==snake_left_num and snake_dir~="right" then
  533. new_dir="left"
  534. end
  535.  
  536. if info_1==snake_right_kind and info_2==snake_right_num and snake_dir~="left" then
  537. new_dir="right"
  538. end
  539. end
  540. snake_dir=new_dir
  541. if snake_dir=="up" then
  542. snake_head_y=snake_head_y-1
  543. end
  544.  
  545. if snake_dir=="down" then
  546. snake_head_y=snake_head_y+1
  547. end
  548.  
  549. if snake_dir=="left" then
  550. snake_head_x=snake_head_x-1
  551. end
  552.  
  553. if snake_dir=="right" then
  554. snake_head_x=snake_head_x+1
  555. end
  556.  
  557. if snake_head_x==snake_field_x1 then
  558. snake_head_x=snake_field_x2-1
  559. end
  560.  
  561.  
  562. if snake_head_x==snake_field_x2 then
  563. snake_head_x=snake_field_x1+1
  564. end
  565.  
  566.  
  567. if snake_head_y==snake_field_y1 then
  568. snake_head_y=snake_field_y2-1
  569. end
  570.  
  571.  
  572. if snake_head_y==snake_field_y2 then
  573. snake_head_y=snake_field_y1+1
  574. end
  575. end
  576. end_snake()
  577. while true do
  578. pull()
  579. if button_check(44,16,51,18) then
  580. startup_menu()
  581. end
  582. end
  583. end
  584.  
  585.  
  586.  
  587. function end_snake()
  588.  
  589. square(snake_field_x1,snake_field_y1,snake_field_x2,snake_field_y2,colors.lightGray," "," ")
  590. if level==2 then
  591. line_y(12,5,14,colors.lightGray)
  592. line_y(24,5,14,colors.lightGray)
  593. end
  594.  
  595. i=snake_len
  596. term.setBackgroundColor(colors.lightGray)
  597. term.setCursorPos(point_pos_x,point_pos_y)
  598. term.write(" ")
  599. term.setBackgroundColor(colors.gray)
  600. while i>0 do
  601. term.setCursorPos(snake_x[i],snake_y[i])
  602. term.write(" ")
  603. i=i-1
  604. end
  605. term.setCursorPos(snake_x[1],snake_y[1])
  606. term.setBackgroundColor(colors.red)
  607. term.write(" ")
  608.  
  609. if game_dif=="easy" and score>snake_easy_highscore then
  610. broke_highscore(snake_easy_highscore,11)
  611. end
  612. if game_dif=="normal" and score>snake_normal_highscore then
  613. broke_highscore(snake_normal_highscore,12)
  614. end
  615. if game_dif=="hard" and score>snake_hard_highscore then
  616. broke_highscore(snake_hard_highscore,13)
  617. end
  618. if game_dif=="extreme" and score>snake_extreme_highscore then
  619. broke_highscore(snake_extreme_highscore,14)
  620. end
  621. if game_dif=="insane" and score>snake_insane_highscore then
  622. broke_highscore(snake_insane_highscore,15)
  623. end
  624. end
  625.  
  626.  
  627. function reset_snake()
  628. term.setBackgroundColor(colors.black)
  629. term.clear()
  630. square(snake_field_x1,snake_field_y1,snake_field_x2,snake_field_y2,colors.cyan," "," ")
  631. button(" Home",44,16,51,18)
  632. write_score()
  633. for i=1,40 do
  634. snake_x[i]=0
  635. snake_y[i]=0
  636. end
  637. snake_head_x=17
  638. snake_head_y=8
  639. snake_len=2
  640. print_snake()
  641. end
  642.  
  643.  
  644.  
  645.  
  646. function gen_point()
  647. do_for=1
  648. while do_for>0 do
  649. point_pos_x=math.random(snake_field_x1+1,snake_field_x2-1)
  650. point_pos_y=math.random(snake_field_y1+1,snake_field_y2-1)
  651. i=snake_len
  652. while i>0 do
  653. if point_pos_x==snake_x[i] and point_pos_y==snake_y[i] then
  654. do_for=do_for+2
  655. end
  656. i=i-1
  657. end
  658.  
  659. if do_for<3 and (point_pos_x==12 or point_pos_x==24) and point_pos_y<15 and point_pos_y>4 then
  660. do_for=do_for+2
  661. end
  662. do_for=do_for-1
  663. end
  664. term.setBackgroundColor(colors.yellow)
  665. term.setCursorPos(point_pos_x,point_pos_y)
  666. term.write(" ")
  667. do_gen_point=false
  668. end
  669.  
  670.  
  671.  
  672.  
  673. function check_snake()
  674. i=2
  675. while i<snake_len+1 do
  676. if snake_head_x==snake_x[i] and snake_head_y==snake_y[i] then
  677. game_running=false
  678. end
  679. i=i+1
  680. end
  681.  
  682. if level==2 then
  683. if (snake_head_x==12 or snake_head_x==24) and snake_head_y>4 and snake_head_y<15 then
  684. game_running=false
  685. end
  686. end
  687.  
  688. if snake_head_x==point_pos_x and snake_head_y==point_pos_y then
  689. snake_len=snake_len+1
  690. do_gen_point=true
  691. score=score+5
  692. if score~=150 then
  693. speaker.playNote("bit",volume,18)
  694. end
  695. end
  696. end
  697.  
  698.  
  699. function clear_snake()
  700. term.setBackgroundColor(colors.black)
  701. term.setCursorPos(snake_x[snake_len+1],snake_y[snake_len+1])
  702. term.write(" ")
  703. end
  704.  
  705.  
  706. function print_snake()
  707. term.setCursorPos(snake_head_x,snake_head_y)
  708. term.setBackgroundColor(colors.lime)
  709. term.write(" ")
  710. if check_change_snake_color>1 then
  711. term.setCursorPos(snake_x[1],snake_y[1])
  712. term.setBackgroundColor(colors.green)
  713. term.write(" ")
  714. end
  715. check_change_snake_color=check_change_snake_color+1
  716. end
  717.  
  718.  
  719.  
  720. function line_x(start_x,start_y,end_x,color)
  721. term.setBackgroundColor(color)
  722. cur_x=start_x
  723. for i=1,end_x-start_x+1 do
  724. term.setCursorPos(cur_x,start_y)
  725. term.write(" ")
  726. cur_x=cur_x+1
  727. end
  728. end
  729.  
  730.  
  731.  
  732. function line_y(start_x,start_y,end_y,color)
  733. term.setBackgroundColor(color)
  734. cur_y=start_y
  735. for i=1,end_y-start_y+1 do
  736. term.setCursorPos(start_x,cur_y)
  737. term.write(" ")
  738. cur_y=cur_y+1
  739. end
  740. end
  741.  
  742.  
  743.  
  744.  
  745.  
  746. function click_me(game_speed)
  747. term.clear()
  748. click_speed=game_speed
  749. game_time=10
  750. score=0
  751.  
  752.  
  753. while game_time~=0 do
  754. filled_square(3,3,9,6,colors.lightGray)
  755. filled_square(11,3,17,6,colors.lightGray)
  756. filled_square(19,3,25,6,colors.lightGray)
  757. filled_square(3,8,9,11,colors.lightGray)
  758. filled_square(11,8,17,11,colors.lightGray)
  759. filled_square(19,8,25,11,colors.lightGray)
  760. filled_square(3,13,9,16,colors.lightGray)
  761. filled_square(11,13,17,16,colors.lightGray)
  762. filled_square(19,13,25,16,colors.lightGray)
  763.  
  764. old_cw=cw
  765. while old_cw==cw do
  766. cw=math.random(1,9)
  767. end
  768. ct=0
  769.  
  770. if cw==1 then
  771. filled_square(3,3,9,6,colors.green)
  772. end
  773.  
  774. if cw==2 then
  775. filled_square(11,3,17,6,colors.green)
  776. end
  777.  
  778. if cw==3 then
  779. filled_square(19,3,25,6,colors.green)
  780. end
  781.  
  782. if cw==4 then
  783. filled_square(3,8,9,11,colors.green)
  784. end
  785.  
  786. if cw==5 then
  787. filled_square(11,8,17,11,colors.green)
  788. end
  789.  
  790. if cw==6 then
  791. filled_square(19,8,25,11,colors.green)
  792. end
  793.  
  794. if cw==7 then
  795. filled_square(3,13,9,16,colors.green)
  796. end
  797.  
  798. if cw==8 then
  799. filled_square(11,13,17,16,colors.green)
  800. end
  801.  
  802. if cw==9 then
  803. filled_square(19,13,25,16,colors.green)
  804. end
  805. write_score()
  806. write_lives_c()
  807. timer_number=os.startTimer(game_speed)
  808.  
  809. while info_1~="timer" and ct==0 do
  810. pull()
  811.  
  812. if button_check(3,3,9,6) then
  813. ct=1
  814. end
  815.  
  816. if button_check(11,3,17,6) then
  817. ct=2
  818. end
  819.  
  820. if button_check(19,3,25,6) then
  821. ct=3
  822. end
  823.  
  824. if button_check(3,8,9,11) then
  825. ct=4
  826. end
  827.  
  828. if button_check(11,8,17,11) then
  829. ct=5
  830. end
  831.  
  832. if button_check(19,8,25,11) then
  833. ct=6
  834. end
  835.  
  836. if button_check(3,13,9,16) then
  837. ct=7
  838. end
  839.  
  840. if button_check(11,13,17,16) then
  841. ct=8
  842. end
  843.  
  844. if button_check(19,13,25,16) then
  845. ct=9
  846. end
  847. end
  848.  
  849. if ct==cw then
  850. score=score+10
  851. speaker.playNote("bit",volume/2.5,20)
  852. else
  853. game_time=game_time-1
  854. write_lives_c()
  855. if cw==1 then
  856. filled_square(3,3,9,6,colors.red)
  857. end
  858.  
  859. if cw==2 then
  860. filled_square(11,3,17,6,colors.red)
  861. end
  862.  
  863. if cw==3 then
  864. filled_square(19,3,25,6,colors.red)
  865. end
  866.  
  867. if cw==4 then
  868. filled_square(3,8,9,11,colors.red)
  869. end
  870.  
  871. if cw==5 then
  872. filled_square(11,8,17,11,colors.red)
  873. end
  874.  
  875. if cw==6 then
  876. filled_square(19,8,25,11,colors.red)
  877. end
  878.  
  879. if cw==7 then
  880. filled_square(3,13,9,16,colors.red)
  881. end
  882.  
  883. if cw==8 then
  884. filled_square(11,13,17,16,colors.red)
  885. end
  886.  
  887. if cw==9 then
  888. filled_square(19,13,25,16,colors.red)
  889. end
  890. speaker.playNote("bit",volume,0)
  891. sleep(0.3)
  892. end
  893. os.cancelTimer(timer_number)
  894. info_1=0
  895. end
  896. sleep(0.8)
  897. term.clear()
  898. term.setCursorPos(13,9)
  899. term.write(" Your Score is: "..score)
  900. term.setTextColor(colors.white)
  901. square(11,7,34,11,colors.black,"-","|")
  902. button(" Home",44,16,51,18)
  903. button(" Play again",1,16,14,18)
  904.  
  905. if game_dif=="easy" and score>click_me_easy_highscore then
  906. broke_highscore(click_me_easy_highscore,6)
  907. end
  908. if game_dif=="normal" and score>click_me_normal_highscore then
  909. broke_highscore(click_me_normal_highscore,7)
  910. end
  911. if game_dif=="hard" and score>click_me_hard_highscore then
  912. broke_highscore(click_me_hard_highscore,8)
  913. end
  914. if game_dif=="extreme" and score>click_me_extreme_highscore then
  915. broke_highscore(click_me_extreme_highscore,9)
  916. end
  917. if game_dif=="insane" and score>click_me_insane_highscore then
  918. broke_highscore(click_me_insane_highscore,10)
  919. end
  920.  
  921. while 1==1 do
  922. pull()
  923.  
  924. if button_check(45,16,51,18) then
  925. startup_menu()
  926. end
  927.  
  928. if button_check(1,16,13,18) then
  929. click_me(click_speed)
  930. end
  931. end
  932. end
  933.  
  934.  
  935.  
  936. function write_lives_c()
  937. term.setBackgroundColor(colors.black)
  938. term.setTextColor(colors.red)
  939. term.setCursorPos(10,1)
  940. if game_time>1 then
  941. term.write(game_time.." ".."Lives left ")
  942. else
  943. term.write(game_time.." ".."Live left ")
  944. end
  945. end
  946.  
  947.  
  948.  
  949. function filled_square(fill_x1,fill_y1,fill_x2,fill_y2,fill_color)
  950. term.setBackgroundColor(fill_color)
  951. curr_fill_pos_x=fill_x1
  952. curr_fill_pos_y=fill_y1
  953. while curr_fill_pos_y~=fill_y2+1 do
  954. while curr_fill_pos_x~=fill_x2+1 do
  955. term.setCursorPos(curr_fill_pos_x,curr_fill_pos_y)
  956. term.write(" ")
  957. curr_fill_pos_x=curr_fill_pos_x+1
  958. end
  959. curr_fill_pos_x=fill_x1
  960. curr_fill_pos_y=curr_fill_pos_y+1
  961. end
  962. term.setBackgroundColor(colors.black)
  963. end
  964.  
  965.  
  966.  
  967.  
  968.  
  969. function button_check(b_check_x1,b_check_y1,b_check_x2,b_check_y2)
  970. if info_1=="mouse_click" and info_3 > b_check_x1-1 and info_3 < b_check_x2+1 and info_4 > b_check_y1-1 and info_4 < b_check_y2+1 then
  971. return true
  972. else
  973. return false
  974. end
  975. end
  976.  
  977.  
  978.  
  979. function button(button_text,button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2)
  980. term.setTextColor(colors.green)
  981. square(button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2,colors.black,"-","|")
  982. term.setCursorPos(button_pos_x1-string.len(button_text)/2+(button_pos_x2-button_pos_x1)/2,button_pos_y1+(button_pos_y2-button_pos_y1)/2)
  983. term.write(button_text)
  984. end
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991. function headline(headline)
  992. headline_lenght=string.len(" "..headline.." ")
  993. headline_pos=27-headline_lenght/2
  994. term.setCursorPos(headline_pos,1)
  995. term.setBackgroundColor(colors.yellow)
  996. term.setTextColor(colors.blue)
  997. term.write(" "..headline.." ")
  998. term.setBackgroundColor(colors.black)
  999. term.setTextColor(colors.white)
  1000. end
  1001.  
  1002.  
  1003.  
  1004. function reset_players()
  1005. player_clear=2
  1006. for i=1,12 do
  1007. term.setCursorPos(2,player_clear)
  1008. term.write(" ")
  1009. term.setCursorPos(39,player_clear)
  1010. term.write(" ")
  1011. player_clear=player_clear+1
  1012. end
  1013. pla1_a=6
  1014. pla1_b=9
  1015. pla2_a=6
  1016. pla2_b=9
  1017. player_1_pos(0)
  1018. if player_count>1 then
  1019. bot_reset=true
  1020. player_2_pos(0)
  1021. end
  1022. end
  1023.  
  1024.  
  1025.  
  1026. function reset_player_data()
  1027. player_1_lives=3
  1028. player_2_lives=3
  1029.  
  1030. player_1_pos_a=6
  1031. player_1_pos_b=8
  1032. pla1_a=player_1_pos_a
  1033. pla1_b=player_1_pos_b
  1034.  
  1035. player_2_pos_a=6
  1036. player_2_pos_b=8
  1037. pla2_a=player_2_pos_a
  1038. pla2_b=player_2_pos_b
  1039. end
  1040.  
  1041.  
  1042.  
  1043. function write_lives_p()
  1044. term.setCursorPos(1,16)
  1045. term.setTextColor(colors.red)
  1046. if player_1_lives > 1 then
  1047. term.write("Player 1: "..player_1_lives.." lives")
  1048. else
  1049. term.write("Player 1: "..player_1_lives.." live")
  1050. end
  1051. if player_count>1 then
  1052. term.setCursorPos(1,17)
  1053. if player_2_lives > 1 then
  1054. term.write("Player 2: "..player_2_lives.." lives")
  1055. else
  1056. term.write("Player 2: "..player_2_lives.." live")
  1057. end
  1058. end
  1059. term.setTextColor(colors.white)
  1060. end
  1061.  
  1062.  
  1063.  
  1064. function check_ball_pos()
  1065. if pos_x==pong_field_x1+2 then
  1066. if pos_y==pla1_a or pos_y==pla1_a+1 or pos_y==pla1_a+2 then
  1067. return true
  1068. end
  1069. end
  1070.  
  1071. if player_count>1 then
  1072. if pos_x==pong_field_x2-2 then
  1073. if pos_y==pla2_a or pos_y==pla2_a+1 or pos_y==pla2_a+2 then
  1074. return true
  1075. end
  1076. end
  1077. end
  1078. end
  1079.  
  1080.  
  1081.  
  1082. function player_bot_pos(plus_min2)
  1083. if math.random(0,10) < bot_dif+1 or bot_reset then
  1084. if plus_min2==1 then
  1085. if pla2_a~=11 then
  1086. term.setCursorPos(39,pla2_a)
  1087. print(" ")
  1088. end
  1089. else
  1090. if pla2_a~=2 then
  1091. term.setCursorPos(39,pla2_b-1)
  1092. print(" ")
  1093. end
  1094. end
  1095. term.setTextColor(colors.green)
  1096. if pla2_a~=2 or plus_min2~=-1 then
  1097. if pla2_a~=11 or plus_min2~=1 then
  1098. pla2_a=pla2_a+plus_min2
  1099. pla2_b=pla2_b+plus_min2
  1100. pos2_a=pla2_a
  1101. for i=1,3 do
  1102. term.setCursorPos(39,pos2_a)
  1103. term.write("|")
  1104. pos2_a=pos2_a+1
  1105. end
  1106. end
  1107. end
  1108. term.setTextColor(colors.white)
  1109. end
  1110. bot_reset=false
  1111. end
  1112.  
  1113.  
  1114.  
  1115. function player_2_pos(plus_min2)
  1116. if is_bot then
  1117. if do_bot_move then
  1118. do_bot_move=false
  1119. player_bot_pos(plus_min2)
  1120. end
  1121. else
  1122.  
  1123. if plus_min2==1 then
  1124. if pla2_a~=11 then
  1125. term.setCursorPos(39,pla2_a)
  1126. print(" ")
  1127. end
  1128. else
  1129. if pla2_a~=2 then
  1130. term.setCursorPos(39,pla2_b-1)
  1131. print(" ")
  1132. end
  1133. end
  1134. term.setTextColor(colors.green)
  1135. if pla2_a~=2 or plus_min2~=-1 then
  1136. if pla2_a~=11 or plus_min2~=1 then
  1137. pla2_a=pla2_a+plus_min2
  1138. pla2_b=pla2_b+plus_min2
  1139. pos2_a=pla2_a
  1140. for i=1,3 do
  1141. term.setCursorPos(39,pos2_a)
  1142. term.write("|")
  1143. pos2_a=pos2_a+1
  1144. end
  1145. end
  1146. end
  1147. term.setTextColor(colors.white)
  1148. end
  1149. end
  1150.  
  1151.  
  1152.  
  1153. function player_1_pos(plus_min)
  1154. if plus_min==1 then
  1155. if pla1_a~=11 then
  1156. term.setCursorPos(2,pla1_a)
  1157. print(" ")
  1158. end
  1159. else
  1160. if pla1_a~=2 then
  1161. term.setCursorPos(2,pla1_b-1)
  1162. print(" ")
  1163. end
  1164. end
  1165. term.setTextColor(colors.green)
  1166. if pla1_a~=2 or plus_min~=-1 then
  1167. if pla1_a~=11 or plus_min~=1 then
  1168. pla1_a=pla1_a+plus_min
  1169. pla1_b=pla1_b+plus_min
  1170. pos1_a=pla1_a
  1171. for i=1,3 do
  1172. term.setCursorPos(2,pos1_a)
  1173. term.write("|")
  1174. pos1_a=pos1_a+1
  1175. end
  1176. end
  1177. end
  1178. term.setTextColor(colors.white)
  1179. end
  1180.  
  1181.  
  1182.  
  1183. function square(x1,y1,x2,y2,back_color_square,symbol_square_x,symbol_square_y,text_color)
  1184. x_1=x1
  1185. y_1=y1
  1186. x_2=x2
  1187. y_2=y2
  1188. while x_1~=x2+1 do
  1189. term.setBackgroundColor(back_color_square)
  1190. term.setCursorPos(x_1,y_1)
  1191. if text_color~=nil then
  1192. term.setTextColor(text_color)
  1193. end
  1194. term.write(symbol_square_x)
  1195. x_1=x_1+1
  1196. end
  1197.  
  1198. while y_1~=y2-1 do
  1199. term.setCursorPos(x_1-1,y_1+1)
  1200. term.write(symbol_square_y)
  1201. y_1=y_1+1
  1202. end
  1203. x2=x2-1
  1204. while x_2~=x1-1 do
  1205. term.setCursorPos(x_2,y_2)
  1206. term.write(symbol_square_x)
  1207. x_2=x_2-1
  1208. end
  1209.  
  1210. while y_2~=y1+1 do
  1211. term.setCursorPos(x_2+1,y_2-1)
  1212. term.write(symbol_square_y)
  1213. y_2=y_2-1
  1214. end
  1215. term.setBackgroundColor(colors.black)
  1216. end
  1217.  
  1218.  
  1219.  
  1220. function ball()
  1221. if ball_test~=1 then
  1222. pos_x=20
  1223. pos_y=7
  1224. term.setCursorPos(pos_x,pos_y)
  1225. term.write("o")
  1226. reset_players()
  1227. sleep(1)
  1228. speaker.playNote("bit",volume*2,22)
  1229. dir_x=math.random(0,1)
  1230. dir_y=math.random(0,1)
  1231. ball_test=1
  1232. end
  1233. old_dir_x=dir_x
  1234. old_x=pos_x
  1235. old_y=pos_y
  1236.  
  1237. if dir_x==0 then
  1238. pos_x=pos_x+1
  1239. else
  1240. pos_x=pos_x-1
  1241. end
  1242.  
  1243. if dir_y==0 then
  1244. pos_y=pos_y+1
  1245. else
  1246. pos_y=pos_y-1
  1247. end
  1248.  
  1249. if pos_x==pong_field_x2-2 and player_count>1 or pos_x==pong_field_x1+2 then
  1250.  
  1251. if check_ball_pos() then
  1252. if dir_x==0 then
  1253. dir_x=1
  1254. else
  1255. dir_x=0
  1256. end
  1257. end
  1258. end
  1259.  
  1260. if pos_y==13 or pos_y==2 then
  1261. if dir_y==0 then
  1262. dir_y=1
  1263. else
  1264. dir_y=0
  1265. end
  1266. end
  1267. term.setCursorPos(pos_x,pos_y)
  1268. term.write("o")
  1269.  
  1270. if pos_x==pong_field_x2-1 then
  1271. if player_count>1 then
  1272. clear_ball()
  1273. player_2_lives=player_2_lives-1
  1274. speaker.playNote("bit",volume,3)
  1275. sleep(0.1)
  1276. speaker.playNote("bit",volume,2)
  1277. sleep(0.1)
  1278. speaker.playNote("bit",volume,0)
  1279. ball_test=0
  1280. else
  1281. if dir_x==0 then
  1282. dir_x=1
  1283. else
  1284. dir_x=0
  1285. end
  1286. end
  1287. end
  1288.  
  1289. if pos_x==pong_field_x1+1 then
  1290. clear_ball()
  1291. player_1_lives=player_1_lives-1
  1292. ball_test=0
  1293. speaker.playNote("bit",volume,3)
  1294. sleep(0.1)
  1295. speaker.playNote("bit",volume,2)
  1296. sleep(0.1)
  1297. speaker.playNote("bit",volume,0)
  1298. end
  1299.  
  1300. if old_dir_x~=dir_x and (player_count==2 or pos_x==pong_field_x1+2) then
  1301. speaker.playNote("bit",volume,18)
  1302. end
  1303. end
  1304.  
  1305.  
  1306.  
  1307. function pull()
  1308. info_1,info_2,info_3,info_4=os.pullEvent()
  1309. end
  1310.  
  1311.  
  1312.  
  1313. function clear_ball()
  1314. term.setCursorPos(old_x,old_y)
  1315. term.write(" ")
  1316. end
  1317.  
  1318.  
  1319.  
  1320. function write_score()
  1321. term.setBackgroundColor(colors.black)
  1322. term.setTextColor(colors.green)
  1323. term.setCursorPos(42,2)
  1324. term.write("Score:")
  1325. term.setTextColor(colors.red)
  1326. term.setCursorPos(44,4)
  1327. term.write(score)
  1328. term.setTextColor(colors.white)
  1329. end
  1330.  
  1331. function dofile(file)
  1332. local f = assert(loadfile(file))
  1333. return f()
  1334. end
  1335.  
  1336.  
  1337. function s_key()
  1338. if info_1=="key" and (info_2==83 or info_2==31) then
  1339. return true
  1340. end
  1341. end
  1342.  
  1343.  
  1344.  
  1345. function w_key()
  1346. if info_1=="key" and (info_2==87 or info_2==17) then
  1347. return true
  1348. end
  1349. end
  1350.  
  1351.  
  1352.  
  1353. function arrow_down_key()
  1354. if info_1=="key" and (info_2==265 or info_2==200) then
  1355. return true
  1356. end
  1357. end
  1358.  
  1359.  
  1360.  
  1361. function arrow_up_key()
  1362. if info_1=="key" and (info_2==264 or info_2==208) then
  1363. return true
  1364. end
  1365. end
  1366.  
  1367.  
  1368.  
  1369. function a_key()
  1370. if info_1=="key" and info_2==30 then
  1371. return true
  1372. end
  1373. end
  1374.  
  1375.  
  1376.  
  1377. function d_key()
  1378. if info_1=="key" and info_2==32 then
  1379. return true
  1380. end
  1381. end
  1382.  
  1383.  
  1384.  
  1385.  
  1386. function pong(ball_speed)
  1387. term.clear()
  1388.  
  1389. score=0
  1390. pong_field_x1=1
  1391. pong_field_y1=1
  1392. do_bot_move=true
  1393.  
  1394. if player_count==2 then
  1395. pong_field_x2=40
  1396. pong_field_y2=14
  1397. end
  1398.  
  1399. if player_count==1 then
  1400. pong_field_x2=30
  1401. pong_field_y2=14
  1402. end
  1403.  
  1404.  
  1405. current_ball_speed=ball_speed
  1406. reset_player_data()
  1407. square(pong_field_x1,pong_field_y1,pong_field_x2,pong_field_y2,colors.white," "," ")
  1408. player_1_pos(0)
  1409. write_score()
  1410. if player_count>1 then
  1411. player_2_pos(0)
  1412. end
  1413.  
  1414. while player_1_lives>0 and player_2_lives>0 do
  1415. do_bot_move=true
  1416. write_lives_p()
  1417. ball()
  1418. clear_ball()
  1419. info_1=0
  1420. write_score()
  1421. os.startTimer(ball_speed)
  1422. while info_1~="timer" do
  1423. info_1=0
  1424. pull()
  1425.  
  1426. if info_1==pong_p1_up_kind and info_2==tonumber(pong_p1_up_num) then
  1427. player_1_pos(-1)
  1428. player_1_pos(-1)
  1429. end
  1430.  
  1431. if info_1==pong_p1_down_kind and info_2==pong_p1_down_num then
  1432. player_1_pos(1)
  1433. player_1_pos(1)
  1434. end
  1435.  
  1436. if player_count>1 and not is_bot then
  1437. if info_1==pong_p2_up_kind and info_2==pong_p2_up_num then
  1438. player_2_pos(-1)
  1439. player_2_pos(-1)
  1440. end
  1441.  
  1442. if info_1==pong_p2_down_kind and info_2==pong_p2_down_num then
  1443. player_2_pos(1)
  1444. player_2_pos(1)
  1445. end
  1446. end
  1447.  
  1448. if is_bot then
  1449. if pla2_a+1 > pos_y then
  1450. player_2_pos(-1)
  1451. do_check=true
  1452. check_ball_for_bot=pos_y
  1453. end
  1454.  
  1455. if pla2_a+1 < pos_y then
  1456. player_2_pos(1)
  1457. check_ball_for_bot=pos_y
  1458. end
  1459. end
  1460. end
  1461. score=score+1
  1462. write_score()
  1463. end
  1464. write_lives_p()
  1465. sleep(1)
  1466. term.clear()
  1467. square(13,8,35,12,colors.black,"-","|")
  1468. term.setTextColor(colors.red)
  1469. term.setCursorPos(16,10)
  1470. if player_1_lives~=0 then
  1471. term.write("Player 1 wins")
  1472. else
  1473. term.write("Player 2 wins")
  1474. end
  1475.  
  1476. if game_dif=="easy" and score>pong_easy_highscore then
  1477. broke_highscore(pong_easy_highscore,1)
  1478. end
  1479. if game_dif=="normal" and score>pong_normal_highscore then
  1480. broke_highscore(pong_normal_highscore,2)
  1481. end
  1482. if game_dif=="hard" and score>pong_hard_highscore then
  1483. broke_highscore(pong_hard_highscore,3)
  1484. end
  1485. if game_dif=="extreme" and score>pong_extreme_highscore then
  1486. broke_highscore(pong_extreme_highscore,4)
  1487. end
  1488. if game_dif=="insane" and score>pong_insane_highscore then
  1489. broke_highscore(pong_insane_highscore,5)
  1490. end
  1491.  
  1492. button("Home",1,16,16,18)
  1493. button(" Play again",38,16,51,18)
  1494. b_check=false
  1495. while not b_check do
  1496. pull()
  1497. b_check=button_check(1,16,16,18)
  1498. if b_check then
  1499. startup_menu()
  1500. end
  1501. b_check=button_check(38,16,51,18)
  1502. if b_check then
  1503. pong(current_ball_speed)
  1504. end
  1505. end
  1506. end
  1507.  
  1508.  
  1509.  
  1510. function broke_highscore(highscore,change_highscore)
  1511. square(10,2,38,6,colors.black,"-","|",colors.yellow)
  1512. term.setCursorPos(12,3)
  1513. term.write("You broke your highscore")
  1514. term.setCursorPos(12,4)
  1515. term.setTextColor(colors.red)
  1516. term.write("Old score:")
  1517. term.setCursorPos(12,5)
  1518. term.setTextColor(colors.green)
  1519. term.write(highscore)
  1520. term.setTextColor(colors.red)
  1521. term.setCursorPos(28,4)
  1522. term.write("New score:")
  1523. term.setCursorPos(28,5)
  1524. term.setTextColor(colors.green)
  1525. term.write(score)
  1526.  
  1527. highscore_len=fs.attributes("highscore")["size"]/2
  1528. highscore=fs.open("highscore","r")
  1529. old_highscore={0}
  1530.  
  1531. for i=1,highscore_len do
  1532. old_highscore[i]=highscore.readLine()
  1533. end
  1534.  
  1535. highscore.close()
  1536. highscore=fs.open("highscore","w")
  1537.  
  1538. for i=1,highscore_len do
  1539. if i~=change_highscore then
  1540. highscore.writeLine(old_highscore[i])
  1541. else
  1542. highscore.writeLine(score)
  1543. end
  1544. end
  1545.  
  1546. highscore.flush()
  1547. highscore.close()
  1548. assign_highscore()
  1549. end
  1550.  
  1551.  
  1552.  
  1553.  
  1554. minecraft_or_craftos_pc()
  1555. assign_highscore()
  1556. assign_settings()
  1557. startup_menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement