Do u like idle games with minimal interaction? If so, I’ve written for you, a lil friend 😄
This one is in Elixir since I thought I’d revisit it after a few years.
defmodule Friend do
@moods [:happy, :hungry, :sleepy, :grumpy]
@ascii %{
happy: ~s"""
(^・o・^)ノ♡
I'm feeling happy!
""",
hungry: ~s"""
( ´・ω・)っ🍞
I'm huuungry...
""",
sleepy: ~s"""
(-_-) zzZ
So... sleepy...
""",
grumpy: ~s"""
(ಠ_ಠ)
I'm grumpy, hmph.
"""
}
@config Path.expand("~/.pet")
def start do
name = init()
state = %{mood: :happy}
loop(name, state)
end
defp prompt do
IO.puts("What's your pet's name?")
name =
IO.read(:stdio, :line)
|> String.trim()
File.write!(@config, name)
name
end
defp init do
if File.exists?(@config) do
@config
|> File.read!()
|> String.trim()
else
prompt()
end
end
defp loop(name, state) do
clear_screen()
IO.puts("\n#{name} appears!:")
IO.puts(@ascii[state.mood])
IO.puts("\nWhat do you want to do?")
IO.puts("1. Feed 🍎")
IO.puts("2. Cuddle 💖")
IO.puts("3. Nap 💤")
IO.puts("4. Do nothing")
IO.write("> ")
action = IO.gets("") |> String.trim()
new_state =
case action do
"1" -> %{state | mood: :happy}
"2" -> %{state | mood: :happy}
"3" -> %{state | mood: :sleepy}
"4" -> next_random_mood(state.mood)
_ -> state
end
loop(name, new_state)
end
defp next_random_mood(current) do
new_mood = Enum.random(@moods -- [current])
%{mood: new_mood}
end
defp clear_screen do
IO.write("\e[H\e[2J")
end
end
Friend.start()
Literally just run elixir friend.exs
and have fun 😃
Backlinks
No backlinks found.