New to lua, need help with tables (and now collisions)

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.
User avatar
fylth
Prole
Posts: 7
Joined: Wed Oct 13, 2010 12:27 pm

New to lua, need help with tables (and now collisions)

Post 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)
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.
User avatar
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

Post 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.
User avatar
Thursdaybloom
Citizen
Posts: 81
Joined: Mon Feb 15, 2010 3:43 am
Location: Australia

Re: New to lua, need help with tables

Post 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 :P

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
User avatar
fylth
Prole
Posts: 7
Joined: Wed Oct 13, 2010 12:27 pm

Re: New to lua, need help with tables

Post 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
User avatar
fylth
Prole
Posts: 7
Joined: Wed Oct 13, 2010 12:27 pm

Re: New to lua, need help with tables

Post 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)
Attachments
proto1.zip
(50.27 KiB) Downloaded 249 times
User avatar
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

Post 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
User avatar
fylth
Prole
Posts: 7
Joined: Wed Oct 13, 2010 12:27 pm

Re: New to lua, need help with tables

Post 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
User avatar
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

Post 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.
User avatar
fylth
Prole
Posts: 7
Joined: Wed Oct 13, 2010 12:27 pm

Re: New to lua, need help with tables

Post 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!
Last edited by fylth on Thu Oct 28, 2010 9:09 am, edited 1 time in total.
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: New to lua, need help with tables (and now collisions)

Post 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!
@rynesaur
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 5 guests