Skip to content

Instantly share code, notes, and snippets.

@thomasjslone
Last active June 13, 2016 09:48
Show Gist options
  • Save thomasjslone/019f3c36eb64c530cc6974436e6c6ea3 to your computer and use it in GitHub Desktop.
Save thomasjslone/019f3c36eb64c530cc6974436e6c6ea3 to your computer and use it in GitHub Desktop.
RKON - Windows terminal text-based game engine in pure Ruby
# game notes
a pretty buggy build this game was but it finnaly runs, hold a to make the dot move, it will bounce off the walls and make a beep sound
the lag is just awful, wich is a combination between the bulky gems being used to check input and make beep
better gems are needed obviously but not bad for such basic coding
game notes here
#H#;#E#;#A#;#D#;#E#;#R#
@game_title = "Sample Game"
@software_version = "0.0.0"
@software_manufacturer = ""
@software_id = "01x"
@release_date "2016-6-1"
@libraries = false
#secret_id = "486702"
#D#;#A#;#T#;#A#
bytes of data here
#C#;#O#;#D#;#E#
@str = ""
@width = 25
@space = " "
@pixel = "O"
@wrapper = "|"
@index_l = @width - 1
@spacer = ""
@direction = 0
@width.times { @spacer << @space.to_s}
@i = 0
loop do
b = @kbhit.Call.zero? ? nil : @getch.Call
s = @spacer.split("")
s[@i.to_i] = @pixel.to_s
l = @i - 1
n = @i + 1
s[l.to_i] = @space.to_s
s[n.to_i] = @space.to_s
if @direction == 0
if b.to_i.chr.to_s == "a"
if @i.to_i == @index_l.to_i
Sound.beep(3000,250)
@i = @index_l.to_i - 1
@direction = 1
else
@i += 1
end
end
elsif @direction == 1
if b.to_i.chr.to_s == "a"
if @i.to_i == 0
Sound.beep(3000,250)
@i = 1
@direction = 0
else
@i -= 1
end
end
end
@spacer = s.join("").to_s
vcan = @canvas.split("\n")
vcan[0] = @wrapper.to_s + @spacer.to_s + @wrapper.to_s
@canvas = vcan.join("\n").to_s
sleep 0.100
end
# game notes
game notes here
#H#;#E#;#A#;#D#;#E#;#R#
@game_title = "Sample Game"
@software_version = "0.0.0"
@software_manufacturer = ""
@software_id = "01x"
@release_date "2016-6-1"
@libraries = false
#secret_id = "486702"
#D#;#A#;#T#;#A#
bytes of data here
#C#;#O#;#D#;#E#
@str = ""
@width = 25
@space = " "
@pixel = "O"
@wrapper = "|"
@index_l = @width - 1
@spacer = ""
@direction = 0
@width.times { @spacer << @space.to_s}
@i = 0
loop do
s = @spacer.split("")
b = @kbhit.Call.zero? ? nil : @getch.Call
s[@i.to_i] = @pixel.to_s
if @direction == 0
if b.to_i.chr.to_s == "a"
if @i.to_i == 0
ri = @index_l.to_i
else
ri = @i - 1
end
end
elsif @direction == 1
if b.to_i.chr.to_s == "a"
if @i.to_i == @index_l.to_i
ri = 0
else
ri = @i + 1
end
end
end
s[ri.to_i] = @space.to_s
@spacer = s.join("").to_s
vcan = @canvas.split("\n")
vcan[0] = @wrapper.to_s + @spacer.to_s + @wrapper.to_s
@canvas = vcan.join("\n").to_s
if @direction == 0
if b.to_i.chr.to_s == "a"
if @i.to_i == @index_l.to_i
@i = @index_l.to_i - 1
@direction = 1
else
@i += 1
end
end
elsif @direction == 1
if b.to_i.chr.to_a == "a"
if @i.to_i == 0
@i = 1
@direction = 0
else
@i -= 1
end
end
end
sleep @frame_rate.to_f
end
# the emulator has been simplified, component classes were removed and combined with the emulator
# now the emulator uses a run method that wor
require 'win32/sound'
require 'Win32API'
include Win32
class Emulator
def initialize
require 'Win32API'
@kbhit = Win32API.new('crtdll', '_kbhit', [ ], 'I')
@getch = Win32API.new('crtdll', '_getch', [ ], 'L')
@resolution = [100,30] ## how many spaces wide by how many lines long
@frame_rate = 0.200 ## how much of a secoond to wait between frames
@whitespace = "" ## this will represent an empty line, which is transperant.
@resolution[0].times { @whitespace << " "}
@canvas = "" ## this is a virtual image of what your game screen look like in real time
@resolution[1].times { @canvas < @whitespace.to_s + "\n"}
@canvas = @canvas.to_s[0..-3].to_s ## just removing that last line break at the end of the screen, ill readd it if th cursor looks bab
@homedir = Dir.getwd.to_s # this wont be much use once sound is stored as bytes in the games source
@game_image = ""
@source = "" # this virale will contain the loaded bytes of the games main file
@data = ""
@header = ""
@running = false # this lets any componet know a game is being processed
@button_down = nil
@main_thread = nil
load_rom("game")
@main_thread = Thread.new { run }
#@l = Thread.new { last }
execute_game
end
def refresh_screen
@resolution[1].times { print "\e[A" }
@resolution[1].times { print @whitespace.to_s + "\n" }
@resolution[1].times { print "\e[A" }
print "\r"
end
def run
@run = true
while @run
#@button_down =
print "r"
@resolution[1].times { print "\e[A" }
@resolution[1].times { print @whitespace.to_s + "\n" }
@resolution[1].times { print "\e[A" }
print @canvas.to_s
sleep @frame_rate.to_f
end
end
def load_rom(name)
prepare_emulator
puts "fuile:"
puts "check:" + File.exist?(@homedir.to_s + "/" + name.to_s + ".rb").to_s
if File.exist?(@homedir.to_s + "/" + name.to_s + ".rb")
file = File.open(@homedir.to_s + "/" + name.to_s + ".rb","rb")
@game_image = file.read.to_s
@source = @game_image.to_s.split("#C#;#O#;#D#;#E#")[-1].to_s
@data = @game_image.split("#D#;#A#;#T#;#A#")[0].to_s.split() #here can be bytecodes for your games graphics so you dont have to store them in your games ruby code
@header = @game_image.split("#H#;#A#;#A#;#D#;#E#;#R#")[0].to_s.split("#D#;#A#;#T#;#A#")[0].to_s # header tells about the game
return "true"
else
return "false"
end
end
def home_screen
wait = true
print "Welcome to RKONS\n"
end
def prepare_emulator
@game_image = nil
@header = nil
@data = nil
@source = nil
@running = false
end
def ev(s)
eval(s.to_s)
end
def execute_game
refresh_screen
@running = true #let every one know main system is running a game
eval(@source.to_s) # process game code
@running = false # done processing game code,
end
end
$rkon = Emulator.new()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment