[GAME] REGEX FROGGER

Post your working scripts, libraries and tools
User avatar
SpeedMaster
Posts: 242
Joined: 12 Nov 2016, 16:09

[GAME] REGEX FROGGER

10 Jan 2019, 18:14

Hello,
I programmed a Frogger game clone using regular expressions. :D
I haven't been able to test it completely yet there may still be a lot of bugs. :cry:
There is no score yet and the difficulty may not be well balanced. I don't know if it works on other computers. :?

Here is the code:

Code: Select all

; =================================================================================================================================
; Name:           REGEX FROGGER 
; Description:    A CLASSIC FROGGER GAME CLONE
; Topic:          https://www.autohotkey.com/boards/viewtopic.php?f=6&t=60902  
; Sript version:  1.2
; AHK Version:    1.1.24.03 (A32/U32/U64)
; Tested on:      Win 7 (x64)
; Author::        SpeedMaster

; How to play:     The object of the game is to help your character (the frog " @ ") cross from the bottom of the screen to the top.
; The player guides a frog which starts at the bottom of the screen, to his home in one of 5 slots at the top of the screen. 
; The lower half of the screen contains a road with motor vehicles  speeding along it horizontally.
; The upper half of the screen consists of a river with logs moving horizontally across the screen. The very top of the screen contains 
; five "frog homes" which are the destinations for each frog.
;
; ==================================================================================================================================
version:=1.2

#SingleInstance force


gui, font, s18 cwhite bold, courier
;gui, font, s18 cwhite , terminal

gui, -dpiscale
loop, 17
gui, add, text, % "x10 " ((a_index=1) ? "y10 " : "yp+20 ") " v1_1_" a_index " h22 c5F574F -border BackGroundTrans left", % "                    "  ; borders
loop, 17
gui, add, text, % "x10 " ((a_index=1) ? "y16 " : "yp+20 ") " v2_1_" a_index " h22 cblue -border BackGroundTrans left"  , % "                    "  ; river
loop, 17
gui, add, text, % "x10 " ((a_index=1) ? "y10 " : "yp+20 ") " v3_1_" a_index " h22 cgreen -border BackGroundTrans left" , % "                    "  ; gras
loop, 17
gui, add, text, % "x10 " ((a_index=1) ? "y14 " : "yp+20 ") " v6_1_" a_index " h22 cgray -border BackGroundTrans center"  , % "                    "  ; street 
loop, 17
gui, add, text, % "x10 " ((a_index=1) ? "y10 " : "yp+20 ") " v4_1_" a_index " h22 cwhite -border BackGroundTrans left" , % "                    "  ; moving frames

loop, 17
gui, add, text, % "x10 " ((a_index=1) ? "y10 " : "yp+20 ") " v5_1_" a_index " h22 clime -border BackGroundTrans left"  , % "                    "  ; frog layer

gui, add, text, % "x10 y350 w120  vc_1_1 h22 clime -border BackGroundTrans left "  , % "         "  ; remaining frogs

gui, font, s10 , arial
gui, add, text, % "x+5 y350 w132  vc_2_1 h22 csilver -border BackGroundTrans 0x200 right"  , % "LEVEL : 1"   ; level HUD

gui, font, s9 italic, arial
gui, add, text, % "x10 y+5 w120   cwhite -border BackGroundTrans left "  , % "REGEX FROGGER"  ; title
gui, add, text, % "x+20 yp w120   csilver -border BackGroundTrans right "  , % "by Speedmaster "  ; autor

rawmapbg=
(
####################
##..##..##..##..#..#
#~~~~~~~~~~~~~~~~~~#
#~~~~~~~~~~~~~~~~~~#
#~~~~~~~~~~~~~~~~~~#
#~~~~~~~~~~~~~~~~~~#
#~~~~~~~~~~~~~~~~~~#
#::::::::::::::::::#
#::::::::::::::::::#
#_  _  _  _  _  _  #
#_  _  _  _  _  _  #
#_  _  _  _  _  _  #
#_  _  _  _  _  _  #
#                  #
#::::::::::::::::::#
#::::::::::::::::::#
####################
)

rawmap=
(
####################
##..##..##..##..#..#
#WWW    WWWWWW   WW#
#   WWWWWW   WWWWW #
#WWW   WWWWWW     W#
#          WWWWWWWW#
#  WW     WWW  WWWW#
#                  #
#                  #
#         :[HH     #
#   [=+            #
#   -(o-     -(o-  #
# >>    >>   >>    #
#  O   O   O   O   #
#                  #
#         @        #
####################
)

rawmap2=
(
####################
##..##..##..##..#..#
#WWW    WWWWWW   WW#
#   WW WW    WW WW #
#       WWWWWW     #
#          WWWWWWWW#
#  WW     WWW  WWWW#
#                  #
#                  #
#         :[HH     #
#   [=+            #
#   -(o-     -(o-  #
# >>    >>   >>    #
#  O   O   O   O   #
#                  #
#         @        #
####################
)

reset:=0
level:=1

start:

CellFont("c_1_2", "italic")
cellfont("c_1_2", "s10 cRed Bold" ,"arial")
cellfont("c_1_3", "s9" ,"arial")
drawchar("c_1_2", "", "red")
drawchar("c_1_3", "")
cellfont("6_1_15", "s10" ,"arial")
cellfont("6_1_16", "s9" ,"arial")

if (frogs<=0) {
	;msgbox,
	frogs:=""
	drawchar("5_1_2", "")
	level:=1
}

loop, 15
	if (a_index>2) {
		drawchar("5_1_" a_index, "")   ; clear frog layers
	}

frogs?:frogs:=3

map:=strsplit(rawmap, "`n", "`r")

	if (reset) {
	map.2:=frogstate.pop()
	}	

	if (youwin) {
		level++
		drawchar("c_1_2", "")
		drawchar("c_1_3", "")
		youwin:=0	
	}
	
if (level=1)
levelspeed:=0
if (level=2) {
map:=strsplit(rawmap2, "`n", "`r")	
levelspeed:=20
}
if (level>=3) {
	map:=strsplit(rawmap2, "`n", "`r")
	levelspeed:=28
}

drawchar("c_2_1", "LEVEL : " . level)
drawchar("5_1_2", sliceword(map.2, "OK"), "lime")

mapbg:=strsplit(rawmapbg, "`n", "`r")
MovingChars:="O|\.|W|>|\+|o|-|\(|\[|=|H|:"

frogx:=11
frogy:=16

for k, v in map {
	drawchar("4_1_" k, sliceword(v, MovingChars))
}
   
  
 for k, v in mapbg {
	drawchar("1_1_" k, sliceword(v, "#"))
    drawchar("2_1_" k, sliceword(v, "~")) 	
	drawchar("3_1_" k, sliceword(v, ":"))
	drawchar("6_1_" k, sliceword(v, "_"))
}	

	drawchar("5_1_16", sliceword(map.16, "@"))
	drawchar("c_1_1", livestochars(frogs))

reset:=0
dead:=0
frogstate:=[]

gui, color, black
gui, +resize
gui, show, w280 h400, REGEX FROGGER V%version%

ColorCell("4_1_11", "red")
ColorCell("4_1_14", "FFA300") ;orange
ColorCell("4_1_12", "yellow")
ColorCell("4_1_13", "FF77A8") ;pink
ColorCell("4_1_10", "aqua")

ColorCell("4_1_7", "AB5236") ; brown
ColorCell("4_1_6", "8B4513") ; brown
ColorCell("4_1_5", "bb6d3e") ; brown
ColorCell("4_1_4", "905424") ; brown
ColorCell("4_1_3", "AB5236") ; brown

loop, 17
  ColorCell("5_1_" a_index, "lime")

hiddencontrols:=["2_1_1", "2_1_2","2_1_8","2_1_9","2_1_10","2_1_11","2_1_12","2_1_13","2_1_14","2_1_15","2_1_16","2_1_17","3_1_1","3_1_2","3_1_3","3_1_4","3_1_5","3_1_6","3_1_7","3_1_10","3_1_11","3_1_12","3_1_13","3_1_14","3_1_17","4_1_1","4_1_8","4_1_9","4_1_15","4_1_16","4_1_17"]

for k, v in hiddencontrols
hidecell(v)

