Advertisement
nitrogenfingers

dualstick

Feb 2nd, 2013
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.84 KB | None | 0 0
  1. --The width and height of the screen
  2. local w,h = term.getSize()
  3. --Whether or not the game is running
  4. local running = true
  5. --The interval between each game update
  6. local refreshRate = 0.15
  7. --The game update timer
  8. local gtID = os.startTimer(refreshRate)
  9.  
  10. --Header for updateBullets function
  11. local updateBullets = nil
  12.  
  13. --[[ Our player:
  14.   x: his X position
  15.   y: his Y position
  16.   icon: the icon drawn at his position
  17.   rotation: the player facing
  18. ]]--
  19. local player = {
  20.   x = math.floor(w/2),
  21.   y = math.floor(h/2),
  22.   icon = "&",
  23.   rotation = 0.0
  24. }
  25.  
  26. --[[ The list of bullets:
  27.   x: the x position
  28.   y: the y position
  29.   xstep: the movement each update on x
  30.   ystep: the movement each update on y
  31.   type: the colour of the bullet
  32. ]]--
  33. local bullets = { }
  34. --The list of all possible bullet types
  35. local bullTypes = { colours.orange, colours.yellow, colours.lime }
  36. --The currently selected bullet type
  37. local selBull = 1
  38.  
  39. --Draws the player, bullets and header info
  40. local function display()
  41.   -- Player draw code
  42.   term.setBackgroundColour(colours.black)
  43.   term.clear()
  44.   term.setTextColour(colours.lime)
  45.   term.setCursorPos(player.x, player.y)
  46.   term.write(player.icon)
  47.  
  48.   -- Footer draw code
  49.   term.setCursorPos(1,h)
  50.   term.setBackgroundColour(bullTypes[selBull])
  51.   term.write(string.rep(" ", w))
  52.  
  53.   -- Bullet draw code
  54.   for _,v in pairs(bullets) do
  55.     term.setCursorPos(v.x, v.y)
  56.     term.setBackgroundColour(v.type)
  57.     term.write(" ")
  58.   end
  59.  
  60.   -- Reticle code
  61.  
  62.   term.setCursorPos(player.x + math.cos(player.rotation) * 8,
  63.       player.y - math.sin(player.rotation) * 6)
  64.   term.setBackgroundColour(colours.black)
  65.   term.setTextColour(colours.green)
  66.   term.write("+")
  67.  
  68. end
  69.  
  70. local function createBullet(x,y,xvel,yvel)
  71.   table.insert(bullets, {
  72.     x = x,
  73.     y = y,
  74.     xstep = xvel,
  75.     ystep = yvel,
  76.     type = bullTypes[selBull]
  77.   })
  78. end
  79.  
  80. --Accepts and acts upon input (and timers)
  81. local function handleInput()
  82.   local id, p1, p2, p3 = os.pullEvent()
  83.  
  84.   if id == "key" then
  85.    
  86.   elseif id == "mouse_click" or id == "mouse_drag" then
  87.  
  88.   elseif id == "mouse_scroll" then
  89.  
  90.   end
  91.  
  92.   if id == "timer" and p1 == gtID then
  93.     updateBullets()
  94.     gtID = os.startTimer(refreshRate)
  95.   end
  96. end
  97.  
  98. --Moves each bullet according to their velocity,
  99. --and removes those that have left the screen
  100. function updateBullets()
  101.   for _,v in pairs(bullets) do
  102.     v.x = v.x + v.xstep
  103.     v.y = v.y + v.ystep
  104.   end
  105.  
  106.   local blistlength = #bullets
  107.   for i=1,blistlength do
  108.     local v = bullets[i]
  109.     if v and (v.x < 1 or v.x > w or
  110.        v.y < 1 or v.y > h-1) then
  111.       table.remove(bullets, i)
  112.       blistlength = blistlength - 1
  113.       i = i - 1  
  114.     end
  115.   end
  116. end
  117.  
  118. --The main game loop
  119. local function mainLoop()
  120.   while running do
  121.     display()
  122.     handleInput()
  123.   end
  124. end
  125.  
  126. mainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement