Making bullets for a simple shooter game
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Making bullets for a simple shooter game
I was wondering how to implement bullets for a game im making. If any more details are needed just say so below. Thanks!
Re: Making bullets for a simple shooter game
Bullets with infinity speed or "slow bullets", that fly (very) slow?
Re: Making bullets for a simple shooter game
Add bullets to a table, keep x,y position for each bullet. Once they are out of screen (or hit a target), just remove them from a table. Iterate with ipairs (or pairs) to draw them, use speed * delta time to move them accordingly.
My boat driving game demo: https://dusoft.itch.io/captain-bradley- ... itius-demo
Re: Making bullets for a simple shooter game
Would it be helpful for me to post the game here? its a two player being operated by WASD and arrow keys and i want them both to have the ability to shoot. (havent done that much on it so far)
Re: Making bullets for a simple shooter game
The instant shooting or you can see how flies the bullet?
Re: Making bullets for a simple shooter game
not completley instant, but not slow, so you can kinda see it.
ive made bullets like that before from a tutorial but in all honesty I forget.
ive made bullets like that before from a tutorial but in all honesty I forget.
Re: Making bullets for a simple shooter game
Like here?
Code: Select all
-- License CC0 (Creative Commons license) (c) darkfrei, 2022
function love.load()
width, height = love.graphics.getDimensions( )
targets = {}
player = {x=400, y=300, angle=0}
timerDefault = 0.5
timer = 0
end
local function newTarget ()
local x = math.random (width)
local y = math.random (height)
table.insert (targets, {x=x, y=y, r=20})
end
function love.update(dt)
timer = timer - dt
if timer < 0 then
newTarget ()
timer = timerDefault
end
end
local function drawTriangle (mode, x, y, length, width , angle) -- position, length, width and angle
love.graphics.push()
love.graphics.translate(x, y)
love.graphics.rotate( angle )
love.graphics.polygon(mode, -length/2, -width /2, -length/2, width /2, length/2, 0)
love.graphics.pop()
end
function love.draw()
love.graphics.print('#targets: '..#targets)
for i, target in ipairs (targets) do
love.graphics.circle('line', target.x, target.y, target.r)
end
drawTriangle ('fill', player.x, player.y, 40, 20 , player.angle)
if player.line then
love.graphics.line(player.line)
player.line = nil
end
end
function love.keypressed(key, scancode, isrepeat)
if false then
elseif key == "escape" then
love.event.quit()
end
end
-- https://love2d.org/forums/viewtopic.php?p=111368#p111368
function distPointToLine(px,py,x1,y1,x2,y2)
local dx,dy = x1-x2,y1-y2
local length = math.sqrt(dx*dx+dy*dy)
dx,dy = dx/length,dy/length
return math.abs( dy*(px-x1) - dx*(py-y1))
end
function love.mousepressed( x, y, button, istouch, presses )
if button == 1 then -- left mouse button
player.line = {player.x, player.y, x, y}
player.angle = math.atan2(y-player.y, x-player.x)
for i, target in ipairs (targets) do
if target.r > distPointToLine(target.x,target.y, player.x, player.y, x, y) then
target.r = target.r-3
if target.r < 10 then
table.remove(targets, i)
end
end
end
elseif button == 2 then -- right mouse button
end
end
Who is online
Users browsing this forum: Ahrefs [Bot] and 2 guests