Ooops
November 28, 2020, 01:40:26 AM

Author Topic: [bb] Topdown Space shooter Example by Pakz [ 1+ years ago ]  (Read 983 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Topdown Space shooter Example
Author : Pakz
Posted : 1+ years ago

Description :

Here a video of it in action : <a href="" target="_blank">[/url]

I made a very simple space shooter example. The ship is a square that rotates into the direction he is traveling in. You can shoot. The enemy turrets can shoot. The enemy ships can Shoot.
You have a hull strength of 10. If that is 0 then the game restarts. If you destroy the ships and the turrets then the game restarts to.

I hope someone finds it useable.


Code :
Code: BlitzBasic
  1. ; Topdown Space Shooter Example by Pakz (Rudy van Etten)
  2.  
  3. ; The map is drawn on a image.
  4. ; Every object needs to be moved with the player movement speed, this is done
  5. ; in the updatemap function.
  6.  
  7. Graphics 640,480,32,2
  8. SetBuffer BackBuffer()
  9. SeedRnd MilliSecs()
  10.  
  11. Const mapwidth = 39
  12. Const mapheight = 29
  13. Dim map(mapwidth,mapheight)
  14. Global mapimage = CreateImage(mapwidth*32+32,mapheight*32+32)
  15. Global mapx# = 0
  16. Global mapy# = 0
  17.  
  18. Global myfont = LoadFont("verdana.ttf",16)
  19. SetFont myfont
  20.  
  21. ; tx = the destination of the ship
  22. Type ship
  23.         Field x#,y#,mx#,my#,angle,tx,ty,shootdelaytime
  24.         Field hull
  25. End Type
  26.  
  27. Type sbullet ; ship bullets
  28.         Field x#,y#,mx#,my#,dtraveled
  29. End Type
  30.  
  31.  
  32. Type turret ; the turrets
  33.         Field x#,y#,angle,shootdelaytime,hull
  34. End Type
  35.  
  36. Type tbullet ; turret bullets
  37.         Field x#,y#,mx#,my#
  38.         Field dtraveled ; Distance traveled by the bullet
  39. End Type
  40.  
  41. Type player
  42.         Field x#,y#,mx#,my#,angle,thrust#
  43.         Field image[360],shootdelaytime
  44.         Field score,hull
  45. End Type
  46.  
  47. Type pbullet
  48.         Field x#,y#,mx#,my#
  49.         Field dtraveled
  50. End Type
  51.  
  52. Global p.player = New player
  53. px = GraphicsWidth()/2-16
  54. py = GraphicsHeight()/2-16
  55.  
  56. initplayer()
  57.  
  58. restart()
  59.  
  60. timer = CreateTimer(60)
  61. While KeyDown(1) = False
  62.         WaitTimer timer
  63.         Cls
  64.         updateplayer()
  65.         updateturrets()
  66.         updateships()
  67.         updatemap()
  68.         updatepbullets()
  69.         updatetbullets()
  70.         updatesbullets()
  71.         pbulletturretcollision()
  72.         pbulletshipcollision() 
  73.         tbulletplayercollision()
  74.         sbulletplayercollision()
  75.         drawmap()
  76.         drawturrets()
  77.         drawships()
  78.         drawplayer()
  79.         drawpbullets()
  80.         drawtbullets()
  81.         drawsbullets()
  82.         Color 255,255,255
  83.         Text GraphicsWidth()-100,5,"Hull:"+phull,1,1
  84.         Text GraphicsWidth()-100,15,"Turrets left:"+turretsleft(),1,1
  85.         Text GraphicsWidth()-100,25,"Ships left:"+shipsleft(),1,1
  86.         If turretsleft() = 0 And shipsleft() = 0 Then restart()
  87.         Flip
  88. Wend
  89. End
  90.  
  91. Function sbulletplayercollision()
  92.         For this.sbullet = Each sbullet
  93.                 If RectsOverlap(thisx-3,thisy-3,6,6,px-16,py-16,32,32) Then
  94.                         phull = phull - 1
  95.                         If phull = 0
  96.                                 restart
  97.                                 Return
  98.                         End If
  99.                         Delete this
  100.                 End If
  101.         Next
  102. End Function
  103.  
  104. Function updatesbullets()
  105.         For this.sbullet = Each sbullet
  106.                 thisx = thisx + thismx
  107.                 thisy = thisy + thismy
  108.                 thisdtraveled = thisdtraveled + 1
  109.                 If thisdtraveled > 640 Then Delete this
  110.         Next
  111. End Function
  112.  
  113. Function drawsbullets()
  114.         For this.sbullet = Each sbullet
  115.                 Color 255,0,0
  116.                 Oval thisx-3,thisy-3,6,6,True
  117.         Next
  118. End Function
  119.  
  120. Function pbulletshipcollision()
  121.         For this.ship = Each ship
  122.         For that.pbullet = Each pbullet
  123.                 If RectsOverlap(thisx-16,thisy-16,32,32,thatx-3,thaty-3,6,6)
  124.                         Delete that
  125.                         thishull = thishull - 1
  126.                 End If
  127.         Next
  128.                 If thishull <= 0 Then Delete this
  129.         Next
  130. End Function
  131.  
  132. Function updateships()
  133.         For this.ship = Each ship
  134.                 da = getangle(this      x,this  y,thisx,thisy)
  135.                 v1 = 0 ; left side
  136.                 v2 = 0 ; right side
  137.                 ta = thisangle ; get the current angle into ta
  138.                 ; Count to the left to get the steps to the target angle
  139.                 exitloop = False
  140.                 While exitloop = False
  141.                         ta = ta - 1
  142.                         v1 = v1 + 1                            
  143.                         If RectsOverlap(ta,ta,4,4,da,da,4,4) Exitloop = True
  144.                         If ta < -180 Then ta = 181
  145.                 Wend
  146.                 ta = thisangle ;'get the current angle into ta
  147.                 ; Count to the right to get the steps top the target angle
  148.                 exitloop = False
  149.                 While exitloop = False
  150.                         ta = ta + 1
  151.                         v2 = v2 + 1
  152.                         If RectsOverlap(ta,ta,4,4,da,da,4,4) exitloop = True
  153.                         If ta >= 180 Then ta = -181
  154.                 Wend
  155.                 ;
  156.                 If v1 > v2 ; if the left side is closer to the target angle
  157.                         thisangle = thisangle - 3
  158.                 Else    ; if the right side is closer to the target angle
  159.                         thisangle = thisangle + 3
  160.                 End If
  161.                 ; move the missile
  162.                 thisx = thisx + Cos(thisangle) * 1
  163.                 thisy = thisy + Sin(thisangle) * 1
  164.                
  165.                 If distance(thisx,thisy,px,py) < 96 Or Rand(0,200) = 1
  166.                         exitloop = False
  167.                         While exitloop = False
  168.                                 x1 = Rand(-(GraphicsWidth()*2),GraphicsWidth()*2)
  169.                                 y1 = Rand(-(GraphicsHeight()*2),GraphicsHeight()*2)
  170.                                 If distance(px,py,x1,y1) > 320
  171.                                         this    x = x1
  172.                                         this    y = y1
  173.                                         exitloop = True
  174.                                 End If
  175.                         Wend
  176.                 End If           
  177.                 If distance(thisx,thisy,px,py) > 300
  178.                         this    x = px
  179.                         this    y = py
  180.                 End If
  181.                 ; Shoot at the player if he is nearby
  182.                 If distance(thisx,thisy,px,py) < 150
  183.                 If thisshootdelaytime < MilliSecs()
  184.                         thisshootdelaytime = MilliSecs() + 1000
  185.                         sb.sbullet = New sbullet
  186.                         ta = getangle(thisx,thisy,px,py)
  187.                         sbx = thisx-16 + Cos(ta)*16
  188.                         sby = thisy-16 + Sin(ta)*16
  189.                         sbmx = Cos(ta)*2
  190.                         sbmy = Sin(ta)*2
  191.                 End If
  192.                 End If
  193.         Next
  194.        
  195. End Function
  196.  
  197. Function initships()
  198.         For i=0 To 3
  199.                 this.ship = New ship
  200.                 thisx = Rand(-(GraphicsWidth()*2),GraphicsWidth()*2)
  201.                 thisy = Rand(-(GraphicsHeight()*2),GraphicsHeight()*2)
  202.                 this    x = px+Rand(-64,64)
  203.                 this    y = py+Rand(-64,64)
  204.                 thisangle = 0
  205.                 thishull = 3
  206.         Next
  207. End Function
  208.  
  209. Function drawships()
  210.         For this.ship = Each ship
  211.                 DrawImage pimage[0],thisx-16,thisy-16
  212.         Next
  213. End Function
  214.  
  215.  
  216. Function shipsleft()
  217.         For this.ship = Each ship
  218.                 cnt = cnt + 1
  219.         Next
  220.         Return cnt
  221. End Function
  222.  
  223. Function turretsleft()
  224.         For this.turret = Each turret
  225.                 cnt = cnt + 1
  226.         Next
  227.         Return cnt
  228. End Function
  229.  
  230. Function tbulletplayercollision()
  231.         For this.tbullet = Each tbullet
  232.                 If RectsOverlap(thisx-3,thisy-3,6,6,px-16,py-16,32,32)
  233.                         Delete this
  234.                         phull = phull - 1
  235.                         If phull < 1
  236.                                 restart
  237.                                 Return
  238.                         End If
  239.                 End If
  240.         Next
  241. End Function
  242.  
  243.  
  244. Function restart()
  245.         Delete Each pbullet
  246.         Delete Each tbullet
  247.         Delete Each sbullet
  248.         Delete Each turret
  249.         Delete Each ship
  250.         phull = 10
  251.         p       hrust = .1
  252.         mapx = 0
  253.         mapy = 0
  254.         Cls
  255.         Text GraphicsWidth()/2,GraphicsHeight()/2,"Get Ready",1,1
  256.         Flip
  257.         Delay 1000
  258.         initmap()
  259.         initships()
  260. End Function
  261.  
  262. Function pbulletturretcollision()
  263.         For this.turret = Each turret
  264.         For that.pbullet = Each pbullet        
  265.                 If RectsOverlap(thisx+4,thisy+4,32-8,32-8,thatx-3,thaty-3,6,6) Then
  266.                         Delete that
  267.                         thishull = thishull - 1
  268.                 End If
  269.         Next
  270.         If thishull < 0 Then Delete this
  271.         Next
  272. End Function
  273.  
  274. Function updateplayer()
  275.         Local my# = 0
  276.         Local mx# = 0
  277.         If KeyDown(203)
  278.                 pangle = pangle - 1
  279.                 If pangle < 0 Then pangle = 360
  280.         End If
  281.         If KeyDown(205)
  282.                 pangle = pangle + 1
  283.                 If pangle >= 360 Then pangle = 0
  284.         End If
  285.         If KeyDown(200)
  286.                 mx = Cos(pangle) * p    hrust
  287.                 my = Sin(pangle) * p    hrust
  288.                 If mx < 0  
  289.                         If pmx > -1 Then pmx = pmx + mx
  290.                 End If
  291.                 If mx > 0
  292.                         If pmx < 1 Then pmx = pmx + mx
  293.                 End If
  294.                 If my < 0
  295.                         If pmy > -1 Then pmy = pmy + my
  296.                 End If
  297.                 If my > 0
  298.                         If pmy < 1 Then pmy = pmy + my
  299.                 End If
  300.         End If
  301.         If KeyDown(57) And pshootdelaytime < MilliSecs()
  302.                 pb.pbullet = New pbullet
  303.                 pbx = GraphicsWidth()/2-16 + Cos(pangle-180) * 16
  304.                 pby = GraphicsHeight()/2-16 + Sin(pangle-180) * 16
  305.                 pbmx = Cos(pangle-180) * 4.5
  306.                 pbmy = Sin(pangle-180) * 4.5
  307.                 pshootdelaytime = MilliSecs() + 200
  308.         End If
  309. End Function
  310.  
  311. Function updateturrets()
  312.         For this.turret = Each turret
  313.                 If distance(thisx,thisy,px,py) < 220 Then
  314.                 If thisshootdelaytime < MilliSecs()
  315.                         a = getangle(thisx,thisy,px,py)
  316.                         that.tbullet = New tbullet
  317.                         thatx = thisx + 16 + Cos(a) * 16
  318.                         thaty = thisy + 16 + Sin(a) * 16
  319.                         thatmx = Cos(a)*2
  320.                         thatmy = Sin(a)*2
  321.                         thisshootdelaytime = MilliSecs()+1000
  322.                 End If
  323.                 End If
  324.         Next
  325. End Function
  326.  
  327. Function updatetbullets() ; update turret bullets
  328.         For this.tbullet = Each tbullet
  329.                 thisdtraveled = thisdtraveled + 1
  330.                 thisx = thisx + thismx
  331.                 thisy = thisy + thismy
  332.                 If thisdtraveled > 640 Then Delete this
  333.         Next
  334. End Function
  335.  
  336.  
  337. Function drawtbullets() ; draw turret bullet
  338.         For this.tbullet = Each tbullet
  339.                 Color 255,255,0
  340.                 Oval thisx-3,thisy-3,6,6,True
  341.         Next
  342. End Function
  343.  
  344. Function drawturrets()
  345.         For this.turret = Each turret
  346.                 Color 0,0,255
  347.                 Oval thisx,thisy,32,32,True
  348.         Next
  349. End Function
  350.  
  351. Function updatepbullets()
  352.         For this.pbullet = Each pbullet
  353.                 thisdtraveled = thisdtraveled + 1
  354.                 thisx = thisx + thismx
  355.                 thisy = thisy + thismy
  356.                 If thisdtraveled > 640 Then Delete this
  357.         Next
  358. End Function
  359.  
  360. ; draw player bullets
  361. Function drawpbullets()
  362.         For this.pbullet = Each pbullet
  363.                 Color 200,0,0
  364.                 Oval thisx,thisy,6,6,True
  365.                 Color 255,255,255
  366.                 Oval thisx,thisy,6,6,False     
  367.         Next
  368. End Function
  369.  
  370. Function updatemap()
  371.                 mapx = mapx + pmx
  372.                 mapy = mapy + pmy
  373.                 ; Move every player bullet with the movement speed of the player
  374.                 For pb.pbullet = Each pbullet
  375.                         pbx = pbx + pmx
  376.                         pby = pby + pmy
  377.                 Next
  378.                 ; Move every turret
  379.                 For t.turret = Each turret
  380.                         tx = tx + pmx
  381.                         ty = ty + pmy
  382.                 Next
  383.                 ; Move every turret bullet
  384.                 For tb.tbullet = Each tbullet
  385.                         tbx = tbx + pmx
  386.                         tby = tby + pmy
  387.                 Next
  388.                 ; Move every ship
  389.                 For s.ship = Each ship
  390.                         sx = sx + pmx
  391.                         sy = sy + pmy
  392.                 Next
  393. End Function
  394.  
  395. Function drawmap()
  396.         DrawImage mapimage,mapx,mapy
  397. End Function
  398.  
  399. Function initplayer()
  400.         Local image = CreateImage(32,32)
  401.         MidHandle image
  402.         SetBuffer ImageBuffer(image)
  403.         Color 255,255,255
  404.         Rect 0,0,32,32,True
  405.         Color 55,55,55
  406.         Oval 0,16-3,6,6,True
  407.         For i=0 To 360
  408.                 pimage[i] = CreateImage(32,32)
  409.                 pimage[i] = CopyImage(image)
  410.                 RotateImage pimage[i],i
  411.         Next
  412.         SetBuffer BackBuffer()
  413. End Function
  414.  
  415. Function drawplayer()
  416.         DrawImage pimage[pangle],px,py
  417. End Function
  418.  
  419. Function initmap()
  420.         Restore mymap
  421.         For y=0 To mapheight
  422.         For x=0 To mapwidth
  423.                 Read a
  424.                 If a = 1 Then map(x,y) = a
  425.                 If a = 2
  426.                         t.turret = New turret
  427.                         tx = x * 32
  428.                         ty = y * 32
  429.                         thull = 3
  430.                 End If
  431.         Next
  432.         Next
  433.         SetBuffer ImageBuffer(mapimage)
  434.         For y=0 To mapheight
  435.         For x=0 To mapwidth
  436.                 Select map(x,y)
  437.                         Case 0:Color 0,0,0
  438.                         Case 1:Color 55,55,155
  439.                 End Select
  440.                 Rect x*32,y*32,32,32,True
  441.         Next
  442.         Next
  443.         SetBuffer BackBuffer()
  444. End Function
  445.  
  446. .mymap
  447. Data 1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1
  448. Data 1,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,1
  449. Data 1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1
  450. Data 0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0
  451. Data 0,0,0,0,1,1,1,0,0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,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,1
  476. Data 1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1
  477.  
  478. Function distance(x1,y1,x2,y2)
  479.         Return Abs(x2-x1)+Abs(y2-y1)
  480. End Function
  481.  
  482. Function getangle(x1,y1,x2,y2)
  483.          Local dx = x2 - x1
  484.          Local dy = y2 - y1
  485.          Return ATan2(dy,dx)+360 Mod 360
  486. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal