1

我正在用 Visual Basic 构建一个迷宫应用程序。游戏依赖于两个变量,X 和 Y,它们是整数。有一个计时器基本上使整个表单无效以进行重绘。现在我的问题是,表格周围点缀着各种正方形和矩形。我将如何创建一个处理程序或类似的东西来检测表单绘制的正方形是否接触这些对象?

代码:

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

哦,是的——玩家必须使用箭头键来改变 X 和 Y,当它重绘时,它会移动。

谢谢

4

1 回答 1

0

在您的示例中,我认为不需要计时器,因为您只需要在用户按下键盘时使其无效。

对于对象碰撞,您需要一个具有矩形坐标的对象列表,然后只测试相交:

Public Class Form1
  Dim block As New Rectangle(0, 0, 35, 35)  
  Dim listOfThings As New List(Of Rectangle)

  Public Sub New()
    InitializeComponent()
    Me.DoubleBuffered = True

    listOfThings.Add(New Rectangle(40, 40, 35, 35))
  End Sub

  Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MyBase.KeyDown
    Select Case e.KeyCode
      Case Keys.Up
        MoveBlock(0, -2)
      Case Keys.Down
        MoveBlock(0, 2)
      Case Keys.Left
        MoveBlock(-2, 0)
      Case Keys.Right
        MoveBlock(2, 0)
      Case Keys.Escape
        Me.Close()
    End Select
  End Sub

  Private Sub MoveBlock(ByVal moveX As Integer, ByVal moveY As Integer)
    block.X += moveX
    block.Y += moveY

    For Each r As Rectangle In listOfThings
      If r.IntersectsWith(block) Then
        MessageBox.Show("Game Over")
      End If
    Next

    Me.Invalidate()
  End Sub

  Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles Me.Paint
    e.Graphics.Clear(Color.White)

    For Each r As Rectangle In listOfThings
      e.Graphics.DrawRectangle(Pens.Red, r)
    Next

    e.Graphics.FillRectangle(Brushes.Blue, block)
  End Sub
End Class
于 2012-04-04T13:59:56.010 回答