Ich baue eine Labyrinth-Anwendung in Visual Basic. Das Spiel beruht auf zwei Variablen, X und Y, die ganze Zahlen sind. Es gibt einen Timer, der das ganze Formular für ein Redraw grundsätzlich ungültig macht. Jetzt ist meine Frage, dass es verschiedene Quadrate und Rechtecke gibt, die um die Form herum verteilt sind. Wie würde ich einen Handler oder etwas Ähnliches erstellen, um zu erkennen, ob das Quadrat, das das Formular zeichnet, diese Objekte berührt?VB.NET So erkennen Sie eine Kollision in einem Labyrinth
Code:
Public Class Form1
Const W As Integer = 35 'Width
Const H As Integer = 35 'Height
Dim X As Integer
Dim Y As Integer
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
'Handles if a key is pressed
Select Case e.KeyCode
Case Keys.Up
Y -= 2
Case Keys.Down
Y += 2
Case Keys.Left
X -= 2
Case Keys.Right
X += 2
Case Keys.Escape
Me.Close()
End Select
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = "Maze"
TmrRedraw.Start()
MsgBox("Press ESC to quit")
Cursor.Position = Me.Location
End Sub
Private Sub TmrRedraw_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TmrRedraw.Tick
If CollisionDetect() = False Then
Me.Invalidate()
CheckForWin()
End If
End Sub
Private Function CollisionDetect()
Dim Collision As Boolean = False
'Here is where the problem lies
Return Collision
End Function
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.FillRectangle(Brushes.Blue, X, Y, W, H)
End Sub
Private Sub CheckForWin()
Dim WinSqX As Integer = WinSquare.Location.X
Dim WinSqY As Integer = WinSquare.Location.Y
If X = WinSqX And Y = WinSqY Then
TmrRedraw.Stop()
MsgBox("Congratulations! You won!")
Me.Close()
End If
End Sub
End Class
Ach ja - der Spieler muss die Pfeiltasten verwenden, um die X- und Y zu ändern, und wenn es neu zeichnet, wird es zu bewegen.
Dank
So: Block des Geräts ist der Spieler hier ist Listofthings eine Liste aller darin enthaltenen der Rechtecke auf dem Brett. 1 Frage: Was ist Public Sub New() - soll dies Form1_Load sein? – Azerty560
@ Azerty560 Ja, es ist im Grunde Ihre x, y, w, h-Variablen als ein einfaches Rechteck-Objekt. – LarsTech
Muss ich die Array-Liste der Things manuell mit allen Rechtecken füllen? (Bedeutet das, dass ich tatsächlich absolut nichts auf dem Formular brauche?) Danke für die Antwort though =) – Azerty560