Chaos_Cash

minigames backup 12.12.23

Dec 12th, 2023 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.42 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()== nil or 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. game_running=true
  225. check_change_snake_color=0
  226. lives=1
  227. snake_len=2
  228. snake_dir="up"
  229. snake_head_x=17
  230. snake_head_y=8
  231. snake_field_x1=1
  232. snake_field_y1=1
  233. snake_field_x2=35
  234. snake_field_y2=17
  235. snake_x={0}
  236. snake_y={0}
  237. do_gen_point=true
  238. term.clear()
  239. square(snake_field_x1,snake_field_y1,snake_field_x2,snake_field_y2,colors.gray," "," ")
  240. button(" Home",44,16,51,18)
  241. print_snake()
  242. write_lives_s()
  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. i=snake_len
  253. while i>0 do
  254. snake_x[i] = snake_x[i-1]
  255. snake_y[i] = snake_y[i-1]
  256. i=i-1
  257. end
  258. snake_x[1] = snake_head_x
  259. snake_y[1] = snake_head_y
  260. write_score()
  261.  
  262. os.startTimer(game_speed)
  263. while info_1~="timer" do
  264. pull()
  265.  
  266. if button_check(44,16,51,18) then
  267. startup_menu()
  268. end
  269.  
  270. if w_key() and snake_dir~="down" then
  271. snake_dir="up"
  272. end
  273.  
  274. if s_key() and snake_dir~="up" then
  275. snake_dir="down"
  276. end
  277.  
  278. if a_key() and snake_dir~="right" then
  279. snake_dir="left"
  280. end
  281.  
  282. if d_key() and snake_dir~="left" then
  283. snake_dir="right"
  284. end
  285. end
  286.  
  287. if snake_dir=="up" then
  288. snake_head_y=snake_head_y-1
  289. end
  290.  
  291. if snake_dir=="down" then
  292. snake_head_y=snake_head_y+1
  293. end
  294.  
  295. if snake_dir=="left" then
  296. snake_head_x=snake_head_x-1
  297. end
  298.  
  299. if snake_dir=="right" then
  300. snake_head_x=snake_head_x+1
  301. end
  302.  
  303. if snake_head_x==snake_field_x1 then
  304. snake_head_x=snake_field_x2-1
  305. end
  306.  
  307.  
  308. if snake_head_x==snake_field_x2 then
  309. snake_head_x=snake_field_x1+1
  310. end
  311.  
  312.  
  313. if snake_head_y==snake_field_y1 then
  314. snake_head_y=snake_field_y2-1
  315. end
  316.  
  317.  
  318. if snake_head_y==snake_field_y2 then
  319. snake_head_y=snake_field_y1+1
  320. end
  321.  
  322. write_lives_s()
  323. clear_snake()
  324. end
  325.  
  326. end
  327.  
  328.  
  329.  
  330. function write_lives_s()
  331. term.setBackgroundColor(colors.black)
  332. term.setCursorPos(1,19)
  333. term.setTextColor(colors.red)
  334. if lives > 1 or lives==0 then
  335. term.write(lives.." lives left")
  336. else
  337. term.write(lives.." live left ")
  338. end
  339. term.setTextColor(colors.white)
  340. end
  341.  
  342.  
  343. function gen_point()
  344. do_for=1
  345. while do_for>0 do
  346. point_pos_x=math.random(snake_field_x1+1,snake_field_x2-1)
  347. point_pos_y=math.random(snake_field_y1+1,snake_field_y2-1)
  348. i=snake_len
  349. while i>0 do
  350. if point_pos_x==snake_x[i] and point_pos_y==snake_y[i] then
  351. do_for=do_for+2
  352. end
  353. i=i-1
  354. end
  355. do_for=do_for-1
  356. end
  357. term.setBackgroundColor(colors.yellow)
  358. term.setCursorPos(point_pos_x,point_pos_y)
  359. term.write(" ")
  360. do_gen_point=false
  361. end
  362.  
  363.  
  364.  
  365.  
  366. function check_snake()
  367. i=2
  368. while i<snake_len+1 do
  369. if snake_head_x==snake_x[i] and snake_head_y==snake_y[i] then
  370. lives=lives-1
  371. game_running=false
  372. end
  373. i=i+1
  374. end
  375.  
  376. if snake_head_x==point_pos_x and snake_head_y==point_pos_y then
  377. snake_len=snake_len+1
  378. do_gen_point=true
  379. score=score+5
  380. end
  381. end
  382.  
  383.  
  384. function clear_snake()
  385. term.setBackgroundColor(colors.black)
  386. term.setCursorPos(snake_x[snake_len],snake_y[snake_len])
  387. term.write(" ")
  388. end
  389.  
  390.  
  391. function print_snake()
  392. term.setCursorPos(snake_head_x,snake_head_y)
  393. term.setBackgroundColor(colors.lime)
  394. term.write(" ")
  395. if check_change_snake_color>1 then
  396. term.setCursorPos(snake_x[1],snake_y[1])
  397. term.setBackgroundColor(colors.green)
  398. term.write(" ")
  399. end
  400. check_change_snake_color=check_change_snake_color+1
  401. end
  402.  
  403. function click_me(game_speed)
  404. term.clear()
  405. click_speed=game_speed
  406. game_time=10
  407. score=0
  408.  
  409.  
  410. while game_time~=0 do
  411. filled_square(3,3,9,6,colors.lightGray)
  412. filled_square(11,3,17,6,colors.lightGray)
  413. filled_square(19,3,25,6,colors.lightGray)
  414. filled_square(3,8,9,11,colors.lightGray)
  415. filled_square(11,8,17,11,colors.lightGray)
  416. filled_square(19,8,25,11,colors.lightGray)
  417. filled_square(3,13,9,16,colors.lightGray)
  418. filled_square(11,13,17,16,colors.lightGray)
  419. filled_square(19,13,25,16,colors.lightGray)
  420.  
  421. old_cw=cw
  422. while old_cw==cw do
  423. cw=math.random(1,9)
  424. end
  425. ct=0
  426.  
  427. if cw==1 then
  428. filled_square(3,3,9,6,colors.green)
  429. end
  430.  
  431. if cw==2 then
  432. filled_square(11,3,17,6,colors.green)
  433. end
  434.  
  435. if cw==3 then
  436. filled_square(19,3,25,6,colors.green)
  437. end
  438.  
  439. if cw==4 then
  440. filled_square(3,8,9,11,colors.green)
  441. end
  442.  
  443. if cw==5 then
  444. filled_square(11,8,17,11,colors.green)
  445. end
  446.  
  447. if cw==6 then
  448. filled_square(19,8,25,11,colors.green)
  449. end
  450.  
  451. if cw==7 then
  452. filled_square(3,13,9,16,colors.green)
  453. end
  454.  
  455. if cw==8 then
  456. filled_square(11,13,17,16,colors.green)
  457. end
  458.  
  459. if cw==9 then
  460. filled_square(19,13,25,16,colors.green)
  461. end
  462. write_score()
  463. write_lives_c()
  464. timer_number=os.startTimer(game_speed)
  465.  
  466. while info_1~="timer" and ct==0 do
  467. pull()
  468.  
  469. if button_check(3,3,9,6) then
  470. ct=1
  471. end
  472.  
  473. if button_check(11,3,17,6) then
  474. ct=2
  475. end
  476.  
  477. if button_check(19,3,25,6) then
  478. ct=3
  479. end
  480.  
  481. if button_check(3,8,9,11) then
  482. ct=4
  483. end
  484.  
  485. if button_check(11,8,17,11) then
  486. ct=5
  487. end
  488.  
  489. if button_check(19,8,25,11) then
  490. ct=6
  491. end
  492.  
  493. if button_check(3,13,9,16) then
  494. ct=7
  495. end
  496.  
  497. if button_check(11,13,17,16) then
  498. ct=8
  499. end
  500.  
  501. if button_check(19,13,25,16) then
  502. ct=9
  503. end
  504. end
  505.  
  506. if ct==cw then
  507. score=score+10
  508. speaker.playNote("bit",volume/2.5,20)
  509. else
  510. game_time=game_time-1
  511. write_lives_c()
  512. if cw==1 then
  513. filled_square(3,3,9,6,colors.red)
  514. end
  515.  
  516. if cw==2 then
  517. filled_square(11,3,17,6,colors.red)
  518. end
  519.  
  520. if cw==3 then
  521. filled_square(19,3,25,6,colors.red)
  522. end
  523.  
  524. if cw==4 then
  525. filled_square(3,8,9,11,colors.red)
  526. end
  527.  
  528. if cw==5 then
  529. filled_square(11,8,17,11,colors.red)
  530. end
  531.  
  532. if cw==6 then
  533. filled_square(19,8,25,11,colors.red)
  534. end
  535.  
  536. if cw==7 then
  537. filled_square(3,13,9,16,colors.red)
  538. end
  539.  
  540. if cw==8 then
  541. filled_square(11,13,17,16,colors.red)
  542. end
  543.  
  544. if cw==9 then
  545. filled_square(19,13,25,16,colors.red)
  546. end
  547. speaker.playNote("bit",volume,0)
  548. sleep(0.3)
  549. end
  550. os.cancelTimer(timer_number)
  551. info_1=0
  552. end
  553. sleep(0.8)
  554. term.clear()
  555. term.setCursorPos(13,9)
  556. term.write(" Your Score is: "..score)
  557. term.setTextColor(colors.white)
  558. square(11,7,34,11,colors.black,"-","|")
  559. button(" Home",44,16,51,18)
  560. button(" Play again",1,16,14,18)
  561. while 1==1 do
  562. pull()
  563.  
  564. if button_check(45,16,51,18) then
  565. startup_menu()
  566. end
  567.  
  568. if button_check(1,16,13,18) then
  569. click_me(click_speed)
  570. end
  571. end
  572. end
  573.  
  574.  
  575.  
  576. function write_lives_c()
  577. term.setBackgroundColor(colors.black)
  578. term.setTextColor(colors.red)
  579. term.setCursorPos(10,1)
  580. if game_time>1 then
  581. term.write(game_time.." ".."Lives left ")
  582. else
  583. term.write(game_time.." ".."Live left ")
  584. end
  585. end
  586.  
  587.  
  588.  
  589. function filled_square(fill_x1,fill_y1,fill_x2,fill_y2,fill_color)
  590. term.setBackgroundColor(fill_color)
  591. curr_fill_pos_x=fill_x1
  592. curr_fill_pos_y=fill_y1
  593. while curr_fill_pos_y~=fill_y2+1 do
  594. while curr_fill_pos_x~=fill_x2+1 do
  595. term.setCursorPos(curr_fill_pos_x,curr_fill_pos_y)
  596. term.write(" ")
  597. curr_fill_pos_x=curr_fill_pos_x+1
  598. end
  599. curr_fill_pos_x=fill_x1
  600. curr_fill_pos_y=curr_fill_pos_y+1
  601. end
  602. term.setBackgroundColor(colors.black)
  603. end
  604.  
  605.  
  606.  
  607.  
  608.  
  609. function button_check(b_check_x1,b_check_y1,b_check_x2,b_check_y2)
  610. 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
  611. return true
  612. else
  613. return false
  614. end
  615. end
  616.  
  617.  
  618.  
  619. function button(button_text,button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2)
  620. term.setTextColor(colors.green)
  621. square(button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2,colors.black,"-","|")
  622. 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)
  623. term.write(button_text)
  624. end
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631. function headline(headline)
  632. headline_lenght=string.len(" "..headline.." ")
  633. headline_pos=27-headline_lenght/2
  634. term.setCursorPos(headline_pos,1)
  635. term.setBackgroundColor(colors.yellow)
  636. term.setTextColor(colors.blue)
  637. term.write(" "..headline.." ")
  638. term.setBackgroundColor(colors.black)
  639. term.setTextColor(colors.white)
  640. end
  641.  
  642.  
  643.  
  644. function reset_players()
  645. player_clear=2
  646. for i=1,12 do
  647. term.setCursorPos(2,player_clear)
  648. term.write(" ")
  649. term.setCursorPos(39,player_clear)
  650. term.write(" ")
  651. player_clear=player_clear+1
  652. end
  653. pla1_a=6
  654. pla1_b=9
  655. pla2_a=6
  656. pla2_b=9
  657. player_1_pos(0)
  658. if player_count>1 then
  659. bot_reset=true
  660. player_2_pos(0)
  661. end
  662. end
  663.  
  664.  
  665.  
  666. function reset_player_data()
  667. player_1_lives=3
  668. player_2_lives=3
  669.  
  670. player_1_pos_a=6
  671. player_1_pos_b=8
  672. pla1_a=player_1_pos_a
  673. pla1_b=player_1_pos_b
  674.  
  675. player_2_pos_a=6
  676. player_2_pos_b=8
  677. pla2_a=player_2_pos_a
  678. pla2_b=player_2_pos_b
  679. end
  680.  
  681.  
  682.  
  683. function write_lives_p()
  684. term.setCursorPos(1,16)
  685. term.setTextColor(colors.red)
  686. if player_1_lives > 1 then
  687. term.write("Player 1: "..player_1_lives.." lives")
  688. else
  689. term.write("Player 1: "..player_1_lives.." live")
  690. end
  691. if player_count>1 then
  692. term.setCursorPos(1,17)
  693. if player_2_lives > 1 then
  694. term.write("Player 2: "..player_2_lives.." lives")
  695. else
  696. term.write("Player 2: "..player_2_lives.." live")
  697. end
  698. end
  699. term.setTextColor(colors.white)
  700. end
  701.  
  702.  
  703.  
  704. function check_ball_pos()
  705. if pos_x==pong_field_x1+2 then
  706. if pos_y==pla1_a or pos_y==pla1_a+1 or pos_y==pla1_a+2 then
  707. return true
  708. end
  709. end
  710.  
  711. if player_count>1 then
  712. if pos_x==pong_field_x2-2 then
  713. if pos_y==pla2_a or pos_y==pla2_a+1 or pos_y==pla2_a+2 then
  714. return true
  715. end
  716. end
  717. end
  718. end
  719.  
  720.  
  721.  
  722. function player_bot_pos(plus_min2)
  723. if math.random(0,10) < bot_dif+1 or bot_reset then
  724. if plus_min2==1 then
  725. if pla2_a~=11 then
  726. term.setCursorPos(39,pla2_a)
  727. print(" ")
  728. end
  729. else
  730. if pla2_a~=2 then
  731. term.setCursorPos(39,pla2_b-1)
  732. print(" ")
  733. end
  734. end
  735. term.setTextColor(colors.green)
  736. if pla2_a~=2 or plus_min2~=-1 then
  737. if pla2_a~=11 or plus_min2~=1 then
  738. pla2_a=pla2_a+plus_min2
  739. pla2_b=pla2_b+plus_min2
  740. pos2_a=pla2_a
  741. for i=1,3 do
  742. term.setCursorPos(39,pos2_a)
  743. term.write("|")
  744. pos2_a=pos2_a+1
  745. end
  746. end
  747. end
  748. term.setTextColor(colors.white)
  749. end
  750. bot_reset=false
  751. end
  752.  
  753.  
  754.  
  755. function player_2_pos(plus_min2)
  756. if is_bot then
  757. if do_bot_move then
  758. do_bot_move=false
  759. player_bot_pos(plus_min2)
  760. end
  761. else
  762.  
  763. if plus_min2==1 then
  764. if pla2_a~=11 then
  765. term.setCursorPos(39,pla2_a)
  766. print(" ")
  767. end
  768. else
  769. if pla2_a~=2 then
  770. term.setCursorPos(39,pla2_b-1)
  771. print(" ")
  772. end
  773. end
  774. term.setTextColor(colors.green)
  775. if pla2_a~=2 or plus_min2~=-1 then
  776. if pla2_a~=11 or plus_min2~=1 then
  777. pla2_a=pla2_a+plus_min2
  778. pla2_b=pla2_b+plus_min2
  779. pos2_a=pla2_a
  780. for i=1,3 do
  781. term.setCursorPos(39,pos2_a)
  782. term.write("|")
  783. pos2_a=pos2_a+1
  784. end
  785. end
  786. end
  787. term.setTextColor(colors.white)
  788. end
  789. end
  790.  
  791.  
  792.  
  793. function player_1_pos(plus_min)
  794. if plus_min==1 then
  795. if pla1_a~=11 then
  796. term.setCursorPos(2,pla1_a)
  797. print(" ")
  798. end
  799. else
  800. if pla1_a~=2 then
  801. term.setCursorPos(2,pla1_b-1)
  802. print(" ")
  803. end
  804. end
  805. term.setTextColor(colors.green)
  806. if pla1_a~=2 or plus_min~=-1 then
  807. if pla1_a~=11 or plus_min~=1 then
  808. pla1_a=pla1_a+plus_min
  809. pla1_b=pla1_b+plus_min
  810. pos1_a=pla1_a
  811. for i=1,3 do
  812. term.setCursorPos(2,pos1_a)
  813. term.write("|")
  814. pos1_a=pos1_a+1
  815. end
  816. end
  817. end
  818. term.setTextColor(colors.white)
  819. end
  820.  
  821.  
  822.  
  823. function square(x1,y1,x2,y2,back_color_square,symbol_square_x,symbol_square_y,text_color)
  824. x_1=x1
  825. y_1=y1
  826. x_2=x2
  827. y_2=y2
  828. while x_1~=x2+1 do
  829. term.setBackgroundColor(back_color_square)
  830. term.setCursorPos(x_1,y_1)
  831. if text_color~=nil then
  832. term.setTextColor(text_color)
  833. end
  834. term.write(symbol_square_x)
  835. x_1=x_1+1
  836. end
  837.  
  838. while y_1~=y2-1 do
  839. term.setCursorPos(x_1-1,y_1+1)
  840. term.write(symbol_square_y)
  841. y_1=y_1+1
  842. end
  843. x2=x2-1
  844. while x_2~=x1-1 do
  845. term.setCursorPos(x_2,y_2)
  846. term.write(symbol_square_x)
  847. x_2=x_2-1
  848. end
  849.  
  850. while y_2~=y1+1 do
  851. term.setCursorPos(x_2+1,y_2-1)
  852. term.write(symbol_square_y)
  853. y_2=y_2-1
  854. end
  855. term.setBackgroundColor(colors.black)
  856. end
  857.  
  858.  
  859.  
  860. function ball()
  861. if ball_test~=1 then
  862. pos_x=20
  863. pos_y=7
  864. term.setCursorPos(pos_x,pos_y)
  865. term.write("o")
  866. reset_players()
  867. sleep(1)
  868. speaker.playNote("bit",volume*2,22)
  869. dir_x=math.random(0,1)
  870. dir_y=math.random(0,1)
  871. ball_test=1
  872. end
  873. old_dir_x=dir_x
  874. old_x=pos_x
  875. old_y=pos_y
  876.  
  877. if dir_x==0 then
  878. pos_x=pos_x+1
  879. else
  880. pos_x=pos_x-1
  881. end
  882.  
  883. if dir_y==0 then
  884. pos_y=pos_y+1
  885. else
  886. pos_y=pos_y-1
  887. end
  888.  
  889. if pos_x==pong_field_x2-2 and player_count>1 or pos_x==pong_field_x1+2 then
  890.  
  891. if check_ball_pos() then
  892. if dir_x==0 then
  893. dir_x=1
  894. else
  895. dir_x=0
  896. end
  897. end
  898. end
  899.  
  900. if pos_y==13 or pos_y==2 then
  901. if dir_y==0 then
  902. dir_y=1
  903. else
  904. dir_y=0
  905. end
  906. end
  907. term.setCursorPos(pos_x,pos_y)
  908. term.write("o")
  909.  
  910. if pos_x==pong_field_x2-1 then
  911. if player_count>1 then
  912. clear_ball()
  913. player_2_lives=player_2_lives-1
  914. speaker.playNote("bit",volume,3)
  915. sleep(0.1)
  916. speaker.playNote("bit",volume,2)
  917. sleep(0.1)
  918. speaker.playNote("bit",volume,0)
  919. ball_test=0
  920. else
  921. if dir_x==0 then
  922. dir_x=1
  923. else
  924. dir_x=0
  925. end
  926. end
  927. end
  928.  
  929. if pos_x==pong_field_x1+1 then
  930. clear_ball()
  931. player_1_lives=player_1_lives-1
  932. ball_test=0
  933. speaker.playNote("bit",volume,3)
  934. sleep(0.1)
  935. speaker.playNote("bit",volume,2)
  936. sleep(0.1)
  937. speaker.playNote("bit",volume,0)
  938. end
  939.  
  940. if old_dir_x~=dir_x and (player_count==2 or pos_x==pong_field_x1+2) then
  941. speaker.playNote("bit",volume,18)
  942. end
  943. end
  944.  
  945.  
  946.  
  947. function pull()
  948. info_1,info_2,info_3,info_4=os.pullEvent()
  949. end
  950.  
  951.  
  952.  
  953. function clear_ball()
  954. term.setCursorPos(old_x,old_y)
  955. term.write(" ")
  956. end
  957.  
  958.  
  959.  
  960. function write_score()
  961. term.setBackgroundColor(colors.black)
  962. term.setTextColor(colors.green)
  963. term.setCursorPos(42,2)
  964. term.write("Score:")
  965. term.setTextColor(colors.red)
  966. term.setCursorPos(44,4)
  967. term.write(score)
  968. term.setTextColor(colors.white)
  969. end
  970.  
  971.  
  972.  
  973. function s_key()
  974. if info_1=="key" and (info_2==83 or info_2==31) then
  975. return true
  976. end
  977. end
  978.  
  979.  
  980.  
  981. function w_key()
  982. if info_1=="key" and (info_2==87 or info_2==17) then
  983. return true
  984. end
  985. end
  986.  
  987.  
  988.  
  989. function arrow_down_key()
  990. if info_1=="key" and (info_2==265 or info_2==200) then
  991. return true
  992. end
  993. end
  994.  
  995.  
  996.  
  997. function arrow_up_key()
  998. if info_1=="key" and (info_2==264 or info_2==208) then
  999. return true
  1000. end
  1001. end
  1002.  
  1003.  
  1004.  
  1005. function a_key()
  1006. if info_1=="key" and info_2==30 then
  1007. return true
  1008. end
  1009. end
  1010.  
  1011.  
  1012.  
  1013. function d_key()
  1014. if info_1=="key" and info_2==32 then
  1015. return true
  1016. end
  1017. end
  1018.  
  1019.  
  1020. function pong(ball_speed)
  1021. term.clear()
  1022.  
  1023. score=0
  1024. pong_field_x1=1
  1025. pong_field_y1=1
  1026. do_bot_move=true
  1027.  
  1028. if player_count==2 then
  1029. pong_field_x2=40
  1030. pong_field_y2=14
  1031. end
  1032.  
  1033. if player_count==1 then
  1034. pong_field_x2=30
  1035. pong_field_y2=14
  1036. end
  1037.  
  1038.  
  1039. current_ball_speed=ball_speed
  1040. reset_player_data()
  1041. square(pong_field_x1,pong_field_y1,pong_field_x2,pong_field_y2,colors.white," "," ")
  1042. player_1_pos(0)
  1043. write_score()
  1044. if player_count>1 then
  1045. player_2_pos(0)
  1046. end
  1047.  
  1048. while player_1_lives>0 and player_2_lives>0 do
  1049. do_bot_move=true
  1050. write_lives_p()
  1051. ball()
  1052. clear_ball()
  1053. info_1=0
  1054. write_score()
  1055. os.startTimer(ball_speed)
  1056. while info_1~="timer" do
  1057. info_1=0
  1058. pull()
  1059.  
  1060. if w_key() then
  1061. player_1_pos(-1)
  1062. player_1_pos(-1)
  1063. end
  1064.  
  1065. if s_key() then
  1066. player_1_pos(1)
  1067. player_1_pos(1)
  1068. end
  1069.  
  1070. if player_count>1 and not is_bot then
  1071. if arrow_down_key() then
  1072. player_2_pos(-1)
  1073. player_2_pos(-1)
  1074. end
  1075.  
  1076. if arrow_up_key() then
  1077. player_2_pos(1)
  1078. player_2_pos(1)
  1079. end
  1080. end
  1081.  
  1082. if is_bot then
  1083. if pla2_a+1 > pos_y then
  1084. player_2_pos(-1)
  1085. do_check=true
  1086. check_ball_for_bot=pos_y
  1087. end
  1088.  
  1089. if pla2_a+1 < pos_y then
  1090. player_2_pos(1)
  1091. check_ball_for_bot=pos_y
  1092. end
  1093. end
  1094. end
  1095. score=score+1
  1096. end
  1097. write_lives_p()
  1098. sleep(1)
  1099. term.clear()
  1100. square(13,8,35,12,colors.black,"-","|")
  1101. term.setTextColor(colors.red)
  1102. term.setCursorPos(16,10)
  1103. if player_1_lives~=0 then
  1104. term.write("Player 1 wins")
  1105. else
  1106. term.write("Player 2 wins")
  1107. end
  1108.  
  1109. pong_highscore=fs.open("pong_highscore","r")
  1110. if tonumber(pong_highscore.readAll())<score then
  1111. square(10,2,38,6,colors.black,"-","|",colors.yellow)
  1112. term.setCursorPos(12,3)
  1113. term.write("You broke your highscore")
  1114. term.setCursorPos(12,4)
  1115. term.setTextColor(colors.red)
  1116. term.write("Old score:")
  1117. term.setCursorPos(12,5)
  1118. term.setTextColor(colors.green)
  1119. pong_highscore=fs.open("pong_highscore","r")
  1120. term.write(pong_highscore.readAll())
  1121. term.setTextColor(colors.red)
  1122. term.setCursorPos(28,4)
  1123. term.write("New score:")
  1124. term.setCursorPos(28,5)
  1125. term.setTextColor(colors.green)
  1126. term.write(score)
  1127. pong_highscore=fs.open("pong_highscore","w")
  1128. pong_highscore.write(score)
  1129. pong_highscore.flush()
  1130. end
  1131.  
  1132. button("Home",1,16,16,18)
  1133. button(" Play again",38,16,51,18)
  1134. b_check=false
  1135. while not b_check do
  1136. pull()
  1137. b_check=button_check(1,16,16,18)
  1138. if b_check then
  1139. startup_menu()
  1140. end
  1141. b_check=button_check(38,16,51,18)
  1142. if b_check then
  1143. pong(current_ball_speed)
  1144. end
  1145. end
  1146. end
  1147.  
  1148.  
  1149. startup_menu()
  1150.  
  1151.  
  1152.  
Add Comment
Please, Sign In to add comment