Advertisement
Chaos_Cash

minigames backup 19.12.23

Dec 19th, 2023
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.07 KB | None | 0 0
  1. --Use w/s to control player one and arrow up/down
  2. --for Player two
  3.  
  4.  
  5.  
  6.  
  7. --for craftos-pc remove the "--" in front of periphemu.create("top","speaker",1) for craftos in minecraft dont do that but place a speaker on top of the computer!!!
  8. periphemu.create("top","speaker",1)
  9. speaker = peripheral.wrap("top")
  10. sound_status="on"
  11. volume=1
  12. pong_highscore=fs.open("pong_highscore","r")
  13. if pong_highscore.readAll()=="" then
  14. pong_highscore=fs.open("pong_highscore","w")
  15. pong_highscore.write("0")
  16. pong_highscore.flush()
  17. end
  18. --
  19.  
  20. function startup_menu()
  21. term.setBackgroundColor(colors.black)
  22. term.clear()
  23. headline("Start menu")
  24. button(" Pong",16,6,25,8)
  25. button(" Exit",44,16,51,18)
  26. button(" Update",15,10,26,13)
  27. button("Click me",27,6,37,8)
  28. button("Sounds:",28,10,37,13)
  29. button("Snake",28,14,37,16)
  30. term.setCursorPos(17,12)
  31. term.write("History")
  32. term.setCursorPos(31,12)
  33. term.write(sound_status.." ")
  34. b_check=false
  35. while true do
  36. pull()
  37.  
  38. if button_check(28,10,37,13) then
  39. if sound_status=="on" then
  40. sound_status="off"
  41. volume=0
  42. else
  43. sound_status="on"
  44. volume=1
  45. end
  46. term.setCursorPos(31,12)
  47. term.write(sound_status.." ")
  48. end
  49.  
  50. if button_check(28,14,37,16) then
  51. difficulty_menu("Snake")
  52. end
  53.  
  54. if button_check(16,6,25,8) then
  55. info_1=0
  56. player_menu()
  57. end
  58.  
  59. if button_check(44,16,51,18) then
  60. term.clear()
  61. term.setCursorPos(-1,1)
  62. error()
  63. end
  64.  
  65. if button_check(15,10,26,13) then
  66. update_history()
  67. end
  68.  
  69. if button_check(27,6,37,8) then
  70. difficulty_menu("Click me")
  71. end
  72. end
  73. end
  74.  
  75.  
  76.  
  77. function update_history()
  78. term.clear()
  79. headline("Update History")
  80. term.setCursorPos(1,4)
  81. print("1.0: Completed basic game")
  82. print("1.1: Difficulties, Better Interface, start menu")
  83. print("1.2: Singleplayer mode, Better Interface, bug fixes")
  84. print("1.3: Pong Vs bot mode, Update History, added Score")
  85. print("1.4: Added Click me, some sounds, major bug fixes")
  86. button("Home",38,16,50,18)
  87. while 1==1 do
  88. pull()
  89. if button_check(38,16,50,18) then
  90. info_1=0
  91. startup_menu()
  92. end
  93. end
  94. end
  95.  
  96.  
  97.  
  98. function player_menu()
  99. is_bot=false
  100. term.clear()
  101. headline("Pong")
  102. button(" 1 Player",5,6,17,10)
  103. button(" 2 Players",19,6,31,10)
  104. button("Vs Bot",33,6,45,10)
  105. b_check=false
  106. while true do
  107. pull()
  108. if button_check(5,6,17,10) then
  109. is_bot=false
  110. info_1=0
  111. player_count=1
  112. difficulty_menu("Pong")
  113. end
  114.  
  115. if button_check(19,6,31,10) then
  116. is_bot=false
  117. info_1=0
  118. player_count=2
  119. difficulty_menu("Pong")
  120. end
  121.  
  122. if button_check(33,6,45,10) then
  123. info_1=0
  124. player_count=2
  125. is_bot=true
  126. difficulty_menu("Pong")
  127. end
  128. end
  129. end
  130.  
  131.  
  132. function difficulty_menu(what_game)
  133. term.clear()
  134. headline(what_game)
  135. button("Easy",8,7,18,11)
  136. button("Normal",22,7,32,11)
  137. button("Hard",36,7,46,11)
  138. button(" Extreme",22,13,32,17)
  139. button("Insane",36,13,46,17)
  140. b_check=false
  141. while true do
  142. pull()
  143.  
  144. if button_check(8,7,18,11) then
  145. game_dif="easy"
  146. if what_game=="Pong" then
  147. info_1=0
  148. bot_dif=6
  149. pong(0.13)
  150. end
  151. if what_game=="Click me" then
  152. click_me(1)
  153. end
  154. if what_game=="Snake" then
  155. snake(0.15)
  156. end
  157. end
  158.  
  159. if button_check(22,7,32,11) then
  160. game_dif="normal"
  161. if what_game=="Pong" then
  162. info_1=0
  163. bot_dif=7
  164. pong(0.1)
  165. end
  166. if what_game=="Click me" then
  167. click_me(0.8)
  168. end
  169. if what_game=="Snake" then
  170. snake(0.125)
  171. end
  172. end
  173.  
  174. if button_check(36,7,46,11) then
  175. game_dif="hard"
  176. if what_game=="Pong" then
  177. info_1=0
  178. bot_dif=8
  179. pong(0.07)
  180. end
  181. if what_game=="Click me" then
  182. click_me(0.6)
  183. end
  184. if what_game=="Snake" then
  185. snake(0.1)
  186. end
  187. end
  188.  
  189. if button_check(22,13,32,17) then
  190. game_dif="extreme"
  191. if what_game=="Pong" then
  192. info_1=0
  193. bot_dif=9
  194. pong(0.05)
  195. end
  196. if what_game=="Click me" then
  197. click_me(0.5)
  198. end
  199. if what_game=="Snake" then
  200. snake(0.075)
  201. end
  202. end
  203.  
  204. if button_check(36,13,46,17) then
  205. game_dif="insane"
  206. if what_game=="Pong" then
  207. info_1=0
  208. bot_dif=9
  209. pong(0.04)
  210. end
  211. if what_game=="Click me" then
  212. click_me(0.45)
  213. end
  214. if what_game=="Snake" then
  215. snake(0.05)
  216. end
  217. end
  218. end
  219. end
  220.  
  221.  
  222. function snake(game_speed)
  223. score=0
  224. level=1
  225. do_set_level_2=true
  226. game_running=true
  227. check_change_snake_color=0
  228. snake_len=2
  229. snake_dir="up"
  230. snake_head_x=18
  231. snake_head_y=9
  232. snake_field_x1=1
  233. snake_field_y1=1
  234. snake_field_x2=36
  235. snake_field_y2=18
  236. snake_x={0,0,0,0}
  237. snake_y={0,0,0,0}
  238. do_gen_point=true
  239. term.clear()
  240. square(snake_field_x1,snake_field_y1,snake_field_x2,snake_field_y2,colors.cyan," "," ")
  241. button(" Home",44,16,51,18)
  242. print_snake()
  243. write_score()
  244. os.sleep(1)
  245. while game_running do
  246. if do_gen_point then
  247. gen_point()
  248. end
  249. print_snake()
  250. info_1=0
  251. check_snake()
  252. if do_set_level_2 and score==200 then
  253. level=2
  254. reset_snake()
  255. do_set_level_2=false
  256. line_y(12,5,14,colors.gray)
  257. line_y(24,5,14,colors.gray)
  258. snake_dir="up"
  259. os.sleep(1)
  260. end
  261.  
  262. i=snake_len+1
  263. while i>0 do
  264. snake_x[i] = snake_x[i-1]
  265. snake_y[i] = snake_y[i-1]
  266. i=i-1
  267. end
  268. snake_x[1] = snake_head_x
  269. snake_y[1] = snake_head_y
  270. write_score()
  271. clear_snake()
  272. os.startTimer(game_speed)
  273. while info_1~="timer" do
  274. pull()
  275.  
  276. if button_check(44,16,51,18) then
  277. startup_menu()
  278. end
  279.  
  280. if w_key() and snake_dir~="down" then
  281. snake_dir="up"
  282. end
  283.  
  284. if s_key() and snake_dir~="up" then
  285. snake_dir="down"
  286. end
  287.  
  288. if a_key() and snake_dir~="right" then
  289. snake_dir="left"
  290. end
  291.  
  292. if d_key() and snake_dir~="left" then
  293. snake_dir="right"
  294. end
  295. end
  296.  
  297. if snake_dir=="up" then
  298. snake_head_y=snake_head_y-1
  299. end
  300.  
  301. if snake_dir=="down" then
  302. snake_head_y=snake_head_y+1
  303. end
  304.  
  305. if snake_dir=="left" then
  306. snake_head_x=snake_head_x-1
  307. end
  308.  
  309. if snake_dir=="right" then
  310. snake_head_x=snake_head_x+1
  311. end
  312.  
  313. if snake_head_x==snake_field_x1 then
  314. snake_head_x=snake_field_x2-1
  315. end
  316.  
  317.  
  318. if snake_head_x==snake_field_x2 then
  319. snake_head_x=snake_field_x1+1
  320. end
  321.  
  322.  
  323. if snake_head_y==snake_field_y1 then
  324. snake_head_y=snake_field_y2-1
  325. end
  326.  
  327.  
  328. if snake_head_y==snake_field_y2 then
  329. snake_head_y=snake_field_y1+1
  330. end
  331. end
  332. end_snake()
  333. while true do
  334. pull()
  335. if button_check(44,16,51,18) then
  336. startup_menu()
  337. end
  338. end
  339. end
  340.  
  341.  
  342.  
  343. function end_snake()
  344.  
  345. square(snake_field_x1,snake_field_y1,snake_field_x2,snake_field_y2,colors.lightGray," "," ")
  346. if level==2 then
  347. line_y(12,5,14,colors.lightGray)
  348. line_y(24,5,14,colors.lightGray)
  349. end
  350.  
  351. i=snake_len
  352. term.setBackgroundColor(colors.lightGray)
  353. term.setCursorPos(point_pos_x,point_pos_y)
  354. term.write(" ")
  355. term.setBackgroundColor(colors.gray)
  356. while i>0 do
  357. term.setCursorPos(snake_x[i],snake_y[i])
  358. term.write(" ")
  359. i=i-1
  360. end
  361. term.setCursorPos(snake_x[1],snake_y[1])
  362. term.setBackgroundColor(colors.red)
  363. term.write(" ")
  364. end
  365.  
  366.  
  367. function reset_snake()
  368. term.setBackgroundColor(colors.black)
  369. term.clear()
  370. square(snake_field_x1,snake_field_y1,snake_field_x2,snake_field_y2,colors.cyan," "," ")
  371. button(" Home",44,16,51,18)
  372. write_score()
  373. for i=1,40 do
  374. snake_x[i]=0
  375. snake_y[i]=0
  376. end
  377. snake_head_x=17
  378. snake_head_y=8
  379. snake_len=2
  380. print_snake()
  381. end
  382.  
  383.  
  384.  
  385.  
  386. function gen_point()
  387. do_for=1
  388. while do_for>0 do
  389. point_pos_x=math.random(snake_field_x1+1,snake_field_x2-1)
  390. point_pos_y=math.random(snake_field_y1+1,snake_field_y2-1)
  391. i=snake_len
  392. while i>0 do
  393. if point_pos_x==snake_x[i] and point_pos_y==snake_y[i] then
  394. do_for=do_for+2
  395. end
  396. i=i-1
  397. end
  398.  
  399. 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
  400. do_for=do_for+2
  401. end
  402. do_for=do_for-1
  403. end
  404. term.setBackgroundColor(colors.yellow)
  405. term.setCursorPos(point_pos_x,point_pos_y)
  406. term.write(" ")
  407. do_gen_point=false
  408. end
  409.  
  410.  
  411.  
  412.  
  413. function check_snake()
  414. i=2
  415. while i<snake_len+1 do
  416. if snake_head_x==snake_x[i] and snake_head_y==snake_y[i] then
  417. game_running=false
  418. end
  419. i=i+1
  420. end
  421.  
  422. if level==2 then
  423. if (snake_head_x==12 or snake_head_x==24) and snake_head_y>4 and snake_head_y<15 then
  424. game_running=false
  425. end
  426. end
  427.  
  428. if snake_head_x==point_pos_x and snake_head_y==point_pos_y then
  429. snake_len=snake_len+1
  430. do_gen_point=true
  431. score=score+5
  432. end
  433. end
  434.  
  435.  
  436. function clear_snake()
  437. term.setBackgroundColor(colors.black)
  438. term.setCursorPos(snake_x[snake_len+1],snake_y[snake_len+1])
  439. term.write(" ")
  440. end
  441.  
  442.  
  443. function print_snake()
  444. term.setCursorPos(snake_head_x,snake_head_y)
  445. term.setBackgroundColor(colors.lime)
  446. term.write(" ")
  447. if check_change_snake_color>1 then
  448. term.setCursorPos(snake_x[1],snake_y[1])
  449. term.setBackgroundColor(colors.green)
  450. term.write(" ")
  451. end
  452. check_change_snake_color=check_change_snake_color+1
  453. end
  454.  
  455.  
  456.  
  457. function line_x(start_x,start_y,end_x,color)
  458. term.setBackgroundColor(color)
  459. cur_x=start_x
  460. for i=1,end_x-start_x+1 do
  461. term.setCursorPos(cur_x,start_y)
  462. term.write(" ")
  463. cur_x=cur_x+1
  464. end
  465. end
  466.  
  467.  
  468.  
  469. function line_y(start_x,start_y,end_y,color)
  470. term.setBackgroundColor(color)
  471. cur_y=start_y
  472. for i=1,end_y-start_y+1 do
  473. term.setCursorPos(start_x,cur_y)
  474. term.write(" ")
  475. cur_y=cur_y+1
  476. end
  477. end
  478.  
  479.  
  480.  
  481.  
  482.  
  483. function click_me(game_speed)
  484. term.clear()
  485. click_speed=game_speed
  486. game_time=10
  487. score=0
  488.  
  489.  
  490. while game_time~=0 do
  491. filled_square(3,3,9,6,colors.lightGray)
  492. filled_square(11,3,17,6,colors.lightGray)
  493. filled_square(19,3,25,6,colors.lightGray)
  494. filled_square(3,8,9,11,colors.lightGray)
  495. filled_square(11,8,17,11,colors.lightGray)
  496. filled_square(19,8,25,11,colors.lightGray)
  497. filled_square(3,13,9,16,colors.lightGray)
  498. filled_square(11,13,17,16,colors.lightGray)
  499. filled_square(19,13,25,16,colors.lightGray)
  500.  
  501. old_cw=cw
  502. while old_cw==cw do
  503. cw=math.random(1,9)
  504. end
  505. ct=0
  506.  
  507. if cw==1 then
  508. filled_square(3,3,9,6,colors.green)
  509. end
  510.  
  511. if cw==2 then
  512. filled_square(11,3,17,6,colors.green)
  513. end
  514.  
  515. if cw==3 then
  516. filled_square(19,3,25,6,colors.green)
  517. end
  518.  
  519. if cw==4 then
  520. filled_square(3,8,9,11,colors.green)
  521. end
  522.  
  523. if cw==5 then
  524. filled_square(11,8,17,11,colors.green)
  525. end
  526.  
  527. if cw==6 then
  528. filled_square(19,8,25,11,colors.green)
  529. end
  530.  
  531. if cw==7 then
  532. filled_square(3,13,9,16,colors.green)
  533. end
  534.  
  535. if cw==8 then
  536. filled_square(11,13,17,16,colors.green)
  537. end
  538.  
  539. if cw==9 then
  540. filled_square(19,13,25,16,colors.green)
  541. end
  542. write_score()
  543. write_lives_c()
  544. timer_number=os.startTimer(game_speed)
  545.  
  546. while info_1~="timer" and ct==0 do
  547. pull()
  548.  
  549. if button_check(3,3,9,6) then
  550. ct=1
  551. end
  552.  
  553. if button_check(11,3,17,6) then
  554. ct=2
  555. end
  556.  
  557. if button_check(19,3,25,6) then
  558. ct=3
  559. end
  560.  
  561. if button_check(3,8,9,11) then
  562. ct=4
  563. end
  564.  
  565. if button_check(11,8,17,11) then
  566. ct=5
  567. end
  568.  
  569. if button_check(19,8,25,11) then
  570. ct=6
  571. end
  572.  
  573. if button_check(3,13,9,16) then
  574. ct=7
  575. end
  576.  
  577. if button_check(11,13,17,16) then
  578. ct=8
  579. end
  580.  
  581. if button_check(19,13,25,16) then
  582. ct=9
  583. end
  584. end
  585.  
  586. if ct==cw then
  587. score=score+10
  588. speaker.playNote("bit",volume/2.5,20)
  589. else
  590. game_time=game_time-1
  591. write_lives_c()
  592. if cw==1 then
  593. filled_square(3,3,9,6,colors.red)
  594. end
  595.  
  596. if cw==2 then
  597. filled_square(11,3,17,6,colors.red)
  598. end
  599.  
  600. if cw==3 then
  601. filled_square(19,3,25,6,colors.red)
  602. end
  603.  
  604. if cw==4 then
  605. filled_square(3,8,9,11,colors.red)
  606. end
  607.  
  608. if cw==5 then
  609. filled_square(11,8,17,11,colors.red)
  610. end
  611.  
  612. if cw==6 then
  613. filled_square(19,8,25,11,colors.red)
  614. end
  615.  
  616. if cw==7 then
  617. filled_square(3,13,9,16,colors.red)
  618. end
  619.  
  620. if cw==8 then
  621. filled_square(11,13,17,16,colors.red)
  622. end
  623.  
  624. if cw==9 then
  625. filled_square(19,13,25,16,colors.red)
  626. end
  627. speaker.playNote("bit",volume,0)
  628. sleep(0.3)
  629. end
  630. os.cancelTimer(timer_number)
  631. info_1=0
  632. end
  633. sleep(0.8)
  634. term.clear()
  635. term.setCursorPos(13,9)
  636. term.write(" Your Score is: "..score)
  637. term.setTextColor(colors.white)
  638. square(11,7,34,11,colors.black,"-","|")
  639. button(" Home",44,16,51,18)
  640. button(" Play again",1,16,14,18)
  641. while 1==1 do
  642. pull()
  643.  
  644. if button_check(45,16,51,18) then
  645. startup_menu()
  646. end
  647.  
  648. if button_check(1,16,13,18) then
  649. click_me(click_speed)
  650. end
  651. end
  652. end
  653.  
  654.  
  655.  
  656. function write_lives_c()
  657. term.setBackgroundColor(colors.black)
  658. term.setTextColor(colors.red)
  659. term.setCursorPos(10,1)
  660. if game_time>1 then
  661. term.write(game_time.." ".."Lives left ")
  662. else
  663. term.write(game_time.." ".."Live left ")
  664. end
  665. end
  666.  
  667.  
  668.  
  669. function filled_square(fill_x1,fill_y1,fill_x2,fill_y2,fill_color)
  670. term.setBackgroundColor(fill_color)
  671. curr_fill_pos_x=fill_x1
  672. curr_fill_pos_y=fill_y1
  673. while curr_fill_pos_y~=fill_y2+1 do
  674. while curr_fill_pos_x~=fill_x2+1 do
  675. term.setCursorPos(curr_fill_pos_x,curr_fill_pos_y)
  676. term.write(" ")
  677. curr_fill_pos_x=curr_fill_pos_x+1
  678. end
  679. curr_fill_pos_x=fill_x1
  680. curr_fill_pos_y=curr_fill_pos_y+1
  681. end
  682. term.setBackgroundColor(colors.black)
  683. end
  684.  
  685.  
  686.  
  687.  
  688.  
  689. function button_check(b_check_x1,b_check_y1,b_check_x2,b_check_y2)
  690. 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
  691. return true
  692. else
  693. return false
  694. end
  695. end
  696.  
  697.  
  698.  
  699. function button(button_text,button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2)
  700. term.setTextColor(colors.green)
  701. square(button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2,colors.black,"-","|")
  702. 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)
  703. term.write(button_text)
  704. end
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711. function headline(headline)
  712. headline_lenght=string.len(" "..headline.." ")
  713. headline_pos=27-headline_lenght/2
  714. term.setCursorPos(headline_pos,1)
  715. term.setBackgroundColor(colors.yellow)
  716. term.setTextColor(colors.blue)
  717. term.write(" "..headline.." ")
  718. term.setBackgroundColor(colors.black)
  719. term.setTextColor(colors.white)
  720. end
  721.  
  722.  
  723.  
  724. function reset_players()
  725. player_clear=2
  726. for i=1,12 do
  727. term.setCursorPos(2,player_clear)
  728. term.write(" ")
  729. term.setCursorPos(39,player_clear)
  730. term.write(" ")
  731. player_clear=player_clear+1
  732. end
  733. pla1_a=6
  734. pla1_b=9
  735. pla2_a=6
  736. pla2_b=9
  737. player_1_pos(0)
  738. if player_count>1 then
  739. bot_reset=true
  740. player_2_pos(0)
  741. end
  742. end
  743.  
  744.  
  745.  
  746. function reset_player_data()
  747. player_1_lives=3
  748. player_2_lives=3
  749.  
  750. player_1_pos_a=6
  751. player_1_pos_b=8
  752. pla1_a=player_1_pos_a
  753. pla1_b=player_1_pos_b
  754.  
  755. player_2_pos_a=6
  756. player_2_pos_b=8
  757. pla2_a=player_2_pos_a
  758. pla2_b=player_2_pos_b
  759. end
  760.  
  761.  
  762.  
  763. function write_lives_p()
  764. term.setCursorPos(1,16)
  765. term.setTextColor(colors.red)
  766. if player_1_lives > 1 then
  767. term.write("Player 1: "..player_1_lives.." lives")
  768. else
  769. term.write("Player 1: "..player_1_lives.." live")
  770. end
  771. if player_count>1 then
  772. term.setCursorPos(1,17)
  773. if player_2_lives > 1 then
  774. term.write("Player 2: "..player_2_lives.." lives")
  775. else
  776. term.write("Player 2: "..player_2_lives.." live")
  777. end
  778. end
  779. term.setTextColor(colors.white)
  780. end
  781.  
  782.  
  783.  
  784. function check_ball_pos()
  785. if pos_x==pong_field_x1+2 then
  786. if pos_y==pla1_a or pos_y==pla1_a+1 or pos_y==pla1_a+2 then
  787. return true
  788. end
  789. end
  790.  
  791. if player_count>1 then
  792. if pos_x==pong_field_x2-2 then
  793. if pos_y==pla2_a or pos_y==pla2_a+1 or pos_y==pla2_a+2 then
  794. return true
  795. end
  796. end
  797. end
  798. end
  799.  
  800.  
  801.  
  802. function player_bot_pos(plus_min2)
  803. if math.random(0,10) < bot_dif+1 or bot_reset then
  804. if plus_min2==1 then
  805. if pla2_a~=11 then
  806. term.setCursorPos(39,pla2_a)
  807. print(" ")
  808. end
  809. else
  810. if pla2_a~=2 then
  811. term.setCursorPos(39,pla2_b-1)
  812. print(" ")
  813. end
  814. end
  815. term.setTextColor(colors.green)
  816. if pla2_a~=2 or plus_min2~=-1 then
  817. if pla2_a~=11 or plus_min2~=1 then
  818. pla2_a=pla2_a+plus_min2
  819. pla2_b=pla2_b+plus_min2
  820. pos2_a=pla2_a
  821. for i=1,3 do
  822. term.setCursorPos(39,pos2_a)
  823. term.write("|")
  824. pos2_a=pos2_a+1
  825. end
  826. end
  827. end
  828. term.setTextColor(colors.white)
  829. end
  830. bot_reset=false
  831. end
  832.  
  833.  
  834.  
  835. function player_2_pos(plus_min2)
  836. if is_bot then
  837. if do_bot_move then
  838. do_bot_move=false
  839. player_bot_pos(plus_min2)
  840. end
  841. else
  842.  
  843. if plus_min2==1 then
  844. if pla2_a~=11 then
  845. term.setCursorPos(39,pla2_a)
  846. print(" ")
  847. end
  848. else
  849. if pla2_a~=2 then
  850. term.setCursorPos(39,pla2_b-1)
  851. print(" ")
  852. end
  853. end
  854. term.setTextColor(colors.green)
  855. if pla2_a~=2 or plus_min2~=-1 then
  856. if pla2_a~=11 or plus_min2~=1 then
  857. pla2_a=pla2_a+plus_min2
  858. pla2_b=pla2_b+plus_min2
  859. pos2_a=pla2_a
  860. for i=1,3 do
  861. term.setCursorPos(39,pos2_a)
  862. term.write("|")
  863. pos2_a=pos2_a+1
  864. end
  865. end
  866. end
  867. term.setTextColor(colors.white)
  868. end
  869. end
  870.  
  871.  
  872.  
  873. function player_1_pos(plus_min)
  874. if plus_min==1 then
  875. if pla1_a~=11 then
  876. term.setCursorPos(2,pla1_a)
  877. print(" ")
  878. end
  879. else
  880. if pla1_a~=2 then
  881. term.setCursorPos(2,pla1_b-1)
  882. print(" ")
  883. end
  884. end
  885. term.setTextColor(colors.green)
  886. if pla1_a~=2 or plus_min~=-1 then
  887. if pla1_a~=11 or plus_min~=1 then
  888. pla1_a=pla1_a+plus_min
  889. pla1_b=pla1_b+plus_min
  890. pos1_a=pla1_a
  891. for i=1,3 do
  892. term.setCursorPos(2,pos1_a)
  893. term.write("|")
  894. pos1_a=pos1_a+1
  895. end
  896. end
  897. end
  898. term.setTextColor(colors.white)
  899. end
  900.  
  901.  
  902.  
  903. function square(x1,y1,x2,y2,back_color_square,symbol_square_x,symbol_square_y,text_color)
  904. x_1=x1
  905. y_1=y1
  906. x_2=x2
  907. y_2=y2
  908. while x_1~=x2+1 do
  909. term.setBackgroundColor(back_color_square)
  910. term.setCursorPos(x_1,y_1)
  911. if text_color~=nil then
  912. term.setTextColor(text_color)
  913. end
  914. term.write(symbol_square_x)
  915. x_1=x_1+1
  916. end
  917.  
  918. while y_1~=y2-1 do
  919. term.setCursorPos(x_1-1,y_1+1)
  920. term.write(symbol_square_y)
  921. y_1=y_1+1
  922. end
  923. x2=x2-1
  924. while x_2~=x1-1 do
  925. term.setCursorPos(x_2,y_2)
  926. term.write(symbol_square_x)
  927. x_2=x_2-1
  928. end
  929.  
  930. while y_2~=y1+1 do
  931. term.setCursorPos(x_2+1,y_2-1)
  932. term.write(symbol_square_y)
  933. y_2=y_2-1
  934. end
  935. term.setBackgroundColor(colors.black)
  936. end
  937.  
  938.  
  939.  
  940. function ball()
  941. if ball_test~=1 then
  942. pos_x=20
  943. pos_y=7
  944. term.setCursorPos(pos_x,pos_y)
  945. term.write("o")
  946. reset_players()
  947. sleep(1)
  948. speaker.playNote("bit",volume*2,22)
  949. dir_x=math.random(0,1)
  950. dir_y=math.random(0,1)
  951. ball_test=1
  952. end
  953. old_dir_x=dir_x
  954. old_x=pos_x
  955. old_y=pos_y
  956.  
  957. if dir_x==0 then
  958. pos_x=pos_x+1
  959. else
  960. pos_x=pos_x-1
  961. end
  962.  
  963. if dir_y==0 then
  964. pos_y=pos_y+1
  965. else
  966. pos_y=pos_y-1
  967. end
  968.  
  969. if pos_x==pong_field_x2-2 and player_count>1 or pos_x==pong_field_x1+2 then
  970.  
  971. if check_ball_pos() then
  972. if dir_x==0 then
  973. dir_x=1
  974. else
  975. dir_x=0
  976. end
  977. end
  978. end
  979.  
  980. if pos_y==13 or pos_y==2 then
  981. if dir_y==0 then
  982. dir_y=1
  983. else
  984. dir_y=0
  985. end
  986. end
  987. term.setCursorPos(pos_x,pos_y)
  988. term.write("o")
  989.  
  990. if pos_x==pong_field_x2-1 then
  991. if player_count>1 then
  992. clear_ball()
  993. player_2_lives=player_2_lives-1
  994. speaker.playNote("bit",volume,3)
  995. sleep(0.1)
  996. speaker.playNote("bit",volume,2)
  997. sleep(0.1)
  998. speaker.playNote("bit",volume,0)
  999. ball_test=0
  1000. else
  1001. if dir_x==0 then
  1002. dir_x=1
  1003. else
  1004. dir_x=0
  1005. end
  1006. end
  1007. end
  1008.  
  1009. if pos_x==pong_field_x1+1 then
  1010. clear_ball()
  1011. player_1_lives=player_1_lives-1
  1012. ball_test=0
  1013. speaker.playNote("bit",volume,3)
  1014. sleep(0.1)
  1015. speaker.playNote("bit",volume,2)
  1016. sleep(0.1)
  1017. speaker.playNote("bit",volume,0)
  1018. end
  1019.  
  1020. if old_dir_x~=dir_x and (player_count==2 or pos_x==pong_field_x1+2) then
  1021. speaker.playNote("bit",volume,18)
  1022. end
  1023. end
  1024.  
  1025.  
  1026.  
  1027. function pull()
  1028. info_1,info_2,info_3,info_4=os.pullEvent()
  1029. end
  1030.  
  1031.  
  1032.  
  1033. function clear_ball()
  1034. term.setCursorPos(old_x,old_y)
  1035. term.write(" ")
  1036. end
  1037.  
  1038.  
  1039.  
  1040. function write_score()
  1041. term.setBackgroundColor(colors.black)
  1042. term.setTextColor(colors.green)
  1043. term.setCursorPos(42,2)
  1044. term.write("Score:")
  1045. term.setTextColor(colors.red)
  1046. term.setCursorPos(44,4)
  1047. term.write(score)
  1048. term.setTextColor(colors.white)
  1049. end
  1050.  
  1051. function dofile(file)
  1052. local f = assert(loadfile(file))
  1053. return f()
  1054. end
  1055.  
  1056.  
  1057. function s_key()
  1058. if info_1=="key" and (info_2==83 or info_2==31) then
  1059. return true
  1060. end
  1061. end
  1062.  
  1063.  
  1064.  
  1065. function w_key()
  1066. if info_1=="key" and (info_2==87 or info_2==17) then
  1067. return true
  1068. end
  1069. end
  1070.  
  1071.  
  1072.  
  1073. function arrow_down_key()
  1074. if info_1=="key" and (info_2==265 or info_2==200) then
  1075. return true
  1076. end
  1077. end
  1078.  
  1079.  
  1080.  
  1081. function arrow_up_key()
  1082. if info_1=="key" and (info_2==264 or info_2==208) then
  1083. return true
  1084. end
  1085. end
  1086.  
  1087.  
  1088.  
  1089. function a_key()
  1090. if info_1=="key" and info_2==30 then
  1091. return true
  1092. end
  1093. end
  1094.  
  1095.  
  1096.  
  1097. function d_key()
  1098. if info_1=="key" and info_2==32 then
  1099. return true
  1100. end
  1101. end
  1102.  
  1103.  
  1104. function pong(ball_speed)
  1105. term.clear()
  1106.  
  1107. score=0
  1108. pong_field_x1=1
  1109. pong_field_y1=1
  1110. do_bot_move=true
  1111.  
  1112. if player_count==2 then
  1113. pong_field_x2=40
  1114. pong_field_y2=14
  1115. end
  1116.  
  1117. if player_count==1 then
  1118. pong_field_x2=30
  1119. pong_field_y2=14
  1120. end
  1121.  
  1122.  
  1123. current_ball_speed=ball_speed
  1124. reset_player_data()
  1125. square(pong_field_x1,pong_field_y1,pong_field_x2,pong_field_y2,colors.white," "," ")
  1126. player_1_pos(0)
  1127. write_score()
  1128. if player_count>1 then
  1129. player_2_pos(0)
  1130. end
  1131.  
  1132. while player_1_lives>0 and player_2_lives>0 do
  1133. do_bot_move=true
  1134. write_lives_p()
  1135. ball()
  1136. clear_ball()
  1137. info_1=0
  1138. write_score()
  1139. os.startTimer(ball_speed)
  1140. while info_1~="timer" do
  1141. info_1=0
  1142. pull()
  1143.  
  1144. if w_key() then
  1145. player_1_pos(-1)
  1146. player_1_pos(-1)
  1147. end
  1148.  
  1149. if s_key() then
  1150. player_1_pos(1)
  1151. player_1_pos(1)
  1152. end
  1153.  
  1154. if player_count>1 and not is_bot then
  1155. if arrow_down_key() then
  1156. player_2_pos(-1)
  1157. player_2_pos(-1)
  1158. end
  1159.  
  1160. if arrow_up_key() then
  1161. player_2_pos(1)
  1162. player_2_pos(1)
  1163. end
  1164. end
  1165.  
  1166. if is_bot then
  1167. if pla2_a+1 > pos_y then
  1168. player_2_pos(-1)
  1169. do_check=true
  1170. check_ball_for_bot=pos_y
  1171. end
  1172.  
  1173. if pla2_a+1 < pos_y then
  1174. player_2_pos(1)
  1175. check_ball_for_bot=pos_y
  1176. end
  1177. end
  1178. end
  1179. score=score+1
  1180. end
  1181. write_lives_p()
  1182. sleep(1)
  1183. term.clear()
  1184. square(13,8,35,12,colors.black,"-","|")
  1185. term.setTextColor(colors.red)
  1186. term.setCursorPos(16,10)
  1187. if player_1_lives~=0 then
  1188. term.write("Player 1 wins")
  1189. else
  1190. term.write("Player 2 wins")
  1191. end
  1192.  
  1193. pong_highscore=fs.open("pong_highscore","r")
  1194. if tonumber(pong_highscore.readAll())<score then
  1195. square(10,2,38,6,colors.black,"-","|",colors.yellow)
  1196. term.setCursorPos(12,3)
  1197. term.write("You broke your highscore")
  1198. term.setCursorPos(12,4)
  1199. term.setTextColor(colors.red)
  1200. term.write("Old score:")
  1201. term.setCursorPos(12,5)
  1202. term.setTextColor(colors.green)
  1203. pong_highscore=fs.open("pong_highscore","r")
  1204. term.write(pong_highscore.readAll())
  1205. term.setTextColor(colors.red)
  1206. term.setCursorPos(28,4)
  1207. term.write("New score:")
  1208. term.setCursorPos(28,5)
  1209. term.setTextColor(colors.green)
  1210. term.write(score)
  1211. pong_highscore=fs.open("pong_highscore","w")
  1212. pong_highscore.write(score)
  1213. pong_highscore.flush()
  1214. end
  1215.  
  1216. button("Home",1,16,16,18)
  1217. button(" Play again",38,16,51,18)
  1218. b_check=false
  1219. while not b_check do
  1220. pull()
  1221. b_check=button_check(1,16,16,18)
  1222. if b_check then
  1223. startup_menu()
  1224. end
  1225. b_check=button_check(38,16,51,18)
  1226. if b_check then
  1227. pong(current_ball_speed)
  1228. end
  1229. end
  1230. end
  1231.  
  1232. startup_menu()
  1233.  
  1234.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement