Congratulations!
Now for some
RANDOM TIPS!
For a more responsive feel for moving and rotating pieces, I'd suggest removing the following lines...
Code: Select all
if love.keyboard.isDown("left") then
moveLeft()
elseif love.keyboard.isDown("right") then
moveRight()
elseif love.keyboard.isDown(" ") then
rotate()
end
... and instead, using the
love.keypressed callback like this:
Code: Select all
function love.keypressed(key)
if key == "left" then
moveLeft()
elseif key == "right" then
moveRight()
elseif key == " " then
rotate()
end
end
Instead of returning 1 from
lockDown, I'd return
true. That way, if I forgot how
lockDown works, I wouldn't get confused about what 1 actually meant. Also, I could write
if not lockDown() then which reads really nicely (you could also replace
if loose == false then with
if not loose then.)
Also, (unless I'm mistaken), in Lua, everything which isn't
nil or
false is a true condition, so instead of...
Code: Select all
while pieceGrid[rotation][i] ~= nil do
you could write...
For slightly more accurate timing, I think, you could replace
timer = 0 with
timer = timer - 0.2.
Using variables instead of literal numbers can make things more readable and flexible. For example, instead of using
0.20, you could use
fallTime or something, or maybe
BLOCK_SIZE instead of
32 (the capital letters being a convention for global variables which aren't expected to change).
You might want to consider stringing together conditions with
and:
Code: Select all
if Piece == "I" then
if (Y == 19 or grid[Y+4][X+2] ~= " ") then
fillIn()
return 1
end
... could be written like:
Code: Select all
if Piece == "I" and (Y == 19 or grid[Y+4][X+2] ~= " ") then
fillIn()
return 1
And also:
Code: Select all
if rotation == 1 then
if Piece == "I" then
if (Y == 21 or grid[Y+2][X] ~= " " or grid[Y+2][X+1]~= " " or grid[Y+2][X+2]~=" " or grid[Y+2][X+3]~=" ") then
fillIn()
return 1
end
elseif Piece == "J" then
if (Y == 21 or grid[Y+2][X] ~= " " or grid[Y+2][X+1] ~= " " or grid[Y+2][X+2] ~= " ") then
fillIn()
return 1
end
-- etc.
... could be written like:
Code: Select all
if rotation == 1 then
if Piece == "I" and (Y == 21 or grid[Y+2][X] ~= " " or grid[Y+2][X+1] ~= " " or grid[Y+2][X+2] ~= " " or grid[Y+2][X+3]~=" ") or
Piece == "J" and (Y == 21 or grid[Y+2][X] ~= " " or grid[Y+2][X+1] ~= " " or grid[Y+2][X+2] ~= " ") or
Piece == "L" and (Y == 21 or grid[Y+2][X] ~= " " or grid[Y+2][X+1] ~= " " or grid[Y+2][X+2] ~= " ") or
Piece == "O" and (Y == 21 or grid[Y+2][X] ~= " " or grid[Y+2][X+1] ~= " ") or
Piece == "S" and (Y == 21 or grid[Y+2][X] ~= " " or grid[Y+2][X+1] ~= " " or grid[Y+1][X+2] ~= " ") or
Piece == "T" and (Y == 21 or grid[Y+2][X] ~= " " or grid[Y+2][X+1] ~= " " or grid[Y+2][X+2] ~= " ") or
Piece == "Z" and (Y == 21 or grid[Y+1][X] ~= " " or grid[Y+2][X+1] ~= " " or grid[Y+2][X+2] ~= " ") then
fillIn()
return 1
end
-- etc.
Another way to write this bit:
Code: Select all
for j = 1, #pieceGrid[rotation][i] do
local c = pieceGrid[rotation][i]:sub(j,j)
if c == "O" then
love.graphics.drawq(blocks,orange,(X+j-2)*32,(Y+i-4)*32)
elseif c == "Y" then
love.graphics.drawq(blocks,yellow,(X+j-2)*32,(Y+i-4)*32)
elseif c == "B" then
love.graphics.drawq(blocks,blue,(X+j-2)*32,(Y+i-4)*32)
-- etc.
end
end
... could be like this:
Code: Select all
for j = 1, #pieceGrid[rotation][i] do
local c = pieceGrid[rotation][i]:sub(j,j)
if c ~= " " then
local color = ({
O = orange,
Y = yellow,
B = blue,
G = green,
C = cyan,
P = purple,
R = red,
})[c]
love.graphics.drawq(blocks,color,(X+j-2)*32,(Y+i-4)*32)
end
end
({"This works because you can access elements of literal tables like this, using parentheses."})[1]
Using this, you could write:
Code: Select all
GeneratorDefault = {"I","J","L","O","S","T","Z"}
generator = math.random(1,7)
Piece = GeneratorDefault[generator]
as...
Code: Select all
Piece = ({"I","J","L","O","S","T","Z"})[math.random(1,7)]
(If the above code made absolutely no sense, don't worry.
)
Here is some help from the future!
When looping through tables and removing elements (such as bullets from a tables of bullets), consider using a "reverse" numeric
for loop instead of
ipairs (apparently using
ipairs and removing elements can cause
problems.).
Code: Select all
for i = #bullets, 1, -1 do
if bullets[i]:isOffscreen() then
table.remove(bullets, i)
end
end
Or you may not even do it like this at all.
Oh, and, also...
http://www.lua.org/about.html wrote:"Lua" (pronounced LOO-ah) means "Moon" in Portuguese. As such, it is neither an acronym nor an abbreviation, but a noun. More specifically, "Lua" is a name, the name of the Earth's moon and the name of the language.
So "Lua" (uncapitalised) is the preferred spelling. Fun facts!
I hope this helps!