Secondo Homework

Applicazione avventura grafica

La seconda applicazione sviluppata consiste in una breve avventura grafica, dove l’utente si muove all’interno di un dungeon e deve sconfiggere un boss finale. Per la flessibilità della gestione dei comandi input, questa applicazione è stata sviluppata solo in VB.net. Il codice è portato qui di seguito:

Public Class Form1

    Private vita As Integer
    Private atk As Integer
    Private mana As Integer
    Private posH As Integer
    Private posV As Integer
    Private key1 As Boolean
    Private key2 As Boolean
    Private spada As Boolean
    Private poz As Boolean
    Private boss As Boolean
    Private vitaBoss As Integer
    Private atkBoss As Integer
    Private randomizer As New Random


    Private Sub startButton_Click() Handles startButton.Click
        For Each c As Control In Me.Controls
            If TypeOf c Is Panel Then
                For Each lbl As Label In c.Controls.OfType(Of Label)()
                    lbl.BackColor = Color.Black
                Next
            End If

        Next
        startButton.Enabled = False
        Timer1.Start()
        Room1.BackColor = Color.White
        p1a2.BackColor = Color.White
        Me.Room1.Text = "+"
        vita = 100
        atk = 20
        mana = 50
        posH = 1
        posV = 1
        key1 = False
        key2 = False
        spada = False
        poz = True
        boss = False
        vitaBoss = 250
        atkBoss = 30
        MoveUp.Enabled = True
        MoveDown.Enabled = True
        MoveLeft.Enabled = True
        MoveRight.Enabled = True
        ButtonA.Enabled = True
        ButtonB.Enabled = True
    End Sub

    Private Sub Posizione()
        If (posH = 1) Then
            If (posV = 1) Then
                Me.Room1.BackColor = Color.White
                Me.Room1.Text = "+"
                Me.LabInt.Text = "Sei nella stanza numero 1" & vbCrLf & "Questa stanza è vuota "
                Me.Room2.Text = ""
            ElseIf (posV = 2) Then
                Me.p2a3.BackColor = Color.White
                Me.p2a4.BackColor = Color.White
                Me.Room2.BackColor = Color.White
                Me.Room1.Text = ""
                Me.Room3.Text = ""
                Me.Room4.Text = ""
                Me.LabInt.Text = "Sei nella stanza numero 2" & vbCrLf & "Questa stanza è vuota "
                Me.Room2.Text = "+"
            ElseIf (posV = 3) Then
                Me.p3a5.BackColor = Color.White
                Me.Room3.BackColor = Color.White
                Me.Room2.Text = ""
                Me.Room5.Text = ""
                Me.LabInt.Text = "Sei nella stanza numero 3" & vbCrLf & "Questa stanza è vuota "
                Me.Room3.Text = "+"
            End If
        ElseIf (posH = 2) Then
            If (posV = 2) Then
                Me.p4a7.BackColor = Color.White
                Me.Room4.BackColor = Color.White
                Me.Room2.Text = ""
                Me.Room7.Text = ""
                If (key1) Then
                    Me.LabInt.Text = "Sei nella stanza numero 4" & vbCrLf & "Questa stanza è vuota "
                Else
                    Me.LabInt.Text = "Sei nella stanza numero 4" & vbCrLf & "Questa stanza è vuota " & vbCrLf & "Ti serve una chiave!"
                End If
                Me.Room4.Text = "+"
            ElseIf (posV = 3) Then
                Me.Room5.BackColor = Color.White
                Me.Room3.Text = ""
                If (Not key1) Then
                    Me.LabInt.Text = "Sei nella stanza numero 5" & vbCrLf & "Hai trovato una chiave!" & vbCrLf & "A: Raccogli"
                Else
                    Me.LabInt.Text = "Sei nella stanza numero 5" & vbCrLf & "Questa stanza è vuota"
                End If
                Me.Room5.Text = "+"
                End If
        ElseIf (posH = 3) Then
            If (posV = 2) Then
                Me.p6a7.BackColor = Color.White
                Me.p7a9.BackColor = Color.White
                Me.Room7.BackColor = Color.White
                Me.Room4.Text = ""
                Me.Room6.Text = ""
                Me.Room9.Text = ""
                Me.LabInt.Text = "Sei nella stanza numero 7" & vbCrLf & "Questa stanza è vuota "
                Me.Room7.Text = "+"
            ElseIf (posV = 1) Then
                Me.Room6.BackColor = Color.White
                Me.Room7.Text = ""
                Me.LabInt.Text = "Sei nella stanza numero 6" & vbCrLf & "Questa stanza è vuota "
                Me.Room6.Text = "+"
            End If
        ElseIf (posH = 4) Then
            If (posV = 2) Then
                Me.p8a9.BackColor = Color.White
                Me.p9a10.BackColor = Color.White
                Me.p9a11.BackColor = Color.White
                Me.Room9.BackColor = Color.White
                Me.Room7.Text = ""
                Me.Room8.Text = ""
                Me.Room10.Text = ""
                Me.Room11.Text = ""
                Me.LabInt.Text = "Sei nella stanza numero 9" & vbCrLf & "Questa stanza è vuota "
                Me.Room9.Text = "+"
            ElseIf (posV = 1) Then
                Me.Room8.BackColor = Color.White
                Me.Room9.Text = ""
                If (spada) Then
                    Me.LabInt.Text = "Sei nella stanza numero 8" & vbCrLf & "Questa stanza è vuota "
                Else
                    Me.LabInt.Text = "Sei nella stanza numero 8" & vbCrLf & "Hai trovato una spada!" & vbCrLf & "A: Raccogli"
                End If
                Me.Room8.Text = "+"
            ElseIf (posV = 3) Then
                Me.p10a12.BackColor = Color.White
                Me.Room10.BackColor = Color.White
                Me.Room9.Text = ""
                Me.Room12.Text = ""
                Me.LabInt.Text = "Sei nella stanza numero 10" & vbCrLf & "Questa stanza è vuota "
                Me.Room10.Text = "+"
            End If
        ElseIf (posH = 5) Then
            If (posV = 2) Then
                Me.p11a12.BackColor = Color.White
                Me.p11a14.BackColor = Color.White
                Me.Room11.BackColor = Color.White
                Me.Room9.Text = ""
                Me.Room12.Text = ""
                Me.Room14.Text = ""
                Me.LabInt.Text = "Sei nella stanza numero 11" & vbCrLf & "Questa stanza è vuota "
                Me.Room11.Text = "+"
            ElseIf (posV = 3) Then
                Me.p11a12.BackColor = Color.White
                Me.p10a12.BackColor = Color.White
                Me.Room12.BackColor = Color.White
                Me.Room10.Text = ""
                Me.Room11.Text = ""
                If (Not key2) Then
                    Me.LabInt.Text = "Sei nella stanza numero 12" & vbCrLf & "Hai trovato una chiave!" & vbCrLf & "A: Raccogli"
                Else
                    Me.LabInt.Text = "Sei nella stanza numero 12" & vbCrLf & "Questa stanza è vuota "
                End If

                Me.Room12.Text = "+"
            End If
        ElseIf (posH = 6) Then
            If (posV = 2) Then
                Me.p14a16.BackColor = Color.White
                Me.p13a14.BackColor = Color.White
                Me.Room14.BackColor = Color.White
                Me.Room11.Text = ""
                Me.Room13.Text = ""
                Me.Room16.Text = ""
                Me.LabInt.Text = "Sei nella stanza numero 14" & vbCrLf & "Questa stanza è vuota "
                Me.Room14.Text = "+"
            ElseIf (posV = 1) Then
                Me.Room13.BackColor = Color.White
                Me.Room14.Text = ""
                Me.LabInt.Text = "Sei nella stanza numero 13" & vbCrLf & "Questa stanza è vuota "
                Me.Room13.Text = "+"
            End If
        ElseIf (posH = 7) Then
            If (posV = 2) Then
                Me.p15a16.BackColor = Color.White
                Me.p16a17.BackColor = Color.White
                Me.p16a18.BackColor = Color.White
                Me.Room16.BackColor = Color.White
                Me.Room14.Text = ""
                Me.Room15.Text = ""
                Me.Room17.Text = ""
                Me.Room18.Text = ""
                Me.LabInt.Text = "Sei nella stanza numero 16" & vbCrLf & "Questa stanza è vuota "
                Me.Room16.Text = "+"
            ElseIf (posV = 1) Then
                Me.Room15.BackColor = Color.White
                Me.Room16.Text = ""
                Me.LabInt.Text = "Sei nella stanza numero 15" & vbCrLf & "Questa stanza è vuota "
                Me.Room15.Text = "+"
            ElseIf (posV = 3) Then
                Me.pFinal.BackColor = Color.LightCoral
                Me.Room17.BackColor = Color.White
                Me.Room16.Text = ""
                If (key2) Then
                    Me.LabInt.Text = "Sei nella stanza numero 17" & vbCrLf & "Questa stanza è vuota "
                Else
                    Me.LabInt.Text = "Sei nella stanza numero 17" & vbCrLf & "Questa stanza è vuota " & vbCrLf & "Ti serve una chiave!"
                End If
                Me.Room17.Text = "+"
            ElseIf (posV = 4) Then
                Me.RoomFinal.BackColor = Color.LightCoral
                Me.Room17.Text = ""
                Me.LabInt.Text = "Stanza del boss!"
                Me.RoomFinal.Text = "+"
                FunBoss()
            End If
        ElseIf (posH = 8) Then
            If (posV = 2) Then
                Me.Room18.BackColor = Color.White
                Me.Room16.Text = ""
                If (Not poz) Then
                    Me.LabInt.Text = "Sei nella stanza numero 18" & vbCrLf & "Questa stanza è vuota "
                Else
                    Me.LabInt.Text = "Sei nella stanza numero 18" & vbCrLf & "Hai trovato una pozione!" & vbCrLf & "Può darti un bonus o un malus casuale!" & vbCrLf & "A: Bevi"
                End If
                Me.Room18.Text = "+"
            End If
        End If

    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        Dim testo As String = "Vita: " & vita & vbCrLf & "Attacco : " & atk & vbCrLf & "Mana: " & mana & vbCrLf & vbCrLf & "Inventario:"
        If (key1) Then
            testo = testo & vbCrLf & "Chiave 1"
        End If
        If (key2) Then
            testo = testo & vbCrLf & "Chiave 2"
        End If
        If (spada) Then
            testo = testo & vbCrLf & "Spada"
        End If
        Me.LabStats.Text = testo
        If (Me.vita <= 0) Then
            Morto()
        End If
        If vitaBoss <= 0 Then
            Vinto()
        End If
    End Sub

    Private Sub Morto()
        Timer1.Stop()
        Me.LabInt.Text = "GAME OVER"
        MessageBox.Show("Sei morto", "Vuoi riprovare?")
        startButton.Enabled = True
        MoveUp.Enabled = False
        MoveDown.Enabled = False
        MoveLeft.Enabled = False
        MoveRight.Enabled = False
        ButtonA.Enabled = False
        ButtonB.Enabled = False

    End Sub
    Private Sub Vinto()
        Timer1.Stop()
        Me.LabInt.Text = "YOU WIN!"
        MessageBox.Show("Complimenti, hai vinto!", "Vuoi giocare di nuovo?")
        startButton.Enabled = True
        MoveUp.Enabled = False
        MoveDown.Enabled = False
        MoveLeft.Enabled = False
        MoveRight.Enabled = False
        ButtonA.Enabled = False
        ButtonB.Enabled = False

    End Sub

    Private Sub Move_Click(sender As Object, e As EventArgs) Handles MoveUp.Click, MoveDown.Click, MoveLeft.Click, MoveRight.Click

        If (posV = 1 And (posH = 1 Or posH = 3 Or posH = 4 Or posH = 6 Or posH = 7)) Then
            If (sender Is MoveUp) Then
                posV += 1
            End If
        ElseIf ((posV = 3 And posH = 2) Or (posV = 2 And posH = 8)) Then
            If (sender Is MoveRight) Then
                posH -= 1
            End If
        ElseIf (posV = 2 And posH = 1) Then
            If (sender Is MoveUp) Then
                posV += 1
            ElseIf (sender Is MoveDown) Then
                posV -= 1
            ElseIf (sender Is MoveLeft) Then
                posH += 1
            End If
        ElseIf (posV = 3 And (posH = 1 Or posH = 4)) Then
            If (sender Is MoveDown) Then
                posV -= 1
            ElseIf (sender Is MoveLeft) Then
                posH += 1
            End If
        ElseIf (posV = 2 And posH = 2) Then
            If (sender Is MoveRight) Then
                posH -= 1
            ElseIf (sender Is MoveLeft And key1) Then
                posH += 1
            End If
        ElseIf (posV = 2 And (posH = 3 Or posH = 6)) Then
            If (sender Is MoveRight) Then
                posH -= 1
            ElseIf (sender Is MoveLeft) Then
                posH += 1
            ElseIf (sender Is MoveDown) Then
                posV -= 1
            End If
        ElseIf (posV = 2 And (posH = 7 Or posH = 4)) Then
            If (sender Is MoveDown) Then
                posV -= 1
            ElseIf (sender Is MoveUp) Then
                posV += 1
            ElseIf (sender Is MoveRight) Then
                posH -= 1
            ElseIf (sender Is MoveLeft) Then
                posH += 1
            End If
        ElseIf (posH = 5 And posV = 2) Then
            If (sender Is MoveUp) Then
                posV += 1
            ElseIf (sender Is MoveRight) Then
                posH -= 1
            ElseIf (sender Is MoveLeft) Then
                posH += 1
            End If
        ElseIf (posV = 3 And posH = 5) Then
            If (sender Is MoveDown) Then
                posV -= 1
            ElseIf (sender Is MoveRight) Then
                posH -= 1
            End If
        ElseIf (posV = 3 And posH = 7) Then
            If (sender Is MoveDown) Then
                posV -= 1
            ElseIf (sender Is MoveUp And key1 And key2) Then
                posV += 1
            End If
        End If


        Posizione()


    End Sub


    Public Sub Interazione(sender As Object, e As EventArgs) Handles ButtonA.Click, ButtonB.Click

        If (posV = 3 And posH = 2) Then
            If (sender Is ButtonA) Then
                key1 = True

            End If
        ElseIf (posV = 3 And posH = 5) Then
            If (sender Is ButtonA) Then
                key2 = True
            End If
        ElseIf (posV = 1 And posH = 4 And Not spada) Then
            If (sender Is ButtonA) Then
                spada = True
                atk += 30
            End If
        ElseIf (posV = 2 And posH = 8 And poz) Then
            If (sender Is ButtonA) Then
                Dim cat As Integer
                Dim eff As Integer
                cat = randomizer.Next(1, 3)
                If (cat = 1) Then
                    eff = randomizer.Next(-20, 50)
                    vita += eff
                Else
                    eff = randomizer.Next(-5, 20)
                    atk += eff
                End If
                poz = False
            End If
        End If

        If (boss) Then
            If (sender Is ButtonA) Then
                vitaBoss -= randomizer.Next(atk - 20, atk)
                vita -= randomizer.Next(atkBoss - 15, atkBoss)
                LabInt.Text = "Vita Boss: " & vitaBoss & vbCrLf & "A: Attacca" & " B: Curati"
            End If
            If ((sender Is ButtonB) And (mana > 0)) Then
                mana -= 10
                vita += 25
                vita -= randomizer.Next(atkBoss - 30, atkBoss)
                LabInt.Text = "Vita Boss: " & vitaBoss & vbCrLf & "A: Attacca" & " B: Curati"
            ElseIf((sender Is ButtonB) And (mana <= 0)) Then
                    LabInt.Text = "Vita Boss: " & vitaBoss & vbCrLf & "Non puoi più curarti, non hai mana" & vbCrLf & " A: Attacca" & " B: Curati"

                End If
            End If

            Posizione()

    End Sub

    Public Sub FunBoss()
        MoveUp.Enabled = False
        MoveDown.Enabled = False
        MoveLeft.Enabled = False
        MoveRight.Enabled = False
        boss = True
        LabInt.Text = "Vita Boss: " & vitaBoss & vbCrLf & "A: Attacca" & " B: Curati"
    End Sub

    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
        Select Case keyData
            Case Keys.Up
                MoveUp.PerformClick()
            Case Keys.Down
                MoveDown.PerformClick()
            Case Keys.Right
                MoveRight.PerformClick()
            Case Keys.Left
                MoveLeft.PerformClick()
            Case Keys.A
                ButtonA.PerformClick()
            Case Keys.B
                ButtonB.PerformClick()
            Case Keys.Enter
                startButton.PerformClick()
        End Select
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

End Class

Link all’applicazione su google drive.

Lascia un commento