breakout.bmx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. Strict
  2. Const WIDTH = 640,HEIGHT = 480, DEPTH = 32
  3. Const ShadowOn = 1
  4. Const ShadowSize = 10
  5. Global gtime
  6. Global Pipes_img:TImage
  7. Global Tiles_img:TImage
  8. Global logo_img:TImage
  9. Global paddle:TImage
  10. Global ballvis:TImage
  11. 'Setup the level
  12. Global Tilelist:TList
  13. Global Balllist:TList
  14. Global playerX#,PlayerY#
  15. Global Score
  16. Private
  17. Global ballcount=0
  18. Function Minf#(a#,b#)
  19. If a<b Return a
  20. Return b
  21. EndFunction
  22. Function Maxf#(a#,b#)
  23. If a>b Return a
  24. Return b
  25. EndFunction
  26. Public
  27. Type ball
  28. Field x#,y#
  29. Field dx#,dy#,spd#,rot#=0
  30. Field visual
  31. Method Update()
  32. x:+ (dx * spd)
  33. y:+ (dy * spd)
  34. If x<34 Or x>606
  35. dx=-dx
  36. EndIf
  37. If y<50
  38. dy=-dy
  39. EndIf
  40. If y>Height-8
  41. ballcount:-1
  42. BallList.Remove(Self)
  43. Else
  44. If dy>0
  45. If y>playery-8
  46. If x>playerx-32 And x<playerx+32
  47. dy=dy*-1
  48. EndIf
  49. EndIf
  50. EndIf
  51. rot:+10
  52. EndIf
  53. EndMethod
  54. Method Draw(offx,offy)
  55. SetRotation rot
  56. DrawImage ballvis,x+offx,y+offy
  57. SetRotation 0
  58. EndMethod
  59. Function Create:Ball(x=Width/2 , y=Height/2)
  60. Local b:Ball = New Ball
  61. ballcount:+1
  62. b.x = x
  63. b.y = y
  64. b.dx = Rnd(-2, 2)
  65. b.dy = Rnd(-2, 2)
  66. b.spd = 4'0.1
  67. Return b
  68. EndFunction
  69. EndType
  70. 'all tiles are a standard size so
  71. Type Tile
  72. Field x#,y#
  73. Field typ = 0
  74. Field state = 0
  75. Field rot#=0,size#=1
  76. Method Draw(offx,offy)
  77. Select state
  78. Case 0
  79. SetRotation rot
  80. If size>1
  81. SetScale size,size
  82. size=size*0.9
  83. Else
  84. size = 1
  85. SetScale 0.95+(0.05*Cos(gTime)),0.95+(0.05*Sin(gTime))
  86. EndIf
  87. Case 1
  88. SetRotation rot
  89. SetScale size,size
  90. EndSelect
  91. Select typ
  92. Case 0
  93. DrawImage tiles_img,x+offx,y+offy+(2*Sin(gtime)),0
  94. Case 1
  95. DrawImage tiles_img,x+offx,y+offy+(2*Sin(gtime)),1
  96. Case 2
  97. DrawImage tiles_img,x+offx,y+offy+(2*Sin(gtime)),2
  98. Case 3
  99. DrawImage tiles_img,x+offx,y+offy+(2*Sin(gtime)),3
  100. Case 4
  101. DrawImage tiles_img,x+offx,y+offy+(2*Sin(gtime)),4
  102. EndSelect
  103. SetScale 1,1
  104. SetRotation 0
  105. EndMethod
  106. Method Update()
  107. Local c
  108. Local b:Ball
  109. If state = 0
  110. 'Check this tile for collision with all of the balls
  111. For b=EachIn BallList
  112. If b.x>x-4 And b.x<x+24
  113. If b.y>y-4 And b.y<y+24
  114. b.dy=-b.dy
  115. Select typ
  116. Case 1
  117. If ballcount=1
  118. For c=0 Until 2
  119. BallList.AddLast(ball.Create(b.x,b.y))
  120. Next
  121. EndIf
  122. state = 1
  123. size = 1
  124. Case 2
  125. typ = 3
  126. size=1.5
  127. Case 3
  128. typ = 4
  129. size=1.5
  130. Default
  131. Score:+((1+typ)*100)
  132. state = 1
  133. EndSelect
  134. Return
  135. EndIf
  136. EndIf
  137. Next
  138. Else
  139. y:+4
  140. rot:+5
  141. size:-.005
  142. If y>HEIGHT
  143. BallList.Remove(b)
  144. EndIf
  145. EndIf
  146. EndMethod
  147. Function Create:Tile(x=0,y=0,typ=0)
  148. Local t:Tile = New Tile
  149. t.x=x
  150. t.y=y
  151. t.typ = typ
  152. Return t
  153. EndFunction
  154. EndType
  155. Graphics WIDTH,HEIGHT,DEPTH
  156. AutoMidHandle True
  157. 'Media
  158. Global back:TImage[2]
  159. back[0] = LoadImage("media\back1.png")
  160. back[1] = LoadImage("media\back2.png")
  161. Pipes_img=LoadAnimImage("media\pipes.png",32,32,0,4)
  162. Tiles_img=LoadAnimImage("media\tiles.png",32,20,0,5)
  163. paddle = LoadImage("media\paddle.png")
  164. ballvis = LoadImage("media\ball.png")
  165. logo_img=LoadImage("media\B-Max.png")
  166. Tilelist:TList = New TList
  167. Balllist:TList = New TList
  168. playerX# = Width/2
  169. PlayerY# = Height-40
  170. Score=0
  171. ResetGame()
  172. HideMouse
  173. While Not KeyDown(KEY_ESCAPE)
  174. 'Update Players Position
  175. playerx = minf(574,maxf(64,MouseX()))
  176. 'Update Balls
  177. UpdateBalls()
  178. 'Update Tiles
  179. UpdateTiles()
  180. 'Draw Level
  181. DrawLevel()
  182. gTime:+10
  183. SetAlpha .75
  184. SetColor 0,0,255
  185. DrawRect 0,0,Width,20
  186. SetBlend ALPHABLEND
  187. SetAlpha 0.5
  188. SetColor 0,0,0
  189. DrawText "Score:"+Score,4,4
  190. SetAlpha 1
  191. SetColor 255,255,255
  192. DrawText "Score:"+Score+" "+ballcount,2,2
  193. Flip
  194. Wend
  195. End
  196. Function DrawLevel()
  197. Local w,aa#
  198. TileImage back[1],0,gTime/20
  199. SetBlend ALPHABLEND
  200. DrawImage logo_img,width/2,height/2
  201. aa#=0.5+(0.5*Cos(gtime/50))
  202. SetBlend AlphaBLEND
  203. SetAlpha aa
  204. TileImage back[0],0,gTime/10
  205. If ShadowOn
  206. SetColor 0,0,0
  207. SetBlend AlphaBLEND
  208. SetAlpha 0.5
  209. DrawPipes ShadowSize+16,ShadowSize+16
  210. DrawTiles ShadowSize+16,ShadowSize+10
  211. DrawPlayer ShadowSize,ShadowSize
  212. DrawBalls ShadowSize,ShadowSize
  213. EndIf
  214. SetColor 255,255,255
  215. SetBlend MASKBLEND
  216. SetAlpha 1
  217. DrawPipes()
  218. DrawTiles()
  219. DrawPlayer()
  220. DrawBalls()
  221. EndFunction
  222. Function ResetGame()
  223. TileList = New TList
  224. BallList = New TList
  225. Local x,y
  226. For y=0 Until 5
  227. For x=0 Until 18
  228. Tilelist.AddLast(Tile.Create(38+x*32,(y*24)+66,4-Y))
  229. Next
  230. Next
  231. BallList.AddLast(Ball.Create())
  232. EndFunction
  233. Function DrawPipes(x=16,y=16)
  234. Local tmp
  235. 'top
  236. For tmp=0 Until 18
  237. DrawImage Pipes_img,x+32+(tmp*32),y+16,3
  238. Next
  239. 'sides
  240. For tmp=0 Until 14
  241. DrawImage Pipes_img,x,y+48+(tmp*32),2
  242. DrawImage Pipes_img,x+Width-32,y+48+(tmp*32),2
  243. Next
  244. 'Corners
  245. DrawImage Pipes_img,x,y+16 ,0
  246. DrawImage Pipes_img,x+Width-32,y+16,1
  247. EndFunction
  248. Function DrawTiles(x_off=10, y_off=10)
  249. Local tl:Tile
  250. Local any=0
  251. For tl=EachIn TileList
  252. tl.Draw(x_off, y_off)
  253. any=1
  254. Next
  255. If Not any
  256. ResetGame()
  257. score:+10000
  258. EndIf
  259. EndFunction
  260. Function DrawBalls(x_off=0, y_off=0)
  261. Local bl:Ball
  262. For bl=EachIn balllist
  263. bl.Draw(x_off, y_off)
  264. Next
  265. EndFunction
  266. Function UpdateBalls()
  267. If ballcount=0
  268. BallList.AddLast(Ball.Create(Width/2,Height/2))
  269. Else
  270. Local bl:Ball
  271. For bl = EachIn BallList
  272. bl.Update()
  273. Next
  274. EndIf
  275. EndFunction
  276. Function UpdateTiles()
  277. Local tl:Tile
  278. For tl=EachIn tilelist
  279. tl.Update()
  280. Next
  281. EndFunction
  282. Function DrawPlayer(x_off=0,y_off=0)
  283. DrawImage paddle, playerx+x_off, playery+y_off
  284. End Function