G
GIHomer
Gast
Hey ho Leute,
von der Schule aus sollen wir ein Projekt machen jeder darf sich ein Spiel aussuchen und soll es programmieren, ich habe mich für "Pong" entschieden.
jetzt habe ich jedoch folgendes Problem das Programm läuft bis hier hin einwandfrei, jedoch erwischt der Computer jeden Ball was es unmöglich macht gegen Ihn zu gewinnen....
Hat vielleicht irgend jemand eine Idee wie ich den CPU dazu bringe mal einen Ball zu verfehlen ? Irgendwie das er für 1-2Sekunden stehen bleibt oder ähnliches ?
Code:
-------------------------------------------------------------------------------------------------
Public Class pongMain
Dim speed As Single = 20 ' Speed
Dim rndInst As New Random(0) ' Random
Dim xVel As Single = Math.Cos(rndInst.Next(5, 20)) * speed
Dim yVel As Single = Math.Sin(rndInst.Next(5, 20)) * speed
' Punkte
Dim compScore As Integer = 0
Dim plrScore As Integer = 0
Private Sub pongMain_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If e.Y > 5 And e.Y < Me.Height - 40 - paddlePlayer.Height Then _
paddlePlayer.Location = New Point(paddlePlayer.Location.X, e.Y)
End Sub
Private Sub gameTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gameTimer.Tick
If gameBall.Location.Y > 5 And gameBall.Location.Y < Me.Height - 40 _
- paddlePlayer.Height Then _
paddleComputer.Location = New Point(paddleComputer.Location.X, gameBall.Location.Y)
gameBall.Location = New Point(gameBall.Location.X + xVel, gameBall.Location.Y + yVel)
' Obere Wand prüfen
If gameBall.Location.Y < 0 Then
gameBall.Location = New Point(gameBall.Location.X, 0)
yVel = -yVel
End If
' Untere Wand prüfen
If gameBall.Location.Y > Me.Height - gameBall.Size.Height - 45 Then
gameBall.Location = New Point(gameBall.Location.X, Me.Height - gameBall.Size.Height - 45)
yVel = -yVel
End If
' Spieler Paddle prüfen
If gameBall.Bounds.IntersectsWith(paddlePlayer.Bounds) Then
gameBall.Location = New Point(paddlePlayer.Location.X - gameBall.Size.Width, _
gameBall.Location.Y)
xVel = -xVel
End If
' Computer Paddle prüfen
If gameBall.Bounds.IntersectsWith(paddleComputer.Bounds) Then
gameBall.Location = New Point(paddleComputer.Location.X + paddleComputer.Size.Width + 1, _gameBall.Location.Y)
xVel = -xVel
End If
' Linke Wand prüfen
If gameBall.Location.X < 0 Then
plrScore += 1
gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
plrScoreDraw.Text = Convert.ToString(plrScore)
End If
' Rechte Wand prüfen
If gameBall.Location.X > Me.Width - gameBall.Size.Width - paddlePlayer.Width Then
compScore += 1
gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
compScoreDraw.Text = Convert.ToString(compScore)
End If
End Sub
Private Sub pongMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
: Windows.Forms.Cursor.Hide()
End Sub
Private Sub pongMain_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
paddlePlayer.Location = New Point(Me.Width - 44, paddlePlayer.Location.Y)
plrScoreDraw.Location = New Point(Me.Width - 54, plrScoreDraw.Location.Y)
End Sub
Private Sub pongMain_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
End Sub
End Class
-------------------------------------------------------------------------------------------------
Mit freundlichen Grüßen,
GIHomer
von der Schule aus sollen wir ein Projekt machen jeder darf sich ein Spiel aussuchen und soll es programmieren, ich habe mich für "Pong" entschieden.
jetzt habe ich jedoch folgendes Problem das Programm läuft bis hier hin einwandfrei, jedoch erwischt der Computer jeden Ball was es unmöglich macht gegen Ihn zu gewinnen....
Hat vielleicht irgend jemand eine Idee wie ich den CPU dazu bringe mal einen Ball zu verfehlen ? Irgendwie das er für 1-2Sekunden stehen bleibt oder ähnliches ?
Code:
-------------------------------------------------------------------------------------------------
Public Class pongMain
Dim speed As Single = 20 ' Speed
Dim rndInst As New Random(0) ' Random
Dim xVel As Single = Math.Cos(rndInst.Next(5, 20)) * speed
Dim yVel As Single = Math.Sin(rndInst.Next(5, 20)) * speed
' Punkte
Dim compScore As Integer = 0
Dim plrScore As Integer = 0
Private Sub pongMain_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If e.Y > 5 And e.Y < Me.Height - 40 - paddlePlayer.Height Then _
paddlePlayer.Location = New Point(paddlePlayer.Location.X, e.Y)
End Sub
Private Sub gameTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gameTimer.Tick
If gameBall.Location.Y > 5 And gameBall.Location.Y < Me.Height - 40 _
- paddlePlayer.Height Then _
paddleComputer.Location = New Point(paddleComputer.Location.X, gameBall.Location.Y)
gameBall.Location = New Point(gameBall.Location.X + xVel, gameBall.Location.Y + yVel)
' Obere Wand prüfen
If gameBall.Location.Y < 0 Then
gameBall.Location = New Point(gameBall.Location.X, 0)
yVel = -yVel
End If
' Untere Wand prüfen
If gameBall.Location.Y > Me.Height - gameBall.Size.Height - 45 Then
gameBall.Location = New Point(gameBall.Location.X, Me.Height - gameBall.Size.Height - 45)
yVel = -yVel
End If
' Spieler Paddle prüfen
If gameBall.Bounds.IntersectsWith(paddlePlayer.Bounds) Then
gameBall.Location = New Point(paddlePlayer.Location.X - gameBall.Size.Width, _
gameBall.Location.Y)
xVel = -xVel
End If
' Computer Paddle prüfen
If gameBall.Bounds.IntersectsWith(paddleComputer.Bounds) Then
gameBall.Location = New Point(paddleComputer.Location.X + paddleComputer.Size.Width + 1, _gameBall.Location.Y)
xVel = -xVel
End If
' Linke Wand prüfen
If gameBall.Location.X < 0 Then
plrScore += 1
gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
plrScoreDraw.Text = Convert.ToString(plrScore)
End If
' Rechte Wand prüfen
If gameBall.Location.X > Me.Width - gameBall.Size.Width - paddlePlayer.Width Then
compScore += 1
gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
compScoreDraw.Text = Convert.ToString(compScore)
End If
End Sub
Private Sub pongMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
: Windows.Forms.Cursor.Hide()
End Sub
Private Sub pongMain_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
paddlePlayer.Location = New Point(Me.Width - 44, paddlePlayer.Location.Y)
plrScoreDraw.Location = New Point(Me.Width - 54, plrScoreDraw.Location.Y)
End Sub
Private Sub pongMain_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
End Sub
End Class
-------------------------------------------------------------------------------------------------
Mit freundlichen Grüßen,
GIHomer
