Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Crazy Scripting : Picture Puzzle 0.9a


  • Please log in to reply
3 replies to this topic
SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Posted Image

#NoTrayIcon
SetBatchLines -1
Process, Priority,, High
CoordMode, Mouse, Relative
SetWorkingDir, %A_ScriptDir%

RegRead, Irfan, HKCR, Applications\i_view32.exe\shell\open\command
RegExMatch( Irfan, "(?<="")[^""]*", IrfanView )                             ; Thanks Titan
IrfanView := IrfanView="" ? A_ScriptDir "\i_view32.exe" : IrfanView
IfNotExist,%IrfanView%, ExitApp
iTemp := A_Temp "\IrfanView" 

IfNotExist,%A_ScriptDir%\Puzzle.PNG,MsgBox,4,Picture Puzzle,Download Puzzle.PNG (290KB) ?!
IfMsgBox,Yes,    URLDownloadToFile,https://ahknet.autohotkey.com/~goyyah/Puzzle.png,Puzzle.PNG
IfNotExist, %iTemp%, FileCreateDir, %iTemp%
IfNotExist, %iTemp%\Puzzle.bmp, RunWait,%IrfanView% Puzzle.png /convert=%iTemp%\Puzzle.bmp
IfNotExist, %iTemp%\Recent.LST, FileAppend, %A_ScriptDir%\Puzzle.PNG, %iTemp%\Recent.LST

Gui, Margin, 10, 5
Menu, RecentMenu, UseErrorLevel
FileRead, Imgs, %iTemp%\Recent.LST
Loop, PARSE, Imgs, |
  Menu, RecentMenu, Add, %A_LoopField%, LoadImage
Menu, FileMenu  , Add, &Open, SelectImage
Menu, FileMenu  , Add, Recent &Files, :RecentMenu
Menu, FileMenu  , Add
Menu, FileMenu  , Add, &Scramble, Scramble
Menu, FileMenu  , Add, &Auto Solve, Auto-Solve
Menu, FileMenu  , Add
Menu, FileMenu  , Add, &Reload, GuiClose
Menu, FileMenu  , Add, &Exit  , GuiClose
Menu, HelpMenu  , Add, &Homepage, Forum
Menu, MenuBar   , Add, &File, :FileMenu  
Menu, MenuBar   , Add, &Help, :HelpMenu
Menu, FileMenu  , Disable, &Auto Solve
Gui, Menu, MenuBar
Gui +LastFound

GuiID:=WinExist(),    Coords:="", Cells:="",    X:=10, Y:=10, pW:=130, pH:=98, C:=1, Ix:=1
Loop, 36  {
    Gui, Add, Picture, x%X% y%Y% w130 h98 +Hidden +Border hWndh%Ix% vCell%Ix% gDrag'N'Drop
    ControlGetPos, tX, tY,,,, % "ahk_id " h%Ix%
    Coords .= tx "-" ty "|"  ,  Cells .= Ix ( Ix<36 ? "|" : "" )
    X:=X+pW-1 , C:=C+1, Ix:=Ix+1
    If ( C > 6 )
       X:=10, Y:=Y+pH-1, C:=1
          }
Gui, Add, Picture, x10 y10 w775 h583 +Border vPuzzle gScramble hwndPuz, %iTemp%\Puzzle.bmp
Gui, Add, StatusBar
SB_SetParts( 630, 100 )
Gui, Show,, Picture Puzzle 0.9a
Return ;                                             | .. End of Auto-Execute Section .. |

#IfWinActive, Picture Puzzle 0.9a ahk_class AutoHotkeyGUI ;                    [ Hotkeys ]
  F2::GoSub, SelectImage
  F5::GoSub, Scramble
#IfWinActive

SelectImage: ;                                                            [ Select Image ] 
  IniRead, iPath, %iTemp%\Puzzle.ini, Settings, LastFolder, %A_MyDocuments%\My Pictures
  FileSelectFile, Img, 3, %iPath%, Select an Image File, (*.jpg; *.bmp; *.png )
  IfNotExist, %Img%, Return
LoadImage:   ;                                                              [ Load Image ]
  SetTimer, GameTimer, Off
  IfExist, %A_ThisMenuItem%, SetEnv,Img, %A_ThisMenuItem%
  FileRead, Imgs, %iTemp%\Recent.LST
  StringReplace, Imgs, Imgs, %Img%,,All
  Imgs := Img "|" Imgs ,  Images :="" , Ix := 0
  Gui, Menu
  Menu, RecentMenu, DeleteAll
  Loop, Parse, Imgs, |
     {
       IfNotExist, %A_LoopField%, Continue
       Menu, RecentMenu, Add, %A_LoopField%, LoadImage       
       Images := Images . A_LoopField . "|" , Ix := Ix+1
       ;Ix ++
       IfGreater, Ix, 25, Break
     }   
  If SubStr( Images, 0) = "|"
     StringTrimRight, Images, Images, 1
  FileDelete, %iTemp%\Recent.LST
  FileAppend, %Images%, %iTemp%\Recent.LST
  Gui,Menu, MenuBar
  
  GuiControl, Hide, Puzzle
  Loop, Parse, cHwnds, |
    Control, Hide,,, ahk_id %A_LoopField%
  Sb_SetText("  " Img, 1)
  SplitPath, Img,, iPath
  IniWrite, %iPath%, %iTemp%\Puzzle.ini, Settings,LastFolder
  Params = %img% /resize=(768,576) /convert=%iTemp%\Puzzle.bmp
  RunWait, %IrfanView% "%Params%"
  GuiControl,, Puzzle, %iTemp%\Puzzle.bmp
  DllCall( "AnimateWindow", UInt,Puz, Int,1000, UInt,393220 )
Return

RecentFiles: ;                                                            [ Recent Files ]
  FileRead, Imgs, %iTemp%\Recent.LST
  Gui, Menu
  Menu, RecentMenu, DeleteAll
  Loop, PARSE, Imgs, `n
     Menu, RecentMenu, Add, %A_LoopField%, LoadImage
  Gui, Menu, MenuBar
Return

Scramble: ;                                                                   [ Scramble ]
  Sort, Cells, Random D|
  GuiControl, Hide, Puzzle
  TilesLeft:=36, cHwnds:="",    X:=0, Y:=0, pW:=128, pH:=96, C:=1, Ix:=1
  Loop,PARSE,Cells,|
        {
          ALF:=A_LoopField,   cHwnds .= h%ALF% ( Ix<36 ? "|" : "" )
          Params = %iTemp%\Puzzle.bmp /crop=(%X%,%Y%,%pW%,%pH%) /convert=%iTemp%\%ALF%.bmp
          RunWait, %IrfanView% "%Params%"
          ControlGetPos, tX, tY,,,, % "ahk_id " h%ALF%
          If ( AGetP( cHwnds, h%ALF% ) = AGetP( Coords, tX "-" tY) ) 
              TilesLeft := TilesLeft-1 
          Else
              Control, Enable,,, % "ahk_id " h%ALF%
          GuiControl,, Cell%ALF%, %iTemp%\%ALF%.bmp
          Control, Show,,, % "ahk_id " h%ALF%
          FileDelete, %iTemp%\%ALF%.bmp

          X:=X+pW-1 , C:=C+1, Ix:=Ix+1
          If ( C > 6 )
          X:=0, Y:=Y+pH-1, C:=1
        }
  e := SB_SetText("`tTiles Left: " TilesLeft,2) , A_Time:=SubStr(A_Now,1,8)
  SetTimer, GameTimer, 1000
Return

Drag'N'Drop: ;                                                             [ Drag'N'Drop ]
  MouseGetPos,,,,sHwnd, 2
  ControlGetPos, sX, sY,,,, ahk_id %sHwnd%
  PostMessage, 0x112,0xF012,0,,ahk_id %sHwnd%   ;   Msg = WM_SYSCOMMAND , wParam = SC_MOVE
  Loop  {
          If ! GetKeyState("LButton","P")
             Break
          Sleep 10
        }   
  Control, Hide,,, ahk_id %sHwnd%
  MouseGetPos,,,,tHwnd, 2
  MouseGetPos,,,,tClass
  ControlGet, tEnabled, Enabled,,, ahk_id %tHwnd%
  If ( ( SubStr(tClass,1,6) != "Static" ) OR ( tHwnd = sHwnd ) OR !tEnabled  ) {
     ControlMove,, sX, sY,,,ahk_id %sHwnd%
     Control,Show,,,ahk_id %sHwnd%
     If ( AGetP( cHwnds, sHwnd ) = AGetP( Coords, sX "-" sY) ) 
        Control, Disable,,,ahk_id %sHwnd%
     Return
     }
 ControlGetPos, tX, tY,,,, ahk_id %tHwnd%
 ControlMove,, sX, sY,,,ahk_id %tHwnd%
 ControlMove,, tX, tY,,,ahk_id %sHwnd%
 Control,Show,,,ahk_id %sHwnd%

   If ( AGetP( cHwnds, sHwnd ) = AGetP( Coords, tX "-" tY) )   {
      Control, Disable,,,ahk_id %sHwnd%
      TilesLeft := TilesLeft-1
 } If ( AGetP( cHwnds, tHwnd ) = AGetP( Coords, sX "-" sY) )   {
      Control, Disable,,,ahk_id %tHwnd%
      TilesLeft := TilesLeft-1
 } SB_SetText("`tTiles Left: " TilesLeft,2)

  If ( TilesLeft <= 0 ) {
     SB_SetText("  Auto-Solved!",1)
     SetTimer, GameTimer, Off
     }
Return 

Auto-Solve: ;                                                               [ Auto-Solve ]
  Loop, Parse, Coords, |
     ControlMove,, % AGetF( A_LoopField,1,"-"), % AGetF( A_LoopField,2,"-"),
                ,, % "ahk_id " AGetF( cHwnds, A_Index )
  WinSet, ReDraw,, ahk_ID %GuiID%
  SB_SetText("  GAME OVER !!!!",1 ) , SB_SetText("`tTiles Left: 0",2 )
  SetTimer, GameTimer, Off
Return

GameTimer: ;                                                                [ Game Timer ]
  If (WinActive("Picture Puzzle 0.9a ahk_class AutoHotkeyGUI") ) { 
  A_Time+=1,S 
  FormatTime,MMSS,%A_Time%,mm:ss 
  SB_SetText("`t" MMSS,3) 
} Return 

Forum: ;                                                                     [ Home Page ]
  Run, iExplore.exe http://www.autohotkey.com/forum/viewtopic.php?t=19465
Return

AGetP(Str="",Fld="", D="")  {  ;                                        ArrayGetPosition()
  Loop,Parse,Str, % ( D="" ? "|" : D )
    IfEqual,A_LoopField,%Fld%, Return,A_Index 
Return 0
                            } 
AGetF(Str="",Fld=1, D="" )  {  ;                                           ArrayGetField()
  Loop,Parse,Str, % ( D="" ? "|" : D ) 
    IfEqual,A_Index,%Fld%, Return,A_LoopField 
                            }
GuiClose: ;                                                           [ ExitApp / ReLoad ]
  IfEqual,A_ThisMenuItem, &Reload, Reload
  Else ExitApp

Requirement: You will need to have IrfanView installed!
Download IrfanView : Latest Version / Old Versions

Alternatively, you may try the .ZIP which has the Script + i_view32.exe ( IrfanView 3.97 ) + Puzzle.PNG.
Download PicturePuzzle0.9a.zip [ 700KB ]

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
1:55

nice work!

you should let the user move a tile that is already placed correctly. it makes it harder.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Thanks for trying it engunneer!

you should let the user move a tile that is already placed correctly. it makes it harder.


My target audience are aged between 7 & 10 and they find the game tough enough when I select a complex image for them.

Try some of these: Polar Bear , Indigo Snake , Giraffe
Or even more challenging Abstract-1 , Abstract2

1:55


It took more than 2 minutes for me, during my first attempt. :D
Just chose that picture because the kids felt attracted.
The Recent Files in the menu has a very long list for me, and most of them take 5+ minutes for the kids.

:)

Joefiesta
  • Members
  • 84 posts
  • Last active: Sep 09 2016 02:27 PM
  • Joined: 01 Nov 2010

This is a great routine and an awesome example of the amazing power of AutoHotkey.

 

But, I broke it.  Was using it to try to create a random sampling of a tile for a mosaic.  Now, don't get me wrong, I'm just pointing this out.  I'm not sure why it broke.   I was experimenting with changing the code, because I wanted the cells to all be square.  Anyhow, something broke.   So I went back to the ORIGINAL version from this post.   And, what I had done still broke the routine. 

 

It turns out that when I resize the temp file version of the .JPG file that the routine creates, and then open a new picture, it appears fine.  But, when I scramble it, some of the cells are the entire picture themselves.  Maybe this is IRFANVIEW.... not sure.... like I said, I haven't dug that deep into it.