I am having two issues with making my Maze Game in VB.NET. I want the levels to be difficult to solve so I am making some *sliding* (NOT MOVING) Panels to interrupt the players.

These are my problems:

1. I want the Panel to slide (Sliding Up and then Down, then Up and Down, then Up...) forever, but it doesn't. It will only slide the times I have written the code.
2. I want the MouseEnter event to work when the mouse touches the Panel during the Panel is sliding.
3. Is it right to use a Timer?

Here are some details and the codes which I have used:

I have first made an Integer = 6 for the speed of sliding.
The timer is not enabled and started. It will be started and enabled when Button1 is clicked.
Timer's interval: 100
Panel's name: Panel1
Timer's name: Timer1

When the user clicks Button1, Timer1 starts, which will slide Panel1. (My code is for moving the panel. I need to slide the panel. IT'S WRONG!)

Timer1's code:

Dim BlockY As Integer = 6
        If Me.LineShape1.Location = New Point(82, 31) Then
            Do Until Me.LineShape1.Location = New Point(82, 307)
                Me.LineShape1.Top = Me.LineShape1.Top + BlockY
        End If
        If Me.LineShape1.Location = New Point(82, 307) Then
            Do Until Me.LineShape1.Location = New Point(82, 31)
                Me.LineShape1.Top = Me.LineShape1.Top - BlockY
        End If

Any help will be appreciated! For further details, please reply.

6 Years
Discussion Span
Last Post by codeorder

See if this helps.
1 Panel, 1 Button, 1 Timer

Public Class Form1
    Private eX, eY As Integer '// get Cursor location.
    Private rCursorBounds As New Rectangle(0, 0, 0, 0) '// rectangle used for Collision check.
    Private BlockY As Integer = 0 '// declared once.

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        eX = e.X : eY = e.Y '// set coordinates for location of cursor.
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        BlockY = 6 '// set speed.
        Timer1.Enabled = Not Timer1.Enabled '// toggle start/stop of timer.
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        With Me.Panel1 '// shorten code.
            If .Location.Y = 31 Then '// use only .Location.Y since it does not change .Location.X.
                Do Until .Location.Y = 307
                    .Top = .Top + BlockY
                    getCollision(Panel1) '// check for Collision.
                    Application.DoEvents() '// let the getCollision Sub finish up.
            End If
            If .Location.Y = 307 Then
                Do Until .Location.Y = 31
                    .Top = .Top - BlockY
            End If
        End With
    End Sub

    Private Sub getCollision(ByVal selectedPanelToCheckForCollision As Panel)
        With rCursorBounds '// only need to set .X and .Y of the rectangle, since width and height is not needed.
            .X = eX : .Y = eY '// set the rectangle location to the location of the Cursor.
        End With
        '// check if Panel.Bounds Intersects With the rectangle of the cursor location.
        If selectedPanelToCheckForCollision.Bounds.IntersectsWith(rCursorBounds) Then
            Timer1.Enabled = False '// disable timer to not display tons of MsgBoxes.
            MsgBox("Collision Detected!", MsgBoxStyle.Critical)
            Timer1.Enabled = True '// enable timer back.
        End If
    End Sub
End Class

You only needed to check for Panel1.Bounds.IntersectsWith , but I cleaned up the code a little, hopefully for better performance.


Oh thanks! This helped me a lot! I have now finished making my game with the help of your code. I want to now make a moving road. My friend gave me the code for moving the road, but I need to change it's speed. Can you please tell me how to change the speed of the moving road?


Change BlockY = 6 '// set speed. to any speed needed just before starting the Timer.
Glad I could help otherwise.:)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.