1

I have these three images that I have drawn to my form.

    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)

But I want to make a Boolean expression for when they are clicked so I can trigger the events to load the game mode selected.

Do I do this through resource code or is there a simply way to do this. My idea seems like it would be bad and not syntaxically correct.

Edit: I've gotten to this:

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

But not really sure where to go from this. Apparently "ButtonEasy.Mouseclick" Handles throws an error "WithEvents variable undefined". Not sure where to go from here.

Thanks in advance!

Edit2: After help from LarsTech I've gotten an Enum in and this:

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

How do I call this in my loop? Currently I have the loop wait for Asynchkeypress to set timescale to 300 which starts the game.

1 Answer 1

1

Is there a reason you don't actually use Buttons to do this?

In any case, you probably should have a class for all that information, which image, which rectangle, etc. This button class would also hold the IsPushed logic.

But for what you currently have, having an enum would probably help:

Public Enum Difficulty
  None
  Easy
  Medium
  Hard
End Enum

Then in the MouseDown event:

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
Sign up to request clarification or add additional context in comments.

10 Comments

I think this is exactly what I was looking for! I'm going to fix it up a bit to fit my code better and I'll come back in a few minutes with the results.
Ok, I added the new code to my post, just not sure how to call it in my loop.
@user1041950 You're not making this easy. What do you mean by loop? WinForms uses events. If you click on the form and you are handling the mousedown event, the code that is in my example will run. You don't need a separate "GameMode" variable, just move the level variable to the class level.
Ok, what I was doing before was making a loop to prepare the game to launch, and I know I probably could have done it better but its probably too late. How do I make the event on the mousedown only work when call "choosegamemode" is up, handle the mousedown event in that sub?
@user1041950 It "sounds" like you have one form trying to handle all of these different "views" of the game. That's not the easiest way to do things in WinForms. But yes, you should have another enum for GameView, for Setup or Play, and you paint and handle mouse events based on which view you are in. Am I close to understanding this?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.