added tetris (WIP)

This commit is contained in:
2021-12-12 13:09:22 +01:00
parent 3b7dd252ab
commit f11adb0557

175
demos/tetris.lua Normal file
View File

@@ -0,0 +1,175 @@
--struct Tetromino { Uint16 figure; Uint8 orig, prev, next; };
tetromino = { {figure=0x0660, orig=0, prev=0, next=0}, {figure=0x4444, orig=1, prev=2, next=2}, {figure=0x0F00, orig=1, prev=1, next=1}, {figure=0x0C60, orig=2, prev=4, next=4}, {figure=0x2640, orig=2, prev=3, next=3}, {figure=0x06C0, orig=3, prev=6, next=6}, {figure=0x4620, orig=3, prev=5, next=5}, {figure=0x4460, orig=4, prev=8, next=10}, {figure=0x2E00, orig=4, prev=9, next=7}, {figure=0xC440, orig=4, prev=10, next=8}, {figure=0x0E80, orig=4, prev=7, next=9}, {figure=0x44C0, orig=5, prev=12, next=14}, {figure=0x0E20, orig=5, prev=13, next=11}, {figure=0x6440, orig=5, prev=14, next=12}, {figure=0x8E00, orig=5, prev=11, next=13}, {figure=0x4640, orig=6, prev=16, next=18}, {figure=0x4E00, orig=6, prev=17, next=15}, {figure=0x4C40, orig=6, prev=18, next=16}, {figure=0x0E40, orig=6, prev=15, next=17} }
starting = { 0, 1, 3, 5, 7, 11, 15 }
papers = { COLOR_LIGHT_RED, COLOR_LIGHT_GREEN, COLOR_LIGHT_BLUE, COLOR_YELLOW, COLOR_LIGHT_CYAN, COLOR_LIGHT_MAGENTA, COLOR_BROWN, COLOR_WHITE }
inks = { COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_BROWN, COLOR_CYAN, COLOR_MAGENTA, COLOR_RED, COLOR_LIGHT_GRAY }
--colors = {0x4c, 0x2a, 0x19, 0x6e, 0x3b, 0xd5, 0x56, 0x7f}
--Uint8 colors[8][3] { {255, 0, 0}, {0, 255, 0}, {0, 0, 255}, {255, 255, 0}, {0, 255, 255}, {255, 0, 255}, {255, 128, 0}, {255, 255, 255} };
board = {} --[20][10];
piece_pos = {x=3, y=0}
current_piece,next_piece,level,lines,score = 0,0,0,0,0
speed = (20-min(19, level))*3
function is_valid_move()
local x = piece_pos.x+3
local y = piece_pos.y+3
local piece = tetromino[current_piece+1].figure
for i=0,15 do
if ((piece & 1 == 1) and ( (x >= 10) or (x < 0) or (y >= 20) or (board[x+y*10] ~= 0) ) ) then return false end
piece = piece >> 1
x=x-1
if x < piece_pos.x then
x=piece_pos.x+3
y=y-1
end
end
return true
end
function check_lines()
local count = 0
local line = 19
while line > 0 do
local complete = true
for x=0,9 do
complete = (board[x+line*10] ~= 0)
if not complete then break end
end
if complete then
count=count+1
for y=line,1,-1 do
for x=0,9 do
board[x+y*10] = board[x+(y-1)*10]
end
end
else
line=line-1
end
end
lines=lines+count
local scoremult = {40,100,300,1200}
if count > 0 then score = score + scoremult[count] * (level+1) end
level = flr(lines / 10)
end
function fix_piece()
local x = piece_pos.x+3
local y = piece_pos.y+3
local piece = tetromino[current_piece+1].figure
for i=0,15 do
if piece & 1 == 1 then
board[x+y*10] = tetromino[current_piece+1].orig+1
end
piece = piece >> 1
x=x-1
if x < piece_pos.x then
x = piece_pos.x+3
y=y-1
end
end
piece_pos = {x=3, y=0}
current_piece = next_piece
next_piece = starting[rnd(7)+1]
check_lines()
end
function draw_cube(x,y,col)
color(inks[col+1],papers[col+1])
print("\001",x+1,y)
end
function draw_tetromino(init_x,init_y,piece_to_draw)
local x = init_x+3
local y = init_y+3
local piece = tetromino[piece_to_draw+1].figure
for i=0,15 do
if piece & 1 == 1 then
draw_cube(x, y, tetromino[piece_to_draw+1].orig)
end
piece = piece >> 1
x=x-1
if x < init_x then
x = init_x+3
y=y-1
end
end
end
--[[
void print(int x, int y, const char* text, Uint8 color) {
int cc = 0;
SDL_SetTextureColorMod(sdlTexture, colors[color][0], colors[color][1], colors[color][2]);
SDL_Rect src {0, 0, 8, 8}, dst {x, y, 16, 16};
while (text[cc] != 0) {
if (text[cc] == 32) continue;
else if (text[cc] >= 65) { src.x = ((text[cc]-65)%6)*8; src.y = ((text[cc]-65)/6)*8; }
else if (text[cc] < 65) { src.x = ((text[cc]-22)%6)*8; src.y = ((text[cc]-22)/6)*8; }
SDL_RenderCopy(sdlRenderer, sdlTexture, &src, &dst);
cc++; dst.x+=16;
}
}
--]]
function init()
setmode(1)
current_piece = starting[rnd(7)+1]
next_piece = starting[rnd(7)+1]
piece_pos = {x=3, y=0}
level,lines,score = 0,0,0
speed = (20-min(19, level))*3
for i=0,20*10 do board[i] = 0 end
setchar(1,0xff,0x81,0x81,0x81,0x81,0x81,0x81,0xff)
border(COLOR_BLUE)
end
function update()
if btnp(KEY_RIGHT) then
piece_pos.x = piece_pos.x + 1
if not is_valid_move() then piece_pos.x=piece_pos.x-1 end
end
if btnp(KEY_LEFT) then
piece_pos.x = piece_pos.x - 1
if not is_valid_move() then piece_pos.x=piece_pos.x+1 end
end
if btnp(KEY_DOWN) then
piece_pos.y = piece_pos.y + 1
if not is_valid_move() then piece_pos.y=piece_pos.y-1 end
end
if btnp(KEY_UP) then
current_piece = tetromino[current_piece+1].next
if not is_valid_move() then current_piece = tetromino[current_piece+1].prev end
end
paper(COLOR_BLACK)
cls()
ink(COLOR_BLUE) print("LEVEL "..tostr(level), 3, 5) -- color 2
ink(COLOR_RED) print("SCORE "..tostr(score), 3, 7) -- color 0
ink(COLOR_GREEN) print("LINES "..tostr(lines), 3, 9) -- color 1
color(COLOR_DARK_GRAY,COLOR_LIGHT_GRAY)
for i=0,20 do print ("\001",14,i+5) print("\001",25,i+5) end
print("\001\001\001\001\001\001\001\001\001\001\001\001",14,25)
for y=0,19 do
for x=0,9 do
if board[x+y*10] ~= 0 then
draw_cube(14+x, y+5, board[x+y*10]-1)
end
end
end
draw_tetromino(27, 5, next_piece)
draw_tetromino(14+piece_pos.x, 5+piece_pos.y, current_piece)
speed=speed-1
if speed == 0 then
speed = (20-min(19, level))*3
piece_pos.y=piece_pos.y+1
if not is_valid_move() then
piece_pos.y=piece_pos.y-1
if piece_pos.y==0 then
init()
else
fix_piece()
play("c")
end
end
end
end