New to lua, need help with tables (and now collisions)
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
New to lua, need help with tables (and now collisions)
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)
*edit* Also this would be useful for creating waves of enemy, instead of one at a time (not that i've implemented this yet)
- Attachments
-
- proto1.zip
- Game prototype
- (50.18 KiB) Downloaded 256 times
Last edited by fylth on Thu Oct 28, 2010 8:42 am, edited 1 time in total.
- nevon
- Commander of the Circuloids
- Posts: 938
- Joined: Thu Feb 14, 2008 8:25 pm
- Location: Stockholm, Sweden
- Contact:
Re: New to lua, need help with tables
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:
It's very possible that I screwed something up in the above code, but hopefully it'll give you an idea.
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
- Thursdaybloom
- Citizen
- Posts: 81
- Joined: Mon Feb 15, 2010 3:43 am
- Location: Australia
Re: New to lua, need help with tables
Thanks NevonPost review
At least one new post has been made to this topic. You may wish to review your post in light of this.
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
}
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
Re: New to lua, need help with tables
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
I'll have a go at implementing this now, thanks again
Re: New to lua, need help with tables
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)
- Attachments
-
- proto1.zip
- (50.27 KiB) Downloaded 249 times
- TechnoCat
- Inner party member
- Posts: 1611
- Joined: Thu Jul 30, 2009 12:31 am
- Location: Milwaukee, WI
- Contact:
Re: New to lua, need help with tables
You are just nearly there.
First, you had: You probably meant
Second, you need to draw the bullets in love.draw() now.
put this somewhere in love.draw()
First, you had:
Code: Select all
function love.keypressed(key,unicode)
if keypressed == " " then
Code: Select all
function love.keypressed(key,unicode)
if key == " " then
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
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
- TechnoCat
- Inner party member
- Posts: 1611
- Joined: Thu Jul 30, 2009 12:31 am
- Location: Milwaukee, WI
- Contact:
Re: New to lua, need help with tables
Don't forget collision detection.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
Re: New to lua, need help with tables
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?
(I've got the first method I tried commented out still)
Thanks for any help!
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
Thanks for any help!
Last edited by fylth on Thu Oct 28, 2010 9:09 am, edited 1 time in total.
Re: New to lua, need help with tables (and now collisions)
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!
@rynesaur
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 5 guests