none
Program that interacts png within a windows form to the game board created RRS feed

  • Question

  • Morning all, hope you are well. Does anybody have a program that can interact with a mouseclick? so I can either move it 3 forward back left or right etc. If you have a code for it feel free to post, I develop greater insight using a code and analysing it effectively.
    Thursday, March 9, 2017 9:41 AM

Answers

  • Here is a start.

    Public Class GameMouseJoyStickGetGold
        Private WithEvents TimerGame As New System.Windows.Forms.Timer With {.Interval = 1000, .Enabled = True}
        Private rectJoyStick, rectGrid As Rectangle
        Private ptSprite, ptGold As Point
        Private szGrid As Size = New Size(10, 10)
        Private GameStage, GameCount As Integer
        Private rand As New Random(Now.Millisecond)
    
        Private Sub Form11_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Text = "Chase That Gold!"
            DoubleBuffered = True
            Form11_Resize(0, Nothing)
        End Sub
    
        Private Sub Form11_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            e.Graphics.Clear(Color.Black)
    
            DrawGrid(e.Graphics, rectGrid)
    
            DrawSprite(e.Graphics, ptGold, Color.Gold)
            DrawSprite(e.Graphics, ptSprite, Color.Red)
    
            DrawJoystick(e.Graphics, rectJoyStick)
    
            'draw headline
            Using f As New Font("arial", 14, FontStyle.Bold),
                br As New SolidBrush(Color.Yellow)
    
                Select Case GameStage
                    Case 0  'start
                        e.Graphics.DrawString("Double Click to Play", f, br, 50, 50)
                    Case 1  'running
                        e.Graphics.DrawString(GameCount.ToString, f, br, 20, 0)
                    Case 2  'looser
                        e.Graphics.DrawString("You Loose!", f, br, 50, 50)
                    Case 3  'winner
                        e.Graphics.DrawString("You Win the Gold!", f, br, 50, 50)
                End Select
            End Using
        End Sub
    
        Private Sub Form11_Resize(sender As Object, e As EventArgs) Handles Me.Resize
            Dim w As Integer = CInt(ClientSize.Width / 4)
            Dim w1 As Integer = w
            If w < 60 Then w = 30
            If w > 200 Then w = 200
    
            rectJoyStick = New Rectangle(ClientSize.Width - (w + 2), ClientSize.Height - (w + 2), w, w)
    
            Dim border As Integer = 10
            w = (3 * w1) - (2 * border)
            If w < 30 Then w = 30
            rectGrid = New Rectangle(border, border, w, w)
    
            Invalidate()
        End Sub
    
        Private Sub Form11_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
            If GameStage = 1 Then MoveSprite(e.Location)  'running
        End Sub
    
        Private Sub Form11_DoubleClick(sender As Object, e As EventArgs) Handles Me.DoubleClick
            If Not GameStage = 1 Then
                ResetGame()
                GameStage = 1 'start running
            End If
        End Sub
    
        Private Sub TimerGame_Tick(sender As Object, e As EventArgs) Handles TimerGame.Tick
            Select Case GameStage
                Case 1  'running
                    GameCount -= 1
                    If GameCount <= 0 Then GameStage = 2  'time ran out looser
                Case 2, 3 'finish countdown
                    GameCount += 1
                    If GameCount > 5 Then GameStage = 0
            End Select
    
            Invalidate()
        End Sub
    
        Private Sub MoveSprite(thisLocation As Point)
            Dim joyButton As Integer = GetJoyButton(thisLocation)
    
            'joybutton = -1 not selected
            'directions:
            '0 1 2
            '3 4 5
            '6 7 8
    
            Select Case joyButton
                Case 0 : ptSprite = New Point(ptSprite.X - 1, ptSprite.Y - 1)
                Case 1 : ptSprite = New Point(ptSprite.X, ptSprite.Y - 1)
                Case 2 : ptSprite = New Point(ptSprite.X + 1, ptSprite.Y - 1)
    
                Case 3 : ptSprite = New Point(ptSprite.X - 1, ptSprite.Y)
                Case 4 : ptSprite = New Point(ptSprite.X, ptSprite.Y)
                Case 5 : ptSprite = New Point(ptSprite.X + 1, ptSprite.Y)
    
                Case 6 : ptSprite = New Point(ptSprite.X - 1, ptSprite.Y + 1)
                Case 7 : ptSprite = New Point(ptSprite.X, ptSprite.Y + 1)
                Case 8 : ptSprite = New Point(ptSprite.X + 1, ptSprite.Y + 1)
            End Select
    
            If ptSprite.X < 0 Then ptSprite.X = 0
            If ptSprite.X > szGrid.Width - 1 Then ptSprite.X = szGrid.Width - 1
            If ptSprite.Y < 0 Then ptSprite.Y = 0
            If ptSprite.Y > szGrid.Height - 1 Then ptSprite.Y = szGrid.Height - 1
    
            'check for collision
            If ptSprite = ptGold Then GameStage = 3 'winner
    
            Invalidate()
    
        End Sub
    
        Private Sub ResetGame()
            Dim x As Integer = rand.Next(0, szGrid.Width + 1)
            Dim y As Integer = rand.Next(0, szGrid.Height + 1)
            ptSprite = New Point(x, y)
            x = rand.Next(0, szGrid.Width + 1)
            y = rand.Next(0, szGrid.Height + 1)
            ptGold = New Point(x, y)
            GameStage = 0   'idle
            GameCount = 10  'count down
        End Sub
    
        Private Function GetJoyButton(thisPt As Point) As Integer
            Dim dx As Integer = CInt(rectJoyStick.Width / 3)
            Dim dx2 As Integer = CInt(dx / 2)
            Dim rect As Rectangle
            Dim count As Integer
            Dim thisJoyButton = -1
            'joybutton = -1 not selected
            'directions:
            '0 1 2
            '3 4 5
            '6 7 8
    
            For y = 0 To 2 * dx Step dx
                For x = 0 To 2 * dx Step dx
                    rect = New Rectangle(rectJoyStick.X + x, rectJoyStick.Y + y, dx, dx)
                    If rect.Contains(thisPt.X, thisPt.Y) Then
                        thisJoyButton = count
                        Invalidate()
                        Exit For
                    End If
                    count += 1
                Next
            Next
    
            Return thisJoyButton
    
        End Function
    
        Private Function GetDxDy(thisRect As Rectangle) As Size
            Return New Size(CInt(thisRect.Width / szGrid.Width), CInt(thisRect.Height / szGrid.Height))
        End Function
    
        Private Sub DrawSprite(g As Graphics, ptSprite As Point, theColor As Color)
            Dim gridSize As Size = GetDxDy(rectGrid)
            Using br As New SolidBrush(theColor)
                g.FillRectangle(br, rectGrid.X + (ptSprite.X * gridSize.Width),
                                rectGrid.Y + (ptSprite.Y * gridSize.Width), gridSize.Width, gridSize.Height)
            End Using
        End Sub
    
        Private Sub DrawGrid(g As Graphics, rectG As Rectangle)
            Dim gridSize As Size = GetDxDy(rectGrid)
    
            Using p1 As New Pen(Color.Red, 1),
                    p2 As New Pen(Color.DimGray, 1)
    
                g.DrawRectangle(p1, rectG)
    
                For x = rectG.X + gridSize.Width To rectG.Right - (gridSize.Width - 1) Step gridSize.Width
                    g.DrawLine(p2, x, rectG.Y, x, rectG.Bottom)
                Next
                For y = rectG.Y + gridSize.Height To rectG.Bottom - gridSize.Height Step gridSize.Height
                    g.DrawLine(p2, rectG.X, y, rectG.Right, y)
                Next
            End Using
        End Sub
    
        Private Sub DrawJoystick(g As Graphics, rectJ As Rectangle)
            With g
                Using brBg As New SolidBrush(Color.White),
                        p1 As New Pen(Color.Gold, 5),
                        p2 As New Pen(Color.Black, 1)
    
                    .FillRectangle(brBg, rectJ)
                    .DrawRectangle(p1, rectJ)
    
                    Dim dx As Integer = CInt(rectJ.Width / 3)
    
                    For x = dx To 2 * dx Step dx
                        .DrawLine(p2, rectJ.X + x, rectJ.Y, rectJ.X + x, rectJ.Y + rectJ.Height)
                        .DrawLine(p2, rectJ.X, rectJ.Y + x, rectJ.X + rectJ.Width, rectJ.Y + x)
                    Next
    
                End Using
            End With
        End Sub
    
    End Class

    • Marked as answer by Programmer_10 Thursday, March 9, 2017 12:24 PM
    Thursday, March 9, 2017 11:49 AM

All replies

  • Apologies for the HTML error
    Thursday, March 9, 2017 9:56 AM
  • Here is a start.

    Public Class GameMouseJoyStickGetGold
        Private WithEvents TimerGame As New System.Windows.Forms.Timer With {.Interval = 1000, .Enabled = True}
        Private rectJoyStick, rectGrid As Rectangle
        Private ptSprite, ptGold As Point
        Private szGrid As Size = New Size(10, 10)
        Private GameStage, GameCount As Integer
        Private rand As New Random(Now.Millisecond)
    
        Private Sub Form11_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Text = "Chase That Gold!"
            DoubleBuffered = True
            Form11_Resize(0, Nothing)
        End Sub
    
        Private Sub Form11_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            e.Graphics.Clear(Color.Black)
    
            DrawGrid(e.Graphics, rectGrid)
    
            DrawSprite(e.Graphics, ptGold, Color.Gold)
            DrawSprite(e.Graphics, ptSprite, Color.Red)
    
            DrawJoystick(e.Graphics, rectJoyStick)
    
            'draw headline
            Using f As New Font("arial", 14, FontStyle.Bold),
                br As New SolidBrush(Color.Yellow)
    
                Select Case GameStage
                    Case 0  'start
                        e.Graphics.DrawString("Double Click to Play", f, br, 50, 50)
                    Case 1  'running
                        e.Graphics.DrawString(GameCount.ToString, f, br, 20, 0)
                    Case 2  'looser
                        e.Graphics.DrawString("You Loose!", f, br, 50, 50)
                    Case 3  'winner
                        e.Graphics.DrawString("You Win the Gold!", f, br, 50, 50)
                End Select
            End Using
        End Sub
    
        Private Sub Form11_Resize(sender As Object, e As EventArgs) Handles Me.Resize
            Dim w As Integer = CInt(ClientSize.Width / 4)
            Dim w1 As Integer = w
            If w < 60 Then w = 30
            If w > 200 Then w = 200
    
            rectJoyStick = New Rectangle(ClientSize.Width - (w + 2), ClientSize.Height - (w + 2), w, w)
    
            Dim border As Integer = 10
            w = (3 * w1) - (2 * border)
            If w < 30 Then w = 30
            rectGrid = New Rectangle(border, border, w, w)
    
            Invalidate()
        End Sub
    
        Private Sub Form11_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
            If GameStage = 1 Then MoveSprite(e.Location)  'running
        End Sub
    
        Private Sub Form11_DoubleClick(sender As Object, e As EventArgs) Handles Me.DoubleClick
            If Not GameStage = 1 Then
                ResetGame()
                GameStage = 1 'start running
            End If
        End Sub
    
        Private Sub TimerGame_Tick(sender As Object, e As EventArgs) Handles TimerGame.Tick
            Select Case GameStage
                Case 1  'running
                    GameCount -= 1
                    If GameCount <= 0 Then GameStage = 2  'time ran out looser
                Case 2, 3 'finish countdown
                    GameCount += 1
                    If GameCount > 5 Then GameStage = 0
            End Select
    
            Invalidate()
        End Sub
    
        Private Sub MoveSprite(thisLocation As Point)
            Dim joyButton As Integer = GetJoyButton(thisLocation)
    
            'joybutton = -1 not selected
            'directions:
            '0 1 2
            '3 4 5
            '6 7 8
    
            Select Case joyButton
                Case 0 : ptSprite = New Point(ptSprite.X - 1, ptSprite.Y - 1)
                Case 1 : ptSprite = New Point(ptSprite.X, ptSprite.Y - 1)
                Case 2 : ptSprite = New Point(ptSprite.X + 1, ptSprite.Y - 1)
    
                Case 3 : ptSprite = New Point(ptSprite.X - 1, ptSprite.Y)
                Case 4 : ptSprite = New Point(ptSprite.X, ptSprite.Y)
                Case 5 : ptSprite = New Point(ptSprite.X + 1, ptSprite.Y)
    
                Case 6 : ptSprite = New Point(ptSprite.X - 1, ptSprite.Y + 1)
                Case 7 : ptSprite = New Point(ptSprite.X, ptSprite.Y + 1)
                Case 8 : ptSprite = New Point(ptSprite.X + 1, ptSprite.Y + 1)
            End Select
    
            If ptSprite.X < 0 Then ptSprite.X = 0
            If ptSprite.X > szGrid.Width - 1 Then ptSprite.X = szGrid.Width - 1
            If ptSprite.Y < 0 Then ptSprite.Y = 0
            If ptSprite.Y > szGrid.Height - 1 Then ptSprite.Y = szGrid.Height - 1
    
            'check for collision
            If ptSprite = ptGold Then GameStage = 3 'winner
    
            Invalidate()
    
        End Sub
    
        Private Sub ResetGame()
            Dim x As Integer = rand.Next(0, szGrid.Width + 1)
            Dim y As Integer = rand.Next(0, szGrid.Height + 1)
            ptSprite = New Point(x, y)
            x = rand.Next(0, szGrid.Width + 1)
            y = rand.Next(0, szGrid.Height + 1)
            ptGold = New Point(x, y)
            GameStage = 0   'idle
            GameCount = 10  'count down
        End Sub
    
        Private Function GetJoyButton(thisPt As Point) As Integer
            Dim dx As Integer = CInt(rectJoyStick.Width / 3)
            Dim dx2 As Integer = CInt(dx / 2)
            Dim rect As Rectangle
            Dim count As Integer
            Dim thisJoyButton = -1
            'joybutton = -1 not selected
            'directions:
            '0 1 2
            '3 4 5
            '6 7 8
    
            For y = 0 To 2 * dx Step dx
                For x = 0 To 2 * dx Step dx
                    rect = New Rectangle(rectJoyStick.X + x, rectJoyStick.Y + y, dx, dx)
                    If rect.Contains(thisPt.X, thisPt.Y) Then
                        thisJoyButton = count
                        Invalidate()
                        Exit For
                    End If
                    count += 1
                Next
            Next
    
            Return thisJoyButton
    
        End Function
    
        Private Function GetDxDy(thisRect As Rectangle) As Size
            Return New Size(CInt(thisRect.Width / szGrid.Width), CInt(thisRect.Height / szGrid.Height))
        End Function
    
        Private Sub DrawSprite(g As Graphics, ptSprite As Point, theColor As Color)
            Dim gridSize As Size = GetDxDy(rectGrid)
            Using br As New SolidBrush(theColor)
                g.FillRectangle(br, rectGrid.X + (ptSprite.X * gridSize.Width),
                                rectGrid.Y + (ptSprite.Y * gridSize.Width), gridSize.Width, gridSize.Height)
            End Using
        End Sub
    
        Private Sub DrawGrid(g As Graphics, rectG As Rectangle)
            Dim gridSize As Size = GetDxDy(rectGrid)
    
            Using p1 As New Pen(Color.Red, 1),
                    p2 As New Pen(Color.DimGray, 1)
    
                g.DrawRectangle(p1, rectG)
    
                For x = rectG.X + gridSize.Width To rectG.Right - (gridSize.Width - 1) Step gridSize.Width
                    g.DrawLine(p2, x, rectG.Y, x, rectG.Bottom)
                Next
                For y = rectG.Y + gridSize.Height To rectG.Bottom - gridSize.Height Step gridSize.Height
                    g.DrawLine(p2, rectG.X, y, rectG.Right, y)
                Next
            End Using
        End Sub
    
        Private Sub DrawJoystick(g As Graphics, rectJ As Rectangle)
            With g
                Using brBg As New SolidBrush(Color.White),
                        p1 As New Pen(Color.Gold, 5),
                        p2 As New Pen(Color.Black, 1)
    
                    .FillRectangle(brBg, rectJ)
                    .DrawRectangle(p1, rectJ)
    
                    Dim dx As Integer = CInt(rectJ.Width / 3)
    
                    For x = dx To 2 * dx Step dx
                        .DrawLine(p2, rectJ.X + x, rectJ.Y, rectJ.X + x, rectJ.Y + rectJ.Height)
                        .DrawLine(p2, rectJ.X, rectJ.Y + x, rectJ.X + rectJ.Width, rectJ.Y + x)
                    Next
    
                End Using
            End With
        End Sub
    
    End Class

    • Marked as answer by Programmer_10 Thursday, March 9, 2017 12:24 PM
    Thursday, March 9, 2017 11:49 AM
  • Thank you Tommy for your helpful program it provides great insight. Has anyone made a program where you interact with your actual mouse and drag an image about on a game board.
    Thursday, March 9, 2017 12:25 PM
  • Thank you Tommy for your helpful program it provides great insight. Has anyone made a program where you interact with your actual mouse and drag an image about on a game board.

    I am sure someone has. You should be able to find something about how to to drag an image.

    You need to make your questions more specific. Otherwise its just a guess as to exactly what you mean. And if the question is too complex and involves too much then no one will answer.

    You need to learn the basics and then you can create what you want from examples.

    There are lots of ways to drag an image. The best way depends on what the rest of the application is doing. For example you can just drag a picturebox easily. Or you can draw your own image and drag it.

    Why dont you try to come up with something that is similar to what you want the application to do and show us what you have. Explain the overall application. Is it a game? Does it have more than one image? Why does the user drag the image? Where is the image going? etc. Then we can help with specific questions you have about the code.
    Thursday, March 9, 2017 12:44 PM
  • I see, thanks for the pointers :) 

    the specific first part yes its a game and I know there will be more than two picture boxes in the game and all are interacting 

    Thursday, March 9, 2017 7:15 PM
  • Hey tommy, you dont mind highlighting anywhere in the code which shows the actual red block moving about as I mouse click. Im new to programming and not much of an expert
    Thursday, March 9, 2017 9:50 PM
  • Hey tommy, you dont mind highlighting anywhere in the code which shows the actual red block moving about as I mouse click. Im new to programming and not much of an expert

    The block isn't 'moved' as such.  The display is redrawn with the block at its current position.  This position is calculated for each button click. The 'move' occurs because the current position is calculated as an adjustment to the previous position.

    Case 0 : ptSprite = New Point(ptSprite.X - 1, ptSprite.Y - 1)
    'etc


               
    Thursday, March 9, 2017 11:24 PM
  • Hey tommy, you dont mind highlighting anywhere in the code which shows the actual red block moving about as I mouse click. Im new to programming and not much of an expert

    So maybe it is too much to start with? I just picked the example as it seemed to be what you asked for more or less. We dont know your current skill level so you have to decide.

    As Acamar explained, in the example, when you click on the joystick grid, the code determines which grid was clicked and then moves the red sprite location (the point ptSprit) in that direction in the MoveSprite sub. In the paint event the sub DrawSprite is called and it draws the red rectangle at the spirte location.

    The sprite location is represented with the point variable as grid row and column so point (3, 5) is row 3 col 5 on the grid. The drawsprite sub calculates the red rectangle from the ptSprite row, col and draws the red rectangle.

    This could be done many ways. Don't have to have a grid with rows and columns.

    Friday, March 10, 2017 12:10 AM