如何在LOVE2D中制作死亡动画?

如何在LOVE2D中制作死亡动画?,love2d,Love2d,在我的游戏中,我已经为玩家和敌人创建了碰撞(碰撞并不完美,但在大多数情况下都有效),但我很难想出如何为玩家创建死亡动画 我希望玩家的飞船在敌人击中你时爆炸,我该怎么办 这是我的播放器文件: Player = Class{} function Player:init(x, y, width, height) self.player = {} self.x = x self.y = y self.height = height self.dx = 0

在我的游戏中,我已经为玩家和敌人创建了碰撞(碰撞并不完美,但在大多数情况下都有效),但我很难想出如何为玩家创建死亡动画

我希望玩家的飞船在敌人击中你时爆炸,我该怎么办

这是我的播放器文件:

Player = Class{}


function Player:init(x, y, width, height)
    self.player = {}
    self.x = x
    self.y = y
    self.height = height
    self.dx = 0
    self.image = love.graphics.newImage('images/player.png')
    self.width = self.image:getWidth()
    self.fire_sound = love.audio.newSource('sounds/laser.wav', 'static')
    self.fire_sound:setVolume(.25)
    self.player_explosion = love.audio.newSource('sounds/player_explosion.wav', 'static')
    self.player_explosion:setVolume(25)
    self.cooldown = 10
    self.bullets = {}
end

function Player:update(dt)
    self.x = self.x + self.dx * dt
    if self.x <= 0 then
        self.dx = 0
        self.x = 0
    end
    if self.x >= WINDOW_WIDTH - self.width * 4 then
        self.dx = 0
        self.x = WINDOW_WIDTH - self.width * 4
    end 
end

function Player:fire()
    if self.cooldown <= 0 then
        love.audio.play(player.fire_sound)
        if BULLET_COUNTER >= 1 then
            love.audio.stop(player.fire_sound)
            love.audio.play(player.fire_sound)
            self.cooldown = 30
            bullet = {}
            bullet.x = player.x + 25
            bullet.y = player.y + 5
            table.insert(self.bullets, bullet)
            BULLET_COUNTER = 0
            return
        end
        self.cooldown = 10
        bullet = {}
        bullet.x = self.x + 25
        bullet.y = self.y + 5
        table.insert(self.bullets, bullet)
        BULLET_COUNTER = BULLET_COUNTER + 1
    end
end

function Player:render()
    love.graphics.setColor(255, 255, 255)
    love.graphics.draw(self.image, self.x, self.y, 0, 4)
end

function Player:checkCollision(enemies)
    for i,e in ipairs(enemies) do
        if e.y + e.height >= self.y and e.y <= self.y + self.height and e.x <= self.x + self.width and e.x + e.width >= self.x + self.width then
            love.audio.stop(self.player_explosion)
            love.audio.play(self.player_explosion)
            table.remove(enemies, i)
            LIVES = LIVES - 1
        end
    end
end

Player=Class{}
函数播放器:初始化(x,y,宽度,高度)
self.player={}
self.x=x
self.y=y
自我高度=高度
self.dx=0
self.image=love.graphics.newImage('images/player.png'))
self.width=self.image:getWidth()
self.fire\u sound=love.audio.newSource('sounds/laser.wav','static'))
自燃声:设定音量(.25)
self.player\u explosion=love.audio.newSource('sounds/player\u explosion.wav','static')
self.player_爆炸:设置音量(25)
自我冷却时间=10
self.bullets={}
结束
功能播放器:更新(dt)
self.x=self.x+self.dx*dt
如果self.x=窗宽-self.WIDTH*4,则
self.dx=0
self.x=窗宽-self.WIDTH*4
结束
结束
函数播放器:fire()
如果self.cooldown=1,则
爱。音频。停止(播放机。火警声)
爱。音频。播放(播放器。开火\u声音)
自我冷却时间=30
项目符号={}
bullet.x=player.x+25
bullet.y=player.y+5
表.插入(自选项目符号、项目符号)
子弹头计数器=0
返回
结束
自我冷却时间=10
项目符号={}
bullet.x=self.x+25
bullet.y=self.y+5
表.插入(自选项目符号、项目符号)
子弹头计数器=子弹头计数器+1
结束
结束
函数播放器:render()
love.graphics.setColor(255、255、255)
爱。图形。绘制(self.image,self.x,self.y,0,4)
结束
功能玩家:检查碰撞(敌人)
因为i,e在ipairs(敌人)做什么
如果e.y+e.height>=self.y和e.y 0不产生敌人,但如果是,则产生敌人
如果生成计时器>0,则
spawnTimer=spawnTimer-dt
其他的
如果游戏状态==‘玩’,则
敌人\控制器:繁殖敌人(love.math.random(0,窗口宽度-小窗口宽度),0,love.math.random(0,2))
产卵计时器=产卵计时器\u最大值
结束
结束
--与敌人的产卵计时器的情况相同,但这是用于扭曲效果
如果starSpawnTimer>0,则
starSpawnTimer=starSpawnTimer-dt
其他的
stars\u控制器:spawnStars(love.math.random(0,窗口宽度-小窗口宽度),0)
starSpawnTimer=starSpawnTimer\u Max
结束
--检查用户输入以向左和向右移动
如果love.keyboard.isDown('right'),那么
如果游戏状态==‘玩’,则
player.dx=播放器速度
结束
如果爱。键盘。我就下(‘左’)了
如果游戏状态==‘玩’,则
player.dx=-player\u速度
结束
其他的
player.dx=0
结束
--检查点火功能的用户输入
如果love.keyboard.isDown('space'),那么
如果游戏状态==‘玩’,则
玩家:火()
结束
结束
--如果敌人离屏幕太远,这会移除他们
--还有敌人是如何向下移动的
--需要为不同的敌人添加移动功能
因为i,e在ipair(敌人\控制者。敌人)中
e、 y=e.y+e.speed
如果e.y>窗高,则
表.移除(敌人(i)
结束
结束
--如果子弹离屏幕太远,则移除子弹
--这也会使子弹向上移动
对于i,v,在ipairs(player.子弹)中
如果v.y<0,则
表.移除(player.bullets,i)
结束
v、 y=v.y-8
结束
--如果星星离屏幕太远,则移除它们
--移动星星以创建扭曲效果
对于ipairs(stars\u controller.stars)中的i,s
s、 y=s.y+s.speed
如果s.y>窗高,则
表.移除(stars\u控制器stars,i)
结束
结束
--检查生命以确定游戏状态
如果LIVES==0,则
游戏状态=‘结束’
结束
玩家:更新(dt)
结束
--按键功能,我们可以退出游戏并开始游戏
功能爱。按键按下(键)
如果键==“escape”,则
love.event.quit()
elseif key=='enter'或key=='return'然后
如果游戏状态==“开始”,则
游戏状态=‘玩’
结束
结束
结束
函数love.draw()
--检查游戏状态,如果是
如果游戏状态==“开始”,则
--绘制扭曲效果
对于_,成对(stars _controller.stars)执行
局部scaleX,scaleY=getImageScaleForNewDimensions(s.image,星形宽度,星形高度)
爱。图形。画图(s.image,s.x,s.y,0,2,4)
结束
--吸引玩家
玩家:render()
love.graphics.setColor(200,0,200,255)
love.graphics.setFont(title\u largeFont)
love.graphics.print(‘欢迎太空攻击’),窗口宽度/2-290,窗口高度/2)
love.graphics.setFont(title\u smallFont)
love.graphics.print('按ENTER键开始!',窗口宽度/2-110,窗口高度/2+100)
结束
如果游戏状态==‘玩’,则
--画地图
love.graphics.setColor(255、255、255)
本地地图宽度,地图高度=GetImageScaleForneWdDimensions(map.image,map.width,map.height)
love.graphics.draw(map.image,0,0,0,map\u宽度,map\u高度)
--牵制敌人
对于e,成对(敌人u控制器.敌人)执行
局部scaleX,scaleY=getImageScaleForNewDimensions(例如图像、小宽度、小高度)
爱。图形。画图(e.image,e.x,e.y,0,scaleX,scaleY)
结束
--绘制扭曲效果
对于_,成对(stars _controller.stars)执行
爱。图形。画图(s.image,s.x,s.y,0,1,4)
结束
--吸引玩家
玩家:render()
--拔出子弹
爱。图形
Class = require 'class'

require 'Player'

GAME_STATE = 'start'

WINDOW_WIDTH = 720
WINDOW_HEIGHT = 900
love.window.setMode(WINDOW_WIDTH, WINDOW_HEIGHT)

SCORE = 0

LIVES = 3

PLAYER_SPEED = 300

-- map variables
map = {}
map.image = love.graphics.newImage('images/background_final.png')
map.width = 720
map.height = 900
map.music = love.audio.newSource('sounds/Battle-in-the-Stars.wav', 'static')

-- enemy variables
enemy = {}
enemies_controller = {}
enemies_controller.enemies = {}
alienImage = love.graphics.newImage('images/alien2.png')
alienship1_Image = love.graphics.newImage('images/enemyship1.png')
alienship2_Image = love.graphics.newImage('images/enemyship4.png')
enemies_controller.explosion = love.audio.newSource('sounds/explosion.wav', 'static')
enemies_controller.explosion:setVolume(.25)
spawnTimer = 0
spawnTimer_Max = 0.75
SMALL_WIDTH = 70
SMALL_HEIGHT = 70

-- star variables for warp effect
star = {}
stars_controller = {}
stars_controller.stars = {}
starSpawnTimer = 0
starSpawnTimer_Max = 0.25
STAR_WIDTH = 100
STAR_HEIGHT = 100

BULLET_COUNTER = 0

-- this function allows us to scale an image we created to a desired height and width
function getImageScaleForNewDimensions(image, newWidth, newHeight)
    local currentWidth, currentHeight = image:getDimensions()
    return ( newWidth / currentWidth ), ( newHeight / currentHeight )
end

-- check collisions for enemies and bullets
function checkCollisions(enemies, bullets)
    for i,e in ipairs(enemies) do
        for j,b in ipairs(bullets) do
            -- checks if the hitboxes for the bullet and the enemy collide
            -- if they do remove the enemy and the bullet that collided
            if b.y <= e.y + e.height and b.x > e.x and b.x < e.x + e.width then
                love.audio.stop(enemies_controller.explosion)
                table.remove(enemies, i)
                table.remove(bullets, j)
                love.audio.play(enemies_controller.explosion)
                SCORE = SCORE + 10
            end
        end
    end
end


function love.load()
    love.graphics.setDefaultFilter('nearest', 'nearest')

    love.window.setTitle('Space Attack')

    title_largeFont = love.graphics.newFont('fonts/Mario-Kart-DS.ttf', 40)
    title_smallFont = love.graphics.newFont('fonts/Mario-Kart-DS.ttf', 20)
    largeFont = love.graphics.newFont('fonts/04B_30__.TTF', 32)
    smallFont = love.graphics.newFont('fonts/04B_30__.TTF', 20)
    love.graphics.setFont(largeFont)

    player = Player(WINDOW_WIDTH / 2 - 40, WINDOW_HEIGHT - 100, 10, 10)

    map.music:setLooping(true)
    map.music:setVolume(.25)
    map.music:play()
end

function enemies_controller:spawnEnemy(x, y, n)
    enemy = {}
    enemy.x = x
    enemy.y = y
    enemy.width = SMALL_WIDTH
    enemy.height = SMALL_HEIGHT
    enemy.bullets = {}
    enemy.cooldown = 10
    -- n is a random variable that determines what type of enemy spawns
    if n == 0 then
        enemy.image = alienImage
        enemy.speed = 5
    elseif n == 1 then
        enemy.image = alienship1_Image
        enemy.speed = 6
    else
        enemy.image = alienship2_Image
        enemy.speed = 7
    end
    table.insert(self.enemies, enemy)
end

function stars_controller:spawnStars(x, y)
    star = {}
    star.x = x
    star.y = y
    star.speed = 10
    star.image = love.graphics.newImage('images/star.png')
    star.width = SMALL_WIDTH
    star.height = SMALL_HEIGHT
    table.insert(self.stars, star)
end

function love.update(dt)
    player.cooldown = player.cooldown - 1

    checkCollisions(enemies_controller.enemies, player.bullets)
    player:checkCollision(enemies_controller.enemies)


    -- checks spawn timer, if > 0 don't spawn an enemy but if it is spawn an enemy
    if spawnTimer > 0 then
        spawnTimer = spawnTimer - dt
    else
        if GAME_STATE == 'play' then
            enemies_controller:spawnEnemy(love.math.random(0, WINDOW_WIDTH - SMALL_WIDTH), 0, love.math.random(0,2))
            spawnTimer = spawnTimer_Max
        end
    end

    -- same situation as spawn timer for enemies, but this is for the warp effect
    if starSpawnTimer > 0 then
        starSpawnTimer = starSpawnTimer - dt
    else
        stars_controller:spawnStars(love.math.random(0, WINDOW_WIDTH - SMALL_WIDTH), 0)
        starSpawnTimer = starSpawnTimer_Max
    end

    -- checking user input to move left and right
    if love.keyboard.isDown('right') then
        if GAME_STATE == 'play' then 
            player.dx = PLAYER_SPEED 
        end
    elseif love.keyboard.isDown('left') then
        if GAME_STATE == 'play' then
            player.dx = -PLAYER_SPEED
        end
    else
        player.dx = 0
    end

    -- checking user input for firing function
    if love.keyboard.isDown('space') then
        if GAME_STATE == 'play' then
            player:fire()
        end
    end

    -- this removes enemies if they are too far off the scree
    -- also how the enemies move downards
    -- NEED TO ADD MOVEMENT FUNCTIONS HERE FOR DIFFERENT ENEMIES
    for i,e in ipairs(enemies_controller.enemies) do
        e.y = e.y + e.speed
        if e.y > WINDOW_HEIGHT then
            table.remove(enemies_controller.enemies, i)
        end
    end

    -- removes bullets if they travel too far off the screen
    -- this also moves the bullets upwards
    for i,v in ipairs(player.bullets) do
        if v.y < 0 then
            table.remove(player.bullets, i)
        end
        v.y = v.y - 8
    end 

    -- removes stars if they travel too far off screen
    -- moves the stars to create warp effect
    for i,s in ipairs(stars_controller.stars) do
        s.y = s.y + s.speed
        if s.y > WINDOW_HEIGHT then
            table.remove(stars_controller.stars, i)
        end
    end

    -- checks lives to determine game state
    if LIVES == 0 then
        GAME_STATE = 'end'
    end

    player:update(dt)
end

-- key pressed function so we can exit the game and start the game
function love.keypressed(key)
    if key == 'escape' then 
        love.event.quit()
    elseif key == 'enter' or key == 'return' then
        if GAME_STATE == 'start' then
            GAME_STATE = 'play'
        end
    end
end


function love.draw()

    -- checks gamestate and if it is
    if GAME_STATE == 'start' then
        -- draw the warp effect
        for _,s in pairs(stars_controller.stars) do
            local scaleX, scaleY = getImageScaleForNewDimensions(s.image, STAR_WIDTH, STAR_HEIGHT)
            love.graphics.draw(s.image, s.x, s.y, 0, 2, 4)
        end

        -- draw the player 
        player:render()

        love.graphics.setColor(200, 0, 200, 255)
        love.graphics.setFont(title_largeFont)
        love.graphics.print('WELCOME TO SPACE ATTACK!', WINDOW_WIDTH / 2 - 290, WINDOW_HEIGHT / 2)
        love.graphics.setFont(title_smallFont)
        love.graphics.print('press ENTER to begin!', WINDOW_WIDTH / 2 - 110, WINDOW_HEIGHT / 2 + 100)
    end

    if GAME_STATE == 'play' then
        -- draw the map
        love.graphics.setColor(255, 255, 255)
        local map_width, map_height = getImageScaleForNewDimensions(map.image, map.width, map.height)
        love.graphics.draw(map.image, 0, 0, 0, map_width, map_height)

        -- draw the enemies
        for _,e in pairs(enemies_controller.enemies) do
            local scaleX, scaleY = getImageScaleForNewDimensions(e.image, SMALL_WIDTH, SMALL_HEIGHT)
            love.graphics.draw(e.image, e.x, e.y, 0, scaleX, scaleY)
        end

        -- draw the warp effect
        for _,s in pairs(stars_controller.stars) do
            love.graphics.draw(s.image, s.x, s.y, 0, 1, 4)
        end

        -- draw the player
        player:render()

        -- draw the bullets
        love.graphics.setColor(255, 255, 255)
        for _,v in pairs(player.bullets) do
            love.graphics.rectangle('fill', v.x, v.y, 10, 10)
        end

        displayScore()
        displayLives()
    end

    if GAME_STATE == 'end' then
        love.audio.stop()
        love.graphics.setColor(200, 0, 200, 255)
        love.graphics.setFont(title_largeFont)
        love.graphics.print('GAME OVER!', WINDOW_WIDTH / 2 - 120, WINDOW_HEIGHT / 2)
        love.graphics.setFont(title_smallFont)
        love.graphics.print('score  ' .. tostring(SCORE), WINDOW_WIDTH / 2 - 60, WINDOW_HEIGHT / 2 + 50)
    end
end

function displayScore()
    if GAME_STATE == 'play' then
        love.graphics.setColor(0, 255, 0, 255)
        love.graphics.setFont(largeFont)
        love.graphics.print('Score: ' .. tostring(SCORE), WINDOW_WIDTH / 2 - 130, 10)
    end
end

function displayLives()
    if GAME_STATE == 'play' then
        love.graphics.setColor(255, 0, 0, 255)
        love.graphics.setFont(smallFont)
        love.graphics.print('Lives: ' .. tostring(LIVES), WINDOW_WIDTH - 150, 10)
    end
end