Difference between revisions of "Love - Bomberman Concept"
Line 2: | Line 2: | ||
The image resources for this tutorial are these: | The image resources for this tutorial are these: | ||
− | [[File:Bomb.png| | + | [[File:Bomb.png|frame]] |
− | [[File:Brick.png| | + | [[File:Brick.png|frame]] |
− | [[File:Explosion.png| | + | [[File:Explosion.png|frame]] |
− | [[File:Ground.png| | + | [[File:Ground.png|frame]] |
− | [[File:Wall.png| | + | [[File:Wall.png|frame]] |
− | [[File:Bomber.png| | + | [[File:Bomber.png|frame]] |
=Variables= | =Variables= |
Revision as of 09:10, 11 July 2019
This will create a Bomberman esque game, in which the player is blocked by bricks and places bombs to remove them. I have tried to make this example very object oriented with classes for Player, Map, and Tile. I have also tried to make most of the code within these classes.
The image resources for this tutorial are these:
Contents
Variables
local size = 9
local space = 5
local width = size * 45 + space
local height = size * 45 + space
local range = 2
Tile Class
function Tile(tiletype)
local self = {
tiletype=tiletype,
timer=0
}
function self.getimage(tiletype)
return love.graphics.newImage(tiletype..".png")
end
function self.setimage()
self.image=self.getimage(self.tiletype)
end
self.image=self.getimage(tiletype)
return self
end
Map Class
function Map()
local self = {}
self.map = {} -- create the matrix
for i=0,size do
self.map[i] = {} -- create a new row
for j=0,size do
if i%2==1 and j%2==1 then
self.map[i][j] = Tile("wall")
else
self.map[i][j] = Tile("ground")
end
end
end
self.map[3][2] = Tile("brick")
self.map[4][7] = Tile("brick")
function self.draw()
for i=0,size do
for j=0,size do
love.graphics.draw(self.map[i][j].image,i*45+space,j*45+space)
end
end
end
end
return self
end
Map Update Method
function self.update(dt)
for i=0,size do
for j=0,size do
tile = self.map[i][j]
if tile.timer > 0 then
tile.timer=tile.timer-dt
else
if tile.tiletype=='explosion' then
tile.tiletype='ground'
tile.setimage()
end
if tile.tiletype=='bomb' then
tile.tiletype='explosion'
tile.setimage()
tile.timer=.5
for a=0, 3 do
if a == 0 then
angle=0
elseif a== 1 then
angle = 360
elseif a== 2 then
angle=90
elseif a== 2 then
angle=180
end
cosa = math.floor(math.cos(math.rad(angle)))
sina = math.floor(math.sin(math.rad(angle)))
for r = 1, range do
xoffset = r * cosa
yoffset = r * sina
if i+xoffset >=0 and i+xoffset <=size
and j+yoffset >=0 and j+yoffset <=size then
if self.map[i+xoffset][j+yoffset].tiletype ~= "wall" then
self.map[i+xoffset][j+yoffset].tiletype='explosion'
self.map[i+xoffset][j+yoffset].setimage()
self.map[i+xoffset][j+yoffset].timer=.5
else
break
end
end
if i-xoffset >=0 and i-xoffset <=size
and j-yoffset >=0 and j-yoffset <=size then
if self.map[i-xoffset][j-yoffset].tiletype ~= "wall" then
self.map[i-xoffset][j-yoffset].tiletype='explosion'
self.map[i-xoffset][j-yoffset].setimage()
self.map[i-xoffset][j-yoffset].timer=.5
else
break
end
end
end
end
end
end
end
end
Player Class
function Player(startx, starty)
local self = {
x=startx,
y=starty,
timer=0,
interval =.3,
lives = 3,
deadtimer = 0
}
self.image = love.graphics.newImage("bomber.png")
function self.draw()
love.graphics.draw(self.image,self.x*45+space,self.y*45+space)
end
return self
end
Player Update Method
function self.update(dt, gamemap)
local x = self.x
local y = self.y
if self.deadtimer > 0 then
self.deadtimer = self.deadtimer - dt
end
if self.timer<=0 then
if love.keyboard.isDown('a') then
self.x = self.x - 1
self.timer=self.interval
elseif love.keyboard.isDown('d') then
self.x = self.x + 1
self.timer=self.interval
elseif love.keyboard.isDown('w') then
self.y = self.y - 1
self.timer=self.interval
elseif love.keyboard.isDown('s') then
self.y = self.y + 1
self.timer=self.interval
end
else
self.timer = self.timer - dt
end
if self.x >= 0 and self.x <= size-1
and self.y >= 0 and self.y <= size-1 then
if gamemap.map[self.x][self.y].tiletype~= "ground" then
self.x=x
self.y=y
end
else
self.x=x
self.y=y
end
if love.keyboard.isDown('b') and gamemap.map[self.x][self.y].tiletype == "ground" then
gamemap.map[self.x][self.y].tiletype= "bomb"
gamemap.map[self.x][self.y].setimage()
gamemap.map[self.x][self.y].timer=3
end
if gamemap.map[self.x][self.y].tiletype== "explosion" and self.deadtimer <= 0 then
self.x=0
self.y=0
self.lives = self.lives-1
self.deadtimer=1
print ("lives "..self.lives)
end
end
LOVE Load, Update, Draw
function love.load()
love.window.setMode(width, height)
gamemap = Map()
player = Player(0,0)
end
function love.update(dt)
player.update(dt, gamemap)
gamemap.update(dt)
end
function love.draw()
gamemap.draw()
player.draw()
end