tempest.bmx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. 'Tempest
  2. 'Coded by David Bird
  3. Strict
  4. Const CWidth#=640
  5. Const CHeight#=480
  6. Const K# = 50
  7. Global CCenterX#=CWidth/2.0
  8. Global CCenterY#=(CHeight/3.0)'*2
  9. Global SHOT_LIST:TList = New TList
  10. Global theLevel:Level
  11. Function TFormSZ#(x#, z#)
  12. z:+5
  13. Return (x/(z/K))
  14. EndFunction
  15. Function TForm(x#, y#, z#, x2d# Var, y2d# Var )
  16. z:+5
  17. y:+100
  18. x2d = CCenterX+(x/(z/K))
  19. y2d = CCenterY+(y/(z/K))
  20. EndFunction
  21. 'Setup Graphics mode
  22. Graphics CWidth,CHeight,32
  23. HideMouse
  24. Global MainPlayer:Player
  25. Type Player
  26. Field e_Index 'the edge the player is on
  27. Field zPos
  28. Field scl#=0.5 'where on the edge 0-1
  29. Method SetEdge(index)
  30. e_index = index
  31. EndMethod
  32. Method AddShot()
  33. Shot.Create(theLevel.edges[e_Index])
  34. EndMethod
  35. Method ShiftLeft()
  36. If e_Index=0
  37. e_Index = theLevel.e_Cnt-1
  38. Else
  39. e_Index:-1
  40. EndIf
  41. EndMethod
  42. Method ShiftRight()
  43. If e_Index=theLevel.e_cnt-1
  44. e_Index = 0
  45. Else
  46. e_Index:+1
  47. EndIf
  48. EndMethod
  49. Method Update()
  50. 'Control it
  51. If KeyHit(KEY_SPACE) self.AddShot()
  52. If KeyDown(KEY_LEFT)
  53. scl:-0.1
  54. If scl<0 Then
  55. self.ShiftLeft()
  56. scl:+1
  57. EndIf
  58. EndIf
  59. If KeyDown(KEY_RIGHT)
  60. scl:+0.1
  61. If scl>1 Then
  62. self.ShiftRight()
  63. scl:-1
  64. EndIf
  65. EndIf
  66. SetRotation 0
  67. 'Draw it
  68. SetColor 255,255,0
  69. Local zz#
  70. Local x#[3],y#[3]
  71. Select theLevel.state
  72. Case Level_Begin
  73. zz# = theLevel.position
  74. Case Level_Ready
  75. zpos=theLevel.position
  76. zz = zpos
  77. Case Level_Complete
  78. zz = zpos
  79. EndSelect
  80. Local zh# = 4
  81. Local e:Edge = theLevel.edges[e_Index]
  82. TForm e.p1.x, e.p1.y, zz,x[0],y[0]
  83. TForm e.p2.x+ ( (e.p1.x - e.p2.x) * scl ), e.p2.y+ ( (e.p1.y - e.p2.y) * scl ),zz-zh,x[1],y[1]
  84. TForm e.p2.x, e.p2.y, zz,x[2],y[2]
  85. DrawLine x[0],y[0],x[1],y[1]
  86. DrawLine x[1],y[1],x[2],y[2]
  87. EndMethod
  88. Function Create:Player()
  89. Local p:Player = New Player
  90. Return p
  91. EndFunction
  92. EndType
  93. Type Point
  94. Field x#,y#
  95. Field e0:edge
  96. Field e1:edge
  97. Function Create:Point( x#, y# )
  98. Local p:Point = New Point
  99. p.x=x
  100. p.y=y
  101. Return p
  102. EndFunction
  103. EndType
  104. Type Edge
  105. Field p1:point
  106. Field p2:point
  107. Field xx#,yy#
  108. Method Draw( zd1#, zd2# )
  109. If zd1<1 zd1=1
  110. If zd2<1 zd2=1
  111. 'draw the edge at zero position,
  112. 'the depth line and the far point
  113. Local x#[4],y#[4]
  114. TForm p1.x,p1.y,zd1, x[0],y[0]
  115. TForm p1.x,p1.y,zd2, x[1],y[1]
  116. TForm p2.x,p2.y,zd1, x[2],y[2]
  117. TForm p2.x,p2.y,zd2, x[3],y[3]
  118. DrawLine x[0],y[0],x[1],y[1]
  119. DrawLine x[0],y[0],x[2],y[2]
  120. DrawLine x[1],y[1],x[3],y[3]
  121. DrawLine x[3],y[3],x[2],y[2]
  122. EndMethod
  123. Function Create:Edge(p1:Point, p2:Point)
  124. Local e:Edge = New edge
  125. 'assign the points
  126. e.p1=p1
  127. e.p2=p2
  128. 'linkem up
  129. p1.e1=e
  130. p2.e0=e
  131. 'store the midpoint for speeding up
  132. e.xx =( ( p2.x - p1.x ) / 2.0 ) + p1.x
  133. e.yy =( ( p2.y - p1.y ) / 2.0 ) + p1.y
  134. Return e
  135. EndFunction
  136. EndType
  137. Const Level_Begin = 0
  138. Const Level_Complete = 1
  139. Const Level_Ready = 2
  140. Type Level
  141. Field depth# = 400
  142. Field position# = 1500
  143. Field move# = 0
  144. Field state = Level_Begin
  145. Field points:TList
  146. Field edges:Edge[10],e_cnt,e_cap=10
  147. Method AddPoint:Point(x#,y#)
  148. Local p:Point = Point.Create( x, y )
  149. points.AddLast( p )
  150. Return p
  151. EndMethod
  152. Method AddEdge:Edge( p1:Point, p2:point )
  153. Local e:Edge = Edge.Create( p1, p2 )
  154. If e_cnt>=e_cap
  155. e_cap:+10
  156. edges=edges[..e_cap]
  157. EndIf
  158. edges[e_cnt] = e
  159. e_cnt:+1
  160. Return e
  161. EndMethod
  162. Method Update()
  163. Select state
  164. Case Level_Begin
  165. If position>50
  166. position:-10
  167. Else
  168. state=Level_Ready
  169. EndIf
  170. Case Level_Ready
  171. Case Level_Complete
  172. position:-10
  173. EndSelect
  174. EndMethod
  175. Method Draw()
  176. Local a=0
  177. Select state
  178. Case Level_Begin
  179. Case Level_Ready
  180. Case Level_Complete
  181. EndSelect
  182. SetRotation 0
  183. SetColor 0,0,100
  184. For a=0 Until e_cnt
  185. edges[a].Draw(position,position+depth)
  186. Next
  187. EndMethod
  188. Function Create:Level()
  189. Local l:Level = New Level
  190. l.points = New TList
  191. Return l
  192. EndFunction
  193. EndType
  194. Type Shot
  195. Field e:edge ' the edge its on
  196. Field z# ' its position
  197. Field r# ' rotation
  198. Field xx#,yy#
  199. Method Draw()
  200. SetColor Rand(255),Rand(255),Rand(255)
  201. Local zz = z+theLevel.position
  202. Local sz = TFormSZ(10,zz)
  203. Local pxx#,pyy#
  204. TForm(xx,yy,zz,pxx,pyy)
  205. For Local a=0 Until 360 Step 45
  206. SetRotation r+a
  207. DrawLine pxx+sz,pyy,pxx-sz,pyy
  208. Next
  209. r:+15
  210. SetRotation 0
  211. EndMethod
  212. Method Update()
  213. z:+5
  214. Local bad:Baddies
  215. If z>theLevel.depth
  216. SHOT_LIST.Remove(Self)
  217. Return
  218. Else
  219. 'check for collisions
  220. For bad = EachIn BaddieList
  221. If bad.CheckColl(e,z)
  222. SHOT_LIST.Remove(Self)
  223. Return
  224. EndIf
  225. Next
  226. EndIf
  227. EndMethod
  228. Function Create:Shot(e:Edge)
  229. Local ns:Shot = New Shot
  230. ns.e = e
  231. ns.xx =e.xx
  232. ns.yy =e.yy
  233. ns.z = -5
  234. SHOT_LIST.AddLast( ns )
  235. Return ns
  236. EndFunction
  237. EndType
  238. Function UpdateShots()
  239. Local s:shot
  240. For s=EachIn SHOT_LIST
  241. s.Update
  242. Next
  243. For s=EachIn SHOT_LIST
  244. s.Draw()
  245. Next
  246. EndFunction
  247. Global BaddieList:Tlist = New TList
  248. Function UpdateBaddies()
  249. Local b:Baddies
  250. For b = EachIn Baddielist
  251. b.Update()
  252. Next
  253. For b = EachIn Baddielist
  254. b.Draw()
  255. Next
  256. EndFunction
  257. Type Baddies
  258. Field OnEdge:Edge
  259. Method Update() Abstract
  260. Method Draw() Abstract
  261. Method CheckColl(e:edge,z#) Abstract
  262. EndType
  263. Type Crawler Extends Baddies
  264. Field EdgeIndex 'used to traverse theLevel edgelist
  265. Field typ '0 just slide up the tube
  266. '1 rolls round the tube
  267. Field Pause 'the pause before changing edge
  268. Field dir 'direction left or right
  269. Field angle 'the angle when changing lanes
  270. Method Update()
  271. EndMethod
  272. Method Draw()
  273. EndMethod
  274. Method CheckColl(e:edge,z#)
  275. EndMethod
  276. Function Create:Crawler(index,typ = 0)
  277. Local c:Crawler = New Crawler
  278. c.OnEdge = theLevel.edges[index]
  279. c.typ = typ
  280. BaddieList.AddLast c
  281. Return c
  282. EndFunction
  283. EndType
  284. Type Spikes Extends Baddies
  285. Field height#=0
  286. Field grow_speed#
  287. Method CheckColl(e:edge,z#)
  288. 'check to see if any will hit
  289. Local sp# = theLevel.Depth-height
  290. If e = OnEdge
  291. If z>sp
  292. height:-40
  293. If height<0
  294. BaddieList.Remove(Self)
  295. Return True
  296. EndIf
  297. Return True
  298. EndIf
  299. EndIf
  300. Return False
  301. EndMethod
  302. Method Draw()
  303. Local xx#,yy#,zz1#,zz2#
  304. zz1 = theLevel.position+theLevel.depth
  305. zz2 = zz1 - height
  306. Local x#[2],y#[2]
  307. xx =( ( OnEdge.p2.x - OnEdge.p1.x ) / 2.0 ) + OnEdge.p1.x
  308. yy =( ( OnEdge.p2.y - OnEdge.p1.y ) / 2.0 ) + OnEdge.p1.y
  309. TForm(xx,yy,zz1,x[0],y[0])
  310. TForm(xx,yy,zz2,x[1],y[1])
  311. SetColor 0,255,0
  312. DrawLine x[0],y[0],x[1],y[1]
  313. SetColor 255,0,0
  314. Plot x[1],y[1]
  315. EndMethod
  316. Method Update()
  317. If height<theLevel.Depth
  318. height:+grow_speed
  319. Else
  320. height = theLevel.Depth
  321. EndIf
  322. EndMethod
  323. Function Create:Spikes(index,speed#)
  324. Local s:Spikes = New Spikes
  325. s.OnEdge=theLevel.Edges[index]
  326. s.height=10
  327. s.grow_speed# = speed
  328. BaddieList.AddLast s
  329. Return s
  330. EndFunction
  331. EndType
  332. Local a
  333. Local p1:Point
  334. Local p2:Point
  335. Local fp:Point
  336. theLevel = Level.Create()
  337. MainPlayer = Player.Create()
  338. For a=0 Until 360 Step 30
  339. p1 = theLevel.AddPoint( Cos(a)*280, Sin(a)*180 )
  340. If p2
  341. theLevel.AddEdge(p1,p2)
  342. Else
  343. fp = p1
  344. EndIf
  345. p2=p1
  346. Next
  347. Local lastedge:edge = theLevel.AddEdge(fp,p2)
  348. MainPlayer.SetEdge( 0 )
  349. For a=0 Until 10
  350. spikes.Create( a ,Rnd(0.5,1.0))
  351. Next
  352. 'main loop
  353. While Not KeyDown(KEY_ESCAPE)
  354. Cls
  355. theLevel.Update()
  356. theLevel.Draw()
  357. UpdateShots()
  358. UpdateBaddies()
  359. MainPlayer.Update()
  360. Flip
  361. Wend
  362. End