Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Tetris [v1.0] ]]--
- --[[
- commande pour lancer la partie : !Tetris L H
- (L et H sont respectivement la longueur et la hauteur du terrain (15 et 25 par défaut)
- ]]--
- --------------------------------------
- -- Évitez de toucher à ce qui suit --
- --------------------------------------
- ig = false
- grid = {}
- pos = {}
- color = { "#00f0ef", "#0000f0", "#f09f00", "#f1f000", "#01ef00", "#9e00f0", "#f00001" }
- function new_game(name, l, h)
- L, H, admin = l, h, name
- cen = math.ceil(L/2)
- dec_x = math.floor((80-L)*5)
- grid = {}
- for x=1,L do
- grid[x] = {}
- for y=1,H do
- grid[x][y] = {"empty"}
- end
- end
- tfm.exec.bindKeyboard(admin,37, true, true)
- tfm.exec.bindKeyboard(admin,38, true, true)
- tfm.exec.bindKeyboard(admin,39, true, true)
- tfm.exec.bindKeyboard(admin,40, true, true)
- score = 0
- ui.addTextArea(-1, "", nil, dec_x+10, 30, 10*L, 10*(H+1), 1, 1, 1, true)
- ui.addTextArea(-2, "<font color='#ffffff'>Score : 0</font>", nil, dec_x+10, 30, 100, 20, 1, 1, 0, true)
- ui.addTextArea(-3, "", nil, dec_x+10*(L-5), 30, 60, 40, 1, 1, 0, true)
- current_p, next_p = math.random(7), math.random(7)
- place(current_p)
- show(next_p)
- end
- function eventLoop(past, left)
- if ig then
- if possible(0,1) then
- move(0,1)
- else
- find_line(H)
- current_p = next_p
- place(current_p)
- next_p = math.random(7)
- show(next_p)
- end
- end
- end
- function eventKeyboard(name, key, down)
- if key == 37 then
- if possible(-1,0) then move(-1,0) end
- elseif key == 39 then
- if possible(1,0) then move(1,0) end
- elseif key == 40 then
- if possible(0,1) then move(0,1) end
- elseif key == 38 then
- rotation()
- end
- end
- function show(piece)
- local str = ""
- if piece ==1 then str = "■■■■"
- elseif piece == 2 then str = "■■■\n ■"
- elseif piece == 3 then str = "■■■\n■"
- elseif piece == 4 then str = "■■\n■■"
- elseif piece == 5 then str = " ■■\n■■"
- elseif piece == 6 then str = "■■■\n ■"
- elseif piece == 7 then str = "■■\n ■■"
- end
- ui.updateTextArea(-3, "<font color='"..color[next_p].."'>"..str.."</font")
- end
- function move(x,y)
- for _, val in ipairs(pos) do
- ui.removeTextArea(L*val.y+val.x)
- grid[val.x][val.y] = {"empty"}
- end
- for key, val in ipairs(pos) do
- pos[key] = {x=val.x+x, y=val.y+y}
- ui.addTextArea(L*(val.y+y)+(val.x+x), "<font color='"..color[current_p].."'>■</font>", nil, dec_x+10*(val.x+x), 30+10*(val.y+y), 20, 20, 1, 1, 0, true)
- grid[val.x+x][val.y+y] = {"moving", current_p}
- end
- end
- function possible(x,y)
- for _,coord in ipairs(pos) do
- if coord.x+x > L or coord.x+x < 1 or coord.y+y > H or grid[coord.x+x][coord.y+y][1] == "fixed" then
- if x==0 and y==1 then
- for _, val in pairs(pos) do
- grid[val.x][val.y] = {"fixed", current_p}
- end
- end
- return false
- end
- end
- return true
- end
- function find_line(line)
- local complet = true
- for i=1, L do
- if grid[i][line][1] ~= "fixed" then
- complet = false
- break
- end
- end
- if complet then
- local delete = ui.removeTextArea
- local show = ui.addTextArea
- for x=1, L do
- if grid[x][line][1] ~= "empty" then
- delete(x+L*line)
- grid[x][line] = {"empty"}
- end
- end
- for l=line-1, 1, -1 do
- for x=1, L do
- if grid[x][l][1] ~= "empty" then
- show(x+(l+1)*L, "<font color='"..color[grid[x][l][2]].."'>■</font>", nil, dec_x+10*x, 30+10*(l+1), 20, 20, 1, 1, 0, true)
- grid[x][l+1] = {grid[x][l][1], grid[x][l][2]}
- delete(x+L*l)
- grid[x][l][1] = "empty"
- end
- end
- end
- score = score + L
- ui.updateTextArea(-2, "<font color='#ffffff'>Score : "..score.."</font>")
- find_line(line)
- elseif line > 1 then
- find_line(line-1)
- end
- end
- function place(piece)
- if piece == 1 then
- pos = {{x=cen-1,y=1}, {x=cen,y=1}, {x=cen+1,y=1}, {x=cen+2,y=1}}
- elseif piece == 2 then
- pos = {{x=cen-1,y=1}, {x=cen,y=1}, {x=cen+1,y=1}, {x=cen+1,y=2}}
- elseif piece == 3 then
- pos = {{x=cen-1,y=1}, {x=cen,y=1}, {x=cen+1,y=1}, {x=cen-1,y=2}}
- elseif piece == 4 then
- pos = {{x=cen,y=2}, {x=cen,y=1}, {x=cen+1,y=1}, {x=cen+1,y=2}}
- elseif piece == 5 then
- pos = {{x=cen,y=2}, {x=cen,y=1}, {x=cen-1,y=2}, {x=cen+1,y=1}}
- elseif piece == 6 then
- pos = {{x=cen-1,y=1}, {x=cen,y=1}, {x=cen+1,y=1}, {x=cen,y=2}}
- elseif piece == 7 then
- pos = {{x=cen-1,y=1}, {x=cen,y=1}, {x=cen+1,y=2}, {x=cen,y=2}}
- end
- end
- function rotation()
- local x, y = pos[2].x, pos[2].y
- local poss = true
- for _,v in ipairs(pos) do
- if not free_pos(x-(y-v.y), y+(x-v.x))
- then poss = false
- break
- end
- end
- if poss then
- local delete = ui.removeTextArea
- local show = ui.addTextArea
- for key, val in ipairs(pos) do
- grid[val.x][val.y] = {"empty"}
- delete(val.y*L+val.x)
- local a, b = x-(y-val.y), y+(x-val.x)
- pos[key] = {x=a,y=b}
- end
- for _,val in ipairs(pos) do
- grid[val.x][val.y] = {"moving",current_p}
- show(val.x+val.y*L, "<font color='"..color[current_p].."'>■</font>", nil, dec_x+10*val.x, 30+10*val.y, 20, 20, 1, 1, 0, true)
- end
- end
- end
- function free_pos(x, y)
- if x>0 and x<=L and y<=H and y>0 and grid[x][y][1] ~= "fixed" then
- return true
- else
- return false
- end
- end
- function eventChatCommand(name, command)
- if not ig then
- local lw = {}
- for w in command:gmatch('%S+') do table.insert(lw, w) end
- if lw[1]=="Tetris" or lw[1] == "tetris" then
- new_game(name, tonumber(lw[2]) or 15, tonumber(lw[3]) or 25)
- end
- ig = true
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement