What's new

Visual Basic Numberofshoot

britbrit

Addict
Joined
Sep 4, 2020
Posts
26
Reaction
135
Points
112
Code:
Public Class Form7
    Dim SRight As Boolean 'Shooter moving right
    Dim SLeft As Boolean  'Shooter moving left
    Dim ShooterSpeed As Integer  'How much the shooter moves
    Dim ShotSpeed As Integer  'How much the shot moves
    Dim InvaderSpeed As Integer  'How much the invaders move
    Dim InvaderDrop As Integer  ' How much the invaders drop
    Const NumOfInvaders As Integer = 1 'how many invaders there are
    Dim IRight(NumOfInvaders) As Boolean  'if the invaders are moving right or left
    Dim Invaders(NumOfInvaders) As PictureBox 'Makes the invader array
    Dim x As Integer 'Used as a counter variable
    Dim ShotDown As Integer 'Number of invaders shot down
    Dim Paused As Boolean  '


    Private Sub TimerMain_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerMain.Tick
        'when the timer tickes:
        MoveShooter()
        FireShot()
        MoveInvader()
        CheckHit()
        CheckGameOver()
    End Sub

    Private Sub Form7_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyValue = Keys.Right Then ' if the right arrow key is pressed
            SRight = True
            SLeft = False
        End If
        If e.KeyValue = Keys.Left Then ' if the left arrow key is pressed
            SLeft = True
            SRight = False
        End If

        If e.KeyValue = Keys.Space And Shot.Visible = False Then ' if the space is pressed and the shot has not been fired
            My.Computer.Audio.Play(My.Resources.Shot, AudioPlayMode.Background)
            Shot.Top = Shooter.Top ' shot comes out the shooters top
            Shot.Left = Shooter.Left + (Shooter.Width / 2) - (Shot.Width / 2) 'shot is centered in the shooter
            Shot.Visible = True
        End If

    End Sub

    Private Sub MoveShooter()
        If SRight = True And Shooter.Left + Shooter.Width < Me.ClientRectangle.Width Then
            Shooter.Left += ShooterSpeed 'shooter moves to the right
        End If
        If SLeft = True And Shooter.Left > Me.ClientRectangle.Left Then
            Shooter.Left -= ShooterSpeed 'shooter moves to the left
        End If
    End Sub

    Private Sub Form7_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
        If e.KeyValue = Keys.Right Then
            SRight = False ' shooter is not moving to the right
        End If
        If e.KeyValue = Keys.Left Then
            SLeft = False ' shooter is not moving to the left
        End If
    End Sub

    Private Sub Form7_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadInvaders()
        LoadSettings()
        Form4.Close()
    End Sub

    Private Sub LoadSettings()
        Paused = False ' game is not paused
        ShotSpeed = 20 ' how fast the shot moves
        ShooterSpeed = 10 ' how fast the shooter moves
        Shot.Visible = False

        For Me.x = 1 To NumOfInvaders
            IRight(x) = True ' invaders are set to move right
            Invaders(x).Left = (-30 * x) - (x * 5) 'how much the invaders are spaced from each other
            Invaders(x).Top = 0  'the invaders are at the top of the window
            Invaders(x).Visible = True
        Next

        InvaderSpeed = 1  'how much the invaders move
        InvaderDrop = 90  'how much the invaders drop
        ShotDown = 0  'start with 0 invaders shot down
        SRight = False  'shooter is not moving
        SLeft = False   'shooter is not moving
        My.Computer.Audio.Play(My.Resources.Intro, AudioPlayMode.Background)
        TimerMain.Enabled = True 'timer must be enabled to play the game
    End Sub

    Private Sub FireShot()
        If Shot.Visible = True Then
            Shot.Top -= ShotSpeed ' shot moves up
        End If

        If Shot.Top + Shot.Height < Me.ClientRectangle.Top Then
            Shot.Visible = False ' shot hits the top of the window and is ready to fire
        End If
    End Sub

    Private Sub MoveInvader()
        For Me.x = 1 To NumOfInvaders
            If IRight(x) = True Then
                Invaders(x).Left += InvaderSpeed ' moves invaders to the right
            Else
                Invaders(x).Left -= InvaderSpeed ' moves invaders to the left
            End If

            If Invaders(x).Left + Invaders(x).Width > Me.ClientRectangle.Width And IRight(x) = True Then
                ' invaders hit right side of the window and drop
                IRight(x) = False
                Invaders(x).Top += InvaderDrop
            End If

            If Invaders(x).Left < Me.ClientRectangle.Left And IRight(x) = False Then
                ' invaders hit left side of the window and drop
                IRight(x) = True
                Invaders(x).Top += InvaderDrop
            End If
        Next
    End Sub

    Private Sub CheckGameOver()
        For Me.x = 1 To NumOfInvaders
            If Invaders(x).Top + Invaders(x).Height >= Shooter.Top And Invaders(x).Visible = True Then
                TimerMain.Enabled = False
                Me.x = NumOfInvaders
                My.Computer.Audio.Play(My.Resources.Dead, AudioPlayMode.Background)
                MsgBox("Game Over - AstroWorld Invaded")
                PlayAgain()
            End If

        Next

        If ShotDown = NumOfInvaders Then
            TimerMain.Enabled = False
            My.Computer.Audio.Play(My.Resources.Win, AudioPlayMode.Background)
            MsgBox("AstroWorld Is Saved")
            NextLvl()
        End If

    End Sub

    Private Sub CheckHit()
        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
                My.Computer.Audio.Play(My.Resources.Hit, AudioPlayMode.Background)
                Shot.Visible = False
                ShotDown += 1
            End If
        Next



    End Sub

    Private Sub LoadInvaders()
        For Me.x = 1 To NumOfInvaders  'dynamically makes invaders
            Invaders(x) = New PictureBox
            Invaders(x).Image = My.Resources.monster_lvl5
            Invaders(x).Width = 178
            Invaders(x).Height = 182
            Invaders(x).SizeMode = PictureBoxSizeMode.StretchImage
            Invaders(x).BackColor = Color.Transparent
            Controls.Add(Invaders(x))
        Next

    End Sub

    Private Sub Form7_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        If e.KeyChar = "P" Or e.KeyChar = "p" Then
            If Paused = True Then  'unpauses game
                TimerMain.Enabled = True
                Paused = False
            Else
                TimerMain.Enabled = False 'pauses game
                Paused = True
            End If
        End If
    End Sub

    Private Sub PlayAgain()
        Dim Result = MsgBox("Play Again?", MsgBoxStyle.YesNo)

        If Result = MsgBoxResult.Yes Then
            LoadSettings() 'resets and restarts game
        Else
            Me.Close()  ' quits game
        End If
    End Sub

    Private Sub NextLvl()
        Dim Result = MsgBox("You win!", vbOKOnly)
        If Result = MsgBoxResult.Ok Then
            Me.Hide()
        Else
            Me.Close()  ' quits game
        End If
    End Sub
End Class



ask ko lang po kung papaano ko paramihin muna ung shoot, instead of 1 lng bago magfalse ung visibility nung monster invader(picbox). maraming salamat sa sasagot guys..
 

Similar threads

Back
Top