Advertisement
Guest User

game cpc1.5

a guest
Jul 12th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.29 KB | None | 0 0
  1. term.clear()
  2. print [[GameAPI r5
  3. Press Backspace at any time to exit the game.]]
  4.  
  5. sleep(2)
  6. screen = {}
  7. screen.width = 40
  8. screen.height = 15
  9. screen.actors = {}
  10.  
  11. function round(v,int)
  12.         local int = (int~=nil) and int or 1
  13.         local n = v%int
  14.        
  15.         if n >= int/2 then
  16.                 return (v-n)+int
  17.         else
  18.                 return (v-n)
  19.         end
  20. end
  21.  
  22. for y=1,screen.height do
  23.         local t = {}
  24.         for x=1,screen.width do
  25.                 table.insert(t,' ')
  26.         end
  27.         table.insert(screen.actors,t)
  28. end
  29.  
  30. function draw()
  31.         term.clear()
  32.         for y,v in pairs(screen.actors) do
  33.                 local line = ''
  34.                 for x,vv in pairs(v) do
  35.                         line = line .. vv
  36.                 end
  37.                 print(line)
  38.         end
  39. end
  40.  
  41. function format(actor)
  42.         if actor.bounded then
  43.                 actor.x = (actor.x>1) and actor.x or 1
  44.                 actor.x = (actor.x<screen.width) and actor.x or screen.width
  45.                 actor.y = (actor.y>1) and actor.y or 1
  46.                 actor.y = (actor.y<screen.height) and actor.y or screen.height
  47.         end
  48.         screen.actors[round(actor.y)][round(actor.x)] = actor.char
  49.         return actor
  50. end
  51.  
  52. actor = {}
  53.  
  54. function actor:new(x,y,char,b)
  55.         actor.x = x
  56.         actor.y = y
  57.         actor.char = char
  58.         actor.bounded = (b~=nil) and b or true
  59.         screen.actors[round(y)][round(x)] = char
  60.         return format(actor)
  61. end
  62.  
  63. function actor:setPos(x,y)
  64.         screen.actors[actor.y][actor.x] = ' '
  65.         actor.x = x
  66.         actor.y = y
  67.         actor = format(actor)
  68. end
  69.  
  70. function actor:setX(x)
  71.         screen.actors[actor.y][actor.x] = ' '
  72.         actor.x = x
  73.         actor = format(actor)
  74. end
  75.  
  76. function actor:setY(y)
  77.         screen.actors[actor.y][actor.x] = ' '
  78.         actor.y = y
  79.         actor = format(actor)
  80. end
  81.  
  82. function actor:addPos(x,y)
  83.         screen.actors[actor.y][actor.x] = ' '
  84.         actor.x = actor.x + x
  85.         actor.y = actor.y + y
  86.         actor = format(actor)
  87. end
  88.  
  89. function actor:addX(x)
  90.         screen.actors[actor.y][actor.x] = ' '
  91.         actor.x = actor.x + x
  92.         actor = format(actor)
  93. end
  94.  
  95. function actor:addY(y)
  96.         screen.actors[actor.y][actor.x] = ' '
  97.         actor.y = actor.y + y
  98.         actor = format(actor)
  99. end
  100.  
  101. function actor:setChar(char)
  102.         screen.actors[actor.y][actor.x] = char
  103.         actor.char = char
  104. end
  105.  
  106. function actor:destroy()
  107.         screen.actors[actor.y][actor.x] = ' '
  108.         actor = nil
  109. end
  110.  
  111. game = {}
  112. game.state = 'running'
  113.  
  114. function game.quit()
  115.         term.clear()
  116.         game.state = 'dead'
  117. end
  118.  
  119. function game.start()
  120.         os.startTimer(1/30)
  121.         while game.state == 'running' do
  122.                 local t,k = os.pullEvent()
  123.                 if t == 'timer' then
  124.                         draw()
  125.                         if game.update ~= nil then game.update() end
  126.                         os.startTimer(1/30)
  127.                 elseif t == 'key' then
  128.                         if k == 14 then
  129.                                 game.quit()
  130.                         else
  131.                                 if game.key ~= nil then game.key(k) end
  132.                         end
  133.                 end
  134.         end
  135. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement