Skip to content

Instantly share code, notes, and snippets.

@G33kDude
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save G33kDude/ccab4b48834644f3ecc5 to your computer and use it in GitHub Desktop.
Save G33kDude/ccab4b48834644f3ecc5 to your computer and use it in GitHub Desktop.
A roguelike for /r/dailyprogrammer
Width := 20, Height := 20
KeyMap := {"up": "up", "left": "left", "down": "down", "right": "right"}
Map := [], BlankSquares := []
Loop, % Width
{
x := A_Index
Loop, % Height
{
y := A_Index
if (x == 1 || x == Width || y == 1 || y == Height)
Map[x, y] := new Wall()
else if (x == Width//2 && y == Height//2)
Map[x, y] := new Player()
else
Map[x, y] := new Empty(), BlankSquares.Insert([x, y])
}
}
RandElements := [Monster, Treasure, Food]
Loop, % Width*Height/4 ; 100 with a standard 20x20 board
{
Pos := BlankSquares.Remove(Rand(1, BlankSquares.MaxIndex()))
Element := RandElements[Rand(1, RandElements.MaxIndex())]
Map[Pos*] := new Element()
}
Gui, Font, s10, Consolas
Gui, Add, Edit, x0 y0 w500 h500 Disabled vDisplay, % DrawMap(Map) "`n`nEnergy: 100"
Gui, Show, w500 h500
Loop
{
Out := "", Elements := []
for x, Col in Map
for y, Element in Col
Elements[[x, y]] := Element
for Pos, Element in Elements
if !Element.Deleted && (Tmp := Element.Step(Map, Pos*))
Out .= "`n" Tmp
GuiControl,, Display, % DrawMap(Map) . Out
}
ExitApp
return
GuiClose:
ExitApp
return
Rand(Min, Max)
{
Random, Rand, Min, Max
return Rand
}
class Monster
{
Symbol := "X"
Heals := -1
Tagline := "Ow, a monster: -1"
Step(Map, x, y)
{
Dirs := [[0,-1],[0,1],[-1,0],[1,0]]
if !Rand(0, Dirs.MaxIndex())
return ; Don't move, chance is 1 in Dirs+1
Loop, % Dirs.MaxIndex()
{
Dir := Dirs.Remove(Rand(1, Dirs.MaxIndex()))
nx := x+Dir[1]
ny := y+Dir[2]
if (Map[nx, ny].Empty)
{
Map[nx, ny].Deleted := True
Map[nx, ny] := this
Map[x, y] := new Empty()
return
}
}
}
}
class Treasure
{
Symbol := "$"
Healthy := True
Heals := 1
Tagline := "Cool, Moneys: +1"
}
class Food
{
Symbol := "#"
Heals := 2
Tagline := "Yum, food: +2"
}
class Wall
{
Symbol := "+"
Solid := True
}
Class Empty
{
Empty := True
Symbol := " "
}
class Player
{
Symbol := "@"
Energy := 100
Step(Map, x, y)
{
global KeyMap
static PID := DllCall("GetCurrentProcessId")
Loop
{
sleep, 50
if !WinActive("ahk_pid " PID)
continue
for Key, Bool in GetKeyStates(KeyMap*)
if Bool
break, 2
}
KeyWait, % KeyMap[Key], t0.2
Pos := {"up": [x, y-1], "left": [x-1, y]
, "down": [x, y+1], "right": [x+1, y]}[Key]
Element := Map[Pos*]
if (Element.Solid)
return
Out := Element.Tagline
if (Element.Heals)
this.Energy += Element.Heals
this.Energy -= 1
if this.Energy < 1
{
MsgBox, Game over
ExitApp
}
Map[Pos*].Deleted := True
Map[Pos*] := this
Map[x, y] := new Empty()
Out .= "`nEnergy: " this.Energy
return Out
}
}
GetKeyStates(Keys*)
{
Out := []
for Index, Key in Keys
Out[Index] := GetKeyState(Key, "P")
return Out
}
DrawMap(Map)
{
Flip := []
for x, Col in Map
for y, Element in Col
Flip[y,x] := Element.Symbol
Buffer := ""
for y, Row in Flip
{
for x, Char in Row
Buffer .= Char
Buffer .= "`n"
}
return Buffer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment