HULP GEZOCHT | SPEL : PONG | - Extra functies

Status
Niet open voor verdere reacties.

AnthoVN

Gebruiker
Lid geworden
8 feb 2012
Berichten
22
Beste programmeurs!

Vandaag besloot ik uit het niets, eens een spelletje te maken.
Aangezien ik echt niets te doen had, besloot ik maar eens pong na te maken.
Dit is mij gelukt

Wel heb ik het probleem, dat de computer onoverwinnelijk is.
Zou iemand mij hiermee kunnen helpen?


code ziet er als volgt uit
Code:
Public Class frmPong
    Dim speed As Single = 17      ' snelheid van de bal
    Dim rndInst As New Random()      ' Random instance
    Dim xVel As Single = Math.Cos(rndInst.Next(5, 10)) * speed
    Dim yVel As Single = Math.Sin(rndInst.Next(5, 10)) * speed

    Dim intscoreSpeler As Integer    'score speler
    Dim intscoreComputer As Integer  'score computer

    Private Sub frmPong_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If e.Y > 5 And e.Y < Me.Height - 40 - picPaddleSpeler.Height Then _
            picPaddleSpeler.Location = New Point(picPaddleSpeler.Location.X, e.Y)
    End Sub

    Private Sub tmrSpel_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrSpel.Tick

        picBal.Location = New Point(picBal.Location.X + xVel, picBal.Location.Y + yVel)

        If picBal.Location.Y < 0 Then
            picBal.Location = New Point(picBal.Location.X, 0)
            yVel = -yVel
        End If


        ' controleer de onderkant
        If picBal.Location.Y > Me.Height - picBal.Size.Height - 45 Then
            picBal.Location = New Point(picBal.Location.X, Me.Height - picBal.Size.Height - 45)
            yVel = -yVel
        End If
        ' controleer de speler  paddle
        If picBal.Bounds.IntersectsWith(picPaddleSpeler.Bounds) Then
            picBal.Location = New Point(picPaddleSpeler.Location.X - picBal.Size.Width, picBal.Location.Y)
            xVel = -xVel
        End If

        ' controleer de computerpaddle
        If picBal.Bounds.IntersectsWith(picPaddleComputer.Bounds) Then
            picBal.Location = New Point(picPaddleComputer.Location.X + picPaddleComputer.Size.Width + 1, picBal.Location.Y)
            xVel = -xVel
        End If

        ' controleer de linker zijkant
        If picBal.Location.X < 0 Then
            intscoreSpeler += 1
            picBal.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
            lblScoreSpeler.Text = Convert.ToString(intscoreSpeler)
        End If

        ' controleer de rechter zijkant
        If picBal.Location.X > Me.Width - picBal.Size.Width - picPaddleSpeler.Width Then
            intscoreComputer += 1
            picBal.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
            lblScoreComputer.Text = Convert.ToString(intscoreComputer)
        End If
        If picBal.Location.Y > 5 And picBal.Location.Y < Me.Height - 40 - picPaddleSpeler.Height Then
            picPaddleComputer.Location = New Point(picPaddleComputer.Location.X, picBal.Location.Y)
        End If
    End Sub

    Private Sub tmrComputer_Tick(sender As System.Object, e As System.EventArgs) Handles tmrComputer.Tick

        If picBal.Location.Y > picPaddleComputer.Location.Y + 64 Then
            picPaddleComputer.Location = New Point(picPaddleComputer.Location.X, picPaddleComputer.Location.Y + 2)
        End If
        If picBal.Location.Y < picPaddleComputer.Location.Y + 64 Then
            picPaddleComputer.Location = New Point(picPaddleComputer.Location.X, picPaddleComputer.Location.Y - 2)
        End If
    End Sub
    Private Sub frmPong_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

        Dim a As Integer
        a = picPaddleSpeler.Location.Y
        Select Case e.KeyCode
            Case Keys.Up
                a -= 20
            Case Keys.Down
                a += 20

            Case Keys.Escape
                Me.Close()
        End Select
        If a > 10 And a < Me.Height - 50 - picPaddleSpeler.Height Then _
        picPaddleSpeler.Location = New Point(picPaddleSpeler.Location.X, a)

    End Sub

    Private Sub frmPong_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Windows.Forms.Cursor.Hide()
    End Sub
End Class

Ook wil ik er extra functies bij toevoegen,
zoals een achtergrondmuziekje, afbeelding wanneer men een punt maakt/tegen krijgt
iemand een idee hoe?

Alvast bedankt, Antho :)
fkw51v.png
 
Laatst bewerkt:
how to play mp3
http://vb-helper.com/howto_play_mp3.html


maar wat ik niet snap,je kan wel pong zo uit je mouw toveren ,maar simpele punten telling lukt je blijkbaar weer niet

Puntentelling lukt me wel,
er staat gewoon 0 - 0 omdat ik het formulier juist opgestart had ;)

De site die je me had gegeven bevat de volgende code
Code:
Private Sub Form_Load()
Dim app_path As String

    app_path = App.Path
    If Right$(app_path, 1) <> "\" Then app_path = app_path _
        & "\"
    txtFile.Text = app_path & "Alice_Cooper-Gimmie.mp3"

    ' Prepare the audio control.
    mmcAudio.Notify = False
    mmcAudio.Wait = True
    mmcAudio.Shareable = False
    mmcAudio.Command = "Close"
End Sub

' Play the MP3 file.
Private Sub cmdPlay_Click()
    If cmdPlay.Caption = "Play" Then
        mmcAudio.FileName = txtFile.Text
        mmcAudio.Command = "Open"
        mmcAudio.Command = "Play"
        cmdPlay.Caption = "Stop"
    Else
        mmcAudio.Command = "Stop"
        mmcAudio.Command = "Close"
    End If
End Sub

Private Sub mmcAudio_StatusUpdate()
    lblAudio.Caption = mmcAudio.Position & "/" & _
        mmcAudio.Length
End Sub

' Prepare the control to play again.
Private Sub mmcAudio_Done(NotifyCode As Integer)
    mmcAudio.Command = "Close"
    lblAudio.Caption = ""
    cmdPlay.Caption = "Play"
End Sub

Private Sub Form_Unload(Cancel As Integer)
    ' Close the multimedia device.
    mmcAudio.Command = "Close"
End Sub
zelf begrijp ik een aantal regels niet,
zoals
Dim app_path As String

app_path = App.Path
If Right$(app_path, 1) <> "\" Then app_path = app_path _
& "\"
en
lblAudio.Caption = mmcAudio.Position & "/" & _
mmcAudio.Length
kan je deze nog even uitleggen/beschrijven?

heb er nu ook de volgende code bijgezet
Code:
 Private Sub frmPong_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

        Dim a As Integer
        a = picPaddleSpeler.Location.Y
        Select Case e.KeyCode
            Case Keys.Up
                a -= 20
            Case Keys.Down
                a += 20

            Case Keys.Escape
                Me.Close()
        End Select
        If a > 10 And a < Me.Height - 50 - picPaddleSpeler.Height Then _
        picPaddleSpeler.Location = New Point(picPaddleSpeler.Location.X, a)

    End Sub

    Private Sub frmPong_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Windows.Forms.Cursor.Hide()
    End Sub
Het begint al vorm te krijgen!
maar ik wil het echt perfectioneren, misschien kan ik het dan zelf inleveren als eindwerk! :)
 
Laatst bewerkt:
Het eerste stukje code kijkt of het laatste teken van het path het teken "/" is. Als het dat niet is, dan wordt het alsnog toegevoegd.

Het tweede stukje code verandert de capition van een label. Daarvan kan je makkelijk proberen wat de uitkomst is...


Een afbeelding laten zien als er een punt gescoort/verloren is, kan (vrij) makkelijk. Even ervan uit gaande dat je je punten systeem al hebt, kan je nog een form toevoegen, daarvan de formborderstyle op none zetten en een picturebox erin zetten. Eventueel nog een timer om de form na een tijdje af te sluiten...
Al er dan gescoord wordt bij de computer, dan verander je eerst de image van de picturebox en dan laat je de nieuwe form zien.

Klein voorbeeldje:

Form1:
Code:
    Private Sub Bij_Computer_Gescoord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bij_Computer_Gescoord.Click
        Form2.PictureBox1.ImageLocation = Application.StartupPath & "\BijComputerGescoord.jpg"
        Form2.ShowDialog()
    End Sub

    Private Sub Bij_Player_Gescoord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bij_Player_Gescoord.Click
        Form2.PictureBox1.ImageLocation = Application.StartupPath & "\BijPlayerGescoord.jpg"
        Form2.ShowDialog()
    End Sub

Form2:
Code:
Public Class Form2

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        Me.Close()
    End Sub

    Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Timer1.Interval = 5000
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.Close()
    End Sub
End Class


Succes!:thumb:
 
Bedankt!
Maar ik ben ook ondertussen een pauze knop - stop knop een herstartknop & een HSscrollbar voor de snelheid aan het maken
indien jullie suggeties hebben, dan hoor ik het graag :)
 
Laatst bewerkt:
voor wie geïnteresseerd is,
dit is wat ik ervan heb gemaakt
Code:
Public Class frmPong
    Private Rood, blauw, groen As Integer
    Dim speed As Single = 15      ' snelheid van de bal
    Dim rndInst As New Random()      ' Random instance
    Dim xVel As Single = Math.Cos(rndInst.Next(5, 10)) * speed
    Dim yVel As Single = Math.Sin(rndInst.Next(5, 10)) * speed

    Dim intscoreSpeler As Integer    'score speler
    Dim intscoreComputer As Integer  'score computer

    Private Sub frmPong_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If e.Y > 5 And e.Y < Me.Height - 40 - picPaddleSpeler.Height Then _
            picPaddleSpeler.Location = New Point(picPaddleSpeler.Location.X, e.Y)
    End Sub

    Private Sub tmrSpel_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrSpel.Tick

        picBal.Location = New Point(picBal.Location.X + xVel, picBal.Location.Y + yVel)

        If picBal.Location.Y < 0 Then
            picBal.Location = New Point(picBal.Location.X, 0)
            yVel = -yVel
        End If


        ' controleer de onderkant
        If picBal.Location.Y > Me.Height - picBal.Size.Height - 45 Then
            picBal.Location = New Point(picBal.Location.X, Me.Height - picBal.Size.Height - 45)
            yVel = -yVel
        End If
        ' controleer de speler  paddle
        If picBal.Bounds.IntersectsWith(picPaddleSpeler.Bounds) Then
            picBal.Location = New Point(picPaddleSpeler.Location.X - picBal.Size.Width, picBal.Location.Y)
            xVel = -xVel
        End If

        ' controleer de computerpaddle
        If picBal.Bounds.IntersectsWith(picPaddleComputer.Bounds) Then
            picBal.Location = New Point(picPaddleComputer.Location.X + picPaddleComputer.Size.Width + 1, picBal.Location.Y)
            xVel = -xVel
        End If

        ' controleer de linker zijkant
        If picBal.Location.X < 0 Then
            intscoreSpeler += 1
            picBal.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
            lblScoreSpeler.Text = Convert.ToString(intscoreSpeler)
        End If

        ' controleer de rechter zijkant
        If picBal.Location.X > Me.Width - picBal.Size.Width - picPaddleSpeler.Width Then
            intscoreComputer += 1
            picBal.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
            lblScoreComputer.Text = Convert.ToString(intscoreComputer)
        End If
        If picBal.Location.Y > 5 And picBal.Location.Y < Me.Height - 40 - picPaddleSpeler.Height Then
            picPaddleComputer.Location = New Point(picPaddleComputer.Location.X, picBal.Location.Y)
        End If
    End Sub

    Private Sub tmrComputer_Tick(sender As System.Object, e As System.EventArgs) Handles tmrComputer.Tick

        If picBal.Location.Y > picPaddleComputer.Location.Y + 64 Then
            picPaddleComputer.Location = New Point(picPaddleComputer.Location.X, picPaddleComputer.Location.Y + 2)
        End If
        If picBal.Location.Y < picPaddleComputer.Location.Y + 64 Then
            picPaddleComputer.Location = New Point(picPaddleComputer.Location.X, picPaddleComputer.Location.Y - 2)
        End If
    End Sub
   

  

    Private Sub btnPauze_Click(sender As System.Object, e As System.EventArgs) Handles btnPauze.Click
        tmrComputer.Enabled = False
        tmrSpel.Enabled = False
        tmrKleur.Enabled = False
        btnHervat.Visible = True
        btnPauze.Visible = False
        RadioButton1.Visible = True
        RadioButton2.Visible = True
        RadioButton3.Visible = True
        btnMuziek.Visible = True


    End Sub

    Private Sub btnHervat_Click(sender As System.Object, e As System.EventArgs) Handles btnHervat.Click
        tmrComputer.Enabled = True
        tmrSpel.Enabled = True
        tmrKleur.Enabled = True
        btnHervat.Visible = False

    End Sub

    Private Sub tmrKleur_Tick(sender As System.Object, e As System.EventArgs) Handles tmrKleur.Tick


        Rood = Int(Rnd() * 255)
        blauw = Int(Rnd() * 255)
        groen = Int(Rnd() * 255)

        picBal.BackColor = Color.FromArgb(Rood, blauw, groen)
    End Sub

    Private Sub frmPong_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        MessageBox.Show("Gelieve het niveau te kiezen", "Niveau", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        HScrollBar1.Visible = False
        btnHervat.Visible = False
        btnPlayMusic.Visible = False
        btnStopMusic.Visible = False
        btnPauseMusic.Visible = False
        btnHideMusic.Visible = False
        Playlist.Visible = False
        AxWindowsMediaPlayer1.Visible = False
        btnPauze.Visible = False
        btnMuziek.Visible = False
       
         
    End Sub

    Private Sub HScrollBar1_Scroll(sender As System.Object, e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll


        
        speed = HScrollBar1.Value

    End Sub
    

    Private Sub RadioButton1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged
         Select True
            Case RadioButton1.Checked
                picPaddleComputer.Size = New System.Drawing.Size(16, 128)
                picPaddleSpeler.Size = New System.Drawing.Size(16, 128)
        End Select

    End Sub

    Private Sub RadioButton2_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton2.CheckedChanged
        Select Case True
            Case RadioButton2.Checked
                picPaddleComputer.Size = New System.Drawing.Size(16, 64)
                picPaddleSpeler.Size = New System.Drawing.Size(16, 64)
                HScrollBar1.Visible = False
        End Select

    End Sub

    Private Sub RadioButton3_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton3.CheckedChanged
        Select Case True

            Case RadioButton3.Checked
                picPaddleComputer.Size = New System.Drawing.Size(16, 44)
                picPaddleSpeler.Size = New System.Drawing.Size(16, 44)
                HScrollBar1.Visible = True
        End Select
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        tmrComputer.Enabled = True
        tmrSpel.Enabled = True
        tmrKleur.Enabled = True
        Button1.Visible = False
        btnPauze.Visible = True

        RadioButton1.Visible = False
        RadioButton2.Visible = False
        RadioButton3.Visible = False
    End Sub

    Private Sub btnMuziek_Click(sender As System.Object, e As System.EventArgs) Handles btnMuziek.Click
        importdiag.ShowDialog()
        btnPlayMusic.Visible = True
        btnStopMusic.Visible = True
        btnPauseMusic.Visible = True
        btnHideMusic.Visible = True
        Playlist.Visible = True
        AxWindowsMediaPlayer1.Visible = True


    End Sub

    Private Sub importdiag_FileOk(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles importdiag.FileOk
        For Each track As String In importdiag.FileNames
            Playlist.Items.Add(track)
        Next
    End Sub

    Private Sub btnplay_Click(sender As System.Object, e As System.EventArgs) Handles btnPlayMusic.Click
        AxWindowsMediaPlayer1.URL = Playlist.SelectedItem
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles btnStopMusic.Click
        AxWindowsMediaPlayer1.Ctlcontrols.stop()
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles btnPauseMusic.Click
        AxWindowsMediaPlayer1.Ctlcontrols.pause()
    End Sub


    Private Sub btnHideMusic_Click(sender As System.Object, e As System.EventArgs) Handles btnHideMusic.Click
        btnPlayMusic.Visible = False
        btnStopMusic.Visible = False
        btnPauseMusic.Visible = False
        Playlist.Visible = False
        AxWindowsMediaPlayer1.Visible = False
        btnHideMusic.Visible = False
        btnMuziek.Visible = False


    End Sub
End Class

Functies:
Bal veranderd continue van kleur
Muziek kan je laten afspelen tijdens het spelen indien je op de button pauze klikt.

Snelheid HScrollbar (DEFECT) - Werkt niet [HULP MAG]
3 niveau's
niveau 1: normaal
Niveau 2: kleinere paletten
Niveau 3: Kleinere paletten + HScrollbaar voor snelheid te bepalen (WERKT NIET)

Enjoy playing pong! :)
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan