Advertisement
Redxone

gametest For OpenComputers

Jul 31st, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.70 KB | None | 0 0
  1. local com = require("component")
  2. local event = require("event")
  3. local gpu = com.gpu
  4. local w, h = gpu.getResolution()
  5. local term = require("term")
  6. local defaultb = tonumber(0x000000)
  7. local defaultf = tonumber(0x00FF00)
  8.  
  9. local pmap = {}
  10. local map = {}
  11.  
  12. local blocks = {
  13.   [0] = {name="Air",bcolor=0x000000,color=0x000000,graphic=" "},
  14.   {name="Stone",bcolor=0xCCCCFF,color=0x000000,graphic="@"},
  15.   {name="Player",bcolor=0x0000FF,color=0x0000FF,graphic=" "},  
  16. }
  17.  
  18.  
  19.  
  20. function makeMap(wd,hd)
  21.       for y=1, hd do
  22.         map[y] = {}
  23.         pmap[y] = {}
  24.           for x=1, wd do
  25.              map[y][x] = "0"
  26.              pmap[y][x] = "0"
  27.           end  
  28.       end
  29. end
  30.  
  31. function drawMap()
  32.     term.clear()
  33.     term.setCursor(1,h)
  34.     gpu.setForeground(defaultf)
  35.     print("LOADING")
  36.      for y = 1, #map do
  37.         for x = 1, #map[y] do
  38.                if(map[y][x] ~= "0")then
  39.                  term.setCursor(x,y)
  40.                  gpu.setForeground(blocks[tonumber(map[y][x])].color)
  41.                  gpu.setBackground(blocks[tonumber(map[y][x])].bcolor)
  42.                  term.write(blocks[tonumber(map[y][x])].graphic)
  43.                  gpu.setBackground(defaultb)
  44.                  gpu.setForeground(defaultf)
  45.               end
  46.         end
  47.      end
  48.  
  49.    term.setCursor(1,h)
  50.    print("COMPLETE")
  51. end
  52.  
  53. function drawMapPixel(y,x)
  54.       term.setCursor(x,y)
  55.       gpu.setForeground(blocks[tonumber(map[y][x])].color)
  56.       gpu.setBackground(blocks[tonumber(map[y][x])].bcolor)
  57.       term.write(blocks[tonumber(map[y][x])].graphic)
  58.       gpu.setBackground(defaultb)
  59.       gpu.setForeground(defaultf)
  60.       term.setCursor(x,y)
  61.       if(pmap[y][x] ~= "0")then
  62.           gpu.setForeground(blocks[tonumber(pmap[y][x])].color)
  63.           gpu.setBackground(blocks[tonumber(pmap[y][x])].bcolor)
  64.           term.write(blocks[tonumber(pmap[y][x])].graphic)
  65.           gpu.setBackground(defaultb)
  66.           gpu.setForeground(defaultf)
  67.       end
  68. end
  69.  
  70. print("Generating Map... \nMay Cause Lag")
  71. makeMap(w,h)
  72. drawMap()
  73.  
  74. local player = {
  75.  
  76.   x = 10,
  77.   y = 10,  
  78.   blockid = 2,
  79.    
  80.       checkcollision = function(self,x,y)
  81.            isColl = false
  82.            if(map[y][x] ~= "0")then
  83.               isColl = true
  84.            end
  85.            return isColl
  86.       end,
  87.        
  88.       move = function(self,dir,ammount)
  89.           if(dir == "up")then
  90.               if(not self:checkcollision(self.x,self.y-1))then
  91.                   map[self.y][self.x] = "0"
  92.                   drawMapPixel(self.y,self.x)
  93.                   self.y = self.y - 1
  94.                   map[self.y][self.x] = self.blockid
  95.                   drawMapPixel(self.y,self.x)
  96.               end
  97.           elseif(dir == "down")then
  98.               if(not self:checkcollision(self.x,self.y+1))then
  99.                   map[self.y][self.x] = "0"
  100.                   drawMapPixel(self.y,self.x)
  101.                   self.y = self.y + 1
  102.                   map[self.y][self.x] = self.blockid
  103.                   drawMapPixel(self.y,self.x)
  104.               end
  105.           elseif(dir == "left")then
  106.               if(not self:checkcollision(self.x-1,self.y))then
  107.                   map[self.y][self.x] = "0"
  108.                   drawMapPixel(self.y,self.x)
  109.                   self.x = self.x - 1
  110.                   map[self.y][self.x] = self.blockid
  111.                   drawMapPixel(self.y,self.x)
  112.               end
  113.           elseif(dir == "right")then
  114.               if(not self:checkcollision(self.x+1,self.y))then
  115.                   map[self.y][self.x] = "0"
  116.                   drawMapPixel(self.y,self.x)
  117.                   self.x = self.x + 1
  118.                   map[self.y][self.x] = self.blockid
  119.                   drawMapPixel(self.y,self.x)
  120.               end
  121.           end
  122.       end,
  123.  
  124.  
  125.       update = function(self, ev)
  126.             if(ev[1] == "key_down")then
  127.                 if(ev[4] == 17)then
  128.                     self:move("up",1)
  129.                 end
  130.                 if(ev[4] == 31)then
  131.                     self:move("down",1)
  132.                 end
  133.                 if(ev[4] == 30)then
  134.                     self:move("left",1)
  135.                 end
  136.                 if(ev[4] == 32)then
  137.                     self:move("right",1)
  138.                 end
  139.             end
  140.       end,
  141.  
  142. }
  143.  
  144.  
  145.  
  146. function drawOn()
  147.    a = {event.pull()}
  148.    
  149.   player:update(a)
  150.    if(a[1] == "touch" or  a[1] == "drag")then
  151.       if(a[5] == 0)then
  152.                   term.setCursor(1,11)
  153.                   --print(map[a[4]][a[3]])
  154.                   map[a[4]][a[3]] = "1"
  155.                   opy = tonumber(a[4])
  156.                   opx = tonumber(a[3])
  157.                   drawMapPixel(opy,opx)
  158.       end
  159.       if(a[5] == 1)then
  160.        return
  161.       end
  162.    end
  163.  
  164.   drawOn()
  165. end
  166.  
  167. drawOn()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement