1

我有这三个图像,我已经绘制到我的表格中。

    GraphicsBuffer.DrawImage(ButtonEasy, New Rectangle(25, 330, 100, 50), 0, 0, 100, 50, GraphicsUnit.Pixel, ImageAttributes)
    GraphicsBuffer.DrawImage(ButtonMedium, New Rectangle(150, 330, 100, 50), 0, 0, 100, 50, GraphicsUnit.Pixel, ImageAttributes)
    GraphicsBuffer.DrawImage(ButtonHard, New Rectangle(275, 330, 100, 50), 0, 0, 100, 50, GraphicsUnit.Pixel, ImageAttributes)

但是我想为它们被单击时创建一个布尔表达式,以便我可以触发事件以加载所选的游戏模式。

我是通过资源代码执行此操作还是有一种简单的方法可以执行此操作。我的想法似乎很糟糕而且语法不正确。

编辑:我已经做到了:

Private Sub ButtonEasy_MouseClick(ByVal sender As Object, ByVal e As MouseEventArgs) _
 Handles ButtonEasy.MouseClick

    Dim buttonEasyRect = New Rectangle(25, 330, 100, 50)
    If buttonEasyRect.Contains(e.Location) Then

    End If

End Sub

但不确定从哪里开始。显然“ButtonEasy.Mouseclick”句柄会引发错误“WithEvents variable undefined”。不知道从这里去哪里。

提前致谢!

Edit2:在 LarsTech 的帮助下,我得到了一个枚举,并且这个:

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseDown
    Dim level As Difficulty = Difficulty.None
    If e.Button = MouseButtons.Left Then
    End If

    If New Rectangle(25, 330, 100, 50).Contains(e.Location) Then
        level = Difficulty.Easy
    ElseIf New Rectangle(150, 330, 100, 50).Contains(e.Location) Then       
        level = Difficulty.Medium
    ElseIf New Rectangle(275, 330, 100, 50).Contains(e.Location) Then
        level = Difficulty.Hard
    End If

    If level = Difficulty.Easy Then
        GameMode = 1
    ElseIf level = Difficulty.Medium Then
        GameMode = 2
    ElseIf level = Difficulty.Hard Then
        GameMode = 3
    End If

End Sub

我如何在我的循环中调用它?目前我有循环等待 Asynchkeypress 将时间刻度设置为 300 以启动游戏。

4

1 回答 1

1

您是否有理由不使用 Buttons 来执行此操作?

在任何情况下,您可能都应该为所有这些信息创建一个类,包括哪个图像、哪个矩形等。这个按钮类也将包含 IsPushed 逻辑。

但是对于您目前拥有的东西,拥有一个枚举可能会有所帮助:

Public Enum Difficulty
  None
  Easy
  Medium
  Hard
End Enum

然后在 MouseDown 事件中:

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseDown
  Dim level As Difficulty = Difficulty.None

  If e.Button = MouseButtons.Left Then
    If New Rectangle(25, 330, 100, 50).Contains(e.Location) Then
      level = Difficulty.Easy
    ElseIf New Rectangle(150, 330, 100, 50).Contains(e.Location) Then
      level = Difficulty.Medium
    ElseIf New Rectangle(275, 330, 100, 50).Contains(e.Location) Then
      level = Difficulty.Hard
    End If
  End If

  If level <> Difficulty.None Then
    MessageBox.Show("You are playing " & level.ToString)
  End If
End Sub
于 2011-11-14T20:01:43.120 回答