zetris: fix rotated sprite

master
Fabien Freling 2019-04-22 19:13:44 +02:00
parent f161168a29
commit 530a47a085
1 changed files with 49 additions and 42 deletions

View File

@ -8,54 +8,50 @@ bgspriteof=-240
pieces = { pieces = {
t={sprite=256, t={sprite=256,
{x=0, y=0}, {x=0, y=0},
{x=-1, y=0}, {x=-1, y=0},
{x=1, y=0}, {x=1, y=0},
{x=0, y=-1} {x=0, y=-1}
} }
} }
-- GAME STATE -- GAME STATE
currentPiece = { piece = pieces.t, currentPiece = { piece = pieces.t,
position = {x = 11, y = 2}, position = {x = 11, y = 2},
rotation = 0 rotation = 0
} }
function rotatedpiece() function rotatedpiece()
points = {} points = {}
if currentPiece.rotation == 0 then if currentPiece.rotation == 0 then
return currentPiece.piece return currentPiece.piece
elseif currentPiece.rotation == 1 then elseif currentPiece.rotation == 1 then
for i=1, 4 do for i=1, 4 do
points[i] = {x=currentPiece.piece[i].x, points[i] = {x=0,y=0}
y=currentPiece.piece[i].y} points[i].x = currentPiece.piece[i].y * -1
--points[i].x = currentPiece.piece[i].y * -1 points[i].y = currentPiece.piece[i].x
--points[i].y = currentPiece.piece[i].x
points[i].x = currentPiece.piece[i].x
points[i].y = currentPiece.piece[i].y
end end
elseif currentPiece.rotation == 2 then elseif currentPiece.rotation == 2 then
for i=1, 4 do for i=1, 4 do
points[i] = {x=0,y=0} points[i] = {x=0,y=0}
points[i].x = currentPiece.piece[i].x * -1 points[i].x = currentPiece.piece[i].x * -1
points[i].y = currentPiece.piece[i].y * -1 points[i].y = currentPiece.piece[i].y * -1
end end
elseif currentPiece.rotation == 3 then elseif currentPiece.rotation == 3 then
for i=1, 4 do for i=1, 4 do
points[i] = {x=0,y=0} points[i] = {x=0,y=0}
points[i].x = currentPiece.piece[i].y points[i].x = currentPiece.piece[i].y
points[i].y = currentPiece.piece[i].x * -1 points[i].y = currentPiece.piece[i].x * -1
end end
end end
return points return points
end end
function piecedropped() function piecedropped()
piece = rotatedpiece() piece = rotatedpiece()
point = currentPiece.position point = currentPiece.position
for i=1, 4 do for i=1, 4 do
x=(point.x + piece[i].x) x=(point.x + piece[i].x)
y=(point.y + piece[i].y) y=(point.y + piece[i].y)
if mget(x, y+1) ~= 0 then if mget(x, y+1) ~= 0 then
return true return true
@ -77,7 +73,7 @@ function canmove(point)
end end
function droppiece() function droppiece()
piece = rotatedpiece() piece = rotatedpiece()
point = currentPiece.position point = currentPiece.position
sprite = piece.sprite + bgspriteof sprite = piece.sprite + bgspriteof
for i=1, 4 do for i=1, 4 do
@ -88,41 +84,52 @@ function droppiece()
end end
function resetcurrent() function resetcurrent()
currentPiece = { piece = pieces.t, currentPiece = {
position = {x = 11, y = 2}, piece = pieces.t,
rotation = 0 } position = {x = 11, y = 2},
rotation = 0
}
end end
function drawpiece() function drawpiece()
piece = rotatedpiece() piece = rotatedpiece()
origin = currentPiece.position origin = currentPiece.position
for i = 1, 4 do for i = 1, 4 do
point = { x = (piece[i].x + origin.x) * blocksize, point = { x = (piece[i].x + origin.x) * blocksize,
y = (piece[i].y + origin.y) * blocksize} y = (piece[i].y + origin.y) * blocksize}
spr(piece.sprite, point.x, point.y) spr(currentPiece.piece.sprite, point.x, point.y)
end end
end end
function TIC() function TIC()
map(0,0,30,17) map(0,0,30,17)
local newPosition = {x = currentPiece.position.x, local newPosition = {
y = currentPiece.position.y} x = currentPiece.position.x,
y = currentPiece.position.y
}
if btnp(2) then if btnp(2) then
newPosition.x = newPosition.x - 1 newPosition.x = newPosition.x - 1
elseif btnp(3) then elseif btnp(3) then
newPosition.x = newPosition.x + 1 newPosition.x = newPosition.x + 1
elseif btnp(1) then elseif btnp(1) then
newPosition.y = newPosition.y + 1 newPosition.y = newPosition.y + 1
elseif btnp(4) then elseif btnp(4) then
r = currentPiece.rotation r = currentPiece.rotation
currentPiece.rotation = (r+1)%4 currentPiece.rotation = (r+1)%4
rpiece = rotatedpiece()
trace('position: '..currentPiece.position.x..', '..currentPiece.position.y)
trace('sprite: '..currentPiece.piece.sprite)
trace('rotation: '..currentPiece.rotation)
for i = 1, 4 do
trace(rpiece[i].x..', '..rpiece[i].y)
end
end end
if canmove(newPosition) then if canmove(newPosition) then
currentPiece.position = newPosition currentPiece.position = newPosition
elseif piecedropped() then elseif piecedropped() then
droppiece() droppiece()
resetcurrent() resetcurrent()
end end