insectoids.bb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. Global info1$="Insectoids!"
  2. Global info2$="Features texturing of 2D graphics"
  3. Include "../start.bb"
  4. image=CreateImage( 256,256 )
  5. texture=CreateTexture( 256,256 )
  6. cube=CreateCube()
  7. EntityTexture cube,texture
  8. EntityFX cube,1
  9. cube2=CopyEntity( cube )
  10. PositionEntity cube2,-2,0,0
  11. ScaleEntity cube2,.5,.5,.5
  12. cube3=CopyEntity( cube )
  13. PositionEntity cube3,2,0,0
  14. ScaleEntity cube3,.5,.5,.5
  15. camera=CreateCamera()
  16. PositionEntity camera,0,0,-3
  17. CameraClsColor camera,0,0,64
  18. AmbientLight 255,255,255
  19. Global buffer=ImageBuffer( image )
  20. Gosub SetupInsectoids
  21. While Not KeyHit(1)
  22. Gosub UpdateInsectoids
  23. SetBuffer buffer
  24. Gosub RenderInsectoids
  25. SetBuffer BackBuffer()
  26. CopyRect 0,0,256,256,0,0,ImageBuffer( image ),TextureBuffer( texture )
  27. If KeyDown(26) TurnEntity cube,0,-2,0
  28. If KeyDown(27) TurnEntity cube,0,+2,0
  29. If KeyDown(30) MoveEntity camera,0,0,.1
  30. If KeyDown(44) MoveEntity camera,0,0,-.1
  31. p.Player=First Player
  32. If p<>Null
  33. RotateEntity cube,0,(p\x-128)/10.0,0
  34. Else
  35. RotateEntity cube,0,0,0
  36. EndIf
  37. TurnEntity cube2,-1,0,.5
  38. TurnEntity cube3,1,0,-.5
  39. UpdateWorld
  40. RenderWorld
  41. Flip
  42. Wend
  43. End
  44. .SetupInsectoids
  45. Const width=256,height=256,num_rots=64
  46. Dim alien_rots(num_rots)
  47. Global game_state,game_timer,level_name$,alien_speed
  48. Global num_aliens,num_flying,fly_timer,num_bulls,num_players
  49. Global c_x#,c_y#,c_xs#,c_ys#,c_dir#,c_phase#,rev_dir,c_speed#,c_xsize#,c_ysize#
  50. Global player_image,stars_image,bomb_image,bull_image,stars_scroll,boom_anim
  51. Global mini_ship,tmp_image,insectoids_image
  52. Global boom_sound=LoadSound( "sounds\boom.wav" )
  53. Global cool_sound=LoadSound( "sounds\cool.wav" )
  54. Global kazap_sound=LoadSound( "sounds\kazap.wav" )
  55. Global shoot_sound=LoadSound( "sounds\shoot.wav" )
  56. Type Alien
  57. Field x#,y#,rot,state
  58. Field f_x#,f_y#
  59. Field dest_y#,dest_rot,rot_step,bomb_cnt
  60. End Type
  61. Type Player
  62. Field x,y,state,lives,ctrl,bang,score
  63. End Type
  64. Type Bomb
  65. Field x#,y#,xs#,ys#
  66. End Type
  67. Type Bullet
  68. Field x,y
  69. End Type
  70. Type Explosion
  71. Field x,y,frame
  72. End Type
  73. SetFont LoadFont( "arial",12,True )
  74. tmp_image=CreateImage( width,FontHeight() )
  75. LoadGraphics()
  76. game_state=0
  77. game_timer=0
  78. Return
  79. .UpdateInsectoids
  80. UpdateGame()
  81. Return
  82. .RenderInsectoids
  83. RenderGame()
  84. Return
  85. Function CreatePlayer( ctrl )
  86. p.Player=New Player
  87. p\lives=3
  88. p\ctrl=ctrl
  89. ResetPlayer( p )
  90. num_players=num_players+1
  91. End Function
  92. Function ResetPlayer( p.Player )
  93. p\x=width/2
  94. p\y=height-20
  95. p\state=1
  96. End Function
  97. Function UpdatePlayer( p.Player )
  98. If p\state<>1 Then Return
  99. Local l,r,f
  100. Select p\ctrl
  101. Case 1
  102. l=KeyDown( 203 )
  103. r=KeyDown( 205 )
  104. f=KeyHit( 57 )
  105. Case 2
  106. ; jx=JoyXDir()
  107. If jx<0 Then l=1 Else If jx>0 Then r=1
  108. ; f=JoyHit(1)
  109. End Select
  110. If l
  111. If p\x>16 Then p\x=p\x-4
  112. Else If r
  113. If p\x<width-16 Then p\x=p\x+4
  114. EndIf
  115. If game_state<>2 Then Return
  116. If f And num_bulls<3
  117. PlaySound shoot_sound
  118. b.Bullet=New Bullet
  119. b\x=p\x:b\y=p\y-16
  120. num_bulls=num_bulls+1
  121. EndIf
  122. dead=False
  123. For a.Alien=Each Alien
  124. If ImagesOverlap( player_image,p\x,p\y,alien_rots( a\rot*num_rots/360 ),a\x,a\y )
  125. dead=True
  126. Exit
  127. EndIf
  128. Next
  129. For bb.Bomb=Each Bomb
  130. If ImagesOverlap( player_image,p\x,p\y,bomb_image,bb\x,bb\y )
  131. dead=True
  132. Exit
  133. EndIf
  134. Next
  135. If Not dead Then Return
  136. PlaySound boom_sound
  137. For k=-105 To 0 Step 15
  138. CreateExplosion( p\x+Rnd(-10,10),p\y+Rnd(-10,10),k )
  139. Next
  140. p\bang=1
  141. p\state=2
  142. p\lives=p\lives-1
  143. game_state=3
  144. End Function
  145. Function RenderPlayer( p.Player )
  146. Color 255,255,255
  147. Text width/2,4,p\score,1
  148. For k=1 To p\lives
  149. DrawImage mini_ship,k*12+4,14
  150. Next
  151. If p\state=1
  152. DrawImage player_image,p\x,p\y
  153. Return
  154. EndIf
  155. p\bang=p\bang+8
  156. r#=p\bang
  157. For i=255 To 1 Step -15
  158. Color i,i,i
  159. For an=0 To 359 Step 6
  160. x=p\x+Cos(an)*r
  161. y=p\y+Sin(an)*r
  162. Rect x,y,3,3
  163. Next
  164. r=r-6:If r<=0 Then Exit
  165. Next
  166. End Function
  167. Function AddPoints( p.Player,pnts )
  168. t=p\score/5000
  169. p\score=p\score+pnts
  170. If p\score/5000<>t Then p\lives=p\lives+1
  171. End Function
  172. Function UpdateBullet( b.Bullet )
  173. b\y=b\y-5
  174. For a.Alien=Each Alien
  175. If ImagesOverlap( bull_image,b\x,b\y,alien_rots( a\rot*num_rots/360 ),a\x,a\y )
  176. PlaySound kazap_sound
  177. If a\state=1
  178. pnts=25
  179. Else If a\state=2
  180. pnts=50
  181. Else
  182. pnts=100
  183. EndIf
  184. AddPoints( First Player,pnts )
  185. CreateExplosion( a\x,a\y )
  186. Delete b:num_bulls=num_bulls-1
  187. If a\state<>1 num_flying=num_flying-1
  188. num_aliens=num_aliens-1
  189. Delete a
  190. Return
  191. EndIf
  192. Next
  193. If b\y>0 Then Return
  194. Delete b:num_bulls=num_bulls-1
  195. End Function
  196. Function RenderBullet( b.Bullet )
  197. DrawImage bull_image,b\x,b\y
  198. End Function
  199. Function UpdateBomb( b.Bomb )
  200. b\x=b\x+b\xs
  201. b\y=b\y+b\ys
  202. If b\y>height Then Delete b
  203. End Function
  204. Function RenderBomb( b.Bomb )
  205. DrawImage bomb_image,b\x,b\y
  206. End Function
  207. Function UpdateAlien( a.Alien )
  208. Select a\state
  209. Case 1
  210. If a\rot<>0
  211. If a\rot>180 a\rot=a\rot+6 Else a\rot=a\rot-6
  212. If a\rot<0 Or a\rot>=360 Then a\rot=0
  213. EndIf
  214. dx=c_x+a\f_x*c_xs - a\x
  215. dy=c_y+a\f_y*c_ys - a\y
  216. If dx<-alien_speed Then dx=-alien_speed Else If dx>alien_speed Then dx=alien_speed
  217. If dy<-alien_speed Then dy=-alien_speed Else If dy>alien_speed Then dy=alien_speed
  218. a\x=a\x+dx:a\y=a\y+dy
  219. If c_dir<0 And a\x<16 Then rev_dir=True
  220. If c_dir>0 And a\x>width-16 Then rev_dir=True
  221. Case 2
  222. a\rot=a\rot+a\rot_step
  223. If a\rot<0 Then a\rot=a\rot+360 Else If a\rot>=360 Then a\rot=a\rot-360
  224. If a\rot<90 Or a\rot>270
  225. a\dest_rot=Rnd(180-40,180+40)
  226. a\dest_y=a\dest_y+Rnd( 100,300 )
  227. a\state=3
  228. EndIf
  229. a\x=a\x+Cos( a\rot-90 )*alien_speed
  230. a\y=a\y+Sin( a\rot-90 )*alien_speed
  231. DropBomb( a )
  232. Case 3
  233. dr=a\rot-a\dest_rot
  234. If Abs(dr)>Abs(a\rot_step)
  235. a\rot=a\rot+a\rot_step
  236. If a\rot<0 Then a\rot=a\rot+360 Else If a\rot>=360 Then a\rot=a\rot-360
  237. EndIf
  238. a\x=a\x+Cos( a\rot-90 )*alien_speed
  239. a\y=a\y+Sin( a\rot-90 )*alien_speed
  240. If a\y>height
  241. a\x=Rnd(width/2)+width/4:a\y=0
  242. num_flying=num_flying-1
  243. a\state=1
  244. Else If a\y>a\dest_y
  245. a\rot_step=-a\rot_step
  246. a\state=2
  247. EndIf
  248. DropBomb( a )
  249. End Select
  250. End Function
  251. Function RenderAlien( a.Alien )
  252. DrawImage alien_rots( a\rot*num_rots/360 ),a\x,a\y
  253. End Function
  254. Function UpdateExplosion( e.Explosion )
  255. e\frame=e\frame+1
  256. If e\frame=18 Then Delete e
  257. End Function
  258. Function RenderExplosion( e.Explosion )
  259. If e\frame<0 Then Return
  260. DrawImage boom_anim,e\x,e\y,e\frame/3
  261. End Function
  262. Function CreateExplosion( x,y,frame=0 )
  263. e.Explosion=New Explosion
  264. e\x=x:e\y=y:e\frame=frame
  265. End Function
  266. Function DropBomb( a.Alien )
  267. If a\bomb_cnt=0 Then a\bomb_cnt=Rnd(50,100)
  268. a\bomb_cnt=a\bomb_cnt-1
  269. If a\bomb_cnt>0 Then Return
  270. p.Player=First Player
  271. If p=Null Then Return
  272. b.Bomb=New Bomb
  273. b\x=a\x
  274. b\y=a\y
  275. If a\x<p\x Then b\xs=1 Else b\xs=-1
  276. b\ys=4
  277. End Function
  278. Function UpdateFormation()
  279. c_phase=(c_phase+c_speed)Mod 360
  280. t#=Sin( c_phase )*.5+.5
  281. c_xs=t*c_xsize+1:c_ys=t*c_ysize+1
  282. If game_state<>1 Then c_x=c_x+c_dir
  283. End Function
  284. Function UpdateFlyTimer()
  285. If num_aliens>3
  286. If fly_timer=0 Then fly_timer=600
  287. fly_timer=fly_timer-1
  288. If fly_timer>120 Then Return
  289. If fly_timer Mod 30<>0 Then Return
  290. EndIf
  291. n=Rnd( num_aliens-num_flying )
  292. For a.Alien=Each Alien
  293. If a\state=1
  294. If n=0
  295. a\dest_y=a\y
  296. a\rot_step=3:If Rnd(1)<.5 Then a\rot_step=-3
  297. num_flying=num_flying+1
  298. a\state=2
  299. Return
  300. EndIf
  301. n=n-1
  302. EndIf
  303. Next
  304. End Function
  305. Function UpdateGame()
  306. Select game_state
  307. Case 0
  308. game_timer=game_timer+1
  309. If KeyHit(57) Then BeginGame()
  310. Case 1
  311. game_timer=game_timer+1
  312. If game_timer=150 Then game_state=2
  313. UpdateFormation()
  314. Case 2
  315. UpdateFlyTimer()
  316. UpdateFormation()
  317. If num_aliens=0 Then BeginLevel()
  318. Case 3
  319. UpdateFormation()
  320. If num_flying=0 And First Explosion=Null
  321. p.Player=First Player
  322. If p\lives>0
  323. ResetPlayer( p )
  324. game_state=2
  325. FlushKeys
  326. Else
  327. game_state=4
  328. game_timer=0
  329. EndIf
  330. EndIf
  331. Case 4
  332. UpdateFlyTimer()
  333. UpdateFormation()
  334. game_timer=game_timer+1
  335. If game_timer=150 Then EndGame()
  336. End Select
  337. rev_dir=False
  338. For a.Alien=Each Alien
  339. UpdateAlien( a )
  340. Next
  341. If rev_dir Then c_dir=-c_dir
  342. For bb.Bomb=Each Bomb
  343. UpdateBomb( bb )
  344. Next
  345. For p.Player=Each Player
  346. UpdatePlayer( p )
  347. Next
  348. For b.Bullet=Each Bullet
  349. UpdateBullet( b )
  350. Next
  351. For e.Explosion=Each Explosion
  352. UpdateExplosion( e )
  353. Next
  354. End Function
  355. Function RenderGame()
  356. TileBlock stars_image,0,stars_scroll
  357. TileImage stars_image,7,stars_scroll*2
  358. TileImage stars_image,23,stars_scroll*3
  359. stars_scroll=(stars_scroll+1) Mod ImageHeight( stars_image )
  360. For a.Alien=Each Alien
  361. RenderAlien( a )
  362. Next
  363. For bb.Bomb=Each Bomb
  364. RenderBomb( bb )
  365. Next
  366. For p.Player=Each Player
  367. RenderPlayer( p )
  368. Next
  369. For b.Bullet=Each Bullet
  370. RenderBullet( b )
  371. Next
  372. For e.Explosion=Each Explosion
  373. RenderExplosion( e )
  374. Next
  375. Select game_state
  376. Case 0
  377. DrawImage insectoids_image,width/2,height/3
  378. If game_timer<150 Or (game_timer-150) Mod 80<40
  379. Color 255,255,255:TitleText( "PRESS SPACE TO START",width/2,height-FontHeight()*2,game_timer )
  380. EndIf
  381. Case 1
  382. Rainbow( game_timer*5 )
  383. TitleText( level_name$,width/2,height/2,game_timer*2 )
  384. Case 4
  385. Color 255,255,255
  386. Text width/2,height/2,"GAME OVER",1,1
  387. End Select
  388. End Function
  389. Function TitleText( txt$,x,y,time )
  390. n=0
  391. If time<100 Then n=100-time
  392. If n=1
  393. Text x,y,txt$,1,1
  394. Else
  395. ExplodeText( x,y,txt$,n*.5+1,n+1 )
  396. EndIf
  397. End Function
  398. Function LoadGraphics()
  399. AutoMidHandle True
  400. stars_image=LoadImage( "graphics\stars.bmp" )
  401. player_image=LoadImage( "graphics\player.bmp" ):ScaleImage player_image,.5,.5
  402. mini_ship=CopyImage( player_image ):ScaleImage mini_ship,.4,.4
  403. bull_image=LoadImage( "graphics\bullet.bmp" ):ScaleImage bull_image,.25,.5
  404. bomb_image=LoadImage( "graphics\bbomb.bmp" ):ScaleImage bomb_image,.3,.3
  405. boom_anim=LoadAnimImage( "graphics\kaboom.bmp",60,48,0,6 )
  406. insectoids_image=LoadImage( "graphics\insectoids_logo.bmp" )
  407. ResizeImage insectoids_image,256,64
  408. ResizeImage boom_anim,8,8:ResizeImage boom_anim,16,16
  409. i=LoadImage( "graphics\alien.bmp" ):ScaleImage i,.3,.3
  410. For k=0 To num_rots-1
  411. alien_rots(k)=CopyImage( i )
  412. RotateImage alien_rots(k),k*360/num_rots
  413. Next
  414. End Function
  415. Function BeginLevel()
  416. PlaySound cool_sound
  417. Read level_name$
  418. If level_name$=""
  419. Restore .levels
  420. Read level_name$
  421. alien_speed=alien_speed+1
  422. If alien_speed>6 Then alien_speed=6
  423. EndIf
  424. c_x=width/2:c_y=height/4:c_phase=0:c_dir=1
  425. Read c_speed,c_xsize,c_ysize
  426. Repeat
  427. Read x
  428. If x=999 Then Exit
  429. Read y,cnt
  430. For k=1 To cnt
  431. a.Alien=New Alien
  432. a\x=c_x
  433. a\y=c_y
  434. a\rot=0
  435. a\state=1
  436. a\f_x=x*16
  437. a\f_y=y*12
  438. x=x+1
  439. Next
  440. num_aliens=num_aliens+cnt
  441. Forever
  442. game_state=1
  443. game_timer=0
  444. End Function
  445. Function BeginGame()
  446. level=0
  447. num_bulls=0
  448. num_aliens=0
  449. num_flying=0
  450. game_state=0
  451. num_players=0
  452. alien_speed=3
  453. CreatePlayer( 1 )
  454. Restore .levels
  455. BeginLevel()
  456. FlushKeys
  457. End Function
  458. Function EndGame()
  459. Delete Each Player
  460. Delete Each Bullet
  461. Delete Each Alien
  462. Delete Each Bomb
  463. game_state=0
  464. game_timer=0
  465. FlushKeys
  466. End Function
  467. Function Rainbow( time )
  468. r=time Mod 768:If r>255 Then r=511-r
  469. g=(time+256)Mod 768:If g>255 Then g=511-g
  470. b=(time+512) Mod 768:If b>255 Then b=511-b
  471. If r<0 Then r=0
  472. If g<0 Then g=0
  473. If b<0 Then b=0
  474. Color r,g,b
  475. End Function
  476. Function ExplodeText( x,y,txt$,xn#,yn# )
  477. w=StringWidth( txt$ )
  478. h=FontHeight()
  479. SetBuffer ImageBuffer( tmp_image )
  480. Cls:Text 0,0,txt$
  481. SetBuffer buffer
  482. For ty=0 To h-1 Step 4
  483. For tx=0 To w-1 Step 4
  484. xo#=(tx-w/2)*xn
  485. yo#=(ty-h/2)*yn
  486. DrawImageRect tmp_image,x+xo,y+yo,tx,ty,4,4
  487. Next
  488. Next
  489. End Function
  490. .levels
  491. Data "LEVEL 1"
  492. Data 1,.25,0
  493. Data -2,-2,5
  494. Data -3,-1,7
  495. Data -3,0,7
  496. Data -3,1,7
  497. Data -3,2,7
  498. Data 999
  499. Data "LEVEL 2"
  500. Data 1,.25,.25
  501. Data 0,-2,1
  502. Data -1,-1,3
  503. Data -2,0,5
  504. Data -3,1,7
  505. Data -4,2,9
  506. Data -5,3,11
  507. Data 999
  508. Data "LEVEL 3"
  509. Data 3,.25,.5
  510. Data -5,-2,11
  511. Data -4,-1,9
  512. Data -3,0,7
  513. Data -4,1,9
  514. Data -5,2,11
  515. Data 999
  516. Data "LEVEL 4"
  517. Data 2,0,1
  518. Data -5,-1,11
  519. Data -5,0,11
  520. Data -5,1,11
  521. Data -5,2,11
  522. Data -5,3,11
  523. Data -5,4,11
  524. Data -5,5,11
  525. Data 999
  526. Data "LEVEL 5"
  527. Data 1,.25,.125
  528. Data -3,-2,7
  529. Data -4,-1,9
  530. Data -5,0,11
  531. Data -5,1,11
  532. Data -5,2,11
  533. Data -5,3,11
  534. Data -5,4,11
  535. Data -5,5,11
  536. Data -5,6,11
  537. Data -5,7,11
  538. Data 999
  539. Data "LEVEL 6"
  540. Data 1,.25,.125
  541. Data -7,-2,15
  542. Data -7,-1,15
  543. Data -7,0,15
  544. Data -7,1,15
  545. Data -7,2,15
  546. Data -7,3,15
  547. Data -7,4,15
  548. Data -7,5,15
  549. Data -7,6,15
  550. Data -7,7,15
  551. Data -7,8,15
  552. Data -7,9,15
  553. Data -7,10,15
  554. Data -7,11,15
  555. Data 999
  556. Data ""