Guest User

Untitled

a guest
Jan 7th, 2018
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. function _init()
  2. cls()
  3. ball_x=3
  4. ball_dx=1
  5. ball_y=33
  6. ball_dy=1
  7. ball_r=2
  8.  
  9. pad_x=52
  10. pad_dx=0
  11. pad_y=120
  12. pad_w=30
  13. pad_h=3
  14. pad_c=7
  15. mode="start"
  16. lives=3
  17. serveball()
  18. points=0
  19. end
  20. function serveball()
  21. ball_x=3
  22. ball_dx=1
  23. ball_y=33
  24. ball_dy=1
  25. ball_r=2
  26. end
  27.  
  28. function gameover()
  29. mode="gameover"
  30. end
  31.  
  32. function _update60()
  33. if mode=="game" then
  34. update_game()
  35. elseif mode=="start" then
  36. update_start()
  37. elseif mode=="gameover" then
  38. update_gameover()
  39. end
  40. end
  41.  
  42. function update_start()
  43. if btn(5) then
  44. startgame()
  45. end
  46. end
  47. function startgame()
  48. mode="game"
  49. serveball()
  50. lives=3
  51. --sfx(4)
  52. --brick_y=20
  53. brick_w=14
  54. brick_h=3
  55. brick_c=14
  56. buildbricks()
  57. points=0
  58. end
  59. function buildbricks()
  60. local i
  61. brick_x={}
  62. brick_y={}
  63. brick_v={}
  64. for i=1,7 do
  65. add(brick_x,5+(i-1)*(brick_w+2))
  66. add(brick_y,20)
  67. add(brick_v,true)
  68. end
  69. end
  70.  
  71. function update_gameover()
  72. if btn(5) then
  73. startgame()
  74. end
  75. end
  76.  
  77. function update_game()
  78. local btnpress=false
  79. local nextx, nexty
  80. if btn(0)--right
  81. then
  82. pad_dx=-2.5
  83. btnpress=true
  84. end
  85. if btn(1)--left
  86. then
  87. pad_dx=2.5
  88. btnpress=true
  89. end
  90. pad_x+=pad_dx
  91. pad_x=mid(0,pad_x,127-pad_w)
  92. if not(btnpress)
  93. then
  94. pad_dx=pad_dx/1.4
  95. end
  96.  
  97. nextx=ball_x+ball_dx
  98. nexty=ball_y+ball_dy
  99.  
  100. if nextx > 124 or nextx < 3
  101. then
  102. nextx=mid(0,nextx,127)
  103. ball_dx= -ball_dx
  104. sfx(0)
  105. end
  106. if nexty < 10 or nexty > 124
  107. then
  108. nexty=mid(0,nexty,127)
  109. ball_dy = -ball_dy
  110. sfx(0)
  111. end
  112.  
  113. if ball_box (nextx,nexty,pad_x,pad_y,pad_w,pad_h,pad_c)
  114. then
  115. --collision of paddle
  116. if deflx_ballbox(ball_x,ball_y,ball_dx,ball_dy,pad_x,pad_y,pad_w,pad_h)then
  117. ball_dx = -ball_dx
  118. else
  119. ball_dy = -ball_dy
  120. end
  121. sfx(1)
  122. points+=1
  123. end
  124. for i=1,#brick_x do
  125.  
  126. if brick_v[i] and ball_box (nextx,nexty,brick_x[i],brick_y[i],brick_w,brick_h,brick_c)
  127. then
  128. --collision of bricks
  129. if deflx_ballbox(ball_x,ball_y,ball_dx,ball_dy,brick_x[i],brick_y[i],brick_w,brick_h)then
  130. ball_dx = -ball_dx
  131. else
  132. ball_dy = -ball_dy
  133. end
  134. sfx(5)
  135. brick_v[i]=false
  136. points+=10
  137. end
  138.  
  139. ball_x=nextx
  140. ball_y=nexty
  141.  
  142. if nexty > 124 then
  143. sfx(3)
  144. lives-=1
  145. if lives<0 then
  146. gameover()
  147. else
  148. serveball()
  149. end
  150. end
  151. end
  152.  
  153. end
  154. function _draw()
  155. if mode=="game" then
  156. draw_game()
  157. elseif mode=="start" then
  158. draw_start()
  159. elseif mode=="gameover" then
  160. draw_gameover()
  161. end
  162. end
  163.  
  164. function draw_start()
  165. cls()
  166. print("welcome to my firstgame",20,20,7)
  167. print("press ❎ to start",32,80,12)
  168. end
  169.  
  170. function draw_gameover()
  171. rectfill(0,60,128,76,0)
  172. print("game over",46,62,8)
  173. print("press ❎ to restart",26,68,7)
  174. --sfx(2)
  175. end
  176. function draw_game()
  177. cls(1)
  178. --balll
  179. circfill(ball_x,ball_y,ball_r,10)
  180. --paddle
  181. rectfill(pad_x,pad_y,pad_x+pad_w,pad_y+pad_h,pad_c)
  182. --brick
  183. for i=1,#brick_x do
  184. if brick_v[i] then
  185. rectfill(brick_x[i],brick_y[i],brick_x[i]+brick_w,brick_y[i]+brick_h,brick_c)
  186. end
  187. end
  188. if brick_v[i] then
  189. rectfill(brick_x[i],brick_y[i],brick_x[i]+brick_w,brick_y[i]+brick_h,brick_c)
  190. end
  191. rectfill(0,0,128,6,0)
  192. print("lives:"..lives,1,1,7)
  193. print("score:"..points,40,1,7)
  194. end
  195.  
  196. function ball_box(bx,by,box_x,box_y,box_w,box_h)
  197. if by-ball_r > box_y+box_h then return false end
  198. if by+ball_r < box_y then return false end
  199. if bx+ball_r < box_x then return false end
  200. if bx-ball_r > box_x+box_w then return false end
  201. return true
  202. end
  203.  
  204. function deflx_ballbox(bx,by,bdx,bdy,tx,ty,tw,th)
  205. -- calculate wether to deflect the ball
  206. -- horizontally or vertically when it hits a box
  207. if bdx == 0 then
  208. -- moving vertically
  209. return false
  210. elseif bdy == 0 then
  211. -- moving horizontally
  212. return true
  213. else
  214. -- moving diagonally
  215. -- calculate slope
  216. local slp = bdy / bdx
  217. local cx, cy
  218. -- check variants
  219. if slp > 0 and bdx > 0 then
  220. -- moving down right
  221. debug1="q1"
  222. cx = tx-bx
  223. cy = ty-by
  224. if cx<=0 then
  225. return false
  226. elseif cy/cx < slp then
  227. return true
  228. else
  229. return false
  230. end
  231. elseif slp < 0 and bdx > 0 then
  232. debug1="q2"
  233. -- moving up right
  234. cx = tx-bx
  235. cy = ty+th-by
  236. if cx<=0 then
  237. return false
  238. elseif cy/cx < slp then
  239. return false
  240. else
  241. return true
  242. end
  243. elseif slp > 0 and bdx < 0 then
  244. debug1="q3"
  245. -- moving left up
  246. cx = tx+tw-bx
  247. cy = ty+th-by
  248. if cx>=0 then
  249. return false
  250. elseif cy/cx > slp then
  251. return false
  252. else
  253. return true
  254. end
  255. else
  256. -- moving left down
  257. debug1="q4"
  258. cx = tx+tw-bx
  259. cy = ty-by
  260. if cx>=0 then
  261. return false
  262. elseif cy/cx < slp then
  263. return false
  264. else
  265. return true
  266. end
  267. end
  268. end
  269. return false
  270. end
Advertisement
Add Comment
Please, Sign In to add comment