vi studio

Lessons · Examples · 34 of 36

Example: Comet Glide

A tap-to-fly arcade game with every() and place.

The Comet Glide template: an arcade game. Tap to push the comet up, gravity pulls it down, and gates of light slide in from the right. src/play.vi runs the game loop with every(0.03, step()) and ends it with stop(loop); everything is placed with place: x, y, on_touch reacts the moment a finger lands, and on_hit notices when the comet touches a pillar. The sky is 1.6 times as tall as it is wide, which is why heights are multiplied by TALL.

src/main.vi
src/config.vi
src/play.vi
src/theme.vi
src/world.vi
src/components/comet.vi
src/components/gates.vi
src/components/panels.vi
src/components/sky.vi

src/main.vi

<# Comet Glide: tap to keep a comet in the air and weave it through
   gates of light. Each gate you pass is a point.
   config.vi    app name, icon and splash screen
   theme.vi     night-sky colours
   world.vi     what is in the game and where it is
   play.vi      the rules: tap, gravity, gates, score, crash
   components/  stars, the comet, the gates and the cards on top #>

main home:
  color: sky
  arrange: center
  children:
    field

  <# The playing area: the whole screen.
     Everything inside is placed with place: x, y. #>

  field:
    size: max, max
    arrange: column
    children:
      star(stars[0])
      star(stars[1])
      star(stars[2])
      star(stars[3])
      star(stars[4])
      star(stars[5])
      star(stars[6])
      star(stars[7])
      star(stars[8])
      star(stars[9])
      gate_top(gates[0])
      gate_bottom(gates[0])
      gate_top(gates[1])
      gate_bottom(gates[1])
      gate_top(gates[2])
      gate_bottom(gates[2])
      comet_parts
      score_display
      ready_card
      over_card
    <# The moment the comet's head overlaps any pillar, it crashes #>
    on_hit:
      comet_head, gate_top: hit()
      comet_head, gate_bottom: hit()
    on_touch: tap()

src/config.vi

<# App settings: name, version, launcher icon, splash screen and screen sizes.
   Every file in src/ can see them, so nothing needs importing.
   Before your first store build, add a store id that never changes again:
     id: com.yourname.myapp #>
config:
  name: "{{app_name}}"
  version: "1.0.0"
  icon: @icon.png
  text_size: follow_phone
  splash:
    color: rgb(14, 16, 38)
    logo: @splash.png
    time: 1 second
  screens:
    mobile: 0 to 600
    tablet: 600 to 1200
    desktop: 1200 to max

src/play.vi

<# The rules: tapping, falling, moving the gates, scoring and crashing. #>

GRAVITY = 0.16
LIFT = -2.4
loop = 0

<# One tap anywhere on the sky #>
tap():
  if (state == "ready"):
    start()
  else if (state == "playing"):
    speed_y = LIFT
  else:
    state = "ready"
    reset_world()

start():
  reset_world()
  state = "playing"
  speed_y = LIFT
  <# step() runs about 33 times a second, until stop(loop) #>
  loop = every(0.03 seconds, step())

step():
  trail = [comet_y, trail[0], trail[1]]
  speed_y = speed_y + GRAVITY
  if (speed_y > 3):
    speed_y = 3
  comet_y = comet_y + speed_y
  if (comet_y < 2):
    comet_y = 2
    speed_y = 0
  move_gates()
  drift_stars()
  if (comet_y > 98):
    crash()

<# The gates speed up a little with every point #>
gate_speed():
  return 0.85 + score * 0.02 if (score < 25) else 1.35

move_gates():
  for gate in gates:
    gate.x = gate.x - gate_speed()
    if (not gate.passed and gate.x + GATE_WIDTH / 2 < COMET_X):
      gate.passed = true
      score = score + 1
    if (gate.x < -10):
      gate.x = gate.x + 180
      gate.gap_y = new_gap()
      gate.passed = false

drift_stars():
  for s in stars:
    s.x = s.x - 0.08
    if (s.x < -2):
      s.x = 102

<# The comet touched a pillar (see on_hit in main.vi) #>
hit():
  if (state == "playing"):
    crash()

crash():
  stop(loop)
  state = "over"
  if (score > best):
    best = score

src/theme.vi

<# Night-sky colours. rgb() takes a 4th value for see-through colours (0 to 1). #>
sky = rgb(14, 16, 38)
star_light = rgb(255, 255, 255, 0.55)
gate_glow = rgb(124, 92, 255, 0.28)
gate_core = rgb(176, 150, 255)
comet_core = rgb(255, 221, 120)
comet_glow = rgb(255, 190, 80, 0.25)
tail_1 = rgb(255, 190, 80, 0.55)
tail_2 = rgb(255, 160, 80, 0.35)
tail_3 = rgb(255, 130, 80, 0.18)
text_soft = rgb(255, 255, 255, 0.65)
panel = rgb(32, 34, 70, 0.92)

src/world.vi

<# Everything in the game world, and how to set it up again.
   Across: x goes from 0 (left) to 100 (right), as a % of the sky's width.
   Down:   y goes from 0 (top) to 100 (bottom), as a % of the sky's height.
   A size's width is a % of the sky's width and its height a % of the
   sky's height, so a pillar's height uses the same steps as y. #>
