ฟอรั่มของบล็อก http://pssix.blogspot.com


โปรแกรมเกมส์สลับภาพ

Share

CaNiNeBrAiN
มือใหม่เพิ่งสมัคร
มือใหม่เพิ่งสมัคร


จำนวนข้อความ : 2
ความนิยม : 0
เข้าร่วมเมื่อ : 24/07/2010

โปรแกรมเกมส์สลับภาพ

ตั้งหัวข้อ by CaNiNeBrAiN on 24/07/10, 11:17 am

เมื่อตอนที่เป็น [You must be registered and logged in to see this link.] ผมเห็นสคริปเกมส์สลับภาพ

อ่ะครับตอนนี้กำลังจำเป็นในการศึกษาช่วยหาให้ด้วยนะครับ
avatar
POS
PSsix
PSsix


จำนวนข้อความ : 1151
ความนิยม : 323
เข้าร่วมเมื่อ : 19/07/2010

Re: โปรแกรมเกมส์สลับภาพ

ตั้งหัวข้อ by POS on 26/07/10, 02:30 pm

นี่ครับ

Code:
#region *** INCLUDES, OPTS, GLOBAL VARIABLES, INITIAL SETUP, and LOOP ***
#noTrayIcon
#Include <Array.au3>
#include <ComboConstants.au3>
#Include <File.au3>
#include <GDIPlus.au3>
#Include <GUIComboBox.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>

Opt('GUIOnEventMode', 1)
Opt('GUICloseOnESC', 1)

Global Const $MOVE_UP = 1, $MOVE_RIGHT = 2, $MOVE_DOWN = 3, $MOVE_LEFT = 4
Global Const $X_CENTER = @DesktopWidth / 2, $Y_CENTER = @DesktopHeight / 2
Global Const $MIN_WIDTH = 446
Global Const $CHILD_EX_STYLE = BitOr($WS_EX_MDICHILD, $WS_EX_TOOLWINDOW, $WS_EX_MDICHILD)
Global Const $GUI_X_BTN_SIZE = 26
Global Const $FILE_IN_LIST = 2, $FILE_NO_PASS = 0, $FILE_PASSES = 1

Global Const $CLICK_SOUND = @WindowsDir & '\media\Windows Menu Command.wav'
Global Const $WIN_SOUND = @WindowsDir & '\media\tada.wav'

Global Const $IMAGE_DUMP = @ScriptDir & '\dump'
DirCreate($IMAGE_DUMP)

Global $cmbDiff, $cmbFile, $guiMain
Global $idDiff = -1 ; reference to the first pictures CTRL ID
Global $xSqSize, $ySqSize, $xRatio, $yRatio
Global $row, $col
Global $squares, $guiList
Global $difficulty
Global $gameWon
Global $soundEnabled = True

Global $fileList, $fullFileList

_ToolGUI()

While 1
    Sleep(200)
WEnd
#endregion
#region *** EVENT TRIGGERED FUNCTIONS ***
Func _Click() ; called only when one of the squares is clicked
    If $gameWon then
        _Shuffle()
        Return
    EndIf
   
    $pieceClicked = @GUI_CtrLId - $idDiff + 1
   
    $adjBlank = _AdjacentIsBlank($pieceClicked)
   
    If $adjBlank < 0 then Return

    If $soundEnabled then SoundPlay($CLICK_SOUND)

    $moveSpeed = 2
    $cPos = WinGetPos($guiList[$pieceClicked])
   
    Switch $adjBlank
        Case $MOVE_UP
            WinMove($guiList[$pieceClicked], '', $cPos[0], $cPos[1] - $cPos[3], $cPos[2], $cPos[3], $moveSpeed)
            $squares[$row][$col] = ''
            $squares[$row - 1][$col] = $pieceClicked
        Case $MOVE_RIGHT
            WinMove($guiList[$pieceClicked], '', $cPos[0] + $cPos[2], $cPos[1] , $cPos[2], $cPos[3], $moveSpeed)
            $squares[$row][$col] = ''
            $squares[$row][$col + 1] = $pieceClicked
        Case $MOVE_DOWN
            WinMove($guiList[$pieceClicked], '', $cPos[0], $cPos[1] + $cPos[3], $cPos[2], $cPos[3], $moveSpeed)
            $squares[$row][$col] = ''
            $squares[$row + 1][$col] = $pieceClicked
        Case $MOVE_LEFT
            WinMove($guiList[$pieceClicked], '', $cPos[0] - $cPos[2], $cPos[1], $cPos[2], $cPos[3], $moveSpeed)
            $squares[$row][$col] = ''
            $squares[$row][$col - 1] = $pieceClicked
    EndSwitch
   
    If _GameWon() then
        If $soundEnabled then SoundPlay($WIN_SOUND)
        Msgbox(0, '', 'You won!')
        $i = $difficulty * $difficulty
        $guiList[$i + 1] = GUICreate($i + 1, $xSqSize - 2, $ySqSize - 2, (($difficulty - 1) * $xSqSize + 5), ($difficulty - 1) * $ySqSize + 56, $WS_POPUPWINDOW, $CHILD_EX_STYLE, $guiMain)
        GUICtrlCreatePic($IMAGE_DUMP & '\image clone (' & $i & ').bmp', 0, 0, $xSqSize, $ySqSize)
            GUICtrlSetOnEvent(-1, '_Shuffle')
        GUISetState()
        $gameWon = True
    EndIf
EndFunc

Func _Exit() ; called only when the user clicks the Exit (or some other form of exit)
    _FileWriteFromArray($IMAGE_DUMP & '\past.txt', $fullFileList, 1)
    FileDelete($IMAGE_DUMP & '\set.txt')
    FileWriteLine($IMAGE_DUMP & '\set.txt', GUICtrlRead($cmbDiff))
    Exit
EndFunc

Func _FileDropped() ; called only when a file is dropped onto the combo box
    _AddFile(@GUI_DragFile)
EndFunc

Func _FileSelect() ; called only when the user clicks the "add file" button
    $file = FileOpenDialog('Select an image...', @MyDocumentsDir, 'Images (*.jpg;*.bmp;*.gif)', 3)
    _AddFile($file)
EndFunc

Func _Shuffle() ; called by WM_COMMAND (user changes difficulty or changes file selection), _AddFile, and by _Click (if game is won)
    $gameWon = False
    $filePath = _GetActualFile(GUICtrlRead($cmbFile))

    If _FilePasses($filePath) = $FILE_NO_PASS then Return

    _Clear()

    _GDIPlus_StartUp ()
    $hImage = _GDIPlus_BitmapCreateFromFile ($filePath)
   
    $temp = GUICtrlRead($cmbDiff)
    Switch $temp
        Case 'Easy'
            $difficulty = 3
        Case 'Medium'
            $difficulty = 4
        Case 'Hard'
            $difficulty = 5
        Case 'Insane'
            $difficulty = 6
    EndSwitch

    Dim $squares[$difficulty + 2][$difficulty + 2]
        For $i = 0 to $difficulty + 1 ; creates a buffer
            $squares[0][$i] = -2
            $squares[$difficulty + 1][$i] = -2
            $squares [$i][0] = -2
            $squares[$i][$difficulty + 1] = -2
        Next
    Dim $guiList[$difficulty * $difficulty + 2] ; base 1 array
    $guiList[$difficulty * $difficulty + 1] = -1

    $xWidth = _GDIPlus_ImageGetWidth ($hImage) 
    $yHeight = _GDIPlus_ImageGetHeight ($hImage)
    $xSqSize = Floor($xWidth / $difficulty)
    $ySqSize = Floor($yHeight / $difficulty)

    If $xWidth >  (7 * @DesktopWidth) / 8 then
        $xRatio = ((7 * @DesktopWidth) / 8) / $xWidth
        $xWidth = Ceiling((7 * @DesktopWidth) / 8)
    Else
        $xRatio = 1
    EndIf
    $xSqSize = Ceiling($xRatio * $xSqSize)
   
    If $yHeight > (13 * @DesktopHeight) / 16 then
        $yRatio = ((13 * @DesktopHeight) / 16) / $yHeight
        $yHeight = Ceiling((13 * @DesktopHeight) / 16)
    Else
        $yRatio = 1
    EndIf
    $ySqSize = Ceiling($yRatio * $ySqSize)

    If $xWidth < $MIN_WIDTH then $xWidth = $MIN_WIDTH
   
    WinMove($guiMain, '',  $X_CENTER - ($xWidth / 2) - 8, $Y_CENTER - ($yHeight / 2) - 70, $xWidth + 16, $yHeight + 62)
    GUICtrlSetPos($cmbDiff, $xWidth - 84, 5, 90, 22)
    GUICtrlSetPos($cmbFile, 12 + 2 * $GUI_X_BTN_SIZE, 5, $xWidth - (9 + 2 * $GUI_X_BTN_SIZE + 92), 22)

    For $i = 0 to ($difficulty * $difficulty) - 1
        $xBase = Mod($i + $difficulty, $difficulty)
        $yBase = Floor($i / $difficulty)
       
        $xStart = $xBase * $xSqSize
        $yStart = $yBase * $ySqSize

        $hClone = _GDIPlus_BitmapCloneArea ($hImage, $xStart, $yStart, $xSqSize, $ySqSize, $GDIP_PXF24RGB)

        _GDIPlus_ImageSaveToFile ($hClone, $IMAGE_DUMP & '\image clone (' & $i + 1 & ').bmp')
        _GDIPlus_ImageDispose ($hClone)
       
        If $i <> ($difficulty * $difficulty) - 1 then
            $pass = False
            $xR = -1
            $yR = -1
            Do
                $xR = Random(1, $difficulty, 1)
                $yR = Random(1, $difficulty, 1)
                $temp = $squares[$xR][$yR]
                If $temp = '' then $pass = True
            Until $pass
           
            $squares[$xR][$yR] = $i + 1
           
            $guiList[$i + 1] = GUICreate($i + 1, $xSqSize - 2, $ySqSize - 2, ($yR - 1) * $xSqSize + 5, ($xR - 1) * $ySqSize + 56, $WS_POPUPWINDOW, $CHILD_EX_STYLE, $guiMain)
       
            If $i = 0 then
                $idDiff = GUICtrlCreatePic($IMAGE_DUMP & '\image clone (' & $i + 1 & ').bmp', 0, 0, $xSqSize, $ySqSize)
            Else
                GUICtrlCreatePic($IMAGE_DUMP & '\image clone (' & $i + 1 & ').bmp', 0, 0, $xSqSize, $ySqSize)
            EndIf
           
            GUICtrlSetOnEvent(-1, '_Click')
           
            GUISetState()
           
        EndIf
    Next
   
    _GDIPlus_ImageDispose ($hImage)
    _GDIPlus_ShutDown ()
   
EndFunc

Func _ToggleSound() ; called only when the user click the "toggle sound" button
    $soundEnabled = Not $soundEnabled
   
    If $soundEnabled then
        GUICtrlSetData(@GUI_CtrlId, '!')
        GUICtrlSetTip(@GUI_CtrlId, 'Disable sound')
    Else
        GUICtrlSetData(@GUI_CtrlId, '')
        GUICtrlSetTip(@GUI_CtrlId, 'Enable sound')
    EndIf
   
EndFunc
#endregion
#region *** GAME SETUP FUNCTIONS ****
Func _PopulateFileList() ; called only by _ToolGUI when the game is first loading
    If Not FileExists($IMAGE_DUMP & '\past.txt') then
        Dim $fileList[1]
        Dim $fullFileList[1]
        $fileList[0] = 0
        $fullFileList[0] = 0
        Return
    EndIf
   
    _FileReadToArray($IMAGE_DUMP & '\past.txt', $fileList)
    If Not IsArray($fileList) then
        Dim $fileList[1]
        Dim $fullFileList[1]
        $fileList[0] = 0
        $fullFileList[0] = 0
        Return
    EndIf
       
    Dim $fullFileList[$fileList[0] + 1]
    $fullFileList[0] = $fileList[0]

    $tempSub = 0
    For $i = 1 to $fileList[0]
        If Not (_FilePasses($fileList[$i]) = $FILE_PASSES) then
            $tempSub += 1
            _ArrayDelete($fileList, $i)
            _ArrayDelete($fullFileList, $i)
        Else
            $fullFileList[$i] = $fileList[$i]
            $fileList[$i] = _GetFileName($fileList[$i])
        EndIf
    Next
    $fileList[0] -= $tempSub
    $fullFileList[0] -= $tempSub
   
    _ResetComboData()
EndFunc

Func _ToolGUI() ; called only once at startup
    $guiMain = GUICreate('Picture Puzzle', $MIN_WIDTH + 10, $GUI_X_BTN_SIZE + 8, $X_CENTER - ($MIN_WIDTH / 2) - 5, -1, -1,  $WS_EX_ACCEPTFILES)
        GUISetOnEvent($GUI_EVENT_CLOSE, '_Exit')
        GUISetOnEvent($GUI_EVENT_DROPPED, '_FileDropped')
   
    GUISetFont(10)
   
    GUICtrlCreateButton('!', 4, 4, $GUI_X_BTN_SIZE, $GUI_X_BTN_SIZE)
        GUICtrlSetTip(-1, 'Disable sound')
        GUICtrlSetOnEvent(-1, '_ToggleSound')
        GUICtrlSetResizing(-1, $GUI_DOCKLEFT + $GUI_DOCKTOP + $GUI_DOCKSIZE)
       
    GUICtrlCreateButton('+', 7 + $GUI_X_BTN_SIZE, 4, $GUI_X_BTN_SIZE, $GUI_X_BTN_SIZE)
        GUICtrlSetTip(-1, 'Select a file')
        GUICtrlSetOnEvent(-1, '_FileSelect')
        GUICtrlSetResizing(-1, $GUI_DOCKLEFT + $GUI_DOCKTOP + $GUI_DOCKSIZE)

    $temp = FileReadLine($IMAGE_DUMP & '\set.txt', 1)
    If Not ($temp = 'Easy' or $temp = 'Medium' or $temp = 'Hard' or $temp = 'Insane') then $temp = 'Easy'

    $cmbDiff = GUICtrlCreateCombo('', $MIN_WIDTH - 84, 5, 90, 22, $CBS_DROPDOWNLIST)
        GUICtrlSetData(-1, 'Easy|Medium|Hard|Insane', $temp)
        GUICtrlSetResizing(-1, $GUI_DOCKLEFT + $GUI_DOCKTOP + $GUI_DOCKSIZE)
        GUICtrlSetTip(-1, 'Change difficulty')

    $cmbFile = GUICtrlCreateCombo('', 12 + 2 * $GUI_X_BTN_SIZE, 5, 292, 22, BitOR($CBS_SORT, $CBS_DROPDOWNLIST))
        GUICtrlSetResizing(-1, $GUI_DOCKLEFT + $GUI_DOCKTOP + $GUI_DOCKSIZE)
        GUICtrlSetTip(-1, 'Drag files here to start a puzzle...')
        GUICtrlSetState(-1, $GUI_DROPACCEPTED)
       
    GUIRegisterMsg($WM_COMMAND, 'WM_COMMAND')
   
    _PopulateFileList()
   
    GUISetState()
EndFunc
#endregion
#region *** CHECKS ***
Func _AdjacentIsBlank($num) ; called only by _Click
    ;get the row and column that $num is in
    For $i = 1 to $difficulty
        For $j = 1 to $difficulty
            If $squares[$i][$j] = $num then
                $row = $i
                $col = $j
                ExitLoop 2
            EndIf
        Next
    Next
   
    If $squares[$row - 1][$col] = '' then Return $MOVE_UP
    If $squares[$row + 1][$col] = '' then Return $MOVE_DOWN
    If $squares[$row][$col - 1] = '' then Return $MOVE_LEFT
    If $squares[$row][$col + 1] = '' then Return $MOVE_RIGHT
    Return -1
EndFunc

Func _FilePasses($file) ; called by _PopulateFileList, _AddFile, and _Shuffle
    If Not FileExists($file) then Return $FILE_NO_PASS
       
    $ext = StringRight($file, 3)
    If $ext <> 'bmp' and $ext <> 'jpg' and $ext <> 'gif' then Return $FILE_NO_PASS
       
    If _ArraySearch($fileList, _GetFileName($file), 1) <> -1 then Return $FILE_IN_LIST
   
    Return $FILE_PASSES
EndFunc

Func _GameWon() ; called only by _Click
    $count = 1
    For $i = 1 to $difficulty
        For $j = 1 to $difficulty
            If $count = $difficulty * $difficulty then Return True
            If $squares[$i][$j] <> $count then
                Return False
            EndIf
            $count += 1
        Next
    Next
   
    Return True
EndFunc
#endregion
#region *** HELPERS ***
Func _AddFile($file) ; called only by _FileDropped and _FileSelect
    $pass = _FilePasses($file)
   
    If $pass = $FILE_NO_PASS then Return
   
    If $pass = $FILE_PASSES then
        _AddFileToArray($file)
    Else ;else the item will be activated but not added to the list because it's already there
        $index = _GUICtrlComboBox_FindString($cmbFile, _GetFileName($file))
        _GUICtrlComboBox_SetCurSel($cmbFile, $index)
    EndIf
   
    _Shuffle()
EndFunc

Func _AddFileToArray($file) ; called only by _AddFile
    _ArrayAdd($fullFileList, $file)
    $file = _GetFileName($file)
    _ArrayAdd($fileList, $file)
   
    $fileList[0] += 1
    $fullFileList[0] += 1

   
    $string = ''
    For $i = 1 to $fileList[0]
        $string &= $fileList[$i] & '|'
    Next
    $string = StringTrimRight($string, 1)
   
    GUICtrlSetData($cmbFile, '')
    GUICtrlSetData($cmbFile, $string, $file)
EndFunc   

Func _Clear() ; called only by _Shuffle
    If IsArray($guiList) then
        For $i = 1 to $difficulty * $difficulty - 1
            GUIDelete($guiList[$i])
        Next

        If $guiList[$difficulty * $difficulty + 1] <> -1 then
            GUIDelete($guiList[$difficulty * $difficulty + 1])
        EndIf
    EndIf
EndFunc

Func _GetActualFile($disp) ; called only by _Shuffle
    For $i = 1 to $fullFileList[0]
        If _GetFileName($fullFileList[$i]) = $disp then
            Return $fullFileList[$i]
        EndIf
    Next
    Return ''
EndFunc

Func _GetFileName($file) ; assumes file exists
    $split = StringSplit($file, '')
    Return $split[$split[0]]
EndFunc

Func _ResetComboData() ; called only by _PopulateFileList
    $string = ''
    For $i = 1 to $fileList[0]
        $string &= $fileList[$i] & '|'
    Next
    $string = StringTrimRight($string, 1)

    GUICtrlSetData($cmbFile, $string) 
EndFunc

Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam) ; checks for combo box activity
    #forceref $hWnd, $iMsg
    Local $hWndFrom, $iIDFrom, $iCode
    $hWndFrom = $ilParam
    $iIDFrom = BitAND($iwParam, 0xFFFF) ; Low Word
    $iCode = BitShift($iwParam, 16) ; Hi Word
    If $iCode = $CBN_SELCHANGE then
        Switch $hWndFrom
            Case GUICtrlGetHandle($cmbDiff)
                _Shuffle()
            Case GUICtrlGetHandle($cmbFile)
                _Shuffle()
        EndSwitch
    EndIf
           
    Return $GUI_RUNDEFMSG
EndFunc  ;==>WM_COMMAND
#endregion
 


||----------------------------------------------------------------------------------------------------------------||

กลุ่ม AutoIt ในเฟสบุ๊ค
[You must be registered and logged in to see this link.]

++++++++++++++++++++++++++++++++++++++++++++
pssix.blogspot.com

CaNiNeBrAiN
มือใหม่เพิ่งสมัคร
มือใหม่เพิ่งสมัคร


จำนวนข้อความ : 2
ความนิยม : 0
เข้าร่วมเมื่อ : 24/07/2010

Re: โปรแกรมเกมส์สลับภาพ

ตั้งหัวข้อ by CaNiNeBrAiN on 26/07/10, 03:17 pm

อ๋อครับ

ขอบคุณนะครับ

แต่พอดีผมได้แล้วเนื่องจากผมไปหาจาก autoitscript.com อ่ะครับ

แต่มีอีกปัญหาอ่ะครับ

คือผมต้องการสร้างเกมส์เกี่ยวกับคณิตศาสตร์อ่ะครับ

เลยอยากได้ซอสโค๊ดเกมส์ ปริศนาหอคอยแห่งฮานอย อ่ะครับ

ช่วยหาให้ด้วยนะครับ

หาไม่เจอเลย !

ขอบคุณนะครับ
avatar
POS
PSsix
PSsix


จำนวนข้อความ : 1151
ความนิยม : 323
เข้าร่วมเมื่อ : 19/07/2010

Re: โปรแกรมเกมส์สลับภาพ

ตั้งหัวข้อ by POS on 26/07/10, 10:10 pm

ตัวอย่างโค้ดคงไม่ได้มีทุกเกมส์หรอกครับ ลองค้นหาในฟอรั่มหลักของเว็บ autoIt ตรงหมวดตัวอย่างสคริตป์ดูครับ คำค้นใช้คำง่ายๆ อย่าง game


||----------------------------------------------------------------------------------------------------------------||

กลุ่ม AutoIt ในเฟสบุ๊ค
[You must be registered and logged in to see this link.]

++++++++++++++++++++++++++++++++++++++++++++
pssix.blogspot.com

    เวลาขณะนี้ 29/05/17, 09:05 am