Sign in to follow this  
Followers 0
Morthawt

A challenging block based game I replicated :D Enjoy (Upgrade 2)

8 posts in this topic

#1 ·  Posted (edited)

The purpose of this autoit-created game I made is to remove all the lit blocks so that all lights are off. Red = lit and Blue = not lit. You can either click around and light your own and then unlight them OR you can use the randomize button to create yourself a grid without you clicking.

Sounds easy right? Well you are not toggling 1 dot.. You are toggling in a pattern like:

x
xxx
 x

The more steps you tell it to randomize the more difficult it is to solve!

I have extensively commented this little project of mine in case anyone is interested. During making it I learned something about detecting mouse clicks inside the interface without needing to register messages!

Let me know what you think about this little thing.

(This is a picture of the same device I had when I was a kid lol..)

Posted Image

Upgraded version 2.

Now you can use Ctrl + s and Ctrl + l to save or load the current grid pattern. For example if you have put in a hard level of difficulty like 50 or 100 you can save the current pattern and try multiple times until you get it right, kind of like seeing if you can beat the boss on a game or something lol.

I think that is all I added so far.

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
AutoItSetOption('MouseCoordMode', 2)
Global $hBlock[11][11], $hBlockColour[11][11], $iFreeForm = 0, $iBegun = 0, $iLitCount = 0, $sBlockOff = '0x155098', $sBlockOn = '0xf02424'
;I chose 11 for the limits of the arrays so a zero value won't crash the program. Other checks deal with higher values.

$Form1 = GUICreate("Unblock", 311, 350, -1, -1) ; Creates the initial GUI
GUISetBkColor('0x000000') ; Sets background of the GUI to black
$hButton1 = GUICtrlCreateButton("Randomize!", 5, 318, 85, 25)
$hCheckbox1 = GUICtrlCreateCheckbox("", 295, 310, 25, 25)
$hHide = GUICtrlCreateLabel('Hide randomization?', 190, 315, 105, 15)
GUICtrlSetFont($hHide, 8, 400, 0, "MS Sans Serif")
GUICtrlSetColor($hHide, '0xffffff') ; Sets the text on the label to being white coloured.

$hRandomSteps = GUICtrlCreateInput(12, 95, 320, 49, 21, BitOR($ES_CENTER, $ES_NUMBER))
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")

AdlibRegister('CheckHotkeyRequirement', 100)


For $y = 1 To 10 ; Incrimentally increasing the Y axis after all blocks have been created along the X axis.
For $x = 1 To 10 ; Incrimentally creating blocks along the X axis.

;Below creates labels, positions them and then assigns the blank (unlit) colour of blue
$hBlock[$x][$y] = GUICtrlCreateLabel("", 1 + ($x - 1) + (($x - 1) * 30), 1 + ($y - 1) + (($y - 1) * 30), 30, 30) ; A label acting as a blank block.
$hBlockColour[$x][$y] = 0 ; This value of 0 means it is unlit.. a 1 means it is currently lit.
GUICtrlSetBkColor(-1, $sBlockOff) ; This colours the block with the "unlit" colour.
Next
Next
GUISetState(@SW_SHOW) ; Shows the interface now that all the blocks have been created.

While 1
$nMsg = GUIGetMsg(1)
Switch $nMsg[0]
Case $GUI_EVENT_CLOSE ; Lets the program exit when you hit the X button.
Exit

Case $GUI_EVENT_PRIMARYDOWN ; Waits till you click and release somewhere on the interface client area.

$iPosX = Ceiling(($nMsg[3] - (Ceiling($nMsg[3] / 30))) / 30) ; Compensating for the blank pixles between blocks
$iPosY = Ceiling(($nMsg[4] - (Ceiling($nMsg[4] / 30))) / 30) ; Compensating for the blank pixles between blocks
If $iPosX < 11 And $iPosY < 11 And $iPosX > 0 And $iPosY > 0 Then ; Checking to make sure we don't exceed the bounds of the array
ToggleBlock($iPosX, $iPosY) ; Performs the toggling process
If $iBegun = 0 Then $hTimer = TimerInit() ; Begins counting your time taken upon your first click.
$iBegun = 1
EndIf

Case $hButton1
CreateLevel(GUICtrlRead($hRandomSteps))
$iBegun = 0 ; Resets the variable thus causing the next block press to begin counting your time taken.

Case Else ; Anything that does not match above gets checked here.
If $iBegun = 1 And $iLitCount = 0 Then ; Here I check to see if the player has clicked a square AND the number of lit blocks.
$iBegun = 0 ; Prevents the following message box from recursively firing off until you begin again.

; The below line simply shows you a message box if you have managed to clear all the blocks and tells you how long it took in seconds.
MsgBox(0, 'Completed!', 'You unblocked the grid in ' & Round(TimerDiff($hTimer) / 1000, 1) & ' seconds :)')
EndIf

EndSwitch
WEnd

Func ToggleBlock($_X, $_Y, $_NotHidden = 1) ; This toggles the lit/unlit status of the blocks. Red = lit, Blue = unlit.

If $hBlockColour[$_X][$_Y] = 1 Then ; Central block - Checks if the block is lit or not.
If $_NotHidden Then GUICtrlSetBkColor($hBlock[$_X][$_Y], $sBlockOff) ; Changes the colour of the current block
$hBlockColour[$_X][$_Y] = 0 ; Sets the status array element so we know next time it is currently unlit..
$iLitCount -= 1

Else
If $_NotHidden Then GUICtrlSetBkColor($hBlock[$_X][$_Y], $sBlockOn) ; Changes the colour of the current block
$hBlockColour[$_X][$_Y] = 1 ; Sets the status array element so we know next time it is currently lit..
$iLitCount += 1
EndIf

If ($_X - 1) >= 1 And $hBlockColour[$_X - 1][$_Y] = 1 Then ; Left block - Checks if the related block is within the grid and current un/lit status.

If $_NotHidden Then GUICtrlSetBkColor($hBlock[$_X - 1][$_Y], $sBlockOff) ; Changes the colour of the current block
$hBlockColour[$_X - 1][$_Y] = 0 ; Sets the status array element so we know next time it is currently unlit..
$iLitCount -= 1

ElseIf ($_X - 1) >= 1 And $hBlockColour[$_X - 1][$_Y] = 0 Then ; Left block - Checks if the related block is within the grid and current un/lit status.

If $_NotHidden Then GUICtrlSetBkColor($hBlock[$_X - 1][$_Y], $sBlockOn) ; Changes the colour of the current block
$hBlockColour[$_X - 1][$_Y] = 1 ; Sets the status array element so we know next time it is currently lit..
$iLitCount += 1
EndIf

If ($_X + 1) < 11 And $hBlockColour[$_X + 1][$_Y] = 1 Then ; Right block - Checks if the related block is within the grid and current un/lit status.

If $_NotHidden Then GUICtrlSetBkColor($hBlock[$_X + 1][$_Y], $sBlockOff) ; Changes the colour of the current block
$hBlockColour[$_X + 1][$_Y] = 0 ; Sets the status array element so we know next time it is currently unlit..
$iLitCount -= 1

ElseIf ($_X + 1) < 11 And $hBlockColour[$_X + 1][$_Y] = 0 Then ; Right block - Checks if the related block is within the grid and current un/lit status.

If $_NotHidden Then GUICtrlSetBkColor($hBlock[$_X + 1][$_Y], $sBlockOn) ; Changes the colour of the current block
$hBlockColour[$_X + 1][$_Y] = 1 ; Sets the status array element so we know next time it is currently lit..
$iLitCount += 1
EndIf

If ($_Y - 1) > 0 And $hBlockColour[$_X][$_Y - 1] = 1 Then ; Upper block - Checks if the related block is within the grid and current un/lit status.

If $_NotHidden Then GUICtrlSetBkColor($hBlock[$_X][$_Y - 1], $sBlockOff) ; Changes the colour of the current block
$hBlockColour[$_X][$_Y - 1] = 0 ; Sets the status array element so we know next time it is currently unlit..
$iLitCount -= 1

ElseIf ($_Y - 1) > 0 And $hBlockColour[$_X][$_Y - 1] = 0 Then ; Upper block - Checks if the related block is within the grid and current un/lit status.

If $_NotHidden Then GUICtrlSetBkColor($hBlock[$_X][$_Y - 1], $sBlockOn) ; Changes the colour of the current block
$hBlockColour[$_X][$_Y - 1] = 1 ; Sets the status array element so we know next time it is currently lit..
$iLitCount += 1
EndIf

If ($_Y + 1) < 11 And $hBlockColour[$_X][$_Y + 1] = 1 Then ; Lower block - Checks if the related block is within the grid and current un/lit status.

If $_NotHidden Then GUICtrlSetBkColor($hBlock[$_X][$_Y + 1], $sBlockOff) ; Changes the colour of the current block
$hBlockColour[$_X][$_Y + 1] = 0 ; Sets the status array element so we know next time it is currently unlit..
$iLitCount -= 1

ElseIf ($_Y + 1) < 11 And $hBlockColour[$_X][$_Y + 1] = 0 Then ; Lower block - Checks if the related block is within the grid and current un/lit status.

If $_NotHidden Then GUICtrlSetBkColor($hBlock[$_X][$_Y + 1], $sBlockOn) ; Changes the colour of the current block
$hBlockColour[$_X][$_Y + 1] = 1 ; Sets the status array element so we know next time it is currently lit..
$iLitCount += 1
EndIf

EndFunc ;==>ToggleBlock

Func CreateLevel($_NumOfRandomPresses) ; Creates a random grid of lit blocks by pressing blocks the number of times chosen by you.
$iLitCount = 0
Local $iNoHide = 1
If $_NumOfRandomPresses = 0 Then Return 1 ; If you chose 0 then this function has nothing to do, so just return 1.
If GUICtrlRead($hCheckbox1) = $GUI_CHECKED Then $iNoHide = 0

WipeGrid($sBlockOff, 0) ; Resets the grid to off status with the $sBlockOff colour.

For $rnd = 1 To $_NumOfRandomPresses ; Cycles through the number of times you told it to.
ToggleBlock(Random(1, 10, 1), Random(1, 10, 1), $iNoHide) ; This does the actual random "pressing" on blocks. If "Hide" ticked, don't show the colour changes yet..
ExitCheck() ; This lets you exit the program even if the script is still randomizing, otherwise it would not exit until it had finished.
Sleep((30 * $iNoHide) + 5)
Next

If Not $iNoHide Then ; If "Hide" is ticked, now that the technical randomizations are done, now light up the correct ones.

For $1 = 1 To 10 ; X axis
For $2 = 1 To 10 ; Y axis
If $hBlockColour[$1][$2] = 1 Then ; If this block is supposed to be "lit" then...
GUICtrlSetBkColor($hBlock[$1][$2], $sBlockOn) ; Light this block since it is scheduled to be lit.
Else
GUICtrlSetBkColor($hBlock[$1][$2], $sBlockOff) ; Turn off this block since it is NOT scheduled to be lit.
EndIf
Next
Next

EndIf
Return 1

EndFunc ;==>CreateLevel

Func ExitCheck()
If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit ; This will check if you want to exit the program and do so if you request it by the X button.
EndFunc ;==>ExitCheck

Func GridSave() ; This saves the current grid status so you can try the same grid later if you fail this time.
Global $sCurrent = ''
$file = FileOpen(@AppDataDir & '\Morthawt - Block Grid Save File.txt', 2)
For $2 = 1 To 10 ; Y axis
For $1 = 1 To 10 ; X axis
$sCurrent &= $hBlockColour[$1][$2] ; Add the next block's state (1 or 0) to the variable
Next
Next
FileWrite($file, $sCurrent) ; Write the string containing the blocks states to file.
EndFunc ;==>GridSave

Func GridLoad()
Global $sCurrent = '', $iCount = 1, $iLitCount = 0, $iBegun = 0
$file = FileRead(@AppDataDir & '\Morthawt - Block Grid Save File.txt') ; Read the contents of the save file to the $file variable.
WipeGrid($sBlockOff, 0) ; Reset the grid to all black.
For $2 = 1 To 10 ; Y axis
For $1 = 1 To 10 ; X axis
$iTemp = StringMid($file, $iCount, 1) ; This is the current block we are working with, it's state is here now.
$hBlockColour[$1][$2] = $iTemp ; This saves the state to the correct array element so it can be used.
If $iTemp = 1 Then
GUICtrlSetBkColor($hBlock[$1][$2], $sBlockOn) ; This turns on a block that is supposed to be on.
$iLitCount += 1
Else
GUICtrlSetBkColor($hBlock[$1][$2], $sBlockOff) ; This turns a block off that is supposed to be off.
EndIf
$iCount += 1 ; This is used to get the correct position from the save file to get the relevent save data for the current block.
Next
Next
EndFunc ;==>GridLoad

Func CheckHotkeyRequirement()
If Not WinActive('Unblock', 'Hide randomization?') Then
HotKeySet('^s') ; Ctrl + s hotkey used to save the current grid pattern.
HotKeySet('^l') ; Ctrl + l hotkey used to load the saved grid pattern.
Else
HotKeySet('^s', 'GridSave') ; Ctrl + s hotkey used to save the current grid pattern.
HotKeySet('^l', 'GridLoad') ; Ctrl + l hotkey used to load the saved grid pattern.
EndIf
EndFunc ;==>CheckHotkeyRequirement

Func WipeGrid($_ColourChoice, $_Speed = 0) ; This resets the grid to all off, with a specific colour and speed.
Local $sColor = $_ColourChoice
For $2 = 1 To 10 ; Y axis
For $1 = 1 To 10 ; X axis
GUICtrlSetBkColor($hBlock[$1][$2], $_ColourChoice) ; Sets the current block to the desired colour.
$hBlockColour[$1][$2] = 0 ; Sets the state so we can know if it is on or off (lit or unlit)
Sleep($_Speed) ; If a delay is specified it will be performed.
Next
Next
$iLitCount = 0
EndFunc ;==>WipeGrid

Posted Image

Edited by Morthawt
1 person likes this

Share this post


Link to post
Share on other sites



That was fun.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Lol the original electronic game had way less blocks as I am using. I wanted it to have enough levels of possibility to entertain the adult mind. I don't want to just routinely press buttons and easily solve it. I want to keep trying until I solve it lol.

Share this post


Link to post
Share on other sites

Upgraded version 2. I have updated the source code on the first post. Basically I added the ability to save the current grid pattern and load it at will with Ctrl + s and Ctrl + l That way you can have as many tries on a particular grid pattern you want.

1 person likes this

Share this post


Link to post
Share on other sites

LoL, i always get to a point where i cannot get rid of them. :P

Nice game.


Spoiler

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

Renamer - Rename files and folders, remove portions of text from the filename etc.

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0