Snake

« Older   Newer »
  Share  
xdmisterx
CAT_IMG Posted on 30/4/2010, 21:26     +1   -1




Chi non ha mai giocato al mitico snake? xD

SPOILER (click to view)
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, $vel = 100

$gui = GUICreate($title,600,500)

GUISetState()

_GDIPlus_Startup()
$hwnd = _GDIPlus_GraphicsCreateFromHWND($gui)
$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($vel)
       
       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()
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($gui)
       $sn[0][0] = $sn[0][0] + $vx
       $sn[0][1] = $sn[0][1] + $vy
       
       ;pareti
       If $sn[0][0] = 600 Then                ;destra
               $sn[0][0] = 0
       ElseIf $sn[0][0] = -10 Then        ;sinistra
               $sn[0][0] = 600
       EndIf
       
       If $sn[0][1] = 500 Then        ;alto
               $sn[0][1] = 0
       ElseIf $sn[0][1] = -10 Then        ;basso
               $sn[0][1] = 500
       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


ciao :)

Edit: corretto un piccolo bug...

Edited by xdmisterx - 1/5/2010, 18:01
 
Top
Kurt_Black_hat 2.0
CAT_IMG Posted on 2/5/2010, 02:58     +1   -1




Io odio snake.
Mi ricorda una volta che avevo fumato troppo e ci giocavo per restare sveglio in treno. Tutta colpa di una ragazza. Che schifo le ragazze, pensavo di tagliarmelo.

...no. Forse no dai.

P.S.
Carino ._.
 
Top
bradipooso
CAT_IMG Posted on 2/5/2010, 08:02     +1   -1




ingegnoso
 
Top
SuperAntani
CAT_IMG Posted on 2/5/2010, 08:29     +1   -1




Dopo mesi e mesi di inattività ho finalmente visto il tuo script di snake e ho pensato "Oddio questo merita". Mi si è riaccesa la scintilla del programmatore e volevo chiederti se ti dispiaceva se ci aggiungevo funzioni, livello di difficoltà... insomma lo arricchivo un po'. Dopo tanto tempo finalmente il tou script mi ha ridato la scintilla xD.
No comunque seriamente ti disturba se ci aggiungo delle cose, possiamo anche farlo insieme questo lavoro.
 
Top
Gianlu-thebest
CAT_IMG Posted on 2/5/2010, 08:32     +1   -1




g00d work!
 
Top
SuperAntani
CAT_IMG Posted on 2/5/2010, 09:33     +1   -1




Non ho resistito e ho fatto una modifica che però non funziona... =( Misterx aiutaci tu xD.

SPOILER (click to view)
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, $vel = 100

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(1)
       $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 $opt_pareti = True Then
                       MsgBox (64,$title,"Hai perso!!!"& @CRLF &"Lunghezza massima raggiunta: "& UBound($sn)-1)
                       Exit
               ElseIf $opt_pareti = False Then
                       $sn[0][0] = 0
               EndIf
       ElseIf $sn[0][0] = -10 Then        ;sinistra
               If $opt_pareti = True Then
                       MsgBox (64,$title,"Hai perso!!!"& @CRLF &"Lunghezza massima raggiunta: "& UBound($sn)-1)
                       Exit
               ElseIf $opt_pareti = False Then
                       $sn[0][0] = 600
               EndIf
       EndIf

       If $sn[0][1] = 500 Then        ;alto
               If $opt_pareti = True Then
                       MsgBox (64,$title,"Hai perso!!!"& @CRLF &"Lunghezza massima raggiunta: "& UBound($sn)-1)
                       Exit
               ElseIf $opt_pareti = False Then
                       $sn[0][1] = 0
               EndIf
       ElseIf $sn[0][1] = -10 Then        ;basso
               If $opt_pareti = True Then
                       MsgBox (64,$title,"Hai perso!!!"& @CRLF &"Lunghezza massima raggiunta: "& UBound($sn)-1)
                       Exit
               ElseIf $opt_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 ()
       If IniRead ("C:\Program Files\Snake by Misterx\config.ini", "Impostazioni", "Pareti", "True") Then
               Global $opt_pareti = True
       ElseIf IniRead ("C:\Program Files\Snake by Misterx\config.ini", "Impostazioni", "Pareti", "False") Then
               Global $opt_pareti = False
       EndIf
EndFunc
 
Top
xdmisterx
CAT_IMG Posted on 2/5/2010, 10:15     +1   -1




Per modificarlo fai pure :)
Ho visto che hai messo le pareti che causano il gameover...a me funge tutto benissimo, cosa non dovrebbe andare?
 
Top
SuperAntani
CAT_IMG Posted on 2/5/2010, 10:30     +1   -1




che il game over alle pareti non dovrebbe andare sempre, ho messo che se legge un ini e restituisce True allora mette il gameover, altrimenti no. Solo che non mi funziona e lo causa sempre. =(
 
Top
xdmisterx
CAT_IMG Posted on 2/5/2010, 10:41     +1   -1




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, $vel = 100
Global $ini = @ScriptDir &"\config.ini"

$opt_pareti = declare_options ()
MsgBox(0,"",$opt_pareti)

$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($vel)
      $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 $opt_pareti = True Then
                      MsgBox (64,$title,"Hai perso!!!"& @CRLF &"Lunghezza massima raggiunta: "& UBound($sn)-1)
                      Exit
              ElseIf $opt_pareti = False Then
                      $sn[0][0] = 0
              EndIf
      ElseIf $sn[0][0] = -10 Then        ;sinistra
              If $opt_pareti = True Then
                      MsgBox (64,$title,"Hai perso!!!"& @CRLF &"Lunghezza massima raggiunta: "& UBound($sn)-1)
                      Exit
              ElseIf $opt_pareti = False Then
                      $sn[0][0] = 600
              EndIf
      EndIf

      If $sn[0][1] = 500 Then        ;alto
              If $opt_pareti = True Then
                      MsgBox (64,$title,"Hai perso!!!"& @CRLF &"Lunghezza massima raggiunta: "& UBound($sn)-1)
                      Exit
              ElseIf $opt_pareti = False Then
                      $sn[0][1] = 0
              EndIf
      ElseIf $sn[0][1] = -10 Then        ;basso
              If $opt_pareti = True Then
                      MsgBox (64,$title,"Hai perso!!!"& @CRLF &"Lunghezza massima raggiunta: "& UBound($sn)-1)
                      Exit
              ElseIf $opt_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 ()
       If Not FileExists($ini) Then
               FileWrite($ini,"[Impostazioni]"& @CRLF &"Pareti = True")
       EndIf
       If IniRead($ini, "Impostazioni", "pareti", "") = "True" Then
               Return True
   ElseIf IniRead ($ini, "Impostazioni", "pareti", "") = "False" Then
               Return False
   EndIf
EndFunc


Il problema era causato dal "If iniread()" perchè lui ritornava sempre vero, in quanto non gli dicevi che stringa comparare.
 
Top
SuperAntani
CAT_IMG Posted on 2/5/2010, 10:56     +1   -1




Grandissimo Misterx... se non ci fossi tu xD.
Ora vedo se riesco a inserire un'interfaccia che permette di modificare quell'opzione (farlo manualmente è not-user-friendly xD)
 
Top
SuperAntani
CAT_IMG Posted on 2/5/2010, 11:50     +1   -1




Bug founded!!!

Se si prende almeno un pezzetto e poi si premono insieme il tasto sotto e il tasto destra va in game-over

EDIT: Non funziona solo con quei tasti, in generale premendone due in contemporanea.

EDIT2: Ecco il sorcio con tanto di opzioni per il gioco xD

SPOILER (click to view)
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


Edited by SuperAntani - 2/5/2010, 14:26
 
Top
bof
CAT_IMG Posted on 2/5/2010, 13:41     +1   -1




CITAZIONE (xdmisterx @ 30/4/2010, 22:26)
Chi non ha mai giocato al mitico snake? xD

SPOILER (click to view)
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, $vel = 100

$gui = GUICreate($title,600,500)

GUISetState()

_GDIPlus_Startup()
$hwnd = _GDIPlus_GraphicsCreateFromHWND($gui)
$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($vel)
       
       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()
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($gui)
       $sn[0][0] = $sn[0][0] + $vx
       $sn[0][1] = $sn[0][1] + $vy
       
       ;pareti
       If $sn[0][0] = 600 Then                ;destra
               $sn[0][0] = 0
       ElseIf $sn[0][0] = -10 Then        ;sinistra
               $sn[0][0] = 600
       EndIf
       
       If $sn[0][1] = 500 Then        ;alto
               $sn[0][1] = 0
       ElseIf $sn[0][1] = -10 Then        ;basso
               $sn[0][1] = 500
       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


ciao :)

Edit: corretto un piccolo bug...

Bellissimo. Ne ho fatto uno anch'io quando ero ancora nuovo nel forum, pero' il mio a differenza tua usava le ncurses.
 
Top
MHack
CAT_IMG Posted on 2/5/2010, 15:24     +1   -1




Wow, bello. Soprattutto perchè è fatto in autoit, io forse riesco a farlo, ma con game maker 7 ;)
 
Top
3ad_Pr0grammer
CAT_IMG Posted on 4/5/2010, 17:12     +1   -1




Very Good! Mi piace molto :D
 
Top
13 replies since 30/4/2010, 21:26   182 views
  Share