Page 1 of 1

help creating a background with for i statement [closed]

Posted: Thu Nov 21, 2019 4:45 pm
by bobbymcbobface
So here's the problem - i need help creating a certain amount of squares to fit my screen width + screen height

some helpful info -
1 in gridXCount/gridYCount is equal to 8 squares of the background
so if gridYCount and gridXCount is equal to 10 then there would 6400 squares in total across the screen

i kind of have a solution using tables but it seems to just make the game stop working (freeze)
Game.love
(14.77 KiB) Downloaded 137 times

Re: help creating a background with for i statement

Posted: Thu Nov 21, 2019 9:20 pm
by zorg
I figure the issue is lines 116-133 in Game.lua, but i'm not completely sure what part you have problems with.
Do you just want to calculate how many squares fit inside an area the size of the window's dimensions? If so, then the solution is simple:

Code: Select all

local width, height = love.graphics.getDimensions()
local gridSquareSize = 8
local squareColumns, squareRows = math.floor(width / gridSquareSize), math.floor(height / gridSquareSize)
The above assumes that i didn't misunderstand your terminology about grids being 8 squares of the background. i assumed you meant pixels by squares; if that's wrong, then just multiply the gridSquareSize by however many pixels you have in a background square.

Not sure why it'd freeze though, haven't looked too deeply at the code.
It's kinda worrying though that apparently schools don't teach kids how division works; 6400/8 is exactly 64/8 * 100, (or 2^6 / 2^3 * 100) which is (2^3 * 100 or) 8 * 100 or 800...

Re: help creating a background with for i statement

Posted: Fri Nov 22, 2019 2:32 pm
by bobbymcbobface
Thanks I'll check this out when I get the chance and get back to you if needed :)

Re: help creating a background with for i statement

Posted: Sat Nov 23, 2019 1:57 pm
by bobbymcbobface
ok so i managed to implement your idea exactly like i wanted but there's one problem - it's laggy as hell i'm guessing it's because i'm drawing a ton of squares is there a work around for this? - i'm aiming for a background similar to Snayke the love2d version of snake here's the snippet of code i'm using

Code: Select all

for a = 1, squareRows do
	for b = 1, squareColumns do
 		--print(A)
		table.insert(Background.Segments, 1, {x = a, y =  b})
		drawCell(a, b)
		A = A + 1
		print(i)
	end
end

Re: help creating a background with for i statement

Posted: Sat Nov 23, 2019 7:03 pm
by zorg
Is that your creation code, your draw code, or both? Because i'd set up the segments first, then only draw the cells in love.draw as to not create tons of segments each frame... the print statement might also slow things down.

Re: help creating a background with for i statement

Posted: Sat Nov 30, 2019 11:35 am
by bobbymcbobface
--atachment removed--
Ok I managed to fix the drawing of the background but for some reason the background isn't resizing when changing the screen size (in settings) to full screen - can you help please?

thanks