Advertisement
qwertyMAN_rus

Human game

Mar 17th, 2017 (edited)
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.99 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local char = require("unicode").char
  4. local clear = require("term").clear
  5. local gpu = component.gpu
  6. local exit = false
  7. local player
  8. local old = {
  9.     size = {gpu.getResolution()},
  10.     Foreground = gpu.getForeground(),
  11.     Background = gpu.getBackground()
  12. }
  13. local new = {
  14.     --size = {gpu.maxResolution()},
  15.     size = {80, 25},
  16.     Foreground = 0x00ff00,
  17.     Background = 0x000000
  18. }
  19. local listimg = {
  20.     man = "ec23"
  21. }
  22.  
  23. for k,v in pairs(listimg) do
  24.     listimg[k] = char("0x" .. v)
  25. end
  26.  
  27. local keyboard = {
  28.     [200] = { 0,-1},
  29.     [203] = {-1, 0},
  30.     [205] = { 1, 0},
  31.     [208] = { 0, 1},
  32.     [ 16] = function() exit = true end
  33. }
  34.  
  35. local function interval(min, n, max)
  36.     return math.max(math.min(max, n), min)
  37. end
  38.  
  39. local function printimg(x, y, color)
  40.     gpu.getForeground(color)
  41.     gpu.set(x,y,listimg["man"])
  42. end
  43.  
  44. local function preload()
  45.     player = {
  46.         x = math.modf(new.size[1]/2),
  47.         y = math.modf(new.size[2]/2),
  48.         color = 0x00ff00
  49.     }
  50. end
  51.  
  52. local function prerendering()
  53.     --empty map
  54.     printimg(player.x, player.y, player.color)
  55. end
  56.  
  57. local function control(input)
  58.     local input = keyboard[input]
  59.     if input then
  60.         if type(input) == "table" then
  61.             local x, y = input[1], input[2]
  62.             player.x = interval(1, player.x + x, new.size[1])
  63.             player.y = interval(1, player.y + y, new.size[2])
  64.         else
  65.             input()
  66.         end
  67.     end
  68. end
  69.  
  70. local function rendering()
  71.     --empty map
  72.     clear()
  73.     printimg(player.x, player.y, player.color)
  74. end
  75.  
  76. local function loop()
  77.     local key = {event.pull("key_down")}
  78.     control(key[4])
  79.     rendering()
  80. end
  81.  
  82. local function game()
  83.     preload()
  84.     prerendering()
  85.     while not exit do
  86.         loop()
  87.         os.sleep(0)
  88.     end
  89. end
  90.  
  91. local function start()
  92.     gpu.setBackground(new.Background)
  93.     gpu.setForeground(new.Foreground)
  94.     clear()
  95.     gpu.setResolution(new.size[1], new.size[2])
  96.    
  97.     game()
  98.    
  99.     gpu.setBackground(old.Background)
  100.     clear()
  101.     gpu.setForeground(old.Foreground)
  102.     gpu.setResolution(old.size[1], old.size[2])
  103. end
  104.  
  105. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement