CODICE
#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.6.1
Author: Misterx
Script Function:
The snake game xD
#ce ----------------------------------------------------------------------------
#include <GuiConstants.au3>
#include <GdiPlus.au3>
#include <Misc.au3>
HotKeySet("{esc}","esci")
HotKeySet("{space}","pausa")
Global $title = "Snake by Misterx", $xdim = 10, $ydim = 10, $step = 10
Global $ini = @ScriptDir &"\config.ini"
gui_options ()
declare_options ()
$gui1 = GUICreate($title,600,500)
GUISetState()
_GDIPlus_Startup()
$hwnd = _GDIPlus_GraphicsCreateFromHWND($gui1)
$bit = _GDIPlus_BitmapCreateFromGraphics(600, 500, $hwnd) ;create bitmap
$buffer = _GDIPlus_ImageGetGraphicsContext($bit) ;create buffer
$snk = _GDIPlus_BrushCreateSolid(0xFF00A9FF)
$obj = _GDIPlus_BrushCreateSolid(0xFFFF0000)
$xmax = 600-$xdim
$ymax = 500-$ydim
Dim $op[2]
$op[0] = rand(0,$xmax,$step)
$op[1] = rand(0,$ymax,$step)
drawo($op[0],$op[1])
Dim $sn[2][2]
$sn[0][0] = 300
$sn[0][1] = 240
draws($sn[0][0],$sn[0][1])
$vx = 0
$vy = 0
While 1
Sleep($velocita)
$msg = GUIGetMsg ()
If _IsPressed(25) Then ;sinistra
If $vx <> $xdim Then
$vx = -$xdim
$vy = 0
EndIf
EndIf
If _IsPressed(26) Then ;su
If $vy <> $ydim Then
$vx = 0
$vy = -$ydim
EndIf
EndIf
If _IsPressed(27) Then ;destra
If $vx <> -$xdim Then
$vx = $xdim
$vy = 0
EndIf
EndIf
If _IsPressed(28) Then ;giù
If $vy <> -$ydim Then
$vx = 0
$vy = +$ydim
EndIf
EndIf
snake()
If $msg = $GUI_EVENT_CLOSE Then Exit
WEnd
;disegna il punto da prendere
Func drawo($x,$y)
_GDIPlus_GraphicsFillRect($buffer,$x,$y,$xdim,$ydim,$obj)
EndFunc
Func rand($rmin,$rmax,$st)
Local $gen = ""
;genero il pacchetto di step
For $_rand = $rmin To $rmax Step $st
$gen &= $_rand &"-"
Next
;splitto in un array per la selezione casuale
$gen_sp = StringSplit($gen,"-",2)
Return $gen_sp[Round(Random($rmin,UBound($gen_sp)-1),0)]
EndFunc ;=> Random con step
Func draws($sx,$sy)
_GDIPlus_GraphicsFillRect($buffer,$sx,$sy,$xdim,$ydim,$snk)
EndFunc
Func snake()
_GDIPlus_GraphicsClear($buffer,0xFF94FFFF)
$hwnd = _GDIPlus_GraphicsCreateFromHWND($gui1)
$sn[0][0] = $sn[0][0] + $vx
$sn[0][1] = $sn[0][1] + $vy
;pareti
If $sn[0][0] = 600 Then ;destra
If $pareti = True Then
MsgBox (64,$title,"Hai perso!!!"& @CRLF &"Lunghezza massima raggiunta: "& UBound($sn)-1)
Exit
ElseIf $pareti = False Then
$sn[0][0] = 0
EndIf
ElseIf $sn[0][0] = -10 Then ;sinistra
If $pareti = True Then
MsgBox (64,$title,"Hai perso!!!"& @CRLF &"Lunghezza massima raggiunta: "& UBound($sn)-1)
Exit
ElseIf $pareti = False Then
$sn[0][0] = 600
EndIf
EndIf
If $sn[0][1] = 500 Then ;alto
If $pareti = True Then
MsgBox (64,$title,"Hai perso!!!"& @CRLF &"Lunghezza massima raggiunta: "& UBound($sn)-1)
Exit
ElseIf $pareti = False Then
$sn[0][1] = 0
EndIf
ElseIf $sn[0][1] = -10 Then ;basso
If $pareti = True Then
MsgBox (64,$title,"Hai perso!!!"& @CRLF &"Lunghezza massima raggiunta: "& UBound($sn)-1)
Exit
ElseIf $pareti = False Then
$sn[0][1] = 500
EndIf
EndIf
;crash
For $crash = 2 to UBound($sn,1)-1
If $sn[0][0] = $sn[$crash][0] And $sn[0][1] = $sn[$crash][1] Then
MsgBox(64,$title,"Hai perso!!!"& @CRLF &"Lunghezza massima raggiunta: "& UBound($sn)-1)
Exit
EndIf
Next
;mangia
If $sn[0][0] = $op[0] And $sn[0][1] = $op[1] Then
$op[0] = rand(0,$xmax,$step)
$op[1] = rand(0,$ymax,$step)
_ArrayAddb($sn,$op[0],$op[1])
WinSetTitle($title,"",$title &" - Lunghezza attuale "& UBound($sn)-1)
EndIf
;sposto le posizioni per il corpo del serpente
For $len = UBound($sn,1)-1 To 1 Step -1
$sn[$len][0] = $sn[$len-1][0]
$sn[$len][1] = $sn[$len-1][1]
draws($sn[$len][0],$sn[$len][1])
Next
draws($sn[0][0],$sn[0][1])
drawo($op[0],$op[1])
_GDIPlus_GraphicsDrawImageRect($hwnd,$bit,0,0,600,500)
EndFunc
Func esci()
Exit
EndFunc
Func _ArrayAddb(ByRef $_array,$_val1,$_val2)
If Not IsArray($_array) Then Return 0
$ub1 = UBound($_array,1)
ReDim $_array[$ub1 + 1][2]
$_array[$ub1][0] = $_val1
$_array[$ub1][1] = $_val2
EndFunc
Func pausa()
$vx = 0
$vy = 0
EndFunc
Func declare_options ()
Global $pareti, $velocita
If IniRead($ini, "Impostazioni", "pareti", "") = "True" Then
$pareti = True
ElseIf IniRead ($ini, "Impostazioni", "pareti", "") = "False" Then
$pareti = False
EndIf
$velocita = IniRead ($ini, "Impostazioni", "Velocita", "")
EndFunc
Func gui_options ()
GUICreate ("Opzioni Snake")
$chk1 = GUICtrlCreateCheckBox ("Impostare il game-over per le pareti?", 5, 5)
GUICtrlCreateLabel ("Inserire qui la velocità che si desidera", 5, 40)
$combo1 = GUICtrlCreateCombo ("Lenta", 5, 60)
GUICtrlSetData ($combo1, "Normale")
GUICtrlSetData ($combo1, "Veloce")
GUICtrlSetData ($combo1, "Velocissima")
$btn1 = GUICtrlCreateButton ("Applica", 200, 200)
GUISetState ()
While 1
Switch GUIGetMsg ()
Case $GUI_EVENT_CLOSE
Exit
Case $btn1
If Not FileExists($ini) Then
FileWrite($ini,"[Impostazioni]"& @CRLF &"Pareti = True" & @CRLF & "Velocita = 100")
EndIf
If GUICtrlRead ($chk1) = $GUI_CHECKED Then
IniWrite ($ini, "Impostazioni", "Pareti", "True")
Else
IniWrite ($ini, "Impostazioni", "Pareti", "False")
EndIf
If GUICtrlRead ($combo1) = "Lenta" Then
IniWrite ($ini, "Impostazioni", "Velocita", "100")
ElseIf GUICtrlRead ($combo1) = "Normale" Then
IniWrite ($ini, "Impostazioni", "Velocita", "75")
ElseIf GUICtrlRead ($combo1) = "Veloce" Then
IniWrite ($ini, "Impostazioni", "Velocita", "50")
ElseIf GUICtrlRead ($combo1) = "Velocissima" Then
IniWrite ($ini, "Impostazioni", "Velocita", "25")
EndIf
ExitLoop
EndSwitch
WEnd
EndFunc