Advertisement
Chaos_Cash

minigames backup 09.12.23

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