Space Invaders Barrier

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
Sosa Montana
Prole
Posts: 4
Joined: Mon Aug 17, 2015 11:35 pm

Space Invaders Barrier

Post by Sosa Montana »

I need help implementing the destruction of the barrier in space invaders. I can't even figure out where to start. I don't know if you need it, but I attached a .love file anyway.

Thanks a lot for the help
Attachments
space invaders.love
(19.23 KiB) Downloaded 126 times
pel
Prole
Posts: 28
Joined: Mon Aug 17, 2015 6:48 am

Re: Space Invaders Barrier

Post by pel »

You could draw the barriers as a collection of pixels and have collision detection on each. When one is hit, you remove it. I would be fun to see the code.
Sosa Montana
Prole
Posts: 4
Joined: Mon Aug 17, 2015 11:35 pm

Re: Space Invaders Barrier

Post by Sosa Montana »

Would I use love.graphics.newImageData to achieve that? And there's a .love file attached so feel free to do what ever you want with it
pel
Prole
Posts: 28
Joined: Mon Aug 17, 2015 6:48 am

Re: Space Invaders Barrier

Post by pel »

Here's a first few steps in the right direction.

In your main.lua add this function under your check collision function.

Code: Select all

function isPointInAxisParallelRectangle(x, y, x1, y1, x2, y2)
    return x > x1 and x < x2 and y > y1 and y < y2
end
Then add this code inside your love.update(dt) function. I added it just before your Check Player Bullet Enemy Collision routine.

Code: Select all

for j,b in ipairs(player.bullets) do
    if isPointInAxisParallelRectangle(b.x, b.y, barrier.x1, barrier.y, barrier.x1 + 44, barrier.y + 32) then
        barrier.imageData = barrier.image:getData()
        local x = b.x - barrier.x1
        local y = b.y - barrier.y
        local r, g, b, a = barrier.imageData:getPixel(x,y)
        if a > 0 then
            barrier.imageData:setPixel(x, y, r, g, b, 0)
            barrier.image:refresh()
            table.remove(player.bullets, j)
        end
    end
end
This will only work on your first barrier. You'll have to modify your barriers.lua file in order to store all your barriers in a table, and have each barrier have a different copy of the image file. Then you can iterate over your barriers one by one. I'm guessing you know how to do that.

What I did here is check if the player's bullet is inside the barrier bounds. The first collision check. If it is, then I get the position of the bullet (x, and y) and check to see if the alpha is not null on the barrier at that location. If it isn't null, then I count it as a hit. I then remove the bullet, and change the pixel alpha to 0 to hide the pixel that got hit.

This is really just a quick first attempt. As you'll see, it's buggy. Hopefully, it can help you find the correct way.

I'm guessing the game doesn't update fast enough, so sometimes the bullet passes through some pixels. Or the x, and y of the bullet I am getting aren't good. Not sure.

Also, you might want to remove more than one pixel when it gets hit. Perhaps you could remove more, like some around the one that got hit, etc.. Lots of refinement here. Like bigger injury to surface from bigger bullets, etc...

Anyhow, I hope this helps a bit.
Sosa Montana
Prole
Posts: 4
Joined: Mon Aug 17, 2015 11:35 pm

Re: Space Invaders Barrier

Post by Sosa Montana »

Thanks so much man I really appreciate it. I'm going to try that out and then I'll let you know how it goes. Thanks again
Post Reply

Who is online

Users browsing this forum: slime and 0 guests