[Game] AHK Snake Puzzle v1.0

Post gaming related scripts
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

[Game] AHK Snake Puzzle v1.0

08 Dec 2017, 18:00

Hello,
I programmed a puzzle snake game. :D
Feel free to comment and enjoy the game. ;)
Here is the code.

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Snake Puzzle
;version 1.0
;by Speeedmaster
;(buffer keys by maestrith)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#SingleInstance force

SetBatchLines, -1 ; The speed at which the lines of the script are executed
SetControlDelay, -1 ; The delay to occur after modifying a control


gui, -dpiscale

;---------------------------------------CHECK IF INI EXIST IF NOT CREATE IT--------------------------------------------------------

iniFile := SubStr( A_ScriptName, 1, -3 ) . "ini"
Ini_File=%A_WorkingDir%\%iniFile%      
ifnotexist,%Ini_File%
 {
  iniContent =
  (
   []
   HISCORE=003000
   STARTSNAKES=20
   STARTMELODY=1
   SOUNDEFFECTS=1
  )

  replaceFile(iniFile, iniContent)
 }

; call replace file function
replaceFile(File, Content)
{
	FileDelete, %File%
	FileAppend, %Content%, %File%
}


;------------------------------------Read the ini file and store its content to variables----------------------------------------------
;Call function ini 
updateini()

;----------------------------------------------------------------------------------------


gosub maps


pico8:={black:"000000", dark_blue:"1D2B53", dark_purple:"7E2553", dark_green:"008751", brown:"AB5236", dark_gray:"5F574F", light_gray:"C2C3C7", white:"FFF1E8", red:"FF004D", orange:"FFA300", yellow:"FFEC27", green:"00E436", blue:"29ADFF", indigo:"83769C", pink:"FF77A8", peach:"FFCCAA"}
ticolor:={1:"fuchsia",  2:"F49808",3:"E5C91A",4:"5EBF28",5:"41C3FF",6:pico8.indigo}



COLUMNS:=32
ROWS:=22
CELLWIDTH:=15
CELLHEIGHT:=15
VSPACING:=1
HSPACING:=1
GRIDFONTSIZE:=110
STARTCELL:="25_5"
STARTSPEED:=100
;STARTSNAKES:=            ;STARTSNAKES
PLAYER:=STARTCELL
MAP_WIDTH:=COLUMNS
MAP_HEIGHT:=ROWS
MAXAPPELS:=15
APPELS:=2
LENGHT:=8
LEVEL:=1
SCORE:=0
HISCORE:=00000
SNAKES:=STARTSNAKES
LASTSCORE:=
LASTHISCORE:=

updateini()


if (startmelody=1)
stopmelody:=0

if (soundeffects=1)
sounddisable:=0

gui, 2: add, text, section w250 h30 border, Start with melody:
Gui, 2: Add, Radio, Group xp+130 yp+2 checked%STARTMELODY% gmelody vmelodygroup, ON
Gui, 2: Add, Radio, xp+60 yp checked%STOPMELODY% gmelody , OFF

gui, 2: add, text, xs w250 h30 border, Sound effects:
Gui, 2: Add, Radio, Group xp+130 yp+2 checked%soundenable% gsoundeffects vsoundeffects, ON
Gui, 2: Add, Radio, xp+60 yp checked%sounddisable% gsoundeffects , OFF

Gui,+hwndMain
Hotkey,IfWinActive,ahk_id%main%
for a,b in {Left:"Left",Right:"Right",Up:"Up",Down:"Down"}
	Hotkey,%a%,%b%,On

gui, font, s15 cyellow

gui, add, text, x25 y5 w65 h30 -border section, Level
gui, add, text, x+10 yp w65 h30 vlevel, % Format("{:02}", level)

gui, add, text, x210 yp w75 h30  -border, Appels
gui, add, text, x+10 yp w75 h30 vappels -border, % Format("{:02}", maxappels)

gui, add, text, x400 yp w85 h30 section -border, Snakes
gui, add, text, x+10 yp w65 h30 vsnakes -border, % Format("{:02}", snakes)


;// create the Playing area (grid 1)

gui, add, text, section x25 y35 w0 h0 -border,

gui, font, s105

r:=0 c:=0 

loop, % rows 
{
     r:= r+1

     loop, % columns
     {
       c:= c+1 
       if c=1
         gui, add, text, section 0x200 center  BackGroundTrans -border  w%cellwidth% h%cellheight% xs  y+%VSPACING% v%c%_%r% gclick, 
       else 
         gui, add, text,         0x200 center  BackGroundTrans -border  w%cellwidth% h%cellheight%  x+%HSPACING% ys  v%c%_%r% gclick, 
     }

      c:=0
}

R:=0 C:=0


gui, font, s15 cyellow

gui, font, s12 bold 
gui, add, text,  xs yp+30 w100 h30  -border, Score:
gui, add, text, x+10 yp w100 h30 vscore -border, % Format("{:05}", score)
gui, add, text,  x+110 yp w100 h30 -border, HI Score:
gui, add, text, x+30 yp w100 h30 vhiscore -border, % Format("{:05}", hiscore)

gui, font, s10 bold italic underline
Gui, Add, text, xs yp+40  h30 greload cffa300, RELOAD
gui, font, s12 norm bold italic

gui, font, s10 bold italic underline
Gui, Add, text, xs+100 yp  h30 gsetup c008751, SETUP
gui, font, s12 norm bold italic
Gui, Add, text, xp+250 yp  h30 csilver, by Speedmaster

gui, add, text, x230 y160 w200 h30 -border backgroundtrans hidden0 cwhite vt_ahead, Get Ready


guicontrol, show1, t_ahead

pen:=new tile("1_1", "X", "teal")
wall:=new tile("1_1", "X", "teal")
appel:=new tile("2_2", "X",pico8.pink, "10","10" )
pear:=new tile("2_2", "X",pico8.green, "5","2" )

walls:=""
appels_pos:=","
pears_pos:=","
snakeparts_pos:=","

snake:=new snake(startcell)

;// draw the borders of the playing area

loop, % rows
{
b1:="1" . "_" . a_index
walls .=b1 ","
wall.position:=b1
wall.draw()
b2:=columns . "_" . a_index
walls .=b2 ","
wall.position:=b2
wall.draw()
}


loop, % columns
{
b1:=a_index . "_" . 1
walls .=b1 ","
wall.position:=b1
wall.draw()
b2:=a_index . "_" . rows
walls .=b2 ","
wall.position:=b2
wall.draw()
}

gui, color,  black
gui, +Resize
gui, show, autosize, Snake Puzzle v1.0


if (startmelody=1)
melodygroup:=1

titlescreen:

guicontrol, show0, t_ahead

sleep, 500

colorshape(titlecolor,"",,2,2)
drawshape(title,"",,2,2,"","")

sleep, 1000

if startmelody
{
 Soundbeep, 659, 360
 Soundbeep, 699, 120
 Soundbeep, 784, 60
 sleep, 120
 sleep, 60
 Soundbeep, 1047, 960
 sleep, 120
 Soundbeep, 587, 360
 Soundbeep, 659, 120
 Soundbeep, 699, 1440
 Soundbeep, 784, 360
 Soundbeep, 880, 120
 Soundbeep, 988, 60
 sleep, 120
 sleep, 60
 Soundbeep, 1397, 960
 sleep, 120
 Soundbeep, 880, 360
 Soundbeep, 988, 120
 Soundbeep, 1047, 480
 Soundbeep, 1175, 480
 Soundbeep, 1329, 240
 sleep, 240
 Soundbeep, 659, 360
 Soundbeep, 699, 120
 Soundbeep, 784, 60
 sleep, 120
 sleep, 60
 Soundbeep, 1047, 960
 sleep, 120
 Soundbeep, 1175, 360
 Soundbeep, 1329, 120
 Soundbeep, 1397, 1440
 Soundbeep, 784, 180
 sleep, 240
 Soundbeep, 784, 120
 Soundbeep, 1329, 480
 Soundbeep, 1175, 120
 sleep, 240
 Soundbeep, 784, 120
 Soundbeep, 1329, 480
 Soundbeep, 1175, 360
 Soundbeep, 784, 120
 Soundbeep, 1329, 480
 Soundbeep, 1175, 360
 Soundbeep, 784, 120
 Soundbeep, 1397, 480
 Soundbeep, 1329, 360
 Soundbeep, 1175, 120
 Soundbeep, 1047, 960
}


reset:
    ;// Launch the game
    Suspend on ; suspend others hotkeys

    Input, selected_direction, L1, {UP}{down}{left}{right}; wait for a key to be pressed
    if ErrorLevel contains EndKey:,MAX
    drawshape(clear,"",,2,2,"","")
    gosub, start
    return

start:

walls2:=","

    if (snakes<0)
    {
    DllCall("ShowCursor","UInt",1) ;hide cursor when on gui
    game.gameover()
    return
    }


if (level=1){
game.levelset(level:=1, snspeed:=80,appels:=appels,maxappels:=10)
}


if (level=2){
game.levelset(level:=2, snspeed:=100,appels:=5,maxappels:=15) 
appel.growth:=6
}

if (level=3){
game.levelset(level:=3, snspeed:=100,appels:=2,maxappels:=14) 
appel.growth:=6
}

if (level=4){
game.levelset(level:=4, snspeed:=100,appels:=5,maxappels:=15) 
appel.growth:=6
drawshape(level04,"",,8,11,"yellow")
}


if (level=5){
game.levelset(level:=5, snspeed:=100,appels:=5,maxappels:=10)
appel.growth:=6
drawshape(level05,"",,2,2, pico8.orange)
}

if (level=6){
game.levelset(level:=6, snspeed:=100,appels:=5,maxappels:=15)
appel.growth:=6
drawshape(level06,"",,2,2,"FC9E5A")
}

if (level=7){
game.levelset(level:=7, snspeed:=100,appels:=5,maxappels:=14)
appel.growth:=6
drawshape(level07,"",,4,4,"red")
}


if (level=8){
game.levelset(level:=8, snspeed:=100,appels:=5,maxappels:=14)
appel.growth:=6
drawshape(level08,"",,2,2, pico8.indigo)
}


if (level=9){
game.levelset(level:=9, snspeed:=115,appels:=5,maxappels:=16)
appel.growth:=4
snake.position:="21_5"
drawshape(level09,"",,2,2,"31A2F2")
}

if (level=10){
game.levelset(level:=10, snspeed:=100,appels:=5,maxappels:=16)
appel.growth:=3
drawshape(level10,"",,2,2,"red")
}

	if (level>10)
	{
     settimer, addpear, off
     game.win()
     return
    }


Row:=0 Col:=0

  loop, % rows {
     Row:= ROw+1

     loop, % columns {
           Col:= Col+1
               cell_to_add=%Col%_%Row%
               if (%cell_to_add%=4)
			   {
				walls2 .= cell_to_add . ","
				%cell_to_add%=
			   } 
     }
      Col:=0

  }
 
detect_arrow_key:

if (lastscore)
{
score:=lastscore
guicontrol,, score, % Format("{:05}", lastscore)
}
else
lastscore:=score

if (lasthiscore)
{
hiscore:=lasthiscore
guicontrol,, hiscore, % Format("{:05}", lasthiscore)
}
else
lasthiscore:=hiscore


Suspend off ; enable hotkeys

guicontrol, show1, t_ahead


; draw the snake on the grid
snake.draw()

Suspend on ; suspend others hotkeys

DllCall("ShowCursor","UInt",1) ;show cursor when on gui

    Input, selected_direction, L1, {UP}{down}{left}{right}; wait for a key to be pressed


    ;// detect with errorlevel selected direction
    if ErrorLevel = EndKey:Up
{
     ;// NORTH
dir:="UP"
}
else if ErrorLevel = EndKey:Down
{
     ;// SOUTH
dir:="Down"
}
else if ErrorLevel = EndKey:Left
{
     ;// WEST
dir:="Left"
}
else if ErrorLevel = EndKey:Right
{
     ;// EAST
dir:="Right"
}

else if ErrorLevel = MAX
       ;// Complain to the user (Errorlevel has no EndKey so the user typed the wrong key)
      {
      msgbox, press arrow key to begin

       ;//wait for keypress
       gosub, detect_arrow_key
      return
      }

	Direc:={Up:"N",Down:"S",Left:"W",Right:"E"}
    snake.MoveQueue:=[]
    snake.MoveQueue.Push(Direc[dir])
    snake.Direction(Direction[dir])
	
Suspend off		
DllCall("ShowCursor","UInt",0) ;hide cursor when on gui	

count:=0

;// Create a bunch of appels
loop, % game.appels
{
;//keep trying until we find a position for the appel
  loop,
    {
     posa:=randompos()
     IfnotInString, appels_pos, % "," posa ","           ;if appel is not in a appel position
     IfnotInString, pears_pos, % "," posa ","            ; if appel is not in a pear position
     IfnotInString, snakeparts_pos, % "," posa ","       ; and if appel is not on snake's body part
	 IfnotInString, walls2, % "," posa ","               ; and if appel is not on a internal wall obstacle
     break
    }
 appels_pos .=posa ","
 appel.position:=posa
 appel.draw()
}


;// Create a pear
;//keep trying until we find a position for the pear
  loop,
    {
     posp:=randompos()
     IfnotInString, appels_pos, % "," posp ","
     IfnotInString, pears_pos, % "," posp ","
     IfnotInString, snakeparts_pos, % "," posp ","
     IfnotInString, walls2, % "," posp ","  
     break
    }

 pears_pos .=posp ","
 pear.position:=posp
 pear.draw()

    guicontrol, show0, t_ahead
    settimer, addpear, 10000, on

    ;// Move the snake and play

DllCall("ShowCursor","UInt",0) ;hide cursor when on gui		
snake.stop:=0	    ; Game main loop

 Loop{
      snake.MoveHead()
	  if (snake.Stop)
	  Break
	
      if (level=3)
       {
        if (snake.hitappel())
        game.speed:=game.speed-4
       } 
	
      }
return

addpear:
;settimer, move, off
;//keep trying until we find a set of coordinates for the pears
  loop,
    {
     posp:=randompos()
     IfnotInString, appels_pos, % "," posp ","
     IfnotInString, pears_pos, % "," posp ","
     IfnotInString, snakeparts_pos, % "," posp ","
     IfnotInString, walls2, % "," posp "," 
     break
    }
pears_pos .=posp ","
pear.position:=posp
pear.draw()
return

Left:
Right:
Up:
Down:

Direction:={Up:"N",Down:"S",Left:"W",Right:"E"}


snake.Direction(Direction[A_ThisLabel])

return

Randompos()
{
global
Random, randx, 3, % columns-2
Random, randy, 3, % rows-2
return % randx . "_" . randy
}

click:
;msgbox, % a_guicontrol
return

~p::pause, toggle

guiclose:
esc:: 
exitapp 
return

setup:
Gui, 2:Default
Gui, 2:Show, w300 h100
return

reload:
reload
return

DrawShape(map,ColSeparator:=",",RowSeparator:="`r`n",startcol:="2", startrow:="2", color:="red", value:="4")
{
global

Row:=0
Col:=0

if startrow>=0
srow:=startrow-1
else
srow:=startrow

if startcol>=0
scol:=startcol-1
else
scol:=startcol

row:=row+srow
col:=col+scol

loop, parse, map, %RowSeparator%
  {
     Row:= ROw+1
         loop, parse, a_loopfield, %ColSeparator%
            {
             Col:= Col+1
              if a_loopfield !=%a_space%  
              if a_loopfield !=%a_enter%   
              if a_loopfield !=0           
              if a_loopfield !=2           
                {
                 GuiControlGet, contentpos,, %Col%_%Row%
                   if (contentpos!=a_loopfield)
                    {
			         clr=%Col%_%Row%
				     guicontrol,+c%color%, %Col%_%Row%
                     guicontrol,, %Col%_%Row%, % a_loopfield
                     if (value!="")
                     %clr%:=value  
                    }
		        }
              if a_loopfield =2
              guicontrol,, %Col%_%Row%, % a_space
            }
             col:=0
             col:=col+scol
  } 
} ;end of drawshape



ColorShape(map,ColSeparator:="`," ,RowSeparator:="`r`n",startcol:="2", startrow:="2")
{
global

Row:=0
Col:=0


if startrow>=0
srow:=startrow-1
else
srow:=startrow

if startcol>=0
scol:=startcol-1
else
scol:=startcol

row:=row+srow
col:=col+scol

loop, parse, map, %RowSeparator%
  {
     Row:= ROw+1
     loop, parse, a_loopfield, %ColSeparator%
        {
         Col:= Col+1
         if a_loopfield !=%a_space%  
         if a_loopfield !=%a_enter%   
         if a_loopfield !=0    
          {
           clr=%Col%_%Row%
           pen.position:=clr
           pen.color:=ticolor[a_loopfield]
           pen.colorcell()
          }
         }
          col:=0
          col:=col+scol
   } 
}



class tile extends game
{
	__new(position:="1_1",symbol:="I",color:="blue", worth:="15", growth:="4", incrasespeed:="0")
	{
		this.symbol:=symbol
		this.color:=color
		this.position:=position
		stringsplit, pos, % this.position, "_"
		this.px:=pos1
        this.py:=pos2
		this.empty:=""
        this.worth:=worth
        this.growth:=growth
		this.incrasespeed:=incrasespeed

	}

		
	 draw()
    {
      global
      guicontrol, % "+c" . this.color, % this.position
	  guicontrol,, % this.position, % this.symbol
    }	
	
	 colorcell()
	{
	  guicontrol, % "+c" this.color, % this.position
    } 
	
	clear()
	{
	  guicontrol,, % this.position, % this.empty
	}
	
	
}


class snake extends tile {

	__new(position:="3_3",lenght:="5", headcolor:="purple", bodycolor:="Green" )
	{
		global
		this.position:=position
		pos := StrSplit(this.position, "_")
		this.lenght:=lenght-1
		this.color:=headcolor
		this.bodycolor:="green"
		this.empty:=""
        this.symbol:="X"
		this.MoveQueue:=[]
		this.stop:=0
		
	}
	
    Direction(Direction){
		global count
		static LastDirection, Problem:={S:"N",N:"S",E:"W",W:"E"}
		if(Problem[Direction]!=LastDirection)
		{
            if (direction != lastdirection) 
             {
			  this.MoveQueue.Push(Direction)
	          LastDirection:=Direction
             }		
        }   

	}
	
	movehead()
	{
     static moveTo
		if(this.MoveQueue.1)
		moveTo:=this.MoveQueue.RemoveAt(1)
		this.snakeHead(moveTo)		
		
	}

	SnakeHead(movementDirection){
		if(movementDirection="E")
         this.move(1,0)
	else    if(movementDirection="W")
         this.move(-1,0)
	else	if(movementDirection="N")
         this.move(0,-1)
	else	if(movementDirection="S")
         this.move(0,1)		 
}

    move(dx, dy)
	{
	global count, maxappels
    count:=count+1
	
	if  this.notpassable()
	this.collision()

    if this.hitappel()
    this.eatappel()

    if this.hitpear()
    this.eatpear()

    if !(this.stop)
      {	
   	    this.drawbody()
        pos := StrSplit(this.position, "_")
        this.position:=pos.1+dx . "_" . pos.2+dy
	    this.drawhead()
        Sleep, % game.Speed
	  } 

    }	
	
	 
	 notpassable()
	 {
       global walls, walls2, snakeparts_pos

     IfInString, walls, %  ","  this.position  ","            ; if snake hit a wall border
     return true

     IfInString, walls2, %  ","  this.position  ","           ; if snake hit an internal wall obstacle
     return true

	 IfInString, snakeparts_pos, %  ","  this.position  ","   ; if sanke hit his own body
     return true

	 }	

     hitappel()
     {
      global appels_pos
      IfInString, appels_pos, %  ","  this.position ","
	  return true

     }

     hitpear()
     {
      global pears_pos
      IfInString, pears_pos, %  ","  this.position ","
	  return true
     }


    updatescore()
	{
      global
      guicontrol,, score, % Format("{:05}", score)
      guicontrol,, appels, % Format("{:02}", maxappels)
	 
	  if (hiscore<score)
       {
         guicontrol,, hiscore, % score
         hiscore:=score
         guicontrol,, hiscore, % Format("{:05}", score)
         updateini(,1)
	    } 
	}

     eatappel()
     {
      global
        score:=score + appel.worth

	    this.updatescore()
		
        this.lenght:=this.lenght+appel.growth
		this.speed:=this.speed-appel.incrasespeed
		if (soundeffects=1)
        SoundBeep, 1000, 100
        maxappels:=maxappels-1
		
		if (maxappels=0)
         {
            this.updatescore()
			this.gonextstage()
         }
		

      if (maxappels>=appels)  ;generate a new appel
       {
         loop,
             {
              posa:=randompos()
              IfnotInString, appels_pos, % "," posa ","           ;if appel is not in a appel position
              IfnotInString, pears_pos, % "," posa ","            ;if appel is not in a pear position
			  IfnotInString, snakeparts_pos, % "," posa ","       ; and if appel is not on snake's body part
			  IfnotInString, walls2, % "," posa ","               ; and if appel is not on a internal wall obstacle
              break
		     }


         appels_pos .=posa . ","
         appel.position:=posa
         appel.draw()

       }

      appels_pos:=RegExReplace(appels_pos, "," this.position ",", ",")
	  
      this.updatescore()
     } ; end eatappel

     eatpear()
     {
      global
      score:=score + pear.worth
      
        this.lenght:=this.lenght+pear.growth

		if (soundeffects=1)
        SoundBeep, 1000, 100

      pears_pos:=RegExReplace(pears_pos, "," this.position ",", ",")
      this.updatescore()
     }

    drawhead()
	 {
		this.draw()
	 }	

	 drawbody()
    {
		global
		guicontrol, % "+c" . this.bodycolor, % this.position
		guicontrol,, % this.position, % this.symbol

		
		if (count<=this.lenght)
       { 
         snakeparts_pos .=this.position ","
	   }

      if (count>=this.lenght)
       {
        regexmatch(snakeparts_pos,"\d+_\d+",snakelastpart) 
        snakeparts_pos:=regexReplace(snakeparts_pos, "^,.+?,", ",",,1)    ; remove wath is between comas ", ," at starting position of the string

         this.cleartail()	
	     count:=count-1
       }
		
    }	


	cleartail()
	{
	 global snakelastpart
	 guicontrol,, % snakelastpart, 

	}
	
}



class game
{

  collision()
   {
    	global
        DllCall("ShowCursor","UInt",1) ;show cursor when on gui	
		this.stop:=1         ;stop moving the snake
        snakes:=snakes-1

        if !(snakes<0)
        guicontrol,, snakes, % Format("{:02}", snakes)

        settimer, addpear, off

        if (soundeffects=1)
        SoundBeep, 1000, 100
		
		sleep, 500
        
		IfInString, walls, %  ","  this.position  ","        ; if snake hit a wall border
		{
          this.color:=wall.color
          this.drawhead()
	    }
        
		drawshape(clear,"",,2,2,""," ")
        sleep, 200
        drawshape(Rip,"",,2,2,"silver",0) 

		this.lenght:="4"
        this.position:=""    ;erase snake head position
        snakelastpart:=""    ;erase location of tail edge
        snakeparts_pos:=","  ;erase location of body parts
        appels_pos:=","      ;erase location of appels
        pears_pos:=","       ;erase loaction of pears 
        this.Direction(" ")

     if (soundeffects=1)
      {
       Soundbeep, 784, 120
       Soundbeep, 1397, 480
       Soundbeep, 1329, 360
       Soundbeep, 1175, 120
       Soundbeep, 1047, 960
      } 
     sleep, 1000
       drawshape(clear,"",,2,2,""," ")
       this.position:=startcell
       this.color:="purple"
	   this.Direction(" ")

	   gosub, start
  
    }



gonextstage()
{
	global
DllCall("ShowCursor","UInt",1) ;show cursor when on gui
	level:=level+1
    lastscore:=""
    lasthiscore:=""
 	this.stop:=1         ;stop moving the snake
    this.lenght:="4"
    appels_pos:=","      ;erase location of appels
    pears_pos:=","       ;erase loaction of pears 
sleep, 1000


loop, parse, snakeparts_pos, `,
{
 this.position:=a_loopfield
 this.clear()
 score:=score + 10
 this.updatescore()
 sleep, 50
}

    this.position:=""    ;erase snake head position
    snakelastpart:=""    ;erase location of tail edge
    snakeparts_pos:=","  ;erase location of body parts
    appels_pos:=","      ;erase location of appels
    pears_pos:=","       ;erase loaction of pears 
    this.Direction(" ")

    drawshape(clear,"",,2,2,""," ")
    this.position:=startcell
    this.color:="purple"
    this.Direction(" ")

    gosub, start

}


gameover()
{
global
 maxappels:=0

	 this.stop:=1         ;stop moving the snake
     drawshape(clear,"",,2,2,""," ")

     drawshape(gameover,"",,2,2,"yellow")
	 snakeparts_pos:=","  ;erase location of body parts
     appels_pos:=","      ;erase location of appels
     this.position:=startcell
     this.color:="purple"
	 this.Direction(" ")
}



win()
{
 global

 this.stop:=1         ;stop moving the snake
 drawshape(clear,"",,2,2,""," ")
 drawshape(congratulation,"",,2,2,"yellow")
 this.position:=startcell
 this.color:="purple"
 this.Direction(" ")

if startmelody
 loop, 2
{
 Soundbeep, 1480, 1071
 Soundbeep, 988, 268
 Soundbeep, 1109, 268
 Soundbeep, 1175, 268
 Soundbeep, 1329, 268
 Soundbeep, 1480, 804
 Soundbeep, 1175, 268
 Soundbeep, 1480, 804
 Soundbeep, 1175, 268
 Soundbeep, 1480, 804
 Soundbeep, 988, 268
 Soundbeep, 1175, 268
 Soundbeep, 988, 268
 Soundbeep, 784, 268
 Soundbeep, 1175, 268
 Soundbeep, 988, 1071
 sleep, 134
 Soundbeep, 1329, 268
 Soundbeep, 1175, 268
 Soundbeep, 1109, 268
 Soundbeep, 1480, 1071
 Soundbeep, 988, 268
 Soundbeep, 1109, 268
 Soundbeep, 1175, 268
 Soundbeep, 1329, 268
 Soundbeep, 1480, 804
 Soundbeep, 1175, 268
 Soundbeep, 1480, 804
 Soundbeep, 1175, 268
 Soundbeep, 1480, 804
 Soundbeep, 988, 268
 Soundbeep, 1175, 268
 Soundbeep, 988, 268
 Soundbeep, 784, 268
 Soundbeep, 1175, 268
 Soundbeep, 988, 1071
}



}

;end collision


levelset(level, snspeed,appels,maxappels)
{

this.level:=level
this.speed:=snspeed
this.appels:=appels
snake.maxappels:=maxappels

guicontrol,, t_ahead, GET READY !
guicontrol,, appels, % Format("{:02}", maxappels)
guicontrol,, level, % Format("{:02}", level)


}


} ; end cl game	


;https://autohotkey.com/board/topic/33506-read-ini-file-in-one-go/
updateini( filename = 0, updatemode = 0 )
;
; updates From/To a whole .ini file
; By default the update mode is set to 0 (Read) 
; and creates variables like this:
; %Section%%Key% = %value%
; You don't have to state the updatemode when reading, just use
; update(filename)
; The function can be called to write back updated variables to
; the .ini by setting the updatemode to 1, like this:
; update(filename, 1) 
{
Local s, c, p, key, k, write

   if not filename
      filename := SubStr( A_ScriptName, 1, -3 ) . "ini"

   FileRead, s, %filename%

   Loop, Parse, s, `n`r, %A_Space%%A_Tab%
   {
      c := SubStr(A_LoopField, 1, 1)
      if (c="[")
         key := SubStr(A_LoopField, 2, -1)
      else if (c=";")
         continue
      else {
         p := InStr(A_LoopField, "=")
         if p {
  	    k := SubStr(A_LoopField, 1, p-1)
	    if updatemode=0
	    	%key%%k% := SubStr(A_LoopField, p+1)
	    if updatemode=1
	    {
	    	write := %key%%k%
	    	IniWrite, %write%, %filename%, %key%, %k% 	
	    }
         }
      }
   }
}




melody:
Gui, Submit , NoHide
if melodygroup=1
startmelody:=1
else
startmelody:=0

updateini(,1)
return

soundeffects:
Gui, Submit , NoHide
if soundeffects=1
soundenable:=1
else
soundenable:=0

updateini(,1)
return


maps:

GAMEOVER=
(
0 
                            
   XXXX    X    X   X  XXXXX  
  X       X X   XX XX  X      
  X  XX  X   X  X X X  XXX    
  X   X  XXXXX  X   X  X      
   XXXX  X   X  X   X  XXXXX  
                              
                              
   XXX   X   X  XXXXX  XXXX   
  X   X  X   X  X      X   X  
  X   X  X   X  XXX    XXXX   
  X   X   X X   X      X   X  
   XXX     X    XXXXX  X   X  
                              
XXXXXX                  
     X            XXX   X      
     XXXXXXXXX    X       
             X    X           
             XXXXXX                   
)

CONGRATULATION=
(
0
         XXXX               
       XXXXXXXX             
   XXX X      X XXX         
   X XXXXXXXXXXXX X         
   X  X        X  X         
    X X  XXX X X X          
     XX  X     XX           
      X  XXX   X            
      X    X   X            
      X  XXX   X            
       X      X   X X XXX X X
        XX  XX     X  X X X X
         X  X      X  XXX XXX
         X  X               
         X  X     X   X X XXX
       XXXXXXXX   X X X X X X
      X        X   XXX  X X X
      XXXXXXXXXX            
                    

)

title=
(
0
 
                            
  XXXX X   X  XXX  X   X XXXX
 X     X   X X   X X  X  X    
 X     XX  X X   X X X   X    
 XXXX  X X X XXXXX XX    XXX 
     X X  XX X   X X X   X    
     X X   X X   X X  X  X    
 XXXX  X   X X   X X   X XXXX

               X
               X
               XXXXXXXX
                      X
        XXXXXXXXXXXXXXX
        X
        XXXXXX  X
)



titlecolor=
(
0
  
                           
  1111 2   2  333  4   4 5555
 1     2   2 3   3 4  4  5    
 1     22  2 3   3 4 4   5    
 1111  2 2 2 33333 44    555 
     1 2  22 3   3 4 4   5    
     1 2   2 3   3 4  4  5    
 1111  2   2 3   3 4   4 5555

               6
               6
               66666666
                      6
        666666666666666
        6
        666666  X
)

RIP=
(
0
           XXXXXX             
        XXXX    XXXX          
       XX          XX         
       X            X         
       X            X         
      XX            XX        
      X  XXX  X XXX  X        
      X  X  X X X  X X        
      X  X  X X X  X X        
      X  XXX  X XXX  X        
    XXX  X X  X X    XXX      
    X X  X  X X X    X X      
    X X              X X      
    X X              X X      
    X XXXXXXXXXXXXXXXX X      
    X                  X      
    XXXXXXXXXXXXXXXXXXXX      
    X                  X      
    XXXXXXXXXXXXXXXXXXXX                            
)


clear=
(
222222222222222222222222222222 
222222222222222222222222222222 
222222222222222222222222222222 
222222222222222222222222222222 
222222222222222222222222222222 
222222222222222222222222222222  
222222222222222222222222222222 
222222222222222222222222222222 
222222222222222222222222222222 
222222222222222222222222222222 
222222222222222222222222222222 
222222222222222222222222222222
222222222222222222222222222222 
222222222222222222222222222222 
222222222222222222222222222222 
222222222222222222222222222222 
222222222222222222222222222222 
222222222222222222222222222222
222222222222222222222222222222
222222222222222222222222222222
)


level04=
(
XXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXX
)


level05=
(
0                             
                              
                              
        X            X        
        X            X        
        X            X        
        X            X        
        X            X        
        X            X        
        X            X        
        X            X        
        X            X        
        X            X        
        X            X        
        X            X        
        X            X        
        X            X        

)


level06=
(
0                             
                              
                              
        XXXXX    XXXXX        
        X            X        
        X            X        
                     X        
        X            X        
        X            X        
        X            X        
        X            X        
        X            X        
        X            X        
        X            X        
        X                     
        X            X        
        X            X        
        XXXXX    XXXXX        
)


level07=
(
0
      
         XXXXXXXXX
      XX           XX
    XX               XX
  XX                   XX

                   
   
                   
  XX                   XX
    XX               XX
      XX           XX
         XXXXXXXXX
        
)


level08=
(
0                           
                              
    X      X       X      X   
    X      X       X      X   
    X      X       X      X   
    X      X       X      X   
    X      X       X      X   
    X      X       X      X   
                              
                              
                              
       X       X       X      
       X       X       X      
       X       X       X      
       X       X       X      
       X       X       X      
       X       X       X      
       X       X       X                             
)


level09=
(
0
                              
                              
  XXXX        XX        XXXX   
              XX              
              XX              
  XXXX        XX        XXXX   
              XX              
        XXX        XXX        
                              
             XXXX             
                              
        XXX        XXX        
              XX              
  XXXX        XX        XXXX   
              XX              
              XX              
  XXXX        XX        XXXX   
                                         
)


level10=
(
0                           
            X                 
                   X          
                    X         
         X           X        
        X             X       
      XX               XX     
                              
             X                
            XXXXXXX           
                 X            
                              
                              
      XX               XX     
        X             X       
         X           X        
          X                   
           X                  
                  X           
                              
)

return




User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: [Game] AHK Snake Puzzle v1.0

09 Dec 2017, 07:38

You finished the game with only 10 snakes, that's great ! :thumbup:

Return to “Gaming Scripts (v1)”

Who is online

Users browsing this forum: No registered users and 37 guests