Row of enemies

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
jvetvicka
Prole
Posts: 1
Joined: Sat Jul 20, 2019 12:41 pm

Row of enemies

Post by jvetvicka » Sat Jul 20, 2019 12:59 pm

Hi,

i am begginer at Lua and Love2D and i need help. I would like to display 4 rows of enemies (like Space invaders), but i dont know how to do (simply). Thx for advice

Code: Select all

function love.draw()
  for i=1, #colors do
    if i <= 3 then
      rectangle.x = i*35 + (155 * (i-1))
      rectangle.y = 35
    elseif i > 3 and i <= 6 then
      s = i - 3
      rectangle.x = s*35 + (155 * (s-1))
      rectangle.y = 225
    elseif i > 6 and i <= 9 then
      t = i - 6
      rectangle.x = t*35 + (155 * (t-1))
      rectangle.y = 415
    else
      f = i - 9
      rectangle.x = f*35 + (155 * (f-1))
      rectangle.y = 605
    end
    love.graphics.setColor(colors[i])
    love.graphics.rectangle("fill", rectangle.x, rectangle.y, 155, 155)
  end
end

User avatar
unixfreak
Citizen
Posts: 77
Joined: Thu Oct 15, 2015 6:25 am
Location: Bristol, UK
Contact:

Re: Row of enemies

Post by unixfreak » Mon Jul 22, 2019 8:03 pm

You'll want to loop over some rows and columns, which place an enemy into a table. That way you can loop over the table easily;

One way to setup a grid of enemies is something like this;

Code: Select all

function love.load()
	enemies = {}
	enemies.w = 50        -- width of enemy
	enemies.h = 30        -- height of enemy
	enemies.rows = 4      -- total enemies per row
	enemies.columns = 3   -- total enemies per column
	enemies.gap = 10      -- space between each enemy
	
	local x = 1
	local y = 1
	
	local wave = {}
	-- calculate the total size (width/height) of the grid of enemies when populated
	wave.w = enemies.rows*(enemies.h+enemies.gap)
	wave.h = enemies.columns*(enemies.w+enemies.gap)
	
	-- loop over the columns one at a time (top to bottom)
	for y = 1, wave.w, enemies.h+enemies.gap do	
	
		-- loop over the rows one at a time (left to right)
		for x = 1, wave.h, enemies.w+enemies.gap do
		
			-- add an enemy 
			table.insert(enemies, {
				x = x + love.graphics.getWidth()/2-wave.w/2, -- center the enemies relative position on screen
				y = y + enemies.gap, -- add a gap from top of the screen
			})
			
			-- move to the next column
			x = x + enemies.w + enemies.gap
		end
	
		-- move to the next row
		y = y  + enemies.h
		
	end
	
end
That way, all your enemies exist in enemies table, and can be looped over easily like so, which is helpful for everything from collision detection or even drawing:

Code: Select all

function love.draw()
	-- draw the enemies by looping over the table
	love.graphics.setColor(1,0,0,1)	
	for i, e in ipairs(enemies) do
		love.graphics.rectangle("fill", e.x,e.y,enemies.w,enemies.h)
	end	
end
Or checking collision, etc:

Code: Select all

function love.update(dt)
	for i,e in ipairs(enemies) do
		checkcollision(e, missile)
	end
end

JJSax
Prole
Posts: 28
Joined: Fri Apr 04, 2014 3:59 am

Re: Row of enemies

Post by JJSax » Mon Jul 22, 2019 8:09 pm

There are a few variables in your provided code that aren't defined here. So I'll have to make some assumptions on what you are needing. Let me know if I misunderstood anything.

this is what I came up with for a incredibly basic code to just draw a block of images. While I was about to work on a more comprehensive one, unixfreak sniped me. His code looks good for what most people need when they make multiple images like that.

Code: Select all

function love.load()
    spookyGhost = love.graphics.newImage("A-plusImage.png")
    ghostColumns = 10
    ghostRows = 4
end

function love.draw()
    for ix = 1, ghostColumns*50, 50 do
        for iy = 1, ghostRows*50, 50 do
            -- you could substitute this for any draw function, the 50 used is to define the image dimensions.
            love.graphics.draw(spookyGhost, ix+ix/20, iy)
        end 
    end
end

Post Reply

Who is online

Users browsing this forum: steVeRoll and 6 guests