Skip to content

Instantly share code, notes, and snippets.

@G33kDude G33kDude/Rogue.ahk
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.