zetris: fix rotated sprite

This commit is contained in:
Fabien Freling 2019-04-22 19:13:44 +02:00
parent f161168a29
commit 530a47a085

View file

@ -19,7 +19,7 @@ pieces = {
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 = {}
@ -27,12 +27,9 @@ function rotatedpiece()
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
@ -50,7 +47,6 @@ function rotatedpiece()
return points return points
end end
function piecedropped() function piecedropped()
piece = rotatedpiece() piece = rotatedpiece()
point = currentPiece.position point = currentPiece.position
@ -88,9 +84,11 @@ function droppiece()
end end
function resetcurrent() function resetcurrent()
currentPiece = { piece = pieces.t, currentPiece = {
piece = pieces.t,
position = {x = 11, y = 2}, position = {x = 11, y = 2},
rotation = 0 } rotation = 0
}
end end
function drawpiece() function drawpiece()
@ -100,14 +98,16 @@ function drawpiece()
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
@ -117,6 +117,13 @@ function TIC()
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