107 lines
2.9 KiB
Text
107 lines
2.9 KiB
Text
out `Small ``Game\'\'\n'
|
||
use term
|
||
|
||
set `score' `0'
|
||
set `win.maxw' {
|
||
# ^ win.maxw is just the variable name. The dot is just a character.
|
||
# the dot doesn't mean anything special here.
|
||
|
||
# We doin RPN :O
|
||
sub `1' \
|
||
# `term:rows' is the name a command being run. \
|
||
# since variable names are quite permissive, this is just the name of a \
|
||
# command defined within the file `term' (imported at the top) \
|
||
# \
|
||
# Also note that a \ at the end of a line, even after a comment \
|
||
# continues a line. meaning, term:rows will be the seconc argument \
|
||
# of the subtraction above. \
|
||
term:rows
|
||
}
|
||
set `win.maxh' {sub `1' term:cols}
|
||
|
||
set `guy.char' `\U0001FBC5'
|
||
set `guy.x' `1'
|
||
set `guy.y' `1'
|
||
|
||
# Unicode is supported inside quotes:
|
||
set `star.char' `⭐'
|
||
|
||
# also non-ascii is allowed in identifiers:
|
||
set `\x1b' `hello'
|
||
# and escapes are allowed in the middle of identifiers:
|
||
out \x1b # << outputs hello
|
||
# and interpolation can be done on its own:
|
||
out <`\x1b'> # << also outputs hello
|
||
# and ofc, you could use ^[ in the raw code:
|
||
out # << bad idea, but yes, it still writes hello.
|
||
# and then we'll undefine it for your sanity:
|
||
del `\x1b'
|
||
|
||
# `def' defines a command
|
||
def randstarpos {
|
||
set `star.x' {
|
||
# Get the real part of the rounded number after adding 1. (
|
||
# rounding removes decimal parts but leaves the immaginary
|
||
# part):
|
||
rea {rnd {add `1' {
|
||
mul win.maxw rnd
|
||
# ^ rnd returns a random number
|
||
# from 0 to 1
|
||
}}}
|
||
}
|
||
set `star.y' {rea {rnd
|
||
add `1' {mul rnd win.maxh}
|
||
}}
|
||
}
|
||
def w {set `guy.y' {rea {rnd {
|
||
max `1' {sub `1' guy.y}
|
||
}}}}
|
||
def a {set `guy.x' {rea {rnd {max `1' {sub `1' guy.x}}}}}
|
||
def s {set `guy.y' {rea {rnd {min win.maxh {add `1' guy.y}}}}}
|
||
def d {set `guy.x' {rea {rnd {min win.maxw {add `1' guy.x}}}}}
|
||
|
||
!randstarpos
|
||
inf !{
|
||
#^the infinite loop command.
|
||
#Note that the bang is nessicary as otherwise, the lazy would always
|
||
#inherit the same state.
|
||
out `\x1b[<rea {rnd guy.y}>;<rea {rnd guy.x}>H<guy.char>'
|
||
# ^writes to stdout
|
||
out `\x1b[<rea {rnd star.y}>;<rea {rnd star.x}>H<star.char>'
|
||
map inp \
|
||
`w' !w \
|
||
`k' !w \
|
||
`a' !a \
|
||
`j' !a \
|
||
`s' !s \
|
||
`h' !s \
|
||
`d' !d \
|
||
`l' !d \
|
||
defa {}
|
||
# ^map -- maps a value to a result
|
||
# The arguments alternate between
|
||
# things to match against and lazies
|
||
# to run as a result of those matches.
|
||
# The result is always a block that
|
||
# will be ccl'd if the match is found.
|
||
# The last thing to match against is
|
||
# always assumed to be the default.
|
||
# It is convention to make the last
|
||
# matching argument `defa'.
|
||
# `inp' gets a single char from stdin.
|
||
|
||
ifs {
|
||
eql `<guy.x>' `<star.x>'
|
||
# ^ eql will return `f' if they're
|
||
# not equal and `' if they're equal.
|
||
eql `<guy.y>' `<star.y>'
|
||
# Note that since output is combined, this block is effectively
|
||
# an and operator. The actual and operator is better tho. It
|
||
# provides short-circuting.
|
||
} then !{
|
||
!randstarpos
|
||
set score {add `1' score}
|
||
out `\x1b[2J\x1b[HScore: <rea {rnd score}>'
|
||
}
|
||
}
|
||
|