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
Contributors
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4392
Joined: Tue Aug 04, 2009 1:47 am

A-maze-ing Game Tutorial
CodenStuff
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:
Code: Select all
Imports VB = Microsoft.VisualBasic
Then directly underneath "Public Class Form" we need to make a mouse locator and declare some functions so add this code underneath that:
Code: Select all
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
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:
Code: Select all
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)
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.

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
Label1.Text = ImpactColor
and that will show you the RGB value of the pixel you hover over.

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
If ImpactColor = "0080FF" Then
                MsgBox("You set off the alarm")
            End If
            If ImpactColor = "FF8040" Then
                MsgBox("You set off the alarm")
            End If
"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.

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 :D

Happy coding! cooll;
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
Nery
Co-Admin Moderator
Co-Admin Moderator
Posts: 1117
Joined: Mon Sep 07, 2009 8:11 pm

Re: A-maze-ing Game Tutorial
Nery
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!
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

Re: A-maze-ing Game Tutorial
GoodGuy17
Mouse hover would delay the reaction of the maze game, so mouse move is more efficient. Just thought I'd let you know.
User avatar
Nery
Co-Admin Moderator
Co-Admin Moderator
Posts: 1117
Joined: Mon Sep 07, 2009 8:11 pm

Re: A-maze-ing Game Tutorial
Nery
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
Return to “Tutorials”