TopDownShooter bullet problem?

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
SkeleLord
Prole
Posts: 1
Joined: Sun Dec 18, 2016 3:08 am

TopDownShooter bullet problem?

Post by SkeleLord »

I have this problem with the bullets, so after the bullets are shot, every time I move my player the bullets move too/ How can I make it so when the bullets are shot, they aren't controlled by the movement of the player.
Another thing is how can I make it so the bullets are shot from the gun of the player(The gun is in the image) I believe this has something to do with the offset that am not familiar with. Am attaching the base image, you can make it so it rotates instead of changing images if you wish. (Other than that, you can multi-comment the "Direction player facing" if statement)
Am just a beginner, and am making this game to learn more and for fun. Any help is appreciated.

Here is the code:

plrImage = love.graphics.newImage("shooterUp.png")

plr = {
xPos = love.graphics.getWidth()/2 - 50,
yPos = love.graphics.getHeight()/2 - 50,
spd = 300,
dir = "Up",
}

bullets = {}

function love.update(dt)
bullet = {
width = 0,
height = 0,
xPos = plr.xPos + 40,
yPos = plr.yPos + 10,
spd = 500
}
function love.keypressed(key)
if key == "space" then
table.insert(bullets, bullet)
end
end
--Movement & Collision
if love.keyboard.isDown("w") and plr.yPos > 0 then
plr.yPos = plr.yPos - plr.spd * dt
plr.dir = "Up"
elseif love.keyboard.isDown("s") and plr.yPos + 50 < love.graphics.getHeight() then
plr.yPos = plr.yPos + plr.spd * dt
plr.dir = "Down"
end
if love.keyboard.isDown("a") and plr.xPos > 0 then
plr.xPos = plr.xPos - plr.spd * dt
plr.dir = "Left"
elseif love.keyboard.isDown("d") and plr.xPos + 50 < love.graphics.getWidth() then
plr.xPos = plr.xPos + plr.spd * dt
plr.dir = "Right"
end

--Direction player facing
if plr.dir == "Up" then
plrImage = love.graphics.newImage("shooterUp.png")
elseif plr.dir == "Down" then
plrImage = love.graphics.newImage("shooterDown.png")
elseif plr.dir == "Right" then
plrImage = love.graphics.newImage("shooterRight.png")
elseif plr.dir == "Left" then
plrImage = love.graphics.newImage("shooterLeft.png")
end

--Bullet Update
for i, v in ipairs(bullets) do
if plr.dir == "Up" then
v.yPos = v.yPos - v.spd * dt
v.width = 1
v.height = 10
elseif plr.dir == "Down" then
v.yPos = v.yPos + v.spd * dt
v.width = 1
v.height = 10
elseif plr.dir == "Right" then
v.xPos = v.xPos + v.spd * dt
v.width = 10
v.height = 1
elseif plr.dir == "Left" then
v.xPos = v.xPos - v.spd * dt
v.width = 10
v.height = 1
end
end
--Bullet Out of bounds check
for i, v in ipairs(bullets) do
if v.yPos < 0 or v.yPos + v.height > love.graphics.getHeight() then
table.remove(bullets, i)
elseif v.xPos < 0 or v.xPos + v.width > love.graphics.getWidth() then
table.remove(bullets, i)
end
end
drawBullet()
end

function drawBullet()
function love.draw()
for i, v in ipairs(bullets) do
love.graphics.setColor(255, 255, 255)
love.graphics.rectangle("fill", v.xPos, v.yPos, v.width, v .height)
end
love.graphics.draw(plrImage, plr.xPos, plr.yPos)
end
end



function love.draw()
love.graphics.draw(plrImage, plr.xPos, plr.yPos)
end
Attachments
shooterUp.png
shooterUp.png (4.94 KiB) Viewed 1552 times
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: TopDownShooter bullet problem?

Post by Sir_Silver »

Code: Select all

--Bullet Update
for i, v in ipairs(bullets) do
if plr.dir == "Up" then
v.yPos = v.yPos - v.spd * dt
v.width = 1
v.height = 10
elseif plr.dir == "Down" then
v.yPos = v.yPos + v.spd * dt
v.width = 1
v.height = 10
elseif plr.dir == "Right" then
v.xPos = v.xPos + v.spd * dt
v.width = 10
v.height = 1
elseif plr.dir == "Left" then
v.xPos = v.xPos - v.spd * dt
v.width = 10
v.height = 1
end
end
Rather than updating the direction a bullet moves every update tick in accordance with the player's direction, you could store the direction the player is moving when he presses the shoot button, and then store that direction on the bullet, and then update the bullets position according to it's direction.

Code: Select all

plrImage = love.graphics.newImage("shooterUp.png")

plr = {
	xPos = love.graphics.getWidth()/2 - 50,
	yPos = love.graphics.getHeight()/2 - 50,
	spd = 300,
	dir = "Up"
}

bullets = {}

function love.update(dt)
	bullet = {
		width = 0,
		height = 0,
		xPos = plr.xPos + 40,
		yPos = plr.yPos + 10,
		spd = 500,
		dir = plr.dir  --Stores the current direction of the player on the bullet object.
	}
	
	
	function love.keypressed(key)
		if key == "space" then
			table.insert(bullets, bullet)
		end
	end
	
	--Movement & Collision
	if love.keyboard.isDown("w") and plr.yPos > 0 then
		plr.yPos = plr.yPos - plr.spd * dt
		plr.dir = "Up"
	elseif love.keyboard.isDown("s") and plr.yPos + 50 < love.graphics.getHeight() then
		plr.yPos = plr.yPos + plr.spd * dt
		plr.dir = "Down"
	end
	if love.keyboard.isDown("a") and plr.xPos > 0 then
		plr.xPos = plr.xPos - plr.spd * dt
		plr.dir = "Left"
	elseif love.keyboard.isDown("d") and plr.xPos + 50 < love.graphics.getWidth() then
		plr.xPos = plr.xPos + plr.spd * dt
		plr.dir = "Right"
	end

	--Direction player facing
	if plr.dir == "Up" then
		plrImage = love.graphics.newImage("shooterUp.png")
	elseif plr.dir == "Down" then
		plrImage = love.graphics.newImage("shooterDown.png")
	elseif plr.dir == "Right" then
		plrImage = love.graphics.newImage("shooterRight.png")
	elseif plr.dir == "Left" then
		plrImage = love.graphics.newImage("shooterLeft.png")
	end

	--Bullet Update
	for i, v in ipairs(bullets) do  --This now updates the bullet's position based on the direction stored on the bullet, not the player's direction.
		if v.dir == "Up" then
			v.yPos = v.yPos - v.spd * dt
			v.width = 1
			v.height = 10
		elseif v.dir == "Down" then
			v.yPos = v.yPos + v.spd * dt
			v.width = 1
			v.height = 10
		elseif v.dir == "Right" then
			v.xPos = v.xPos + v.spd * dt
			v.width = 10
			v.height = 1
		elseif v.dir == "Left" then
			v.xPos = v.xPos - v.spd * dt
			v.width = 10
			v.height = 1
		end
	end

	--Bullet Out of bounds check
	for i, v in ipairs(bullets) do
		if v.yPos < 0 or v.yPos + v.height > love.graphics.getHeight() then
			table.remove(bullets, i)
		elseif v.xPos < 0 or v.xPos + v.width > love.graphics.getWidth() then
			table.remove(bullets, i)
		end
	end
		
	drawBullet()
end

function drawBullet()
	function love.draw()
		for i, v in ipairs(bullets) do
			love.graphics.setColor(255, 255, 255)
			love.graphics.rectangle("fill", v.xPos, v.yPos, v.width, v .height)
		end
		
		love.graphics.draw(plrImage, plr.xPos, plr.yPos)
	end
end



function love.draw()
	love.graphics.draw(plrImage, plr.xPos, plr.yPos)
end
The code I've written above has implemented a fix for the problem you were describing, I've also taken the time to format the code so it is more easily readable. The fixes for it should be commented within the code, basically the direction a bullet should be going is now stored on the bullet at it's creation and then when it comes to updating, we update the bullet's position based on it's stored direction. Aside from this issue, there are a lot of other issues I see within the code - like trying to draw in love.update, and constantly creating bullets every tick to name a few - but I didn't want to make your game for you, so I decided to mostly leave it untouched as far as that goes. :P
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest