Need some help manipulating my bullets...

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
Elemeandor
Prole
Posts: 1
Joined: Wed Mar 09, 2016 10:30 pm

Need some help manipulating my bullets...

Post by Elemeandor »

Hello everyone. I'm trying to make my (first) fan game. I've dabbled in C++ a little before but I'm sort of new to programming in general and lua. I spent a while looking up guides and tutorials on both lua and love 2d but I thought it was finally time to register here and come to you guys for help.

The game I'm working on is supposed to be a curtain shoot em up. I didn't want to get too far ahead of myself so everything I've done so far is in line with the guides and tutorials I've read / watched. My intention is to eventually have multiple playable characters and different types of bullets for each character as well. I have no idea how to get homing bullets to work.

So I tried something simple. By default, bullets will travel towards the top of the screen when you fire them. I wanted the to stop traveling vertically when they lined up with an enemy on the x-axis and instead, travel horizontally. But I can't get that to work either. Here is part of what I have so far.

Code: Select all

canShoot = true
canShootTimerMax = 0.2
canShootTimer = canShootTimerMax --lines 3,4,5 solve my infinite bullet problem
BrontoBTimerMax = 0.4
BrontoBTimer = BrontoBTimerMax
isAlive = true

function love.load()
   Score = 0
   hero = {} -- new table for the hero
    
 hero.x = 300    -- x,y coordinates of the hero
 hero.y = 450
 hero.width = 30
 hero.height = 15
 hero.speed = 180
 hero.bullets = {} -- table for hero's shots
 hero.bullets.BWidth = 2
 hero.bullets.BHeight = 5
 
 enemies = {} --table for enemies.
 enemies.width = 40
 enemies.height = 40
   end

Code: Select all

 canShootTimer = canShootTimer - (1 * dt)
if canShootTimer < 0 then
  canShoot = true
end
 
 
 if love.keyboard.isDown("z") and canShoot and isAlive then
	K_shoot() 
	end
	
 for i,v in ipairs(hero.bullets) do
	v.y = v.y - dt * 400
	
	if v.y < 0 then
	table.remove (hero.bullets, i)
	end
	end

BrontoBTimer = BrontoBTimer - (1 * dt)
if BrontoBTimer < 0 then
	BrontoBTimer = BrontoBTimerMax
	
	randomNumber = math.random(10, love.graphics.getWidth()-10) 
	Bronto_Burt ={x = randomNumber, y =(-10)} 
	table.insert(enemies, Bronto_Burt)
end

--Enemy position
for i, e in ipairs(enemies) do
	e.y = e.y + (200 * dt)

	if e.y > 770 then
	table.remove(enemies, i)
	end
end

--collision between enemies and bullets
for i, e in ipairs(enemies) do
	for j, v in ipairs(hero.bullets) do
	if CheckCollision(e.x, e.y, enemies.width, enemies.height, v.x, v.y, hero.bullets.BWidth, hero.bullets.BHeight) then
	table.remove(hero.bullets, j)
	table.remove(enemies, i)
	Score = Score + 10
end
end	

Code: Select all

function K_shoot()
local shot = {}
shot.x = hero.x + hero.width/2
shot.y = hero.y
table.insert(hero.bullets,shot)
canShoot = false
canShootTimer = canShootTimerMax
end

function CheckCollision (x1, y1, w1, h1, x2, y2, w2, h2)
return x1 < x2+w2 and
	   x2 < x1+w1 and
	   y1 < y2+h2 and
	   y2 < y1+h1
	   end
My biggest problem is that I can't seem to find any way for the bullets to interact with the enemies unless I run a for loop for one of the tables inside of the other. If I do that while the bullets are moving vertically, it screws things up. And even then, whatever code I write doesn't do what I want it to (that is, I can't check to see if the bullets and the enemies line up on the x-axis by saying "if bullet.y = enemies.y then...")

I've yammered on a lot, but any help would be appreciated. I really hope I'm not a lost cause here.
User avatar
Skeiks
Citizen
Posts: 51
Joined: Wed Jan 28, 2015 1:51 pm

Re: Need some help manipulating my bullets...

Post by Skeiks »

It would really help if you uploaded a .love file instead of posting the code. I would be able to run the program and see what's going on exactly. For your problem with the bullets though, you need to look at it a little differently.

You're going to have to do a for loop for each bullet or for each enemy. You can optimize it, but there's almost no way to get around that. To make the bullets like up with the enemies horizontally though, try to think of bullets have 4 different states, for each direction. Once you know that the bullet reached an enemy, you want it to switch from going up to going to the left. It's not too difficult to conceptualize this in pseudocode.

Code: Select all

foreach bullet in player.bullets do
	foreach enemy in enemies do
		if enemy.y > bullet.y-10 and enemy.y < bullet.y+10 then
			if enemy.x > bullet.x then
				bullet.direction = 'right'
			else
				bullet.direction = 'left'
			en
		end
	end
	
	
	if bullet.direction = 'up' then
		bullet.xSpeed = 0
		bullet.ySpeed = -5
	end
	if bullet.direction = 'left' then
		bullet.xSpeed = -5
		bullet.ySpeed = 0
	end
	if bullet.direction = 'right' then
		bullet.xSpeed = 5
		bullet.ySpeed = 0
	end
	bullet.x = bullet.x + bullet.xSpeed
	bullet.y = bullet.x + bullet.ySpeed
end
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 11 guests