COMET_X = 25
GAP = 30
GATE_WIDTH = 16

<# "ready" (waiting for a tap), "playing" or "over" #>
state = "ready"
score = 0
best = 0

comet_y = 45
speed_y = 0
<# where the comet was a moment ago: drawn as its tail #>
trail = [45, 45, 45]

<# Three gates of light. Each has an opening around gap_y. #>
gates = [
  { x: 110, gap_y: 50, passed: false },
  { x: 170, gap_y: 40, passed: false },
  { x: 230, gap_y: 60, passed: false }
]

stars = [
  { x: 8, y: 12, size: 1.2 }, { x: 22, y: 64, size: 0.8 }, { x: 35, y: 30, size: 1.0 },
  { x: 47, y: 85, size: 1.4 }, { x: 58, y: 18, size: 0.8 }, { x: 66, y: 52, size: 1.2 },
  { x: 74, y: 76, size: 0.9 }, { x: 83, y: 38, size: 1.3 }, { x: 91, y: 8, size: 0.8 },
  { x: 96, y: 92, size: 1.1 }
]

new_gap():
  return random(25, 76)

reset_world():
  score = 0
  comet_y = 45
  speed_y = 0
  trail = [45, 45, 45]
  n = 0
  for gate in gates:
    gate.x = 110 + n * 60
    gate.gap_y = new_gap()
    gate.passed = false
    n = n + 1

src/components/comet.vi

<# The comet: a tail of fading sparks that follows where it just was,
   a soft glow, and a bright head. #>
<# It covers the whole sky (same size as the field), so its parts can use
   the same x and y as everything else. #>
comet_parts:
  size: 100, 100
  arrange: column
  place: 50, 50
  children:
    spark(trail[2], 9.5, 2.6, tail_3)
    spark(trail[1], 7, 3.4, tail_2)
    spark(trail[0], 4.5, 4.4, tail_1)
    comet_halo
    comet_head

spark(y, behind, size, tint):
  size: size
  color: tint
  shape: circle
  place: COMET_X - behind, y

comet_halo:
  size: 12
  color: comet_glow
  shape: circle
  place: COMET_X comet_y

comet_head:
  size: 6
  color: comet_core
  shape: circle
  place: COMET_X comet_y

src/components/gates.vi

<# A gate is two pillars of light with an opening between them.
   The top pillar runs from above the screen (y = -40) down to the opening,
   the bottom one from the opening down below the screen (y = 140).
   A pillar's middle is halfway between its two ends. #>
gate_top(gate):
  size: GATE_WIDTH, gate.gap_y - GAP / 2 + 40
  color: gate_glow
  shape: rounded
  arrange: center
  place: gate.x, (gate.gap_y - GAP / 2 - 40) / 2
  children:
    pillar_core

gate_bottom(gate):
  size: GATE_WIDTH, 140 - gate.gap_y - GAP / 2
  color: gate_glow
  shape: rounded
  arrange: center
  place: gate.x, (140 + gate.gap_y + GAP / 2) / 2
  children:
    pillar_core

<# The bright line in the middle of each pillar #>
pillar_core:
  size: 30, max
  color: gate_core
  shape: rounded

src/components/panels.vi

<# The score at the top while you play #>
score_display:
  visible: state == "playing"
  size: 40, auto
  text: "{score}"
  text_style: 48, bold, white
  arrange: center
  place: 50, 9

<# Before the first tap #>
ready_card:
  size: 80, auto
  place: 50, 25
  color: panel
  shape: rounded
  space_inside: top bottom 28
  space_between: 8
  arrange: center
  visible: state == "ready"
  children:
    game_title
    game_tagline
    tap_hint
    best_line

  game_title:
    text: "Comet Glide"
    text_style: 34, bold, white

  game_tagline:
    text: "Weave through the gates of light"
    text_style: 15, text_soft

  tap_hint:
    text: "Tap to fly"
    text_style: 18, bold, comet_core
    space_around: top 16

  best_line:
    text: "Best: {best}"
    text_style: 14, text_soft

<# After a crash #>
over_card:
  size: 80, auto
  place: 50, 25
  color: panel
  shape: rounded
  space_inside: top bottom 28
  space_between: 6
  arrange: center
  visible: state == "over"
  children:
    over_title
    over_score
    over_best
    over_hint

  over_title:
    text: "Comet down!"
    text_style: 28, bold, white

  over_score:
    text: "{score}"
    text_style: 64, bold, comet_core

  over_best:
    text: "New best!" if (score == best and score > 0) else "Best: {best}"
    text_style: 16, semibold, text_soft

  over_hint:
    text: "Tap to try again"
    text_style: 15, text_soft
    space_around: top 14

src/components/sky.vi

<# A tiny star. Stars drift slowly left, behind everything else. #>
star(s):
  size: s.size
  color: star_light
  shape: circle
  place: s.x, s.y

Every example here runs in Vi Studio: open it in your browser (phones too) and press Start learning on the welcome page. Each code block there has a Try it button that runs it in the phone preview.

More in Examples

About Vi. Vi is a free, open-source programming language for building real mobile apps. You describe each screen in plain words, and Vi compiles it to React Native (Expo) for Android, iOS and the web. Its editor, Vi Studio, runs on Windows, in any browser and on your phone, with the app running live as you type. Read more about Vi.