Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local w,h = term.getSize()
- local tick = os.startTimer(0.1)
- local bulletfire = os.startTimer(0.2)
- local btick = os.startTimer(0.001)
- local canfire = false
- local blocks = {
- ['1'] = {name='grass',graphic=" ",tcolor="lime",bcolor="green",solid=false},
- ['W'] = {name='wall',graphic=" ",tcolor="gray",bcolor="gray",solid=true},
- ['C'] = {name='coin',graphic="O",tcolor="yellow",bcolor="orange",solid=false},
- }
- local map = {}
- local enemies = {}
- local bullet = {}
- ----- make map -----
- for y=0,h do
- map[y] = string.rep('1',w)
- end
- function getBlock(x,y)
- return blocks[tostring(map[y]:sub(x,x))]
- end
- function drawBlock(x,y)
- local block = getBlock(x,y)
- term.setTextColor(colors[block.tcolor])
- term.setBackgroundColor(colors[block.bcolor])
- term.setCursorPos(x,y)
- term.write(block.graphic)
- end
- function drawMap()
- for y=1,h do
- for x=1,w do
- drawBlock(x,y)
- end
- end
- end
- function setBlock(x,y,bid)
- map[y] = table.concat{map[y]:sub(1,x-1), tostring(bid), map[y]:sub(x+1)}
- drawBlock(x,y)
- end
- local player = {
- x=5,
- y=5,
- hp=100,
- tcolor = "blue",
- score = 0,
- draw = function(self)
- term.setCursorPos(self.x,self.y)
- term.setTextColor(colors[self.tcolor])
- term.setBackgroundColor(colors[getBlock(self.x,self.y).bcolor])
- write("&")
- end,
- translate = function(self,ox,oy)
- if(not self:checkCollision(ox,oy))then
- drawBlock(self.x,self.y)
- self.x = self.x + ox
- self.y = self.y + oy
- self:draw()
- end
- end,
- checkCollision = function(self,ox,oy)
- if(getBlock(self.x+ox,self.y+oy).solid)then
- return true
- else
- for i = 1, #enemies do
- if(enemies[i] ~= nil and enemies[i].isAlive)then
- if( (self.x+ox == enemies[i].x and self.y == enemies[i].y) or
- (self.x == enemies[i].x and self.y+oy == enemies[i].y) or
- (self.x+ox == enemies[i].x and self.y+oy == enemies[i].y)
- )then
- return true
- end
- end
- end
- return false
- end
- end,
- update = function(self,ev)
- if(getBlock(self.x,self.y).name == "coin")then
- self.score = self.score + 15
- setBlock(self.x,self.y,'1')
- self:draw()
- end
- if(ev[1] == "key")then
- if(ev[2] == keys.w and self.y > 2)then
- self:translate(0,-1)
- end
- if(ev[2] == keys.s and self.y < h)then
- self:translate(0,1)
- end
- if(ev[2] == keys.d and self.x < w)then
- self:translate(1,0)
- end
- if(ev[2] == keys.a and self.x > 1)then
- self:translate(-1,0)
- end
- end
- end,
- }
- function addEnemy(x,y)
- local nenemy = {
- x = 0,
- y = 0,
- tcolor = "red",
- movechance = 2,
- hp = 100,
- dmg=5,
- isAlive = true,
- checkCollision = function(self,ox,oy)
- if(getBlock(self.x+ox,self.y+oy).solid)then
- return true
- elseif( (self.x+ox == player.x and self.y == player.y) or
- (self.x == player.x and self.y+oy == player.y) or
- (self.x+ox == player.x and self.y+oy == player.y)
- )then
- player.hp = player.hp - self.dmg
- return true
- else
- for i = 1, #enemies do
- if(i ~= self.id and enemies[i] ~= nil)then
- if( (self.x+ox == enemies[i].x and self.y == enemies[i].y) or
- (self.x == enemies[i].x and self.y+oy == enemies[i].y) or
- (self.x+ox == enemies[i].x and self.y+oy == enemies[i].y)
- )then
- return true
- end
- end
- end
- for i = 1, #bullet do
- if(bullet[i] ~= nil)then
- if( (self.x+ox == bullet[i].x and self.y == bullet[i].y) or
- (self.x == bullet[i].x and self.y+oy == bullet[i].y) or
- (self.x+ox == bullet[i].x and self.y+oy == bullet[i].y)
- )then
- self.hp = self.hp - bullet[i].dmg
- bullet[i]:destroy()
- return true
- end
- end
- end
- return false
- end
- end,
- destroy = function(self)
- self.isAlive = false
- drawBlock(self.x,self.y)
- return true
- end,
- draw = function(self)
- term.setCursorPos(self.x,self.y)
- term.setTextColor(colors[self.tcolor])
- term.setBackgroundColor(colors[getBlock(self.x,self.y).bcolor])
- write("&")
- end,
- translate = function(self,ox,oy)
- if( not self:checkCollision(ox,oy) )then
- drawBlock(self.x,self.y)
- self.x = self.x + ox
- self.y = self.y + oy
- self:draw()
- end
- end,
- update = function(self)
- if(self.isAlive)then
- -- move twords player --
- if(self.hp <= 0)then
- if(math.random(1,8) == 7)then
- setBlock(self.x,self.y,'C')
- end
- player.score = player.score + 25
- self:destroy()
- end
- local cmove = math.random(-self.movechance,self.movechance)
- if(cmove == self.movechance)then
- if(player.x > self.x)then self:translate(1,0) end
- if(player.x < self.x)then self:translate(-1,0) end
- if(player.y > self.y)then self:translate(0,1) end
- if(player.y < self.y)then self:translate(0,-1) end
- end
- end
- end,
- }
- nenemy.isAlive = true
- nenemy.x = x
- nenemy.y = y
- nenemy.id = #enemies+1
- enemies[#enemies+1] = nenemy
- nenemy:draw()
- end
- function updateEnemies()
- for i = 1, #enemies do
- if(enemies[i] ~= nil and enemies[i].isAlive)then
- enemies[i]:update()
- end
- end
- end
- function addBullet(x,y,dirx,diry)
- local nbullet = {
- x = 0,
- y = 0,
- dirx=0,
- diry=0,
- tcolor = "yellow",
- speed=1, -- 2 or more causes problems!
- dmg=25,
- isAlive=true,
- checkCollision = function(self,ox,oy)
- if(getBlock(self.x+ox,self.y+oy).solid)then
- self:destroy()
- return true
- else
- for i = 1, #enemies do
- if(enemies[i] ~= nil and enemies[i].isAlive)then
- if( (self.x+ox == enemies[i].x and self.y == enemies[i].y) or
- (self.x == enemies[i].x and self.y+oy == enemies[i].y) or
- (self.x+ox == enemies[i].x and self.y+oy == enemies[i].y)
- )then
- enemies[i].hp = enemies[i].hp - self.dmg
- self:destroy()
- return true
- end
- end
- end
- return false
- end
- end,
- draw = function(self)
- term.setCursorPos(self.x,self.y)
- term.setTextColor(colors[self.tcolor])
- term.setBackgroundColor(colors[getBlock(self.x,self.y).bcolor])
- write(".")
- end,
- destroy = function(self)
- drawBlock(self.x,self.y)
- self.isAlive = false
- drawBlock(self.x,self.y)
- return true
- end,
- translate = function(self,ox,oy)
- if( not self:checkCollision(ox,oy) )then
- drawBlock(self.x,self.y)
- self.x = self.x + ox
- self.y = self.y + oy
- self:draw()
- end
- end,
- update = function(self)
- if(self.isAlive)then
- if(self.x+(self.dirx*self.speed) <= 1)then self:destroy() return false end
- if(self.y+(self.diry*self.speed) <= 1)then self:destroy() return false end
- if(self.x+(self.dirx*self.speed) >= w)then self:destroy() return false end
- if(self.y+(self.diry*self.speed) >= h)then self:destroy() return false end
- self:translate(self.dirx*self.speed,self.diry*self.speed)
- end
- end,
- }
- nbullet.x = x
- nbullet.y = y
- nbullet.dirx = dirx
- nbullet.diry = diry
- nbullet.id = #bullet+1
- bullet[#bullet+1] = nbullet
- nbullet:draw()
- end
- function updateBullets()
- for i = 1, #bullet do
- if(bullet[i] ~= nil and bullet[i].isAlive)then
- bullet[i]:update()
- end
- end
- end
- setBlock(10,10,'C')
- addEnemy(10,11)
- addEnemy(10,15)
- addEnemy(10,17)
- addEnemy(10,18)
- addEnemy(12,18)
- addEnemy(11,15)
- drawMap()
- player:draw()
- while true do
- if(#bullet >= 50 and not canfire)then
- for i = 1, #bullet do
- if(bullet[i] ~= nil)then
- drawBlock(bullet[i].x,bullet[i].y)
- end
- end
- bullet = {}
- end
- local hasAlive = false
- for i = 1, #enemies do
- if(enemies[i].isAlive)then
- hasAlive = true
- else
- drawBlock(enemies[i].x,enemies[i].y)
- end
- end
- if(not hasAlive)then
- enemies = {}
- end
- local e = {os.pullEvent()}
- player:update(e)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.blue)
- term.setCursorPos(1,1)
- term.clearLine()
- term.write("Score: " .. player.score .. " HP: " .. player.hp)
- if(e[1] == "timer")then
- if(e[2] == tick)then
- tick = os.startTimer(0.1)
- updateEnemies()
- end
- if(e[2] == bulletfire)then
- bulletfire = os.startTimer(0.2)
- if(canfire==false)then canfire = true end
- updateBullets()
- end
- if(e[2] == btick)then
- btick = os.startTimer(0.001)
- updateBullets()
- end
- end
- if(e[1] == "key" and e[2] == keys.p)then addEnemy(math.random(3,50),math.random(3,15)) end
- if( (e[1] == "mouse_click" or e[1] == "mouse_drag") and e[2] == 2)then
- if(getBlock(e[3],e[4]).name == "wall")then
- setBlock(e[3],e[4],"1")
- else
- setBlock(e[3],e[4],"W")
- end
- end
- if( (e[1] == "mouse_click" and e[2] == 1) and canfire)then
- canfire=false
- local dx = 0
- local dy = 0
- if(e[3] < player.x)then
- dx = -1
- elseif(e[3] > player.x)then
- dx = 1
- elseif(e[3] == player.x)then
- dx=0
- end
- if(e[4] < player.y)then
- dy = -1
- elseif(e[4] > player.y)then
- dy = 1
- elseif(e[4] == player.y)then
- dy=0
- end
- addBullet(player.x+dx,player.y+dy,dx,dy)
- updateBullets()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement