Sha256: b282a888b2d8d25d513dac8716abb328d86a35fb2fb7c3b51cd10f1e2faaa3e0

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

#!/usr/bin/env ruby

require 'libtcod'

#actual size of the window
SCREEN_WIDTH = 80
SCREEN_HEIGHT = 50
 
LIMIT_FPS = 20  #20 frames-per-second maximum
 
def handle_keys
  #key = TCOD.console_check_for_keypress()  #real-time
  key = TCOD.console_wait_for_keypress(true)  #turn-based

  if key.vk == TCOD::KEY_ENTER && key.lalt
    #Alt+Enter: toggle fullscreen
    TCOD.console_set_fullscreen(!TCOD.console_is_fullscreen())
  elsif key.vk == TCOD::KEY_ESCAPE
    return true  #exit game
  end

  #movement keys
  if TCOD.console_is_key_pressed(TCOD::KEY_UP)
      $playery -= 1
  elsif TCOD.console_is_key_pressed(TCOD::KEY_DOWN)
      $playery += 1
  elsif TCOD.console_is_key_pressed(TCOD::KEY_LEFT)
      $playerx -= 1
  elsif TCOD.console_is_key_pressed(TCOD::KEY_RIGHT)
      $playerx += 1
  end

  false
end
 
#############################################
# Initialization & Main Loop
#############################################
 
TCOD.console_set_custom_font('arial10x10.png', TCOD::FONT_TYPE_GREYSCALE | TCOD::FONT_LAYOUT_TCOD, 0, 0)
TCOD.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'ruby/TCOD tutorial', false, TCOD::RENDERER_SDL)
TCOD.sys_set_fps(LIMIT_FPS)
 
$playerx = SCREEN_WIDTH/2
$playery = SCREEN_HEIGHT/2
 
until TCOD.console_is_window_closed
  TCOD.console_set_default_foreground(nil, TCOD::Color::WHITE)
  TCOD.console_put_char(nil, $playerx, $playery, '@'.ord, TCOD::BKGND_NONE)

  TCOD.console_flush()

  TCOD.console_put_char(nil, $playerx, $playery, ' '.ord, TCOD::BKGND_NONE)

  #handle keys and exit game if needed
  will_exit = handle_keys
  break if will_exit
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
libtcod-0.0.3 examples/python_tutorial_part_1/main.rb