HP Forums

Full Version: Simple Tetris. free for you to improve on
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Around 80 lines of code... not bad for an interractive game..
Among the first things that needs to be improved (appart from the graphics!) is the key handling... specificaly, the wait should be cut in sub waits with key checking to allow more movements per 'cycles'...

DoesNotFit(game, piece, p) // return true if piece does not fit
begin
local j;
if game(im(p), re(p)) then return 1; end;
for j:=1 to size(piece) do
p:= p+piece(j);
if game(im(p), re(p)) then return 1; end;
end;
return 0;
end;

EXPORT Tetris()
BEGIN
local game= makemat(J=1 or J=12 or I=1 or I=25,25,12), pause=0.200, score= 0;
local shapes= {{(1, 0), (0, 1), (-1, 0)},
{(1, 0), (0, 1), (1, 0)},
{(-1, 0), (0, 1), (-1, 0)},
{(1, 0), (1, 0), (0, 1)},
{(-1, 0), (-1, 0), (0, 1)},
{(1, 0), (1, 0), (-1, 1)},
{(1, 0), (1, 0), (1, 0)}}; // Bar

while 1 do
local piece=shapes(randint(6)+1), p=(5,2), i, j; // get a random piece

if DoesNotFit(game, piece, p) then return score; end; // test if we lose

// create background
dimgrob_p(G1, 320, 240);
dimgrob_p(G2, 320, 240);
line_p(G1,99, 0, 99, 232, #FF); line_p(G1, 99, 232, 202, 232, #FF); line_p(G1,202, 232, 202, 0, #FF);
for i:=1 to 23 do
for j:=1 to 10 do
if game(i+1,j+1) then rect_p(G1, j*10+91, i*10-9, j*10+100, i*10, #0); end;
end;
end;

while NOT DoesNotFit(game, piece, p) do
// Draw the background on back buffer
blit_p(G2, G1);
// draw the piece
i:=p;
rect_p(G2, i*10-(-81,19), i*10-(-90,10), #0);
for j:=1 to size(piece) do
i:= i+piece(j);
rect_p(G2, i*10-(-81,19), i*10-(-90,10), #0);
end;
// draw backbuffer on screen
blit_p(G2);
// pause
wait(pause);
// keys?
if IsKeyDown(37) then if NOT DoesNotFit(game, piece, p-1) then p:= p-1; end; end;
if IsKeyDown(39) then if NOT DoesNotFit(game, piece, p+1) then p:= p+1; end; end;
if IsKeyDown(33) then
local rotate= piece*(0, 1);
if NOT DoesNotFit(game, rotate, p) then piece:= rotate; end;
end;
// try to go down...
if DoesNotFit(game, piece, p+(0,1)) then break; else p:= p+(0,1); end;
end;
// put piece in game!
game(im(p), re(p)):= 1;
for j:=1 to size(piece) do
p:= p+piece(j);
game(im(p), re(p)):=1;
end;
// remove lines?
for i:=24 downto 2 do
if game(i)=[1,1,1,1,1,1,1,1,1,1,1,1] then
for j:=i-1 downto 2 do game(j+1):= game(j); end;
game(2):= [1,0,0,0,0,0,0,0,0,0,0,1];
score:= score+1;
i:= i+1;
end;
end;
// faster!
pause:= pause-0.001;
end;
END;

Edited: 20 Nov 2013, 2:54 a.m.

Is this a demo how to hang the Prime?

Trying to run it I get invalid input, and it reboots the calculator when stepping (also crashes the emulator)

J'ai une erreur entrée non valide à l'exécution.

By the way... I added txt support in my small utility, full hpprgm support, multiple file upload :) so this tetris is a perfect example for the little video... but since it does not work :/
https://www.youtube.com/watch?v=FxG-R0QZ-qI