Love2d如何制作子弹

时间:2013-11-13 15:32:33

标签: lua love2d

嘿伙计们,所以我试图在我的游戏中创建一些子弹,我想你可能会帮助我解决我遇到的问题。

所以我的子弹以速度和所有这一切开火,但是如何让它们从玩家身上射击? 我如何制作它可以向各个方向射击?因为它只能启动atm。

这是我的代码。

local AdvTiledLoader = require("AdvTiledLoader.Loader")
require("camera")

function love.load()
love.graphics.setBackgroundColor( 204, 255, 204 )
crazy = love.graphics.newImage("wizard3.png")
key1 = love.graphics.newImage("key1.png")

bullets = {}
bullet_speed = 100


width = crazy:getWidth()
height = crazy:getHeight()


AdvTiledLoader.path = "maps/"
map = AdvTiledLoader.load("map.tmx")
map:setDrawRange(0, 0, map.width * map.tileWidth, map.height * map.tileHeight)

camera:setBounds(0, 0, map.width * map.tileWidth - love.graphics.getWidth(), map.height * map.tileHeight - love.graphics.getHeight() )

world =     {
            gravity = 1536,
            ground = 512,
            }

player =    {
            x = 256,
            y = 256,
            x_vel = 0,
            y_vel = 0,
            jump_vel = -1024,
            speed = 512,
            flySpeed = 700,
            state = "",
            h = 48,
            w = 40,
            standing = false,
            }

item =      {
            x = 100,
            y = 3222,
            h = 32,
            w = 32,
            picked = false,
            }

function player:jump()
    if self.standing then
        self.y_vel = self.jump_vel
        self.standing = false
    end
end

function player:pick()
    if ((self.x - width/2 < item.x and self.x + width/2 > item.x) and (self.y - height/2 < item.y and self.y + height/2 > item.y)) then
        print("picked")
    else
        print("no item found")
    end
end

function player:right()
    self.x_vel = self.speed
end

function player:left()
    self.x_vel = -1 * (self.speed)
end

function player:stop()
    self.x_vel = 0
end

function player:collide(event)
    if event == "floor" then
        self.y_vel = 0
        self.standing = true
    end
    if event == "cieling" then
        self.y_vel = 0
    end
end

function player:update(dt)
    local halfX = self.w / 2
    local halfY = self.h / 2

    self.y_vel = self.y_vel + (world.gravity * dt)

    self.x_vel = math.clamp(self.x_vel, -self.speed, self.speed)
    self.y_vel = math.clamp(self.y_vel, -self.flySpeed, self.flySpeed)

    local nextY = self.y + (self.y_vel*dt)
    if self.y_vel < 0 then
        if not (self:isColliding(map, self.x - halfX, nextY - halfY))
            and not (self:isColliding(map, self.x + halfX - 1, nextY - halfY)) then
            self.y = nextY
            self.standing = false
        else
            self.y = nextY + map.tileHeight - ((nextY - halfY) % map.tileHeight)
            self:collide("cieling")
        end
    end
    if self.y_vel > 0 then
        if not (self:isColliding(map, self.x-halfX, nextY + halfY))
            and not(self:isColliding(map, self.x + halfX - 1, nextY + halfY)) then
                self.y = nextY
                self.standing = false
        else
            self.y = nextY - ((nextY + halfY) % map.tileHeight)
            self:collide("floor")
        end
    end

    local nextX = self.x + (self.x_vel * dt)
    if self.x_vel > 0 then
        if not(self:isColliding(map, nextX + halfX, self.y - halfY))
            and not(self:isColliding(map, nextX + halfX, self.y + halfY - 1)) then
            self.x = nextX
        else
            self.x = nextX - ((nextX + halfX) % map.tileWidth)
        end
    elseif self.x_vel < 0 then
        if not(self:isColliding(map, nextX - halfX, self.y - halfY))
            and not(self:isColliding(map, nextX - halfX, self.y + halfY - 1)) then
            self.x = nextX
        else
            self.x = nextX + map.tileWidth - ((nextX - halfX) % map.tileWidth)
        end
    end

    self.state = self:getState()
end

function player:isColliding(map, x, y)
    local layer = map.tl["Solid"]
    local tileX, tileY = math.floor(x / map.tileWidth), math.floor(y / map.tileHeight)
    local tile = layer.tileData(tileX, tileY)
    return not(tile == nil)
end

function player:getState()
    local tempState = ""
    if self.standing then
        if self.x_vel > 0 then
            tempState = "right"
        elseif self.x_vel < 0 then
            tempState = "left"
        else
            tampState = "stand"
        end
    end
    if self.y_vel > 0 then
        tempState = "fall"
    elseif self.y_vel < 0 then
        tempState = "jump"
    end
    return tempState
end
 end

 function love.draw()
camera:set()


for i,v in ipairs(bullets) do
   love.graphics.rectangle("fill", 300, v.y, 5, 10)
 end

love.graphics.draw(key1, item.x - item.w/2, item.y - item.h/2)

love.graphics.draw(crazy, player.x - width/2, player.y - height/2)

--love.graphics.setColor( 255, 161, 65 )
--love.graphics.rectangle("fill", player.x - player.w/2, player.y - player.h/2, player.w, player.h)

love.graphics.setColor( 255, 255, 255 )
map:draw()

camera:unset()
end

function love.update(dt)
if dt > 0.05 then
    dt = 0.05
end




for i,v in ipairs(bullets) do
  v.y = v.y - bullet_speed * dt
  if v.y < 100 then
     table.remove(bullets, 1)
  end   
end


if love.keyboard.isDown("d") then
    player:right()
end
if love.keyboard.isDown("a") then
    player:left()
end
if love.keyboard.isDown(" ") and not(hasJumped) then
    player:jump()
end
if love.keyboard.isDown("p") then
    player:pick()
end

player:update(dt)

camera:setPosition( player.x - (love.graphics.getWidth()/2), player.y - (love.graphics.getHeight()/2))
end

function love.keyreleased(key)
if (key == "a") or (key == "d") then
    player.x_vel = 0
end
end

function love.mousepressed(x, y, k)
print("Fire")
  if k == "l" then
     bullets[#bullets + 1] = {
         y = 495
        }
    end
end

如果你能看到别的东西,我会写一个糟糕的方式,请说出来:)它帮了我很多。

谢谢:)

1 个答案:

答案 0 :(得分:0)

您可以使用player.x和y以及mouse.x和y以及math.atan2参数来计算所有内容。它应该看起来像这样(mouse.x是love.mouse.getX()):

local bullet = {}

function bullet.shoot( start_x, start_y, mouse_x, mouse_y, speed )
    local speed = speed or 200
    local dir = math.atan2(( mouse_y - start_y ), ( mouse_x - start_x ))
    local dx, dy = speed * math.cos(dir), speed * math.sin(dir) 
    table.insert( bullet, { x = start_x, y = start_y, dx = dx, dy = dy } )
end

function bullet.update(dt)
    for i, v in ipairs( bullet ) do
        v.x = v.x + v.dx * dt
        v.y = v.y + v.dy * dt
    end
end

这就是它的全部! :)