Page 1 of 2
New to lua, need help with tables (and now collisions)
Posted: Wed Oct 13, 2010 12:33 pm
by fylth
Hey all, i've been trying to make a simple game with Love, and have ran into a small problem. I'm entirely new to lua, however the alternatives to Love were Gamemaker (not a whole lot of control) or VB (not really suitable). I've managed to make a simple games so far (well, I say game, it's a ship that flies), however I have no idea how to make the gun shoot without deleting previous bullets. I'm guessing to use an array/table, but i'm having trouble getting it to work. After trying for several days i've decided to ask for help.
*edit* Also this would be useful for creating waves of enemy, instead of one at a time (not that i've implemented this yet)
Re: New to lua, need help with tables
Posted: Wed Oct 13, 2010 12:48 pm
by nevon
I haven't actually read through your code, but I'll give you an idea of how you can do it.
A bullet is a table containing the bullet's X and Y coordinates. You could have a bunch of other stuff in there, of course, like the vector it's travelling along or whatever, but nevermind that for now.
You would then have another table containing all active bullets. Let's call this table "projectiles".
Then you could do something like the following:
Code: Select all
function love.load()
projectiles = {}
bulletspeed = 50
end
function love.update(dt)
local removetable = {}
for i,v in ipairs(projectiles) do
--update the bullets to move them
v.x = v.x + bulletspeed*dt
--Check if it's out of bounds
if v.x > 800 then --if the screen is 800px wide
table.insert(removetable, i)
end
end
for i,v in ipairs(removetable) do
--If the bullet is in the removetable, remove it from the projectiles table
table.remove(projectiles, v-i+1) --The offset is so that it doesn't go out of sync with the projectiles table
end
end
function love.keypressed(key, unicode)
if key == " " then
--if the user presses space, create a bullet
table.insert(projectiles, {x=50, y=300}) --example values
end
end
It's very possible that I screwed something up in the above code, but hopefully it'll give you an idea.
Re: New to lua, need help with tables
Posted: Wed Oct 13, 2010 12:50 pm
by Thursdaybloom
Post review
At least one new post has been made to this topic. You may wish to review your post in light of this.
Thanks Nevon
I'll post my reply anyway for you fylth
You're only defining a single bullet.
Code: Select all
bullet = {
image = love.graphics.newImage("bullet.png"),
x = 0,
y = 0
}
When you press space to shoot the bullet you're editing the values for this single bullet so every press of space resets these parameters.
Code: Select all
if love.keyboard.isDown(" ") then
shoot = true
local randnum = math.random(-8, 8)
bullet.x = player.x + (28 + randnum)
bullet.y = player.y
end
bullet.y = bullet.y - 3
You can see above that pressing space resets bullet.y to player.y. You're taking a single image file and changing its location. You need to instead be creating a new bullet entry each time you press space. You can keep the bullet table you have and just create new entries in it, for example bullet[1].y and bullet[2].y
Re: New to lua, need help with tables
Posted: Wed Oct 13, 2010 12:53 pm
by fylth
Ah, thankyou very much, the comments in your code really helped me understand what was going on
I'll have a go at implementing this now, thanks again
Re: New to lua, need help with tables
Posted: Wed Oct 13, 2010 1:45 pm
by fylth
Ok, I think I have most of the code working, as in I don't get any crashes, however the image of the bullet isn't being created. Here's the updated code (probably needs some cleaning up now, that comes after functionality)
Re: New to lua, need help with tables
Posted: Wed Oct 13, 2010 1:59 pm
by TechnoCat
You are just nearly there.
First, you had:
Code: Select all
function love.keypressed(key,unicode)
if keypressed == " " then
You probably meant
Code: Select all
function love.keypressed(key,unicode)
if key == " " then
Second, you need to draw the bullets in love.draw() now.
put this somewhere in love.draw()
Code: Select all
for i,v in ipairs(projectiles) do
love.graphics.draw(v.image, v.x,v.y)
end
Re: New to lua, need help with tables
Posted: Wed Oct 13, 2010 4:15 pm
by fylth
Awesome, worked a treat
Now I just need some enemy spawns and a score system and we have a game! Best get to work hadn't I
Re: New to lua, need help with tables
Posted: Wed Oct 13, 2010 4:36 pm
by TechnoCat
fylth wrote:Awesome, worked a treat
Now I just need some enemy spawns and a score system and we have a game! Best get to work hadn't I
Don't forget collision detection.
Re: New to lua, need help with tables
Posted: Thu Oct 28, 2010 8:41 am
by fylth
Well I finally got some time away from doing assignment work to work on this some more, and I almost immediately hit another roadblock: Collisions.
Now to me this seems like it should be simple, check if two sprites are in the same location, if so delete both sprites (I plan on making it reduce health eventually so that I can have incrementally hard waves, but I would rather get this working at all to start with)
I've tried two different methods of using this, one quite complicated and one less so (which I saw in an example in another thread so thought was worth a try). Any idea what I need to change?
Code: Select all
for i,v in ipairs(enemy) do
enemyx = v.x
enemyy = v.y
for i,v in ipairs(projectiles) do
bulletx = v.x
bullety = v.y
-- if bulletx > (enemyy - 32) and bullety < (enemyy) and bulletx > (enemyx + 32) and bullety < (enemyx) then
-- table.remove(enemy, i)
-- table.remove(projectile, i)
if bulletx == enemyx and bullety == enemyy then
table.remove(enemy, i)
table.remove(projectile, i)
end
end
end
(I've got the first method I tried commented out still)
Thanks for any help!
Re: New to lua, need help with tables (and now collisions)
Posted: Thu Oct 28, 2010 9:02 am
by Ryne
Doing coordinate collisions could be a problem depending on the type of game, unless it's a side-scroller without the ability to jump. Since your sprites could be 32x32 and only have a single pixel collision point, technically the images could be touching but not collide. I was thinking of a way to create a pixel barrier around the sprite the same way but I don't think it would be very efficient. I'm not great with collisions myself but I think I've seen something about using "bounding boxes", I'm not certain if that was about collisions or not, though it seemed to be. Good luck!