ssoni

spaceInvaders_Done.vb

Jun 6th, 2022
1,934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.64 KB | None | 0 0
  1. Public Class Form1
  2.  
  3.     Public enemyList As New List(Of PictureBox)
  4.     Public fortressList As New List(Of PictureBox)
  5.     Public moveAmt As Integer
  6.     Public score As Integer
  7.  
  8.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  9.         Call initGame()
  10.  
  11.     End Sub
  12.  
  13.     Public Sub initGame()
  14.         moveAmt = 2
  15.         score = 0
  16.         lblScore.Text = score
  17.         picBullet.Visible = False
  18.  
  19.         enemyList.Add(picEnemy1)
  20.         enemyList.Add(picEnemy2)
  21.         enemyList.Add(picEnemy3)
  22.         enemyList.Add(picEnemy4)
  23.         enemyList.Add(picEnemy5)
  24.         enemyList.Add(picEnemy6)
  25.         enemyList.Add(picEnemy7)
  26.         enemyList.Add(picEnemy8)
  27.         enemyList.Add(picEnemy9)
  28.         enemyList.Add(picEnemy10)
  29.         enemyList.Add(picEnemy11)
  30.         enemyList.Add(picEnemy12)
  31.         enemyList.Add(picEnemy13)
  32.         enemyList.Add(picEnemy14)
  33.         enemyList.Add(picEnemy15)
  34.         enemyList.Add(picEnemy16)
  35.  
  36.         fortressList.Add(picFort1)
  37.         fortressList.Add(picFort2)
  38.         fortressList.Add(picFort3)
  39.         fortressList.Add(picFort4)
  40.  
  41.  
  42.     End Sub
  43.  
  44.     Private Sub picBullet_Click(sender As Object, e As EventArgs) Handles picBullet.Click
  45.  
  46.     End Sub
  47.  
  48.     Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
  49.  
  50.         'move ship
  51.         If e.KeyCode = Keys.Left Then
  52.             picShip.Left = picShip.Left - 3
  53.         ElseIf e.KeyCode = Keys.Right Then
  54.             picShip.Left = picShip.Left + 3
  55.         End If
  56.  
  57.         'dont refire the same bullet
  58.         If picBullet.Visible = True Then
  59.             Exit Sub
  60.         End If
  61.  
  62.         'fire bubble
  63.         If e.KeyCode = Keys.Space Then
  64.             picBullet.Visible = True
  65.             picBullet.Top = picShip.Top
  66.             picBullet.Left = picShip.Left + picShip.Width / 2 - picBullet.Width / 2
  67.         End If
  68.  
  69.     End Sub
  70.  
  71.     Private Sub tmrMoveBullet_Tick(sender As Object, e As EventArgs) Handles tmrMoveBullet.Tick
  72.  
  73.         'move bullet
  74.         If picBullet.Visible = True Then
  75.             picBullet.Top = picBullet.Top - 5
  76.         End If
  77.  
  78.         'hide bullet is missed
  79.         If picBullet.Top <= 0 Then
  80.             picBullet.Visible = False
  81.         End If
  82.  
  83.     End Sub
  84.  
  85.     Public Function detectHits(p1 As PictureBox, p2 As PictureBox) As Boolean
  86.         If ((p1.Left + p1.Width > p2.Left) And
  87.                 (p1.Left < p2.Left + p2.Width) And
  88.                 (p1.Top + p1.Height > p2.Top) And
  89.                 (p1.Top < p2.Top + p2.Height)) Then
  90.             Return True
  91.         Else
  92.             Return False
  93.         End If
  94.     End Function
  95.  
  96.     Private Sub tmrCollision_Tick(sender As Object, e As EventArgs) Handles tmrCollision.Tick
  97.         Dim p As PictureBox
  98.  
  99.         If picBullet.Visible = False Then
  100.             Exit Sub
  101.         End If
  102.  
  103.         For Each p In enemyList
  104.             If p.Visible = True Then
  105.                 If detectHits(picBullet, p) Then
  106.                     picBullet.Visible = False
  107.                     p.Visible = False
  108.                     score = score + 1
  109.                     lblscore.text = score
  110.                     'CHECK IF ROUND OVER
  111.                     If levelOver() = True Then
  112.                         Call nextLevel
  113.                     End If
  114.                 End If
  115.             End If
  116.         Next
  117.  
  118.         For Each p In fortressList
  119.             If detectHits(picBullet, p) Then
  120.                 picBullet.Visible = False
  121.             End If
  122.         Next
  123.     End Sub
  124.  
  125.     Private Sub tmrMoveEnemies_Tick(sender As Object, e As EventArgs) Handles tmrMoveEnemies.Tick
  126.         Dim p As PictureBox
  127.         Dim edge As Boolean
  128.         edge = False
  129.  
  130.         For Each p In enemyList
  131.             'move enemy
  132.             p.Left = p.Left + moveAmt
  133.  
  134.             'check edge bounce on both sides
  135.             If p.Visible = True Then
  136.                 If (p.Left + p.Width > Me.Width) Or (p.Left <= 0) Then
  137.                     edge = True
  138.                 End If
  139.             End If
  140.         Next
  141.  
  142.         If edge = True Then
  143.  
  144.             'reverse direction
  145.             moveAmt = -moveAmt
  146.  
  147.             'move them down
  148.             For Each p In enemyList
  149.                 p.Top = p.Top + 20
  150.             Next
  151.  
  152.             If gameOver() = True Then
  153.                 tmrMoveEnemies.Enabled = False
  154.                 MsgBox("Game over")
  155.             End If
  156.         End If
  157.  
  158.     End Sub
  159.  
  160.     Public Function gameOver() As Boolean
  161.         Dim p As PictureBox
  162.         Dim done As Boolean
  163.  
  164.         done = False
  165.  
  166.         For Each p In enemyList
  167.             If p.Visible = True Then
  168.                 If p.Top + p.Height > picShip.Top Then
  169.                     done = True
  170.                 End If
  171.             End If
  172.         Next
  173.  
  174.         Return done
  175.     End Function
  176.  
  177.     Public Function levelOver() As Boolean
  178.         Dim p As PictureBox
  179.         Dim done As Boolean
  180.  
  181.         done = True
  182.  
  183.         For Each p In enemyList
  184.             If p.Visible = True Then
  185.                 done = False
  186.             End If
  187.         Next
  188.  
  189.         Return done
  190.  
  191.     End Function
  192.  
  193.     Public Sub nextlevel()
  194.         Dim p As PictureBox
  195.         Dim x As Integer
  196.         Dim y As Integer
  197.  
  198.         For Each p In enemyList
  199.             p.Visible = True
  200.             If enemyList.IndexOf(p) < 4 Then
  201.                 p.Top = 50
  202.             ElseIf enemyList.IndexOf(p) < 8 Then
  203.                 p.Top = 100
  204.             ElseIf enemyList.IndexOf(p) < 12 Then
  205.                 p.Top = 150
  206.             Else
  207.                 p.Top = 200
  208.             End If
  209.         Next
  210.  
  211.         moveAmt = moveAmt * 2
  212.     End Sub
  213.  
  214.  
  215. End Class
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
Advertisement
Add Comment
Please, Sign In to add comment