Page 1 of 1

Making bullets for a simple shooter game

Posted: Sat Jan 22, 2022 4:52 am
by Josh.G
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

Posted: Sat Jan 22, 2022 1:46 pm
by darkfrei
Josh.G wrote: Sat Jan 22, 2022 4:52 am I was wondering how to implement bullets for a game im making. If any more details are needed just say so below. Thanks!
Bullets with infinity speed or "slow bullets", that fly (very) slow?

Re: Making bullets for a simple shooter game

Posted: Sun Jan 23, 2022 5:15 pm
by dusoft
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.

Re: Making bullets for a simple shooter game

Posted: Fri Jan 28, 2022 5:55 am
by Josh.G
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

Posted: Fri Jan 28, 2022 7:55 am
by darkfrei
Josh.G wrote: Fri Jan 28, 2022 5:55 am 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)
The instant shooting or you can see how flies the bullet?

Re: Making bullets for a simple shooter game

Posted: Tue Feb 01, 2022 4:20 pm
by Josh.G
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.

Re: Making bullets for a simple shooter game

Posted: Tue Feb 01, 2022 5:52 pm
by darkfrei
Josh.G wrote: Tue Feb 01, 2022 4:20 pm 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.
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
2022-02-01T19_00_57-Untitled.png
2022-02-01T19_00_57-Untitled.png (20.12 KiB) Viewed 4273 times
fast-bullets-01.love
(973 Bytes) Downloaded 176 times