Page 1 of 1

I have a problem with bullets

Posted: Mon Aug 31, 2015 9:46 am
by Taraji-Productions
Hi
I have a strange issue with bullets that i didn't figure it out !
I've Put a good peace of code and surely I got some bugs and I fixed them.
However the Bullets still didn't appear
tried changing some var and much more , no results .
Please any one tell me where is the problem , or if someone have the time and intelligence could please make me a whole new code for the bullet.lua file .
I want the bullet to follow the buu.lua smoothly or if that's impossible just follow it with these sharp edges , it's ok .
but if not please tell me how to fix my old code here it is
this is the main.lua code

Code: Select all

require "player"
require "buu"
require "bullet"

function love.load()

end
function love.update(dt)
	player.update()
	buu.update()
	bullet.update()
	bullet.shoot()
end
function love.draw()
	player.draw()
	buu.draw()
	bullet.draw()
end
this is the player.lua code

Code: Select all

	player = {}
	player.x = 500
	player.y = 300
	player.w = 20
	player.h = 20
	g = love.graphics
	Dirx = love.math.random(0, 700)
	Diry = love.math.random(0, 500)
function player.update(dt)
if player.x < Dirx then 
	player.x = player.x + 1  
elseif player.x > Dirx then 
	player.x = player.x - 1 
end
if player.y < Diry then 
	player.y = player.y + 1
elseif player.y > Diry then 
	player.y = player.y - 1
end

if player.x == Dirx and player.y == Diry then 
	Dirx = love.math.random(0, 700)
	Diry = love.math.random(0, 500)
end	
	



end
function player.draw()
	g.setColor(255,255,0)
	g.rectangle("fill", player.x, player.y, player.w, player.h)
	g.rectangle("line", Dirx, Diry, 20, 20)
end


this is the buu.lua code

Code: Select all

	buu = {}
	buu.x = 100
	buu.y = 100
	buu.w = 20
	buu.h = 20
	g = love.graphics
	Dirx1 = love.math.random(0, 700)
	Diry1 = love.math.random(0, 500)
function buu.update(dt)
if buu.x < Dirx1 then 
	buu.x = buu.x + 1  
elseif buu.x > Dirx1 then 
	buu.x = buu.x - 1 
end
if buu.y < Diry1 then 
	buu.y = buu.y + 1
elseif buu.y > Diry1 then 
	buu.y = buu.y - 1
end

if buu.x == Dirx1 and buu.y == Diry1 then 
	Dirx1 = love.math.random(0, 700)
	Diry1 = love.math.random(0, 500)
end	
end
function buu.draw()
	g.setColor(255,0,150)
	g.rectangle("fill", buu.x, buu.y, 20, 20)
	g.rectangle("line", Dirx1, Diry1, 20, 20)
end
and finally , this is the bullet.lua code

Code: Select all

	bullet = {}
	Var1 = 0
	bullet.speed = 3
	g = love.graphics
	
function bullet.spawn(x,y,dir)
	table.insert(bullet,{width=5, height=5, x=x, y=y, dir=dir, stat=stat})
end
function bullet.draw()
	for i,v in ipairs(bullet) do
		g.setColor(255,255,0)
		g.rectangle("fill", v.x,v.y,v.width,v.height)
	end
	g.setColor(255,255,255)
	g.print(Var1, 10, 10)
end
function bullet.update(dt)
	Var1 = Var1 + 1
	if Var1 == 250 then
		Var1 = Var1 - Var1 
		stat = "shoot"	
	end
		
	for i,v in ipairs(bullet) do
		if dir == "right" and stat == "shoot" and buu.x < player.x + (player.w / 2) then
			v.x = v.x - bullet.speed * dt
		elseif dir == "left" and stat == "shoot" and buu.x > player.x + (player.w / 2) then
			v.x = v.x + bullet.speed * dt
		end
		if dir == "up" and shoot == "shoot" and buu.y <  player.y + (player.h / 2) then
			v.y = v.y - bullet.speed * dt
		elseif dir == "down" and stat == "shoot" and buu.y > player.y + (player.h / 2) then
			v.y = v.y + bullet.speed * dt
		end
	end
end
function bullet.shoot()
	for i,v in ipairs(bullet) do
		if stat == "shoot" and buu.x < player.x + (player.w / 2) then
			bullet.spawn(player.x + (player.w / 2), player.y + (player.h / 2), "right", "shoot")
		elseif stat == "shoot" and buu.x > player.x + (player.w / 2) then
			bullet.spawn(player.x + (player.w / 2), player.y + (player.h / 2), "left", "shoot")
		end
		if stat == "shoot" and buu.y <  player.y + (player.h / 2) then
			bullet.spawn(player.x + (player.w / 2), player.y + (player.h / 2), "up", "shoot")
		elseif stat == "shoot" and buu.y > player.y + (player.h / 2) then
			bullet.spawn(player.x + (player.w / 2), player.y + (player.h / 2), "down", "shoot")
		end	
	end	
end
And thanks for helping <3

Re: I have a problem with bullets

Posted: Mon Aug 31, 2015 10:26 am
by ArchAngel075
From the looks initially it seems you are not localizing DirX and DirY for player.lua and buu.lua .
This will cause any edits to DirX on the player.lua to modify the DirX in buu.lua.

I can't exactly tell if its intended or not.

Re: I have a problem with bullets

Posted: Mon Aug 31, 2015 11:59 am
by Taraji-Productions
I did see the Dirx and Diry are for the Player.lua
however the Dirx1 and Diry1 are for the buu.lua
i wound't miss that ! :)