Skip to content

Instantly share code, notes, and snippets.

@scorredoira
Created July 26, 2020 19:14
Show Gist options
  • Save scorredoira/214c2352577ddb03813dae8da9fd5891 to your computer and use it in GitHub Desktop.
Save scorredoira/214c2352577ddb03813dae8da9fd5891 to your computer and use it in GitHub Desktop.
terminal test game
const TUNNEL_HEIGHT = 0.26
const MAIN_LOOP_DELAY_MILIS = 15
const START_DELAY_MILLIS = 1000
export function main() {
terminal.init()
defer(() => terminal.close())
let size = terminal.size()
let map = generateMap(size.width, size.height)
redraw(map)
time.sleep(START_DELAY_MILLIS * time.Millisecond)
go(() => {
for (let i = 1; i < map.end; i++) {
map.scrollX++
redraw(map)
if (map.crashed) {
showCrash()
return
}
time.sleep(MAIN_LOOP_DELAY_MILIS * time.Millisecond)
}
})
LOOP:
while (true) {
let ev = terminal.pollEvent()
switch (ev.type) {
case terminal.EventKey:
switch (ev.key) {
case terminal.KeyArrowUp:
map.posY--
break
case terminal.KeyArrowDown:
map.posY++
break
case terminal.KeyArrowLeft:
map.posX--
break
case terminal.KeyArrowRight:
map.posX++
break
default:
break LOOP
}
}
}
}
function redraw(map: Map) {
let scrollX = map.scrollX
let botton = map.screenSize.height
for (let y = 0; y < botton; y++) {
for (let x = 0, w = map.screenSize.width; x < w; x++) {
let y1 = map.ceiling[x + scrollX]
let y2 = map.floor[x + scrollX]
let bg
if (y <= y1 || y >= y2) {
bg = terminal.ColorCyan
} else {
bg = terminal.ColorDefault
}
terminal.setCell(x, y, " ", terminal.ColorDefault, bg)
}
}
terminal.setCell(map.posX, map.posY, " ", terminal.ColorDefault, terminal.ColorGreen)
terminal.flush()
checkChrash(map)
}
function checkChrash(map: Map) {
let x = map.posX + map.scrollX
if (map.posY <= map.ceiling[x]) {
map.crashed = true
return
}
if (map.posY >= map.floor[x]) {
map.crashed = true
return
}
}
function showCrash() {
let colors = [
terminal.ColorRed,
terminal.ColorYellow,
terminal.ColorRed,
terminal.ColorYellow,
terminal.ColorRed
]
for (let c of colors) {
terminal.clear(terminal.ColorDefault, c)
terminal.flush()
time.sleep(100 * time.Millisecond)
}
terminal.close()
os.exit(1)
}
interface Map {
ceiling: number[]
floor: number[]
scrollX: number
end: number
posX: number
posY: number
crashed?: boolean
screenSize: {
height: number
width: number
}
}
function generateMap(width: number, height: number): Map {
let w = width * 100
let ceiling = array.make(w) as number[]
let floor = array.make(w) as number[]
let tunnelHeight = math.floor(height * TUNNEL_HEIGHT)
let y1 = 0 + tunnelHeight
let y2 = height - tunnelHeight
for (let i = 0; i < w; i++) {
let r = math.rand(2)
if (r >= 1) {
if (y2 < height) {
y1++
y2++
}
} else {
if (y1 > 0) {
y1--
y2--
}
}
ceiling[i] = y1
floor[i] = y2
}
let posY = math.floor(ceiling[3] + tunnelHeight)
return {
ceiling: ceiling,
floor: floor,
scrollX: 0,
end: w - width,
posX: 3,
posY: posY,
screenSize: {
width: width,
height: height
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment