Flappy player
Terminal game347 lines of .st411 lines of generated Rust
Press SPACE to flap. @
On 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 "function" "play" {
body = [
ability.clear.Sky,
ability.set.Elapsed,
ability.if.Physics,
ability.switch.PlayKey,
ability.call.Render,
ability.text.Help,
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 "if" "Fallen" {
condition = data.value.player.current.y > const.SCREEN_HEIGHT
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.set.ToPlaying]
}
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 "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.Again,
ability.text.DeadQuit,
ability.switch.DeadKey,
]
}
ability "clear" "Dead" {
}
ability "text" "Dead" {
y = 5
message = "You are dead!"
}
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
}
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
}Input, plan and generated Rust#
input.txt · key script, one line per frame (21 lines)
input.txt · key script, one line per frame (21 lines)
p
spacestructop 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.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.if.Fallen
+ ability.set.ToEnd
+ ability.function.restart
+ ability.set.NewPlayer
+ ability.set.NoTime
+ ability.set.ToPlaying
+ 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.Again
+ ability.text.DeadQuit
+ ability.switch.DeadKey
+ ability.call.RestartAgain
+ ability.quit.DeadQuit
+ ability.value.player
+ 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-player # a folder holding the .st files above
structop init
structop plan
structop apply