Making bullets for a simple shooter game

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Josh.G
Prole
Posts: 16
Joined: Thu Jul 29, 2021 6:16 pm

Making bullets for a simple shooter game

Post 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!
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: Making bullets for a simple shooter game

Post 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?
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
dusoft
Party member
Posts: 635
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Making bullets for a simple shooter game

Post 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.
Josh.G
Prole
Posts: 16
Joined: Thu Jul 29, 2021 6:16 pm

Re: Making bullets for a simple shooter game

Post 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)
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: Making bullets for a simple shooter game

Post 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?
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Josh.G
Prole
Posts: 16
Joined: Thu Jul 29, 2021 6:16 pm

Re: Making bullets for a simple shooter game

Post 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.
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: Making bullets for a simple shooter game

Post 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 4265 times
fast-bullets-01.love
(973 Bytes) Downloaded 174 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Google [Bot] and 1 guest