Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local com = require("component")
- local event = require("event")
- local gpu = com.gpu
- local w, h = gpu.getResolution()
- local term = require("term")
- local defaultb = tonumber(0x000000)
- local defaultf = tonumber(0x00FF00)
- local pmap = {}
- local map = {}
- local blocks = {
- [0] = {name="Air",bcolor=0x000000,color=0x000000,graphic=" "},
- {name="Stone",bcolor=0xCCCCFF,color=0x000000,graphic="@"},
- {name="Player",bcolor=0x0000FF,color=0x0000FF,graphic=" "},
- }
- function makeMap(wd,hd)
- for y=1, hd do
- map[y] = {}
- pmap[y] = {}
- for x=1, wd do
- map[y][x] = "0"
- pmap[y][x] = "0"
- end
- end
- end
- function drawMap()
- term.clear()
- term.setCursor(1,h)
- gpu.setForeground(defaultf)
- print("LOADING")
- for y = 1, #map do
- for x = 1, #map[y] do
- if(map[y][x] ~= "0")then
- term.setCursor(x,y)
- gpu.setForeground(blocks[tonumber(map[y][x])].color)
- gpu.setBackground(blocks[tonumber(map[y][x])].bcolor)
- term.write(blocks[tonumber(map[y][x])].graphic)
- gpu.setBackground(defaultb)
- gpu.setForeground(defaultf)
- end
- end
- end
- term.setCursor(1,h)
- print("COMPLETE")
- end
- function drawMapPixel(y,x)
- term.setCursor(x,y)
- gpu.setForeground(blocks[tonumber(map[y][x])].color)
- gpu.setBackground(blocks[tonumber(map[y][x])].bcolor)
- term.write(blocks[tonumber(map[y][x])].graphic)
- gpu.setBackground(defaultb)
- gpu.setForeground(defaultf)
- term.setCursor(x,y)
- if(pmap[y][x] ~= "0")then
- gpu.setForeground(blocks[tonumber(pmap[y][x])].color)
- gpu.setBackground(blocks[tonumber(pmap[y][x])].bcolor)
- term.write(blocks[tonumber(pmap[y][x])].graphic)
- gpu.setBackground(defaultb)
- gpu.setForeground(defaultf)
- end
- end
- print("Generating Map... \nMay Cause Lag")
- makeMap(w,h)
- drawMap()
- local player = {
- x = 10,
- y = 10,
- blockid = 2,
- checkcollision = function(self,x,y)
- isColl = false
- if(map[y][x] ~= "0")then
- isColl = true
- end
- return isColl
- end,
- move = function(self,dir,ammount)
- if(dir == "up")then
- if(not self:checkcollision(self.x,self.y-1))then
- map[self.y][self.x] = "0"
- drawMapPixel(self.y,self.x)
- self.y = self.y - 1
- map[self.y][self.x] = self.blockid
- drawMapPixel(self.y,self.x)
- end
- elseif(dir == "down")then
- if(not self:checkcollision(self.x,self.y+1))then
- map[self.y][self.x] = "0"
- drawMapPixel(self.y,self.x)
- self.y = self.y + 1
- map[self.y][self.x] = self.blockid
- drawMapPixel(self.y,self.x)
- end
- elseif(dir == "left")then
- if(not self:checkcollision(self.x-1,self.y))then
- map[self.y][self.x] = "0"
- drawMapPixel(self.y,self.x)
- self.x = self.x - 1
- map[self.y][self.x] = self.blockid
- drawMapPixel(self.y,self.x)
- end
- elseif(dir == "right")then
- if(not self:checkcollision(self.x+1,self.y))then
- map[self.y][self.x] = "0"
- drawMapPixel(self.y,self.x)
- self.x = self.x + 1
- map[self.y][self.x] = self.blockid
- drawMapPixel(self.y,self.x)
- end
- end
- end,
- update = function(self, ev)
- if(ev[1] == "key_down")then
- if(ev[4] == 17)then
- self:move("up",1)
- end
- if(ev[4] == 31)then
- self:move("down",1)
- end
- if(ev[4] == 30)then
- self:move("left",1)
- end
- if(ev[4] == 32)then
- self:move("right",1)
- end
- end
- end,
- }
- function drawOn()
- a = {event.pull()}
- player:update(a)
- if(a[1] == "touch" or a[1] == "drag")then
- if(a[5] == 0)then
- term.setCursor(1,11)
- --print(map[a[4]][a[3]])
- map[a[4]][a[3]] = "1"
- opy = tonumber(a[4])
- opx = tonumber(a[3])
- drawMapPixel(opy,opx)
- end
- if(a[5] == 1)then
- return
- end
- end
- drawOn()
- end
- drawOn()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement