A-maze-ing Game Tutorial
Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
4 posts
Page 1 of 1
Hello,
In this tutorial I will show you how I made the A-maze-ing game in the competitions section. It isnt too complicated and only uses a small amount of code for the main part.
OK I will start off with the main part of the code which I used to detect wall collisions. First we need to make an import:
If you want to see it working just add a lable control to your form and add this to the bottom of the timer tick event code:
Now how did I use this code for the maze?. First I drew a maze picture in a paint program using blue and orange walls and then used that image as my forms backgroundimage. I needed to detect when the mouse touched a wall on my maze image so I used this detection code directly underneath this line "ImpactColor = VB.Right(Stemp, 2) & Mid(Stemp, 3, 2) & VB.Left(Stemp, 2)" :
Now the problem is because it detects the pixel color underneath the mouse no matter where it is on screen, if say for example the users desktop had orange or blue colors on it and they moved the mouse over it it would set my alarm off which isnt a good thing because I only want the alarm to go off if the mouse is inside my form. So to solved that I added "Timer1.Enabled = False" to the "Form_MouseLeave" event and "Timer1.Enabled = True" to the "Form_MouseEnter" event.
Now it would only detect the color if the mouse was inside my form and it would stop detecting if the mouse left the form.
And thats all there is to it. All I did then was add a button at the start of my maze which enabled the timer and then added 10 pictureboxs to the form used for the tokens and when a picturebox was clicked it would make it invisible and make the next picturebox visible until it got to picturebox 10 which was used as the exit box and to verify that you completed the maze.
Simple and easy
Happy coding! cooll;
In this tutorial I will show you how I made the A-maze-ing game in the competitions section. It isnt too complicated and only uses a small amount of code for the main part.
OK I will start off with the main part of the code which I used to detect wall collisions. First we need to make an import:
Code: Select all
Then directly underneath "Public Class Form" we need to make a mouse locator and declare some functions so add this code underneath that:Imports VB = Microsoft.VisualBasic
Code: Select all
OK now we need to add a timer to our form, enable it and set its interval to "1" and add this code in the timers tick event:Private Structure MOUSELOCATION
Dim x As Integer
Dim y As Integer
End Structure
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Integer, ByVal x As Integer, ByVal y As Integer) As Integer
Private Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As MOUSELOCATION) As Integer
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Integer) As Integer
Code: Select all
That is now our "collision" detection system and the way it works is it detects the color of the pixel which is directly underneath the mouse pointer. No matter where your mouse pointer is on screen it will always detect what color the pixel is underneath it in RGB format for example "FF0000" etc.Dim mousep As MOUSELOCATION
Dim Stemp As String
Dim PixelColor As Integer
Dim Lmouse As Integer
Dim ImpactColor As String
Lmouse = GetWindowDC(0)
Call GetCursorPos(mousep)
PixelColor = GetPixel(Lmouse, mousep.x, mousep.y)
Stemp = VB.Right("000000" & Hex(PixelColor), 6)
ImpactColor = VB.Right(Stemp, 2) & Mid(Stemp, 3, 2) & VB.Left(Stemp, 2)
If you want to see it working just add a lable control to your form and add this to the bottom of the timer tick event code:
Code: Select all
and that will show you the RGB value of the pixel you hover over.Label1.Text = ImpactColor
Now how did I use this code for the maze?. First I drew a maze picture in a paint program using blue and orange walls and then used that image as my forms backgroundimage. I needed to detect when the mouse touched a wall on my maze image so I used this detection code directly underneath this line "ImpactColor = VB.Right(Stemp, 2) & Mid(Stemp, 3, 2) & VB.Left(Stemp, 2)" :
Code: Select all
"0080FF" is the RGB value of the color Blue and "FF8040" is the RGB value of the color Orange which are the 2 colors I used for my maze walls. So everytime the mouse touched one of those colors it set the alarm off, simple.If ImpactColor = "0080FF" Then
MsgBox("You set off the alarm")
End If
If ImpactColor = "FF8040" Then
MsgBox("You set off the alarm")
End If
Now the problem is because it detects the pixel color underneath the mouse no matter where it is on screen, if say for example the users desktop had orange or blue colors on it and they moved the mouse over it it would set my alarm off which isnt a good thing because I only want the alarm to go off if the mouse is inside my form. So to solved that I added "Timer1.Enabled = False" to the "Form_MouseLeave" event and "Timer1.Enabled = True" to the "Form_MouseEnter" event.
Now it would only detect the color if the mouse was inside my form and it would stop detecting if the mouse left the form.
And thats all there is to it. All I did then was add a button at the start of my maze which enabled the timer and then added 10 pictureboxs to the form used for the tokens and when a picturebox was clicked it would make it invisible and make the next picturebox visible until it got to picturebox 10 which was used as the exit box and to verify that you completed the maze.
Simple and easy

Happy coding! cooll;
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
Actually you can also just use the "Mouse Hover" property of each one of the lines, but it would be also a too big code for a thing that you can do with a small part of code.
Good Job, I've just learnt a new way of doing a maze without that boriness of the mouse hover in each line!
Good Job, I've just learnt a new way of doing a maze without that boriness of the mouse hover in each line!
Mouse hover would delay the reaction of the maze game, so mouse move is more efficient. Just thought I'd let you know.
Mouse Move isn't totally efficient, if you move the mouse quickly through the maze you can pass through walls, just like noclip.
4 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023