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