Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Arxk
- Made this little game a while ago, most of the things I created in Visual Studio were deleted transferring computers, so I don't have that many examples. It's just a little space invaders game.
- -- Arxk
- Public Class Form3
- Dim ShooterSpeed As Integer
- Dim InvaderSpeed As Integer
- Dim ShotSpeed As Integer
- Dim InvaderDrop As Integer
- Dim Level2 As Boolean
- Dim Level3 As Boolean
- Dim box As PictureBox = New PictureBox
- Dim Score As Integer
- Dim HighScore As Integer
- Dim abletomove As Boolean
- Dim SRight As Boolean
- Dim SLeft As Boolean
- Const NumOfInvaders As Integer = 25
- Dim IRight(NumOfInvaders) As Boolean
- Dim Invaders(NumOfInvaders) As PictureBox
- Dim x As Integer
- Dim intCount As Integer
- Dim shotDown As Integer
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- // This would load the form, setting all the values to default to make sure no previous values are shown.
- abletomove = True
- ShotSpeed = 9
- ShooterSpeed = 3
- InvaderSpeed = 5
- InvaderDrop = 42
- // Start loading the invaders
- LoadInvaders()
- For Me.x = 1 To NumOfInvaders
- IRight(x) = True
- Next
- Score = 0
- HighScore = 0
- Label3.Visible = False
- Shot.Visible = False
- End Sub
- Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
- // This would handle the pressing of keys, so pressing left sets the left value to true and moving you, then of course pressing up just shoots the invaders. Makes it visible etc.
- If abletomove = True Then
- Select Case e.KeyCode
- Case Keys.Left
- SLeft = True
- SRight = False
- Case Keys.Right
- SRight = True
- SLeft = False
- Case Keys.Up
- If Shot.Visible = False Then
- Shot.Top = PictureBox1.Top
- Shot.Left = PictureBox1.Left + (PictureBox1.Width / 2) - (Shot.Width / 2)
- Shot.Visible = True
- End If
- End Select
- End If
- End Sub
- Private Sub FormMain_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
- // When letting go of your keys, it would set those left and right values back to false, pretty self-explanatory.
- If abletomove = True Then
- If e.KeyValue = Keys.Right Then
- SRight = False
- End If
- If e.KeyValue = Keys.Left Then
- SLeft = False
- End If
- End If
- End Sub
- Private Sub FireShot()
- // This handles the firing of the bullet, makes sure that when it reaches the top, it is set to invisible.
- If Shot.Visible = True Then
- Shot.Top -= ShotSpeed
- End If
- If Shot.Top + Shot.Height < Me.ClientRectangle.Top Then
- Shot.Visible = False
- End If
- End Sub
- Private Sub CheckHit()
- // This will check if one of the invaders are hit, and when they are hit, it will make the invader invisible.
- For Me.x = 1 To NumOfInvaders
- If (Shot.Top + Shot.Height >= Invaders(x).Top) And (Shot.Top <= Invaders(x).Top + Invaders(x).Height) And (Shot.Left + Shot.Width >= Invaders(x).Left) And (Shot.Left <= Invaders(x).Left + Invaders(x).Width) And Shot.Visible = True And Invaders(x).Visible = True Then
- Invaders(x).Visible = False
- Shot.Visible = False
- shotDown += 1
- End If
- Next
- End Sub
- Private Sub MoveInvader()
- // This would handle the moving of the invaders, obviously having an integer as InvaderSpeed, controlling it with a timer.
- For Me.x = 1 To NumOfInvaders
- If IRight(x) = True Then
- Invaders(x).Left += InvaderSpeed
- Else
- Invaders(x).Left -= InvaderSpeed
- End If
- If Invaders(x).Left + Invaders(x).Width > Me.ClientRectangle.Width And IRight(x) = True Then
- IRight(x) = False
- Invaders(x).Top += InvaderDrop
- End If
- If Invaders(x).Left < Me.ClientRectangle.Left And IRight(x) = False Then
- IRight(x) = True
- Invaders(x).Top += InvaderDrop
- End If
- Next
- End Sub
- Private Sub CheckGameOver()
- // This handles what happens after the game ends, it will check if all the invaders are gone, and if they are then it will do everything necessary to restart the game and prompt a messaging telling them the game is over.
- For Me.x = 1 To NumOfInvaders
- If Invaders(x).Top + Invaders(x).Height >= PictureBox1.Top And Invaders(x).Visible = True Then
- My.Settings.MediumScore = Score
- TmrShoot.Enabled = False
- abletomove = False
- SRight = False
- SLeft = False
- coinsUp.Enabled = False
- Label3.Visible = True
- Me.BackColor = Color.Red
- MsgBox("Game OVER! CLICK OK TO CLOSE!")
- Me.Close()
- End If
- Next
- If shotDown = NumOfInvaders Then
- My.Settings.MediumScore = Score
- Level2 = True
- TmrShoot.Enabled = False
- Me.x = NumOfInvaders
- SRight = False
- SLeft = False
- Label4.Visible = True
- Me.BackColor = Color.White
- Score = 0
- Label1.Text = "SCORE: " & Score
- coinsUp.Enabled = False
- Label3.Visible = False
- abletomove = False
- MsgBox("YOU WON! CLICK OK TO CLOSE!")
- Me.Close()
- End If
- End Sub
- Private Sub MoveShooter()
- // This will move the shooter, basically the same method as moving the invaders.
- If SRight = True And PictureBox1.Left + PictureBox1.Width < Me.ClientRectangle.Width Then
- PictureBox1.Left += ShooterSpeed
- End If
- If SLeft = True And PictureBox1.Left > Me.ClientRectangle.Left Then
- PictureBox1.Left -= ShooterSpeed
- End If
- End Sub
- Private Sub Button1_Click(sender As Object, e As EventArgs)
- // Just handles the click of the start button, it will load the invaders and start the game.
- LoadInvaders()
- abletomove = True
- TmrShoot.Enabled = True
- coinsUp.Enabled = True
- End Sub
- Private Sub coinsUp_Tick(sender As Object, e As EventArgs) Handles coinsUp.Tick
- Score = Score + 3
- Label1.Text = "SCORE: " & Score
- End Sub
- Private Sub LoadInvaders()
- // This will load the invaders, it's called when the game originally starts and they are removed once the game has ended.
- For Me.x = 1 To NumOfInvaders
- Invaders(x) = New PictureBox
- Invaders(x).SizeMode = PictureBoxSizeMode.StretchImage
- Invaders(x).Image = My.Resources.invader
- Invaders(x).Width = 40
- Invaders(x).Height = 32
- Invaders(x).BackColor = Color.Transparent
- Invaders(x).Left = (-50 * x) - (x * 5)
- Controls.Add(Invaders(x))
- Next
- End Sub
- Private Sub TmrShoot_Tick(sender As Object, e As EventArgs) Handles TmrShoot.Tick
- // This timer handles everything, from firing shots to the movement of the shooter and the invaders moving.
- FireShot()
- MoveShooter()
- CheckHit()
- CheckGameOver()
- MoveInvader()
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement