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

Row of enemies

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


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
      f = i - 9
      rectangle.x = f*35 + (155 * (f-1))
      rectangle.y = 605
    end[i])"fill", rectangle.x, rectangle.y, 155, 155)

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

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 = 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*(
	wave.h = enemies.columns*(
	-- loop over the columns one at a time (top to bottom)
	for y = 1, wave.w, do	
		-- loop over the rows one at a time (left to right)
		for x = 1, wave.h, do
			-- add an enemy 
			table.insert(enemies, {
				x = x +, -- center the enemies relative position on screen
				y = y +, -- add a gap from top of the screen
			-- move to the next column
			x = x + enemies.w +
		-- move to the next row
		y = y  + enemies.h
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,0,0,1)	
	for i, e in ipairs(enemies) do"fill", e.x,e.y,enemies.w,enemies.h)
Or checking collision, etc:

Code: Select all

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

Posts: 36
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 ="A-plusImage.png")
    ghostColumns = 10
    ghostRows = 4

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.
  , ix+ix/20, iy)

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests