Created
January 8, 2017 10:30
-
-
Save dncrht/d06e1bc7ee172de65f3f6a370c097c9c to your computer and use it in GitHub Desktop.
Usborne's Bouncing Bug port to ncurses and Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ncurses' | |
class BouncingBug | |
FRAMES_PER_SECOND = 4 | |
TIME_BETWEEN_FRAMES = 1.0 / FRAMES_PER_SECOND | |
attr_reader :scr | |
def initialize | |
Ncurses.initscr # Initialize ncurses | |
Ncurses.curs_set(0) # Hide cursor | |
Ncurses.cbreak # Provide unbuffered input | |
Ncurses.noecho # Turn off input echoing | |
Ncurses.nonl # Turn off newline translation | |
Ncurses.stdscr.intrflush(false) # Turn off flush-on-interrupt | |
Ncurses.stdscr.keypad(true) # Turn on keypad mode | |
Ncurses.stdscr.nodelay(true) # http://hughm.cs.ukzn.ac.za/~murrellh/os/notes/ncurses.html#cursor | |
@scr = Ncurses.stdscr | |
end | |
def call | |
main | |
ensure | |
Ncurses.echo | |
Ncurses.nocbreak | |
Ncurses.nl | |
Ncurses.endwin | |
end | |
def main | |
scr.mvaddstr(0, 0, 'BOUNCING BUG : SCORE') | |
build_garden | |
flowers = grow_flowers | |
x = rand(18) + 1 | |
y = rand(18) + 1 | |
dx = 1 | |
dy = 0 | |
score = 0 | |
next_tick = Time.now | |
while true | |
dx, dy = read_player_input(dx, dy) | |
if Time.now > next_tick | |
x, y = update_player(x, y, dx, dy) | |
if flowers[y - 1][x - 1] | |
score += 100 | |
end | |
next_tick = Time.now + TIME_BETWEEN_FRAMES | |
end | |
draw_scene(x, y, score) | |
end | |
end | |
def read_player_input(dx, dy) | |
key = scr.getch | |
case key | |
when Ncurses::KEY_UP | |
dx = -1 | |
dy = 0 | |
when Ncurses::KEY_DOWN | |
dx = 1 | |
dy = 0 | |
when Ncurses::KEY_LEFT | |
dy = -1 | |
dx = 0 | |
when Ncurses::KEY_RIGHT | |
dy = 1 | |
dx = 0 | |
when 113 # Q | |
exit | |
end | |
[dx, dy] | |
end | |
def update_player(x, y, dx, dy) | |
if dx != 0 || dy != 0 | |
scr.mvaddstr(y_playfield(y), x, ' ') # restore background | |
end | |
x += dy | |
y += dx | |
if y < 1 then y = 1 end | |
if y > 18 then y = 18 end | |
if x < 1 then x = 1 end | |
if x > 18 then x = 18 end | |
[x, y] | |
end | |
def draw_scene(x, y, score) | |
scr.mvaddstr(y_playfield(y), x, '🐛') | |
scr.mvaddstr(0, 21, score.to_s) | |
end | |
def build_garden(boundary = '🌳') | |
20.times { |x| scr.mvaddstr(2, x, boundary) } | |
18.times do |y| | |
scr.mvaddstr(y + 3, 0, boundary) | |
scr.mvaddstr(y + 3, 19, boundary) | |
end | |
20.times { |x| scr.mvaddstr(21, x, boundary) } | |
end | |
def grow_flowers | |
flowers = Array.new(18) { [] } | |
5.times do | |
x = rand(18) | |
y = rand(18) | |
flowers[y][x] = true | |
scr.mvaddstr(y_playfield(y) + 1, x + 1, '🌷') | |
end | |
flowers | |
end | |
def y_playfield(y) | |
y + 2 | |
end | |
end | |
BouncingBug.new.call |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment