Advertisement
Krystman

Pico-8 Hero - Breakout - End of Ep. 24

Apr 3rd, 2017
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.71 KB | None | 0 0
  1. --goals
  2. -- 6. powerups
  3. -- - speed down
  4. -- - megaball
  5. -- - multiball
  6.  
  7. -- 7. juicyness
  8. -- arrow anim
  9. -- text blinking
  10. -- particles
  11. -- screenshake
  12. -- 8. high score
  13. -- 9. better collision
  14. -- 10. gameplay tweaks
  15. -- - smaller paddle
  16.  
  17. function _init()
  18. cls()
  19. mode="start"
  20. level=""
  21. debug=""
  22. levelnum = 1
  23. levels={}
  24. --levels[1] = "x5b"
  25. levels[1] = "b9b/p9p"
  26. --levels[1] = "hxixsxpxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxb"
  27. --levels[1] = "////x4b/s9s"
  28. end
  29.  
  30. function _update60()
  31. if mode=="game" then
  32. update_game()
  33. elseif mode=="start" then
  34. update_start()
  35. elseif mode=="gameover" then
  36. update_gameover()
  37. elseif mode=="levelover" then
  38. update_levelover()
  39. end
  40. end
  41.  
  42. function update_start()
  43. if btnp(4) then
  44. startgame()
  45. end
  46. end
  47.  
  48. function startgame()
  49. mode="game"
  50. ball_r=2
  51. ball_dr=0.5
  52.  
  53. pad_x=52
  54. pad_y=120
  55. pad_dx=0
  56. pad_wo=24--original pad width
  57. pad_w=24 --current pad with
  58. pad_h=3
  59. pad_c=7
  60.  
  61. brick_w=9
  62. brick_h=4
  63.  
  64. levelnum = 1
  65. level = levels[levelnum]
  66. buildbricks(level)
  67. --brick_y=20
  68.  
  69. lives=3
  70. points=0
  71.  
  72. sticky=true
  73.  
  74. chain=1 --combo chain multiplier
  75.  
  76. serveball()
  77. end
  78.  
  79. function nextlevel()
  80. mode="game"
  81. pad_x=52
  82. pad_y=120
  83. pad_dx=0
  84.  
  85. levelnum+=1
  86. if levelnum > #levels then
  87. -- we've beaten the gane
  88. -- we need some kind of special
  89. -- screen here
  90. mode="start"
  91. return
  92. end
  93. level=levels[levelnum]
  94. buildbricks(level)
  95.  
  96. sticky=true
  97. chain=1
  98.  
  99. serveball()
  100. end
  101.  
  102. function buildbricks(lvl)
  103. local i,j,o,chr,last
  104. brick_x={}
  105. brick_y={}
  106. brick_v={}
  107. brick_t={}
  108.  
  109. j=0
  110. -- b = normal brick
  111. -- x = empty space
  112. -- i = indestructable brick
  113. -- h = hardened brick
  114. -- s = sploding brick
  115. -- p = powerup brick
  116.  
  117. for i=1,#lvl do
  118. j+=1
  119. chr=sub(lvl,i,i)
  120. if chr=="b"
  121. or chr=="i"
  122. or chr=="h"
  123. or chr=="s"
  124. or chr=="p" then
  125. last=chr
  126. addbrick(j,chr)
  127. elseif chr=="x" then
  128. last="x"
  129. elseif chr=="/" then
  130. j=(flr((j-1)/11)+1)*11
  131. elseif chr>="1" and chr<="9" then
  132. for o=1,chr+0 do
  133. if last=="b"
  134. or last=="i"
  135. or last=="h"
  136. or last=="s"
  137. or last=="p" then
  138. addbrick(j,last)
  139. elseif last=="x" then
  140. --nothing
  141. end
  142. j+=1
  143. end
  144. j-=1
  145. end
  146. end
  147. end
  148.  
  149. function resetpills()
  150. pill_x={}
  151. pill_y={}
  152. pill_v={}
  153. pill_t={}
  154. end
  155.  
  156. function addbrick(_i,_t)
  157. add(brick_x,4+((_i-1)%11)*(brick_w+2))
  158. add(brick_y,20+flr((_i-1)/11)*(brick_h+2))
  159. add(brick_v,true)
  160. add(brick_t,_t)
  161. end
  162.  
  163. function levelfinished()
  164. if #brick_v == 0 then return true end
  165.  
  166. for i=1,#brick_v do
  167. if brick_v[i] == true and brick_t[i] != "i" then
  168. return false
  169. end
  170. end
  171. return true
  172. end
  173.  
  174. function serveball()
  175. ball_x=pad_x+flr(pad_w/2)
  176. ball_y=pad_y-ball_r
  177. ball_dx=1
  178. ball_dy=-1
  179. ball_ang=1
  180. pointsmult=1
  181. chain=1
  182. resetpills()
  183.  
  184. sticky=true
  185. sticky_x=flr(pad_w/2)
  186.  
  187. --0.50
  188. --1.30
  189. powerup=0
  190. powerup_t=0
  191.  
  192. end
  193.  
  194. function setang(ang)
  195. ball_ang=ang
  196. if ang==2 then
  197. ball_dx=0.50*sign(ball_dx)
  198. ball_dy=1.30*sign(ball_dy)
  199. elseif ang==0 then
  200. ball_dx=1.30*sign(ball_dx)
  201. ball_dy=0.50*sign(ball_dy)
  202. else
  203. ball_dx=1*sign(ball_dx)
  204. ball_dy=1*sign(ball_dy)
  205. end
  206. end
  207.  
  208. function sign(n)
  209. if n<0 then
  210. return -1
  211. elseif n>0 then
  212. return 1
  213. else
  214. return 0
  215. end
  216. end
  217.  
  218. function gameover()
  219. mode="gameover"
  220. end
  221.  
  222. function levelover()
  223. mode="levelover"
  224. end
  225.  
  226. function update_gameover()
  227. if btnp(4) then
  228. startgame()
  229. end
  230. end
  231.  
  232. function update_levelover()
  233. if btnp(4) then
  234. nextlevel()
  235. end
  236. end
  237.  
  238. function update_game()
  239. local buttpress=false
  240. local nextx,nexty,brickhit
  241.  
  242. if powerup == 4 then
  243. -- check if pad should grow
  244. pad_w = flr(pad_wo * 1.5)
  245. elseif powerup == 5 then
  246. -- check if pad should shrink
  247. pad_w = flr(pad_wo / 2)
  248. pointsmult=2
  249. else
  250. pad_w = pad_wo
  251. pointsmult=1
  252. end
  253.  
  254. if btn(0) then
  255. --left
  256. pad_dx=-2.5
  257. buttpress=true
  258. --pad_x-=5
  259. if sticky then
  260. ball_dx=-1
  261. end
  262. end
  263. if btn(1) then
  264. --right
  265. pad_dx=2.5
  266. buttpress=true
  267. --pad_x+=5
  268. if sticky then
  269. ball_dx=1
  270. end
  271. end
  272. if sticky and btnp(4) then
  273. sticky=false
  274. ball_x=mid(3,ball_x,124)
  275. end
  276.  
  277. if not(buttpress) then
  278. pad_dx=pad_dx/1.3
  279. end
  280. pad_x+=pad_dx
  281. pad_x=mid(0,pad_x,127-pad_w)
  282.  
  283. if sticky then
  284. --ball_x=pad_x+flr(pad_w/2)
  285. ball_x=pad_x+sticky_x
  286. ball_y=pad_y-ball_r-1
  287. else
  288. --regular ball physics
  289. nextx=ball_x+ball_dx
  290. nexty=ball_y+ball_dy
  291.  
  292. --check if ball hit wall
  293. if nextx > 124 or nextx < 3 then
  294. nextx=mid(0,nextx,127)
  295. ball_dx = -ball_dx
  296. sfx(0)
  297. end
  298. if nexty < 10 then
  299. nexty=mid(0,nexty,127)
  300. ball_dy = -ball_dy
  301. sfx(0)
  302. end
  303.  
  304. -- check if ball hit pad
  305. if ball_box(nextx,nexty,pad_x,pad_y,pad_w,pad_h) then
  306. -- deal with collision
  307. if deflx_ball_box(ball_x,ball_y,ball_dx,ball_dy,pad_x,pad_y,pad_w,pad_h) then
  308. --ball hit paddle on the side
  309. ball_dx = -ball_dx
  310. if ball_x < pad_x+pad_w/2 then
  311. nextx=pad_x-ball_r
  312. else
  313. nextx=pad_x+pad_w+ball_r
  314. end
  315. else
  316. --ball hit paddle on the top/bottom
  317. ball_dy = -ball_dy
  318. if ball_y > pad_y then
  319. --bottom
  320. nexty=pad_y+pad_h+ball_r
  321. else
  322. --top
  323. nexty=pad_y-ball_r
  324. if abs(pad_dx)>2 then
  325. --change angle
  326. if sign(pad_dx)==sign(ball_dx) then
  327. --flatten angle
  328. setang(mid(0,ball_ang-1,2))
  329. else
  330. --raise angle
  331. if ball_ang==2 then
  332. ball_dx=-ball_dx
  333. else
  334. setang(mid(0,ball_ang+1,2))
  335. end
  336. end
  337. end
  338. end
  339. end
  340. sfx(1)
  341. chain=1
  342.  
  343. --catch powerup
  344. if powerup==3 and ball_dy < 0 then
  345. sticky = true
  346. sticky_x = ball_x-pad_x
  347. end
  348. end
  349.  
  350. brickhit=false
  351. for i=1,#brick_x do
  352. -- check if ball hit brick
  353. if brick_v[i] and ball_box(nextx,nexty,brick_x[i],brick_y[i],brick_w,brick_h) then
  354. -- deal with collision
  355. if not(brickhit) then
  356. if deflx_ball_box(ball_x,ball_y,ball_dx,ball_dy,brick_x[i],brick_y[i],brick_w,brick_h) then
  357. ball_dx = -ball_dx
  358. else
  359. ball_dy = -ball_dy
  360. end
  361. end
  362. brickhit=true
  363. hitbrick(i,true)
  364. end
  365. end
  366. ball_x=nextx
  367. ball_y=nexty
  368.  
  369. -- check if ball left screen
  370. if nexty > 127 then
  371. sfx(2)
  372. lives-=1
  373. if lives<0 then
  374. gameover()
  375. else
  376. serveball()
  377. end
  378. end
  379.  
  380. end -- end of sticky if
  381.  
  382. -- move pills
  383. -- check collision for pills
  384. for i=1,#pill_x do
  385. if pill_v[i] then
  386. pill_y[i]+=0.7
  387. if pill_y[i] > 128 then
  388. pill_v[i]=false
  389. end
  390. if box_box(pill_x[i],pill_y[i],8,6,pad_x,pad_y,pad_w,pad_h) then
  391. powerupget(pill_t[i])
  392. pill_v[i]=false
  393. sfx(11)
  394. end
  395. end
  396. end
  397.  
  398. checkexplosions()
  399.  
  400. if levelfinished() then
  401. _draw()
  402. levelover()
  403. end
  404.  
  405. if powerup!=0 then
  406. powerup_t-=1
  407. --debug = powerup_t
  408. if powerup_t<=0 then
  409. powerup=0
  410. end
  411. end
  412.  
  413. end
  414.  
  415. function powerupget(_p)
  416. if _p == 1 then
  417. -- slowdown
  418. powerup = 1
  419. powerup_t = 0
  420. elseif _p == 2 then
  421. -- life
  422. powerup = 0
  423. powerup_t = 0
  424. lives+=1
  425. elseif _p == 3 then
  426. -- catch
  427. powerup = 3
  428. powerup_t = 900
  429. elseif _p == 4 then
  430. -- expand
  431. powerup = 4
  432. powerup_t = 900
  433. elseif _p == 5 then
  434. -- reduce
  435. powerup = 5
  436. powerup_t = 900
  437. elseif _p == 6 then
  438. -- megaball
  439. powerup = 6
  440. powerup_t = 0
  441. elseif _p == 7 then
  442. -- multiball
  443. powerup = 7
  444. powerup_t = 0
  445. end
  446. end
  447.  
  448. function hitbrick(_i,_combo)
  449. if brick_t[_i]=="b" then
  450. sfx(2+chain)
  451. brick_v[_i]=false
  452. if _combo then
  453. points+=10*chain*pointsmult
  454. chain+=1
  455. chain=mid(1,chain,7)
  456. end
  457. elseif brick_t[_i]=="i" then
  458. sfx(10)
  459. elseif brick_t[_i]=="h" then
  460. sfx(10)
  461. brick_t[_i]="b"
  462. elseif brick_t[_i]=="p" then
  463. sfx(2+chain)
  464. brick_v[_i]=false
  465. if _combo then
  466. points+=10*chain*pointsmult
  467. chain+=1
  468. chain=mid(1,chain,7)
  469. end
  470. spawnpill(brick_x[_i],brick_y[_i])
  471. elseif brick_t[_i]=="s" then
  472. sfx(2+chain)
  473. brick_t[_i]="zz"
  474. if _combo then
  475. points+=10*chain*pointsmult
  476. chain+=1
  477. chain=mid(1,chain,7)
  478. end
  479. end
  480. end
  481.  
  482. function spawnpill(_x,_y)
  483. local _t
  484.  
  485. _t = flr(rnd(7))+1
  486. _t = 5
  487. add(pill_x,_x)
  488. add(pill_y,_y)
  489. add(pill_v,true)
  490. add(pill_t,_t)
  491.  
  492. --pill_x[#pill_x+1]=_x
  493. --pill_y[#pill_x]=_y
  494. --pill_v[#pill_x]=true
  495. --pill_t[#pill_x]=_t
  496. end
  497.  
  498. function checkexplosions()
  499. for i=1,#brick_x do
  500. if brick_t[i] == "zz" then
  501. brick_t[i]="z"
  502. end
  503. end
  504.  
  505. for i=1,#brick_x do
  506. if brick_t[i] == "z" then
  507. explodebrick(i)
  508. end
  509. end
  510.  
  511. for i=1,#brick_x do
  512. if brick_t[i] == "zz" then
  513. brick_t[i]="z"
  514. end
  515. end
  516. end
  517.  
  518. function explodebrick(_i)
  519. brick_v[_i]=false
  520. for j=1,#brick_x do
  521. if j!=_i
  522. and brick_v[j]
  523. and abs(brick_x[j]-brick_x[_i]) <= (brick_w+2)
  524. and abs(brick_y[j]-brick_y[_i]) <= (brick_h+2)
  525. then
  526. hitbrick(j,false)
  527. end
  528. end
  529.  
  530. end
  531.  
  532. function _draw()
  533. if mode=="game" then
  534. draw_game()
  535. elseif mode=="start" then
  536. draw_start()
  537. elseif mode=="gameover" then
  538. draw_gameover()
  539. elseif mode=="levelover" then
  540. draw_levelover()
  541. end
  542. end
  543.  
  544. function draw_start()
  545. cls()
  546. print("pico hero breakout",30,40,7)
  547. print("press — to start",32,80,11)
  548. end
  549.  
  550. function draw_gameover()
  551. rectfill(0,60,128,75,0)
  552. print("game over",46,62,7)
  553. print("press — to restart",27,68,6)
  554. end
  555.  
  556. function draw_levelover()
  557. rectfill(0,60,128,75,0)
  558. print("stage clear!",46,62,7)
  559. print("press — to continue",27,68,6)
  560. end
  561.  
  562. function draw_game()
  563. local i
  564.  
  565. cls(1)
  566. circfill(ball_x,ball_y,ball_r, 10)
  567. if sticky then
  568. -- serve preview
  569. line(ball_x+ball_dx*4,ball_y+ball_dy*4,ball_x+ball_dx*6,ball_y+ball_dy*6,10)
  570. end
  571.  
  572. rectfill(pad_x,pad_y,pad_x+pad_w,pad_y+pad_h,pad_c)
  573.  
  574. --draw bricks
  575. for i=1,#brick_x do
  576. if brick_v[i] then
  577. if brick_t[i] == "b" then
  578. brickcol = 14
  579. elseif brick_t[i] == "i" then
  580. brickcol = 6
  581. elseif brick_t[i] == "h" then
  582. brickcol = 15
  583. elseif brick_t[i] == "s" then
  584. brickcol = 9
  585. elseif brick_t[i] == "p" then
  586. brickcol = 12
  587. elseif brick_t[i] == "z" or brick_t[i] == "zz" then
  588. brickcol = 8
  589. end
  590. rectfill(brick_x[i],brick_y[i],brick_x[i]+brick_w,brick_y[i]+brick_h,brickcol)
  591. end
  592. end
  593.  
  594. for i=1,#pill_x do
  595. if pill_v[i] then
  596. if pill_t[i]==5 then
  597. palt(0,false)
  598. palt(15,true)
  599. end
  600. spr(pill_t[i],pill_x[i],pill_y[i])
  601. palt()
  602. end
  603. end
  604.  
  605. rectfill(0,0,128,6,0)
  606. if debug!="" then
  607. print(debug,1,1,7)
  608. else
  609. print("lives:"..lives,1,1,7)
  610. print("score:"..points,40,1,7)
  611. print("chain:"..chain.."x",100,1,7)
  612. end
  613. end
  614.  
  615. function ball_box(bx,by,box_x,box_y,box_w,box_h)
  616. -- checks for a collion of the ball with a rectangle
  617. if by-ball_r > box_y+box_h then return false end
  618. if by+ball_r < box_y then return false end
  619. if bx-ball_r > box_x+box_w then return false end
  620. if bx+ball_r < box_x then return false end
  621. return true
  622. end
  623.  
  624. function box_box(box1_x,box1_y,box1_w,box1_h,box2_x,box2_y,box2_w,box2_h)
  625. -- checks for a collion of the two boxes
  626. if box1_y > box2_y+box2_h then return false end
  627. if box1_y+box1_h < box2_y then return false end
  628. if box1_x > box2_x+box2_w then return false end
  629. if box1_x+box1_w < box2_x then return false end
  630. return true
  631. end
  632.  
  633. function deflx_ball_box(bx,by,bdx,bdy,tx,ty,tw,th)
  634. local slp = bdy / bdx
  635. local cx, cy
  636. if bdx == 0 then
  637. return false
  638. elseif bdy == 0 then
  639. return true
  640. elseif slp > 0 and bdx > 0 then
  641. cx = tx - bx
  642. cy = ty - by
  643. return cx > 0 and cy/cx < slp
  644. elseif slp < 0 and bdx > 0 then
  645. cx = tx - bx
  646. cy = ty + th - by
  647. return cx > 0 and cy/cx >= slp
  648. elseif slp > 0 and bdx < 0 then
  649. cx = tx + tw - bx
  650. cy = ty + th - by
  651. return cx < 0 and cy/cx <= slp
  652. else
  653. cx = tx + tw - bx
  654. cy = ty - by
  655. return cx < 0 and cy/cx >= slp
  656. end
  657. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement