love2d 代码库(四)血条跟随和怪物死亡

引言

这次的代码是要做到血条跟着怪物的移动而移动,且血量为0时,怪兽死亡并消失。

效果如下:

在这里插入图片描述
与之前有不同的是此处有一个碰撞检测函数,这块代码内容我在代码库(二)碰撞检测里有写。

代码如下:

<main.lua>


direc = 0.1
-- 碰撞检测
function colide_check(block1,block2)

    block1.right = block1.x + block1.width
    block1.left = block1.x
    block1.top = block1.y
    block1.bottom = block1.y + block1.height


    block2.right = block2.x + block2.width
    block2.left = block2.x
    block2.top = block2.y
    block2.bottom = block2.y + block2.height
    --碰撞判断,函数返回true or false
    if block1.right>block2.left and block1.bottom>block2.top and  block1.top<block2.bottom and block1.left<block2.right then
        return true
    else
        return false
    end   
 end
function love.load()
    Object= require "classic"
    require "Car"
    require "Bullet"
    require "Blood"
    require "Eenemy"
    -- 新建子类car
    car = Car(80,478.8,500)
    -- 新建子类spider
    spider = Enemey(600, 450)
    -- 新建子类blood
    blood = Blood(600,420)
    --用来储存子弹的列表
    bulletList = {}
end


function love.keypressed(key)
	--按下空格键就发射子弹
    if key == "space" then
        car:shoot(key)
    end
    
end 
function love.update(dt)
    car:update(dt)
    spider:update(dt)
    --遍历子弹列表,计算每颗子弹的参数
    for k,v in ipairs(bulletList) do 
        v:update(dt)
    end
    
end


function love.draw()
    
    car:draw()
    spider:draw()
    --打印汽车的坐标
    love.graphics.print("car.x="..car.x.."car.y="..car.y)
    --遍历子弹列表,绘制每颗子弹
    for k, v in ipairs(bulletList)
    do
        v:draw()
    end
end

<Bullet.lua>

--259*146
Bullet = Object:extend()
function Bullet:new(x,y,speed)
    self.x=x
    self.y=y
    self.width=259/5
    self.height = 146/5
    self.speed = speed 
    self.image=love.graphics.newImage("Bullet.png")
    self.angle=0
    
end

function Bullet:draw()
    love.graphics.draw(self.image, self.x,self.y,self.angle,0.2,0.2)
end

function Bullet:update(dt)
    if self.speed>0 then
        self.angle = math.rad(0)
    else
        self.angle = math.rad(180)
    end
    self.x = self.x + self.speed * dt
end

<Car.lua>

--1952*1212
Car = Object:extend()
function Car:new(x,y,speed)
    self.x=x
    self.y=y
    self.width =1952/10
    self.height = 1212/10
    self.speed=speed
    self.image=love.graphics.newImage("car1.png")
    self.direc=0.1
    self.bulletSpeed=0
    self.bullet_x=0
    self.bullet_y=0
end

function Car:draw()
    love.graphics.draw(self.image, self.x,self.y,0,direc,0.1,700,0)
    
end
function Car:update(dt)
    if love.keyboard.isDown("right") then
        self.x = self.x + self.speed * dt
        direc=-0.1
        self.bulletSpeed=700
        self.bullet_x = self.x+80
        self.bullet_y = self.y +20
    elseif love.keyboard.isDown("left") then
        direc = 0.1
        self.x = self.x - self.speed * dt
        self.bulletSpeed = -700
        self.bullet_x = self.x - 80
        self.bullet_y = self.y + 50
    end
    
end
function Car:shoot()
    table.insert(bulletList, Bullet(self.bullet_x, self.bullet_y,self.bulletSpeed))
end

<Blood.lua>

Blood = Object:extend()
function Blood:new(x,y)
    self.x=x
    self.y=y
    self.maxhealth = 100
    self.curhealth = 100
end
function Blood:update(x,y,max,cur)
    self.x=x
    self.y=y
    self.maxhealth = max
    self.curhealth = cur
    
end
function Blood:draw()
    love.graphics.rectangle("line", self.x, self.y-30, self.maxhealth, 20)
    love.graphics.setColor(255, 0, 0)
    love.graphics.rectangle("fill", self.x+1, self.y-29, self.curhealth-1, 18)
    love.graphics.setColor(1, 1, 1)
    
end


<Eenemy.lua>

--1917*1508
Enemey = Object:extend()
function Enemey:new(x,y)
    self.x =x
    self.y = y
    self.width=1917/10
    self.height=1508/10
    self.speed = 100
    self.maxhealth =100
    self.curhealth = 100
    self.image = love.graphics.newImage("spider.png")
    self.alive=true
end

function Enemey:update(dt)
    self.x = self.x-self.speed*dt
    for k, v in ipairs(bulletList) do
        if colide_check(v, self) then
            
            self.curhealth = self.curhealth - 10
            if self.curhealth < 0 then
                self.curhealth = 0
                self.alive = false
            end
            table.remove(bulletList,k)
        end
    end
    
    blood:update(self.x,self.y,self.maxhealth,self.curhealth)
end

function Enemey:draw()
    if  self.alive then
        love.graphics.draw(self.image,self.x,self.y,0,0.1,0.1)
        blood:draw()
    end
end

总结

  1. 如果用列表来储存每个怪物的信息会使代码逻辑更加清晰,功能会加简单
  2. 血量没必要单独成一个文件,直接写进Enemy的属性和方法就行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值