settimer, moverow14, % 240 - ((240*LevelSpeed)//100), on
settimer, moverow13, % 220 - ((220*LevelSpeed)//100), On
settimer, moverow12, % 240 - ((240*LevelSpeed)//100), On
settimer, moverow11, % 65 - ((65*LevelSpeed)//100), On    ;speed car
settimer, moverow10, % 230 - ((230*LevelSpeed)//100), On

settimer, moverow7, 200, On
settimer, moverow6, 130, On
settimer, moverow5, 100, On
settimer, moverow4, 250, On
settimer, moverow3, 230, On
return

moverow14:
if (youwin)
	return

if dead {
ColorCell("5_1_" frogy, "red")
	return
}

if instr(map.14, "@O", 1) {
	dead()
}

map.14:=regexreplace( map.14, "@ ", " @")
map.14:=regexreplace(map.14, "(.)(.)(.{17})(.)", "$1$3$2$4")
drawchar("4_1_" 14, sliceword(map.14 . "`n", "O"))
return

moverow13:
if (youwin)
	return

if dead {
	ColorCell("5_1_" frogy, "red")
	return
}

if RegExMatch(map[frogy], "\>@") { 
	dead()
}

map.13:=regexreplace( map.13, " @", "@ ")
map.13:=regexreplace(map.13, "(.)(.{17})(.)(.)", "$1$3$2$4")
drawchar("4_1_" 13,  RegExReplace(map.13, "#|@", " "))
return


moverow12:
if (youwin)
	return

if dead {
	ColorCell("5_1_" frogy, "red")
	return
}

if RegExMatch(map[frogy], "@-") { 
	dead()
}

map.12:=regexreplace( map.12, "@ ", " @")
map.12:=regexreplace(map.12, "(.)(.)(.{17})(.)", "$1$3$2$4")
drawchar("4_1_" 12, sliceword(map.12 . "`n", movingchars))
return


moverow11:
if (youwin)
	return

if dead {
	ColorCell("5_1_" frogy, "red")
	return
}

if RegExMatch(map[frogy], "\+@") {
	dead()
}


map.11:=regexreplace( map.11, " @", "@ ")
map.11:=regexreplace(map.11, "(.)(.{17})(.)(.)", "$1$3$2$4")
drawchar("4_1_" 11, sliceword(map.11 . "`n", movingchars))
return

moverow10:
if (youwin)
	return

if dead {
	ColorCell("5_1_" frogy, "red")
	return
}


if instr(map.10, "@:", 1) {
	dead()
}

map.10:=regexreplace( map.10, "@ ", " @")
map.10:=regexreplace(map.10, "(.)(.)(.{17})(.)", "$1$3$2$4")
drawchar("4_1_" 10, sliceword(map.10 . "`n", movingchars))
return


moverow7:
if (youwin)
	return

if (dead)
	return

map.7:=regexreplace(map.7, "(.)(.)(.{17})(.)", "$1$3$2$4")
drawchar("4_1_" 7, sliceword(map.7 . "`n", movingchars))

if (frogy!=2)
drawchar("5_1_" frogy, sliceword(map[frogy] . "`n", "@"))

if RegExMatch(map.7, "@#") {
	dead()
	return
}
return



moverow6:
if (youwin)
	return

if dead
	return

map.6:=regexreplace(map.6, "(.)(.)(.{17})(.)", "$1$3$2$4")
drawchar("4_1_" 6, sliceword(map.6 . "`n", movingchars))

if (frogy!=2)
drawchar("5_1_" frogy, sliceword(map[frogy] . "`n", "@"))


if RegExMatch(map.6, "@#") {
	dead()
	return
}
return


moverow5:
if (youwin)
	return

if dead
	return

map.5:=regexreplace(map.5, "(.)(.{17})(.)(.)", "$1$3$2$4")
drawchar("4_1_" 5, sliceword(map.5 . "`n", movingchars))

if (frogy!=2)
drawchar("5_1_" frogy, sliceword(map[frogy] . "`n", "@"))

if RegExMatch(map.5, "#@") {
	dead()
	return
}


return

moverow4:
if (youwin)
	return

if dead
	return

map.4:=regexreplace(map.4, "(.)(.)(.{17})(.)", "$1$3$2$4")
drawchar("4_1_" 4, sliceword(map.4 . "`n", movingchars))

if (frogy!=2)
drawchar("5_1_" frogy, sliceword(map[frogy] . "`n", "@"))

if RegExMatch(map.4, "@#") {
	dead()
	return
}


return

moverow3:
if (youwin)
	return

if (dead)
	return

map.3:=regexreplace(map.3, "(.)(.{17})(.)(.)", "$1$3$2$4")
drawchar("4_1_" 3, sliceword(map.3 . "`n", movingchars))

if (frogy!=2)
drawchar("5_1_" frogy, sliceword(map[frogy] . "`n", "@"))

if RegExMatch(map.3, "#@") {
	dead()
	return
}


return


~enter::

loop, 17
	if (a_index>2) {
		drawchar("5_1_" a_index, "")   ; clear frog layers
	}
drawchar("c_1_2", "")
drawchar("c_1_3", "")

if (dead) || (youwin) {
	resetgame()
}
return


~Z::
~W::
~up::

if (dead) || (youwin) {
	return
}

frogx:=instr(map[frogy], "@")

if ispassable(0,"-1") {
	if (frogy<=7)
		map[frogy]:=regexreplace(map[frogy] ,"@", "W") 
	
	map[frogy]:=regexreplace(map[frogy] ,"@"," ") ; clear previous position in data map
	drawchar("5_1_" frogy, "") ; clear frog screen layer  (layer 5)
	frogy:=frogy-1
	map[frogy]:=regexreplace( map[frogy] ,".","@",,1,frogx)

	if (frogy=2) {
		map[frogy]:=regexreplace( map[frogy] ,"#@\.","#OK",,1) ; succes
		map[frogy]:=regexreplace( map[frogy] ,"#\.@","#OK",,1) ; scces
	}
	
	if (frogy=2) {
		drawchar("5_1_" frogy, sliceword(map[frogy] . "`n", "OK"))
		frogy:=16
		map[frogy]:="#         @        #"
		drawchar("5_1_" frogy, sliceword(map[frogy] . "`n", "@"))
		checkwin()
	
		if youwin {
			gosub, endgame
			return
		}
	}	

	if (frogy!=2)
	drawchar("5_1_" frogy, sliceword(map[frogy] . "`n", "@"))

}	
else {
	if (frogy<=7) {
		map[frogy]:=regexreplace(map[frogy] ,"@", "W")
		drawchar("4_1_" frogy, sliceword(map[frogy] . "`n","W")) 
		}
	drawchar("5_1_" frogy, "") ; clear frog screen layer  (layer 5)		
	frogy:=frogy-1
	map[frogy]:=regexreplace( map[frogy] ,"#| |O|>|-|o|\(|\+|=|\[","@",,1,frogx)
	dead()
	drawchar("5_1_" frogy, sliceword(map[frogy] . "`n", "@"), "red")
}
return

~s::
~down::
if (frogy=16)
	return

if (dead) || (youwin)  {
	return
}

frogx:=instr(map[frogy], "@")

if ispassable(0,"1")  {
		map[frogy]:=regexreplace(map[frogy] ,"@", (frogy>7) ?  " " : "W") 
	drawchar("5_1_" frogy, sliceword(map[frogy] . "`n", "@"))
	frogy:=frogy+1

	map[frogy]:=regexreplace( map[frogy] ,".","@",,1,frogx) 
	drawchar("5_1_" frogy, sliceword(map[frogy] . "`n", "@"))
}
else {
	if (frogy<=7) {
		map[frogy]:=regexreplace(map[frogy] ,"@", "W")
		drawchar("4_1_" frogy, sliceword(map[frogy] . "`n","W")) 
		}
	drawchar("5_1_" frogy, "") ; clear frog screen layer  (layer 5)		
	frogy:=frogy+1
	map[frogy]:=regexreplace( map[frogy] ,"#| |O|>|-|o|\(|\+|=|\[","@",,1,frogx)

	dead()

	drawchar("5_1_" frogy, sliceword(map[frogy] . "`n", "@"), "red")
}

return

~q::
~a::
~left::

if (dead) || (youwin)  {
	return
}

if RegExMatch(map[frogy], "O@|-@|H@") {
	frogx--
	drawchar("5_1_" frogy, regexreplace(sliceword(map[frogy] . "`n", "@"), " @","@ ",,1))
	dead()
	return
}

if RegExMatch(map[frogy], " @W") { 
	dead()
}

map[frogy]:=regexreplace(map[frogy], " @", "@ ")
map[frogy]:=regexreplace(map[frogy], "W@", "@W")

drawchar("5_1_" frogy, sliceword(map[frogy] . "`n", "@"))
return

~d::
~right::

if (dead) || (youwin)  {
	return
}

if RegExMatch(map[frogy], "W@ ") { 
	dead()
}

map[frogy]:=regexreplace(map[frogy], "@ ", " @")
map[frogy]:=regexreplace(map[frogy], "@W", "W@")

drawchar("5_1_" frogy, sliceword(map[frogy] . "`n", "@"))
return

endgame:
drawchar("5_1_16" , "")
drawchar("3_1_15", "")
drawchar("3_1_16", "")
drawchar("6_1_15", "CONGRATULATION !", "lime")
drawchar("6_1_16", "PRESS ENTER TO CONTINUE", "silver")
return

return
guiclose:
~esc::
ExitApp
return


ispassable(dx,dy) {
	global
	
if  (substr( map[frogy+dy],instr(map[frogy],"@")+dx, 1) = a_space)  && (frogy>8)  && (dy<0)	
	return true

if  (substr( map[frogy+dy],instr(map[frogy],"@")+dx, 1) = a_space)  && (frogy>=8)  && (dy>0)	
	return true

if  (substr( map[frogy+dy],instr(map[frogy],"@")+dx, 1) = a_space)  && (frogy=7)  && (dy>0)	
	return true


if (substr( map[frogy+dy],instr(map[frogy],"@")+dx, 1) = "W")	
	return true

if (substr( map[frogy+dy],instr(map[frogy],"@")+dx, 1) = ".")	
	return true
}	

resetgame() {
global
loop, 15
drawchar("5_1_" a_index, "")
dead:=0
gosub, start	
}

dead() {
	global 
	dead:=1
	frogs--
	if (frogs) {
		drawchar("3_1_15", "")
		drawchar("3_1_16", "")
		drawchar("6_1_15", "YOU ARE DEAD !", "red")
		drawchar("6_1_16", "PRESS ENTER TO TRY AGAIN", "silver")
	}
	else {
		drawchar("3_1_15", "")
		drawchar("3_1_16", "")	
		drawchar("6_1_15", "GAME OVER ", "red")	
		drawchar("6_1_16", "PRESS ENTER TO PLAY AGAIN", "silver")
	}

if (frogs>0)
	reset:=1
    frogstate.push(map.2)
}

livestochars(num) {
	 loop, % num-1
		out .= "@"
return out	
}


drawchar(varname, chartodraw:="@", color:="")
{
 global
guicontrol,, %varname%, %chartodraw%
if (color)
colorcell(varname, color)
}

ColorCell(cell_to_paint, color:="red")
{
 GuiControl, +c%color%  , %cell_to_paint%
 GuiControl, MoveDraw, % cell_to_paint
}

hidecell(cell_to_hide) {

guicontrol, hide, % cell_to_hide
}	

CellFont(cell, params:="", fontname:="")
{
 Gui, Font, %params%, %fontname% 
 GuiControl, font , %cell% 
 guicontrol, movedraw, %cell%
}
	

SliceWord(text, words) {
X:=1, output := ""

if (text) && (!words)
return regexreplace(text, "(?!\v).", " ")

while (X := RegExMatch(text, "(.*?)(" words "|\v)", M, X+StrLen(M)))
	output .= Spaces(StrLen(M1)) M2
return output
}

Spaces(x)
{
	Loop %x%
		Result .= " "
	return Result
}

checkwin() {
	global map, youwin
if !(instr(map.2, "."))
	youwin:=1

}



Here's what it should look like.
---
Regex Frogger.PNG
Regex Frogger.PNG (25.5 KiB) Viewed 237 times
Last edited by SpeedMaster on 12 Jan 2019, 17:39, edited 1 time in total.
User avatar
Delta Pythagorean
Posts: 443
Joined: 13 Feb 2017, 13:44
GitHub: DelPyth
Location: Equestria

Re: [GAME] REGEX FROGGER

11 Jan 2019, 15:44

Pardon my French but I'll be damned
Helgef
Posts: 3420
Joined: 17 Jul 2016, 01:02
Contact:

Re: [GAME] REGEX FROGGER

12 Jan 2019, 05:02

Another nice one :clap:. It is impossible to win though, Helgef limitation ofc.

Thanks for sharing, cheers.
Hellbent
Posts: 254
Joined: 23 Sep 2017, 13:34

Re: [GAME] REGEX FROGGER

12 Jan 2019, 06:18

Very nice. Works on win 8.1
User avatar
SpeedMaster
Posts: 242
Joined: 12 Nov 2016, 16:09

Re: [GAME] REGEX FROGGER

12 Jan 2019, 17:53

Thanks for your comments. ;) I'm glad to see it works on other computers too. :)
I made a new update v1.2 (I removed some minor bugs.) :|
Helgef wrote:
12 Jan 2019, 05:02
It is impossible to win though, Helgef limitation ofc.
I thought I had made the game too easy. :angel:

Regards.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: arcticir, Drugwash and 29 guests