Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7751689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:36:44+00:00 2026-06-01T11:36:44+00:00

I’m building a maze application in visual basic. The game relies on two variables,

  • 0

I’m building a maze application in visual basic. The game relies on two variables, X and Y which are integers. There’s a timer which basically invalidates the whole form for a redraw. Now my question is, that there are various squares and rectangles dotted around the form. How would I create a handler, or something as such to detect if the square that the form draws is touching these objects?

Code:

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

Oh yes – the player must use the arrow keys to change the X and Y, and when it redraws, it will move.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:36:46+00:00Added an answer on June 1, 2026 at 11:36 am

    I don’t see the need for the timer in your example since you just need to invalidate whenever the user is pressing the keyboard.

    For object collision, you would need a list of objects that have their rectangle coordinates, and then just test for an intersection:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.