Mittwoch, 6. Juli 2011

Visual Basic 2010 Pacman Batman Spiel programmieren SelMcKenzie Selzer-McKenzie

Visual Basic 2010 Pacman Batman Spiel programmieren SelMcKenzie Selzer-McKenzie
Author D.Selzer-McKenzie

Heute zeige ich Ihnen, wie man ein komplettes Pacman Batman Spiel programmiert. Der Start erfolgt dann auf der Tatsatur über die Pfeiltasten.
Der Code ist natürlich umfangreich und ich habe ihn hier relativ gross geschrieben. Sie müssten das Video also seitenweise anhalten, um den Code abschreiben zu können.
Sie sehen, das Spiel läuft wunderbar.
Selzer-McKenzie
Der Code lautet:
Public Class Form1
    Inherits System.Windows.Forms.Form
    Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Short
    Const VK_ESC = &H1B
    Dim blockxPosition As Integer = 0
    Dim direction As String
    Dim keypressed As String
    Private bigdots() As PictureBox = {}
    Private pict() As PictureBox = {}

    Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim txt As TextBox = DirectCast(sender, TextBox)
        Debug.WriteLine(txt.Name & ": [" & txt.Text & "]")
    End Sub

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        WhichKey = e.KeyCode
        TurningSub()
        If ShouldExit = True Then
            ShouldExit = False
            Exit Sub
        End If
        If e.KeyCode = 39 Then
            If turning = True And PacMan.Top Mod 32 <> 0 Then Exit Sub
            Timer2.Enabled = False
            Timer3.Enabled = False
            Timer4.Enabled = False
            Timer1.Enabled = True
        End If
        If e.KeyCode = 37 Then
            If turning = True And PacMan.Top Mod 32 <> 0 Then Exit Sub
            Timer1.Enabled = False
            Timer3.Enabled = False
            Timer4.Enabled = False
            Timer2.Enabled = True
        End If
        If e.KeyCode = 40 Then
            If turning = True And PacMan.Left Mod 32 <> 0 Then Exit Sub
            Timer1.Enabled = False
            Timer2.Enabled = False
            Timer4.Enabled = False
            Timer3.Enabled = True
        End If
        If e.KeyCode = 38 Then
            If turning = True And PacMan.Left Mod 32 <> 0 Then Exit Sub
            Timer1.Enabled = False
            Timer2.Enabled = False
            Timer3.Enabled = False
            Timer4.Enabled = True
        End If
        If e.KeyCode = VK_ESC Then End
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim i As Integer
        For i = 0 To 317
            i = pict.Length
            i = bigdots.Length
            ReDim Preserve pict(i)
            ReDim Preserve bigdots(i)
            pict(i) = New PictureBox
            bigdots(i) = New PictureBox
            pict(i).SizeMode = PictureBoxSizeMode.AutoSize
            bigdots(i).SizeMode = PictureBoxSizeMode.AutoSize
            pict(i).Image = PictureBox1.Image
            bigdots(i).Image = dot03.Image
            pict(i).Visible = False
            bigdots(i).Visible = False
            pict(i).Name = "TextBox" & i.ToString()
            bigdots(i).Name = "TextBox" & i.ToString()
            If pict.Length > 1 Then
                pict(i).Left = pict(i - 1).Left
                bigdots(i).Left = bigdots(i - 1).Left
                pict(i).Top = pict(i - 1).Top + pict(i - 1).Height + 4
                bigdots(i).Top = bigdots(i - 1).Top + bigdots(i - 1).Height + 4
                pict(i).Size = pict(i - 1).Size
                bigdots(i).Size = bigdots(i - 1).Size
            End If
            pict(i).Tag = i
            bigdots(i).Tag = i
            AddHandler pict(i).TextChanged, AddressOf TextBox_TextChanged
            AddHandler bigdots(i).TextChanged, AddressOf TextBox_TextChanged
            Me.Controls.Add(pict(i))
            Me.Controls.Add(bigdots(i))
        Next i
        reset()
        loadMaze()


    End Sub
    Sub loadMaze()
        turning = False
        PacManRow = 2
        PacManColumn = 2
        Dim a(13) As String
        Dim count As Integer
        Dim x As Integer
        Dim i, j, y, rows, columns, movedown, moveacross As Integer
        rows = 14
        columns = 20
        first = first + 1
        If first > 3 Then first = 3
        'first = 3
        If first = 1 Then
            a(1) = "1111111111111111111"
            a(2) = "1222222222212222231"
            a(3) = "1211121111222111121"
            a(4) = "1211121111112111121"
            a(5) = "1211121000012111121"
            a(6) = "1222221000012111121"
            a(7) = "1211121000012111121"
            a(8) = "1211121111112222221"
            a(9) = "1211122222222111121"
            a(10) = "1222112111112111121"
            a(11) = "1212112111112111121"
            a(12) = "1322222222222222231"
            a(13) = "1111111111111111111"
        End If
        If first = 2 Then
            a(1) = "1111111111111111111"
            a(2) = "1222221111112222231"
            a(3) = "1211122222222111121"
            a(4) = "1211121111112111221"
            a(5) = "1211121000012111211"
            a(6) = "1222221000012222211"
            a(7) = "1211121000012111211"
            a(8) = "1222221111112222221"
            a(9) = "1211122222222111121"
            a(10) = "1211112111112111121"
            a(11) = "1211112111112111121"
            a(12) = "1322222222222222231"
            a(13) = "1111111111111111111"
        End If
        If first = 3 Then
            a(1) = "1111111111111111111"
            a(2) = "1222222122212222231"
            a(3) = "1211212221222111121"
            a(4) = "1221221111112121221"
            a(5) = "1121121000012121211"
            a(6) = "1222221000012222221"
            a(7) = "1121121000012111211"
            a(8) = "1222221111112222221"
            a(9) = "1211122222222111121"
            a(10) = "1212212121212122221"
            a(11) = "1212112121212121121"
            a(12) = "1322222221222222231"
            a(13) = "1111111111111111111"
        End If

        For count = 1 To 20
            For x = 1 To 13
                maze(x, count) = Mid(a(x), count, 1)
            Next x
        Next count

        movedown = pict(0).Height
        moveacross = pict(0).Width
        pict(0).Visible = False
        bigdots(0).Visible = 0
        For i = 0 To rows
            For j = 0 To columns
                y = y + 1
                If first > 1 Then pict(y).Visible = 0
                If maze(i, j) = "1" Then
                    If first = 1 Then pict(y).Image = Wall.Image
                    pict(y).Top = movedown * (i - 1)
                    pict(y).Left = movedown * (j - 1)
                    pict(y).Visible = 1
                End If
                If maze(i, j) <> "1" And first = 1 Then pict(y).Image = Wall.Image
                If maze(i, j) = "2" Then
                    If first = 1 Then
                        bigdots(y).Visible = 1
                    End If
                    bigdots(y).Top = movedown * (i - 1)
                    bigdots(y).Left = movedown * (j - 1)
                    bigdots(y).Visible = 1
                    bead(i, j) = y
                End If
                If maze(i, j) <> "2" And maze(i, j) <> "3" And first = 1 _
                    Then bigdots(i).Image = dot05.Image
                If maze(i, j) = "3" Then
                    If first = 1 Then bigdots(i).Image = dot05.Image
                    bigdots(y).Image = dot05.Image
                    bigdots(y).Top = movedown * (i - 1)
                    bigdots(y).Left = movedown * (j - 1)
                    bigdots(y).Visible = 1
                    bead(i, j) = y
                End If
            Next j
        Next i
        AxWindowsMediaPlayer1.URL = "MeineMusik.mid"
    End Sub

    Sub BADGUYS1CONTROL()
        Dim direct As Integer
        y1 = PacMan.Top - BadMan1.Top
        x1 = PacMan.Left - BadMan1.Left
        If maze(BADman1ROW + 1, BADman1COLUMN) = "1" And maze(BADman1ROW - 1, _
           BADman1COLUMN) = "1" And maze(BADman1ROW, BADman1COLUMN + 1) = "1" _
       Then
            Timer8.Enabled = False
            Timer7.Enabled = False
            Timer5.Enabled = True
            Timer6.Enabled = False
            Exit Sub
        End If
        If maze(BADman1ROW + 1, BADman1COLUMN) = "1" And maze(BADman1ROW - 1, _
           BADman1COLUMN) = "1" And maze(BADman1ROW, BADman1COLUMN - 1) = "1" _
       Then
            Timer8.Enabled = False
            Timer7.Enabled = False
            Timer5.Enabled = False
            Timer6.Enabled = True
            Exit Sub
        End If
        If maze(BADman1ROW, BADman1COLUMN + 1) = "1" _
            And maze(BADman1ROW, BADman1COLUMN - 1) = "1" _
            And maze(BADman1ROW + 1, BADman1COLUMN) = "1" Then
            Timer8.Enabled = False
            Timer7.Enabled = True
            Timer5.Enabled = False
            Timer6.Enabled = False
            Exit Sub
        End If
        If maze(BADman1ROW, BADman1COLUMN + 1) = "1" _
            And maze(BADman1ROW, BADman1COLUMN - 1) = "1" _
            And maze(BADman1ROW - 1, BADman1COLUMN) = "1" Then
            Timer8.Enabled = True
            Timer7.Enabled = False
            Timer5.Enabled = False
            Timer6.Enabled = False
            Exit Sub
        End If
        If maze(BADman1ROW, BADman1COLUMN - 1) = "1" _
            And maze(BADman1ROW - 1, BADman1COLUMN) = "1" Then
            Randomize()
            direct = Int(Rnd() * 2) + 1
            If direct = 1 Then
                Timer8.Enabled = True
                Timer6.Enabled = False
            End If
            If direct = 2 Then
                Timer6.Enabled = True
                Timer8.Enabled = False
            End If
            Timer7.Enabled = False
            Timer5.Enabled = False
            Exit Sub
        End If
        If maze(BADman1ROW, BADman1COLUMN + 1) = "1" _
            And maze(BADman1ROW - 1, BADman1COLUMN) = "1" Then
            Randomize()
            direct = Int(Rnd() * 2) + 1
            If direct = 1 Then
                Timer5.Enabled = True
                Timer8.Enabled = False
            End If
            If direct = 2 Then
                Timer5.Enabled = False
                Timer8.Enabled = True
            End If
            Timer7.Enabled = False
            Timer6.Enabled = False
            Exit Sub
        End If
        If maze(BADman1ROW, BADman1COLUMN - 1) = "1" _
            And maze(BADman1ROW + 1, BADman1COLUMN) = "1" Then
            Randomize()
            direct = Int(Rnd() * 2) + 1
            If direct = 1 Then
                Timer6.Enabled = True
                Timer7.Enabled = False
            End If
            If direct = 2 Then
                Timer7.Enabled = True
                Timer6.Enabled = False
            End If
            Timer8.Enabled = False
            Timer5.Enabled = False
            Exit Sub
        End If
        If maze(BADman1ROW, BADman1COLUMN + 1) = "1" _
            And maze(BADman1ROW + 1, BADman1COLUMN) = "1" Then
            Randomize()
            direct = Int(Rnd() * 2) + 1
            If direct = 1 Then
                Timer5.Enabled = True
                Timer7.Enabled = False
            End If
            If direct = 2 Then
                Timer7.Enabled = True
                Timer5.Enabled = False
            End If
            Timer8.Enabled = False
            Timer6.Enabled = False
            Exit Sub
        End If
        If eatbad = 0 Then
            If BadMan1.Left Mod 32 = 0 And y1 < 0 Then
                If maze(BADman1ROW - 1, BADman1COLUMN) <> "1" Then
                    Timer8.Enabled = False
                    Timer7.Enabled = True
                    Timer5.Enabled = False
                    Timer6.Enabled = False
                    Exit Sub
                End If
            End If
            If BadMan1.Left Mod 32 = 0 And y1 > 0 _
                And maze(BADman1ROW + 1, BADman1COLUMN) <> "1" Then
                Timer7.Enabled = False
                Timer8.Enabled = True
                Timer5.Enabled = False
                Timer6.Enabled = False
                Exit Sub
            End If
            If BadMan1.Top Mod 32 = 0 And x1 > 0 _
                And maze(BADman1ROW, BADman1COLUMN + 1) <> "1" Then
                Timer7.Enabled = False
                Timer8.Enabled = False
                Timer5.Enabled = False
                Timer6.Enabled = True
                Exit Sub
            End If
            If BadMan1.Top Mod 32 = 0 And x1 < 0 _
                And maze(BADman1ROW, BADman1COLUMN - 1) <> "1" Then
                Timer7.Enabled = False
                Timer8.Enabled = False
                Timer5.Enabled = True
                Timer6.Enabled = False
                Exit Sub
            End If
        End If
        If eatbad = 1 Then
            If BadMan1.Left Mod 32 = 0 And y1 < 0 Then
                If maze(BADman1ROW + 1, BADman1COLUMN) <> "1" Then
                    Timer8.Enabled = True
                    Timer7.Enabled = False
                    Timer5.Enabled = False
                    Timer6.Enabled = False
                    Exit Sub
                End If
            End If
            If BadMan1.Left Mod 32 = 0 And y1 > 0 _
                And maze(BADman1ROW - 1, BADman1COLUMN) <> "1" Then
                Timer7.Enabled = True
                Timer8.Enabled = False
                Timer5.Enabled = False
                Timer6.Enabled = False
                Exit Sub
            End If
            If BadMan1.Top Mod 32 = 0 And x1 > 0 _
                And maze(BADman1ROW, BADman1COLUMN - 1) <> "1" Then
                Timer7.Enabled = False
                Timer8.Enabled = False
                Timer5.Enabled = True
                Timer6.Enabled = False
                Exit Sub
            End If
            If BadMan1.Top Mod 32 = 0 And x1 < 0 _
                And maze(BADman1ROW, BADman1COLUMN + 1) <> "1" Then
                Timer7.Enabled = False
                Timer8.Enabled = False
                Timer5.Enabled = False
                Timer6.Enabled = True
                Exit Sub
            End If
        End If
    End Sub
    Sub badguys2control()
        Dim direct As Integer
        y2 = PacMan.Top - BadMan2.Top
        x2 = PacMan.Left - BadMan2.Left
        If maze(BADMAN2ROW + 1, BADMAN2COLUMN) = "1" _
            And maze(BADMAN2ROW - 1, BADMAN2COLUMN) = "1" _
            And maze(BADMAN2ROW, BADMAN2COLUMN + 1) = "1" Then
            Timer10.Enabled = False
            Timer11.Enabled = False
            Timer9.Enabled = True
            Timer12.Enabled = False
            Exit Sub
        End If
        If maze(BADMAN2ROW + 1, BADMAN2COLUMN) = "1" _
            And maze(BADMAN2ROW - 1, BADMAN2COLUMN) = "1" _
            And maze(BADMAN2ROW, BADMAN2COLUMN - 1) = "1" Then
            Timer9.Enabled = False
            Timer11.Enabled = False
            Timer12.Enabled = False
            Timer10.Enabled = True
            Exit Sub
        End If
        If maze(BADMAN2ROW, BADMAN2COLUMN + 1) = "1" _
            And maze(BADMAN2ROW, BADMAN2COLUMN - 1) = "1" _
            And maze(BADMAN2ROW + 1, BADMAN2COLUMN) = "1" Then
            Timer9.Enabled = False
            Timer11.Enabled = True
            Timer10.Enabled = False
            Timer12.Enabled = False
            Exit Sub
        End If
        If maze(BADMAN2ROW, BADMAN2COLUMN + 1) = "1" _
            And maze(BADMAN2ROW, BADMAN2COLUMN - 1) = "1" _
            And maze(BADMAN2ROW - 1, BADMAN2COLUMN) = "1" Then
            Timer12.Enabled = True
            Timer9.Enabled = False
            Timer10.Enabled = False
            Timer11.Enabled = False
            Exit Sub
        End If
        If maze(BADMAN2ROW, BADMAN2COLUMN - 1) = "1" _
            And maze(BADMAN2ROW - 1, BADMAN2COLUMN) = "1" Then
            Randomize()
            direct = Int(Rnd() * 2) + 1
            If direct = 1 Then
                Timer12.Enabled = True
                Timer10.Enabled = False
            End If
            If direct = 2 Then
                Timer10.Enabled = True
                Timer12.Enabled = False
            End If
            Timer9.Enabled = False
            Timer11.Enabled = False
            Exit Sub
        End If
        If maze(BADMAN2ROW, BADMAN2COLUMN + 1) = "1" _
            And maze(BADMAN2ROW - 1, BADMAN2COLUMN) = "1" Then
            Randomize()
            direct = Int(Rnd() * 2) + 1
            If direct = 1 Then
                Timer9.Enabled = True
                Timer12.Enabled = False
            End If
            If direct = 2 Then
                Timer9.Enabled = False
                Timer12.Enabled = True
            End If
            Timer10.Enabled = False
            Timer11.Enabled = False
            Exit Sub
        End If
        If maze(BADMAN2ROW, BADMAN2COLUMN - 1) = "1" _
            And maze(BADMAN2ROW + 1, BADMAN2COLUMN) = "1" Then
            Randomize()
            direct = Int(Rnd() * 2) + 1
            If direct = 1 Then
                Timer10.Enabled = True
                Timer11.Enabled = False
            End If
            If direct = 2 Then
                Timer11.Enabled = True
                Timer10.Enabled = False
            End If
            Timer12.Enabled = False
            Timer9.Enabled = False
            Exit Sub
        End If
        If maze(BADMAN2ROW, BADMAN2COLUMN + 1) = "1" _
            And maze(BADMAN2ROW + 1, BADMAN2COLUMN) = "1" Then
            Randomize()
            direct = Int(Rnd() * 2) + 1
            If direct = 1 Then
                Timer9.Enabled = True
                Timer11.Enabled = False
            End If
            If direct = 2 Then
                Timer11.Enabled = True
                Timer9.Enabled = False
            End If
            Timer12.Enabled = False
            Timer10.Enabled = False
            Exit Sub
        End If
        If eatbad = 0 Then
            If BadMan2.Left Mod 32 = 0 And y2 < 0 Then
                If maze(BADMAN2ROW - 1, BADMAN2COLUMN) <> "1" Then
                    Timer12.Enabled = False
                    Timer11.Enabled = True
                    Timer9.Enabled = False
                    Timer10.Enabled = False
                    Exit Sub
                End If
            End If
            If BadMan2.Left Mod 32 = 0 And y2 > 0 _
                And maze(BADMAN2ROW + 1, BADMAN2COLUMN) <> "1" Then
                Timer11.Enabled = False
                Timer12.Enabled = True
                Timer9.Enabled = False
                Timer10.Enabled = False
                Exit Sub
            End If
            If BadMan2.Top Mod 32 = 0 And x2 > 0 _
                And maze(BADMAN2ROW, BADMAN2COLUMN + 1) <> "1" Then
                Timer11.Enabled = False
                Timer12.Enabled = False
                Timer9.Enabled = False
                Timer10.Enabled = True
                Exit Sub
            End If
            If BadMan2.Top Mod 32 = 0 And x2 < 0 _
                And maze(BADMAN2ROW, BADMAN2COLUMN - 1) <> "1" Then
                Timer11.Enabled = False
                Timer12.Enabled = False
                Timer9.Enabled = True
                Timer10.Enabled = False
                Exit Sub
            End If
        End If
        If eatbad = 1 Then
            If BadMan2.Left Mod 32 = 0 And y2 < 0 Then
                If maze(BADMAN2ROW + 1, BADMAN2COLUMN) <> "1" Then
                    Timer12.Enabled = True
                    Timer11.Enabled = False
                    Timer9.Enabled = False
                    Timer10.Enabled = False
                    Exit Sub
                End If
            End If
            If BadMan2.Left Mod 32 = 0 And y2 > 0 _
                And maze(BADMAN2ROW - 1, BADMAN2COLUMN) <> "1" Then
                Timer11.Enabled = True
                Timer12.Enabled = False
                Timer9.Enabled = False
                Timer10.Enabled = False
                Exit Sub
            End If
            If BadMan2.Top Mod 32 = 0 And x2 > 0 _
                And maze(BADMAN2ROW, BADMAN2COLUMN - 1) <> "1" Then
                Timer11.Enabled = False
                Timer12.Enabled = False
                Timer9.Enabled = True
                Timer10.Enabled = False
                Exit Sub
            End If
            If BadMan2.Top Mod 32 = 0 And x2 < 0 _
                And maze(BADMAN2ROW, BADMAN2COLUMN + 1) <> "1" Then
                Timer11.Enabled = False
                Timer12.Enabled = False
                Timer9.Enabled = False
                Timer10.Enabled = True
                Exit Sub
            End If
        End If
    End Sub

    Sub reset()
        PacMan.Top = 32
        PacMan.Left = 32
        BadMan1.Top = 192
        BadMan1.Left = 256
        BadMan2.Top = 192
        BadMan2.Left = 288
        BadMan1.Image = badman1a.Image
        BadMan2.Image = badman2a.Image
        PacMan.Image = PacClosedRight.Image
        x1 = 0
        y1 = 0
        x2 = 0
        y2 = 0
        y = 0
        win = 0
        score = 0
        BADman1COUNT = 0
    End Sub

    Sub TAKETHEMOUT()
        Timer13.Enabled = True
    End Sub

    Sub TurningSub()
        If WhichKey = 40 Or 38 Then
            If PacMan.Left Mod 32 = 0 Then
                turning = True
                Exit Sub
            End If
        End If
        If WhichKey = 37 Or 39 Then
            If PacMan.Top Mod 32 = 0 Then
                turning = True
                Exit Sub
            End If
        End If

        If WhichKey = 37 Then
            If maze(PacManRow, PacManColumn - 1) = "1" Then
                ShouldExit = True
                Exit Sub
            Else
                turning = True
                Exit Sub
            End If
        End If
        If WhichKey = 39 Then
            If maze(PacManRow, PacManColumn + 1) = "1" Then
                ShouldExit = True
                Exit Sub
            Else
                turning = True
                Exit Sub
            End If
        End If
        If WhichKey = 40 Then
            If maze(PacManRow + 1, PacManColumn) = "1" Then
                ShouldExit = True
                Exit Sub
            Else
                turning = True
                Exit Sub
            End If
        End If
        If WhichKey = 38 Then
            If maze(PacManRow - 1, PacManColumn) = "1" Then
                ShouldExit = True
                Exit Sub
            Else
                turning = True
                Exit Sub
            End If
        End If

    End Sub

    Private Sub eatGhosts_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eatGhosts.Tick
        Static count
        count = count + 1
        If count = 1 Then
            y1 = PacMan.Top + 16 - (BadMan1.Top + 16)
            x1 = PacMan.Left + 16 - (BadMan1.Left + 16)
            If Math.Abs(x1) < Math.Abs(y1) Then

            End If
            If Timer5.Enabled = True _
                And maze(BADman1ROW, BADman1COLUMN + 1) <> "1" Then
                Timer6.Enabled = 1
                Timer7.Enabled = 0
                Timer8.Enabled = 0
                Timer5.Enabled = 0
            End If
            If Timer6.Enabled = True _
                And maze(BADman1ROW, BADman1COLUMN - 1) <> "1" Then
                Timer6.Enabled = 0
                Timer7.Enabled = 0
                Timer8.Enabled = 0
                Timer5.Enabled = 1
            End If
            If Timer7.Enabled = True _
                And maze(BADman1ROW + 1, BADman1COLUMN) <> "1" Then
                Timer8.Enabled = 1
                Timer6.Enabled = 0
                Timer7.Enabled = 0
                Timer5.Enabled = 0
            End If
            If Timer8.Enabled = True _
                And maze(BADman1ROW - 1, BADman1COLUMN) <> "1" Then
                Timer8.Enabled = 0
                Timer6.Enabled = 0

Keine Kommentare:

Kommentar veröffentlichen

Hinweis: Nur ein Mitglied dieses Blogs kann Kommentare posten.