Flappy dragon
Terminal game512 lines of .st520 lines of generated Rust
You are dead!
You earned 2 points
(P) Play Again
(Q) Quit GameOn this page
The .st files#
intent.st
structop {
type_app {
exec = {
lang = "Rust"
}
}
lib_provider {
core = {
source = "StructOp/core"
version = "@latest"
}
Screen = {
source = "StructOp/game"
version = "@latest"
}
}
}main.st
ability "enum" "GameMode" {
values = [Menu, Playing, End]
}
ability "game" "Flappy" {
title = "Flappy Dragon"
key = ability.value.key
frame_ms = ability.value.frame_ms
on_tick = [ability.switch.Tick]
}
ability "switch" "Tick" {
value = data.value.mode.current
cases = [
{ value = GameMode.Menu, run = [ability.call.MainMenu] },
{ value = GameMode.End, run = [ability.call.Dead] },
{ value = GameMode.Playing, run = [ability.call.Play] },
]
}
ability "call" "MainMenu" {
function = ability.function.main_menu
}
ability "call" "Dead" {
function = ability.function.dead
}
ability "call" "Play" {
function = ability.function.play
}
ability "struct" "Player" {
fields = [
{ name = "x", type = Int },
{ name = "y", type = Int },
{ name = "velocity", type = Float },
]
}
ability "function" "gravity_and_move" {
body = [ability.if.Terminal, ability.set.Fall, ability.set.Forward, ability.if.Ceiling]
}
ability "if" "Terminal" {
condition = data.value.player.current.velocity < 2.0
then = [ability.set.Gravity]
}
ability "set" "Gravity" {
target = ability.value.player.velocity
value = data.value.player.current.velocity + 0.2
}
ability "set" "Fall" {
target = ability.value.player.y
value = data.value.player.current.y + int(data.value.player.current.velocity)
}
ability "set" "Forward" {
target = ability.value.player.x
value = data.value.player.current.x + 1
}
ability "if" "Ceiling" {
condition = data.value.player.current.y < 0
then = [ability.set.Top]
}
ability "set" "Top" {
target = ability.value.player.y
value = 0
}
ability "function" "flap" {
body = [ability.set.Up]
}
ability "set" "Up" {
target = ability.value.player.velocity
value = -2.0
}
ability "function" "render" {
body = [ability.draw.Dragon]
}
ability "draw" "Dragon" {
x = 0
y = data.value.player.current.y
glyph = "@"
color = Color.yellow
}
ability "struct" "Obstacle" {
fields = [
{ name = "x", type = Int },
{ name = "gap_y", type = Int },
{ name = "size", type = Int },
]
}
# Obstacle::new(x, score): sets ability.value.obstacle
ability "function" "new_obstacle" {
params = [{ name = "x", type = Int }, { name = "score", type = Int }]
locals = [{ name = "gap_y", type = Int }]
body = [ability.random.Gap, ability.set.Obstacle]
}
ability "random" "Gap" {
target = gap_y
from = 10
to = 40
}
ability "set" "Obstacle" {
target = ability.value.obstacle
value = {
x = x
gap_y = gap_y
size = max(2, 20 - score)
}
}
ability "function" "render_obstacle" {
params = [{ name = "player_x", type = Int }]
locals = [{ name = "screen_x", type = Int }, { name = "half_size", type = Int }, { name = "y", type = Int }]
body = [ability.set.ScreenX, ability.set.Half, ability.for.Top, ability.for.Bottom]
}
ability "set" "ScreenX" {
target = screen_x
value = data.value.obstacle.current.x - player_x
}
ability "set" "Half" {
target = half_size
value = data.value.obstacle.current.size / 2
}
# Draw the top half of the obstacle
ability "for" "Top" {
counter = y
from = 0
while = y < data.value.obstacle.current.gap_y - half_size
next = y + 1
body = [ability.draw.TopWall]
}
ability "draw" "TopWall" {
x = screen_x
y = y
glyph = "|"
color = Color.red
}
# Draw the bottom half of the obstacle
ability "for" "Bottom" {
counter = y
from = data.value.obstacle.current.gap_y + half_size
while = y < const.SCREEN_HEIGHT
next = y + 1
body = [ability.draw.BottomWall]
}
ability "draw" "BottomWall" {
x = screen_x
y = y
glyph = "|"
color = Color.red
}
ability "function" "hit_obstacle" {
returns = Bool
locals = [{ name = "half_size", type = Int }]
body = [ability.set.HitHalf]
return = data.value.player.current.x == data.value.obstacle.current.x && (data.value.player.current.y < data.value.obstacle.current.gap_y - half_size || data.value.player.current.y > data.value.obstacle.current.gap_y + half_size)
}
ability "set" "HitHalf" {
target = half_size
value = data.value.obstacle.current.size / 2
}
ability "function" "play" {
body = [
ability.clear.Sky,
ability.set.Elapsed,
ability.if.Physics,
ability.switch.PlayKey,
ability.call.Render,
ability.text.Help,
ability.text.Score,
ability.call.RenderObstacle,
ability.if.Passed,
ability.if.Fallen,
]
}
ability "clear" "Sky" {
background = Color.navy
}
ability "set" "Elapsed" {
target = ability.value.frame_time
value = data.value.frame_time.current + data.value.frame_ms.current
}
ability "if" "Physics" {
condition = data.value.frame_time.current > const.FRAME_DURATION
then = [ability.set.ResetTime, ability.call.Move]
}
ability "set" "ResetTime" {
target = ability.value.frame_time
value = 0.0
}
ability "call" "Move" {
function = ability.function.gravity_and_move
}
ability "switch" "PlayKey" {
value = data.value.key.current
cases = [
{ value = Key.space, run = [ability.call.Flap] },
]
}
ability "call" "Flap" {
function = ability.function.flap
}
ability "call" "Render" {
function = ability.function.render
}
ability "text" "Help" {
x = 0
y = 0
message = "Press SPACE to flap."
}
ability "text" "Score" {
x = 0
y = 1
message = "Score: ${data.value.score.current}"
}
ability "call" "RenderObstacle" {
function = ability.function.render_obstacle
args = [data.value.player.current.x]
}
ability "if" "Passed" {
condition = data.value.player.current.x > data.value.obstacle.current.x
then = [ability.set.Point, ability.call.NextObstacle]
}
ability "set" "Point" {
target = ability.value.score
value = data.value.score.current + 1
}
ability "call" "NextObstacle" {
function = ability.function.new_obstacle
args = [data.value.player.current.x + const.SCREEN_WIDTH, data.value.score.current]
}
ability "if" "Fallen" {
condition = data.value.player.current.y > const.SCREEN_HEIGHT || hit_obstacle()
then = [ability.set.ToEnd]
}
ability "set" "ToEnd" {
target = ability.value.mode
value = GameMode.End
}
ability "function" "restart" {
body = [ability.set.NewPlayer, ability.set.NoTime, ability.call.FirstObstacle, ability.set.ToPlaying, ability.set.NoScore]
}
ability "set" "NewPlayer" {
target = ability.value.player
value = {
x = 5
y = 25
velocity = 0.0
}
}
ability "set" "NoTime" {
target = ability.value.frame_time
value = 0.0
}
ability "call" "FirstObstacle" {
function = ability.function.new_obstacle
args = [const.SCREEN_WIDTH, 0]
}
ability "set" "NoScore" {
target = ability.value.score
value = 0
}
ability "set" "ToPlaying" {
target = ability.value.mode
value = GameMode.Playing
}
ability "function" "main_menu" {
body = [
ability.clear.Menu,
ability.text.Welcome,
ability.text.Play,
ability.text.Quit,
ability.switch.MenuKey,
]
}
ability "clear" "Menu" {
}
ability "text" "Welcome" {
y = 5
message = "Welcome to Flappy Dragon"
}
ability "text" "Play" {
y = 8
message = "(P) Play Game"
}
ability "text" "Quit" {
y = 9
message = "(Q) Quit Game"
}
ability "switch" "MenuKey" {
value = data.value.key.current
cases = [
{ value = Key.p, run = [ability.call.Restart] },
{ value = Key.q, run = [ability.quit.MenuQuit] },
]
}
ability "call" "Restart" {
function = ability.function.restart
}
ability "quit" "MenuQuit" {
}
ability "function" "dead" {
body = [
ability.clear.Dead,
ability.text.Dead,
ability.text.Points,
ability.text.Again,
ability.text.DeadQuit,
ability.switch.DeadKey,
]
}
ability "clear" "Dead" {
}
ability "text" "Dead" {
y = 5
message = "You are dead!"
}
ability "text" "Points" {
y = 6
message = "You earned ${data.value.score.current} points"
}
ability "text" "Again" {
y = 8
message = "(P) Play Again"
}
ability "text" "DeadQuit" {
y = 9
message = "(Q) Quit Game"
}
ability "switch" "DeadKey" {
value = data.value.key.current
cases = [
{ value = Key.p, run = [ability.call.RestartAgain] },
{ value = Key.q, run = [ability.quit.DeadQuit] },
]
}
ability "call" "RestartAgain" {
function = ability.function.restart
}
ability "quit" "DeadQuit" {
}vars.st
constant "SCREEN_WIDTH" {
type = Int
default = 80
}
constant "SCREEN_HEIGHT" {
type = Int
default = 50
}
constant "FRAME_DURATION" {
type = Float
default = 75.0
}
ability "value" "player" {
type = Player
initial = {
x = 5
y = 25
velocity = 0.0
}
}
data "value" "player" {
of = ability.value.player
}
# State::new() calls Obstacle::new(SCREEN_WIDTH, 0); restart() replaces it before play
ability "value" "obstacle" {
type = Obstacle
initial = {
x = 80
gap_y = 25
size = 20
}
}
data "value" "obstacle" {
of = ability.value.obstacle
}
ability "value" "score" {
type = Int
initial = 0
}
data "value" "score" {
of = ability.value.score
}
ability "value" "frame_time" {
type = Float
initial = 0.0
}
data "value" "frame_time" {
of = ability.value.frame_time
}
ability "value" "mode" {
type = GameMode
initial = GameMode.Menu
}
data "value" "mode" {
of = ability.value.mode
}
ability "value" "key" {
type = Key
}
data "value" "key" {
of = ability.value.key
}
ability "value" "frame_ms" {
type = Float
}
data "value" "frame_ms" {
of = ability.value.frame_ms
}tests/play.sttest
# 200 frames of the key script: mid-flight, the first wall on screen
run "first_wall" {
input = "\np\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nspace\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nspace\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nspace\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nspace\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
expect {
output_contains = "Score: 0 |"
}
}Input, plan and generated Rust#
input.txt · key script, one line per frame (577 lines)
input.txt · key script, one line per frame (577 lines)
p
space
space
space
space
space
space
space
space
… 177 more linesstructop plan
structop plan
# program.app will be created
+ program.app (.structop/build/app)
+ ability.enum.GameMode
+ ability.game.Flappy
+ ability.switch.Tick
+ ability.call.MainMenu
+ ability.call.Dead
+ ability.call.Play
+ ability.struct.Player
+ ability.function.gravity_and_move
+ ability.if.Terminal
+ ability.set.Gravity
+ ability.set.Fall
+ ability.set.Forward
+ ability.if.Ceiling
+ ability.set.Top
+ ability.function.flap
+ ability.set.Up
+ ability.function.render
+ ability.draw.Dragon
+ ability.struct.Obstacle
+ ability.function.new_obstacle
+ ability.random.Gap
+ ability.set.Obstacle
+ ability.function.render_obstacle
+ ability.set.ScreenX
+ ability.set.Half
+ ability.for.Top
+ ability.draw.TopWall
+ ability.for.Bottom
+ ability.draw.BottomWall
+ ability.function.hit_obstacle
+ ability.set.HitHalf
+ ability.function.play
+ ability.clear.Sky
+ ability.set.Elapsed
+ ability.if.Physics
+ ability.set.ResetTime
+ ability.call.Move
+ ability.switch.PlayKey
+ ability.call.Flap
+ ability.call.Render
+ ability.text.Help
+ ability.text.Score
+ ability.call.RenderObstacle
+ ability.if.Passed
+ ability.set.Point
+ ability.call.NextObstacle
+ ability.if.Fallen
+ ability.set.ToEnd
+ ability.function.restart
+ ability.set.NewPlayer
+ ability.set.NoTime
+ ability.call.FirstObstacle
+ ability.set.ToPlaying
+ ability.set.NoScore
+ ability.function.main_menu
+ ability.clear.Menu
+ ability.text.Welcome
+ ability.text.Play
+ ability.text.Quit
+ ability.switch.MenuKey
+ ability.call.Restart
+ ability.quit.MenuQuit
+ ability.function.dead
+ ability.clear.Dead
+ ability.text.Dead
+ ability.text.Points
+ ability.text.Again
+ ability.text.DeadQuit
+ ability.switch.DeadKey
+ ability.call.RestartAgain
+ ability.quit.DeadQuit
+ ability.value.player
+ ability.value.obstacle
+ ability.value.score
+ ability.value.frame_time
+ ability.value.mode
+ ability.value.key
+ ability.value.frame_ms
Plan: 1 to add, 0 to change, 0 to destroy.Generated main.rs
To inspect this generated file, select Rust in intent.st, initialize the project and export its source.
Export locally
structop init
structop apply -source -no-runTry it
cd rust-flappy-dragon # a folder holding the .st files above
structop init
structop plan
structop apply