Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So recently i did my 1st code, the snake game. How can i restart the app after losing(eating myself or going to a wall).


Public Class Form1

#Region "Snake"
    Dim snake(1000) As PictureBox
    Dim comp_cobra As Integer = -1
    Dim esquerda_direita As Integer = 0
    Dim cima_baixo As Integer = 0
    Dim m As Integer = 0



    Private Sub cabeça()
        comp_cobra += 1
        snake(comp_cobra) = New PictureBox
        With snake(comp_cobra)
            .Height = 10
            .Width = 10
            .BackColor = Color.Black
            .Top = (pb1.Top + pb1.Bottom) / 2
            .Left = (pb1.Left + pb1.Right) / 2
        End With
        Me.Controls.Add(snake(comp_cobra))
        snake(comp_cobra).BringToFront()

        compi_cobra()
        compi_cobra()
    End Sub

    Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
        Timer1.Start()
        Select Case e.KeyChar
            Case "d"
                esquerda_direita = 10
                cima_baixo = 0

            Case "a"
                esquerda_direita = -10
                cima_baixo = 0
            Case "w"
                esquerda_direita = 0
                cima_baixo = -10
            Case "s"
                esquerda_direita = 0
                cima_baixo = 10

        End Select
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        cabeça()

        create_mouse()
    End Sub

    Private Sub compi_cobra()
        comp_cobra += 1
        snake(comp_cobra) = New PictureBox
        With snake(comp_cobra)
            .Height = 10
            .Width = 10
            .BackColor = Color.Black
            .Top = snake(comp_cobra - 1).Top
            .Left = snake(comp_cobra - 1).Left + 10
        End With
        Me.Controls.Add(snake(comp_cobra))
        snake(comp_cobra).BringToFront()
    End Sub
    Private Sub tsnake_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        For i = comp_cobra To 1 Step -1
            snake(i).Top = snake(i - 1).Top
            snake(i).Left = snake(i - 1).Left
        Next
        snake(0).Top += cima_baixo
        snake(0).Left += esquerda_direita
        colide_parede()
        comer_comida()
        colide_cobra()
    End Sub
#End Region

#Region "Colisão"
    Private Sub colide_parede()
        If snake(0).Left < pb1.Left - 10 Then
            Timer1.Stop()
            MsgBox("Perdeste")

        End If
        If snake(0).Right > pb1.Right + 10 Then
            Timer1.Stop()
            MsgBox("Perdeste")

        End If
        If snake(0).Bottom > pb1.Bottom + 10 Then
            Timer1.Stop()
            MsgBox("Perdeste")

        End If
        If snake(0).Top < pb1.Top - 10 Then
            Timer1.Stop()
            MsgBox("Perdeste")

        End If
    End Sub

    Private Sub colide_cobra()
        For i = 1 To comp_cobra
            If snake(0).Bounds.IntersectsWith(snake(i).Bounds) Then
                Timer1.Stop()
                MsgBox("Perdeste")
            End If
        Next
    End Sub
#End Region

#Region "comida"
    Dim mouse As PictureBox
    Dim r As Random = New Random
    Private Sub create_mouse()
        mouse = New PictureBox
        With mouse
            .Width = 10
            .Height = 10
            .BackColor = Color.Red
            .Top = r.Next(pb1.Top, pb1.Left - 10)
            .Left = r.Next(pb1.Left, pb1.Right - 10)
        End With
        Me.Controls.Add(mouse)
        mouse.BringToFront()


    End Sub
    Private Sub comer_comida()
        If snake(0).Bounds.IntersectsWith(mouse.Bounds) Then
            compi_cobra()
            mouse.Top = r.Next(pb1.Top, pb1.Bottom - 10)
            mouse.Left = r.Next(pb1.Left, pb1.Right - 10)
            m = m + 10
            Label1.Text = m
        End If

    End Sub
#End Region

    Private Sub Button1_Click(sender As Object, e As EventArgs)


    End Sub

    Private Sub pb1_Click(sender As Object, e As EventArgs) Handles pb1.Click

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub Button1_Click_1(sender As Object, e As EventArgs)

    End Sub

    Private Sub Label2_Click(sender As Object, e As EventArgs)


    End Sub

    Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
    End Sub

End Class
Posted
Comments
Sergey Alexandrovich Kryukov 13-Dec-15 11:43am    
Wrong question. You should not restart the application, you should restart the game.
Of course you can restart the application, but you should not do it.
—SA

1 solution

To restart a Windows Forms application, use Application.Restart[^]:
VB.NET
Application.Restart()
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Dec-15 11:44am    
This is a way to do it, by, knowing what the inquirer tried to do, it becomes apparent that this is nothing but a wrong idea.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900