×

Langue

Fermer
Atelier 801
  • Forums
  • Dev Tracker
  • Connexion
    • English Français
      Português do Brasil Español
      Türkçe Polski
      Magyar Română
      العربية Skandinavisk
      Nederlands Deutsch
      Bahasa Indonesia Русский
      中文 Filipino
      Lietuvių kalba 日本語
      Suomi עברית
      Italiano Česky
      Hrvatski Slovensky
      Български Latviešu
      Estonian
  • Langue
  • Forums
  • /
  • Transformice
  • /
  • Modules
  • /
  • [script] Tetris
[script] Tetris
Haku
« Sénateur »
1407601740000
    • Haku#0807
    • Profil
    • Derniers messages
#1
  1

Tetris
Example script


A simple implementation of the classical puzzle game Tetris. Please note that this is not a full minigame and is just meant as an experimental example.

Some features:
  • Random bag system used in official games
  • Wallkicks system used in official games
  • Hard drop with the I key
  • Up to 3 players, they're randomly chosen (I think)

Controls:
  • I J K L to move the pieces
  • SPACE to rotate

Screenie:
http://i.imgur.com/0x3mTwb.png


Here's the script:
Code Lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
--local map = '<C><P /><Z><S><S X="10" Y="215" L="20" H="410" T="12" P="0,0,0.3,0.2,0,0,0,0" o="324650" /><S X="250" Y="215" L="20" H="410" T="12" P="0,0,0.3,0.2,0,0,0,0" o="324650" /><S X="130" Y="395" L="240" H="50" T="12" P="0,0,0.3,0.2,0,0,0,0" o="324650" /><S X="130" Y="25" L="240" H="30" T="12" P="0,0,0.3,0.2,0,0,0,0" o="324650" /><S X="-65" Y="205" L="50" H="330" T="1" P="0,0,0,0.2,0,0,0,0" /></S><D><DS X="130" Y="349" /></D><O /></Z></C>'
local map = '<C><P /><Z><S /><D><DS X="120" Y="219" /></D><O /></Z></C>'
local keys = {up = 73, left = 74, down = 75, right = 76, rotate = 32}
local I,O,T,S,Z,J,L = 1,2,3,4,5,6,7
local pieceGrids = {
{{0,0,0,0},{1,2,3,4},{0,0,0,0},{0,0,0,0}},
{{0,0,0,0},{0,1,2,0},{0,3,4,0},{0,0,0,0}},
{{0,1,0},{2,3,4},{0,0,0}},
{{0,1,2},{3,4,0},{0,0,0}},
{{1,2,0},{0,3,4},{0,0,0}},
{{1,0,0},{2,3,4},{0,0,0}},
{{0,0,1},{2,3,4},{0,0,0}}
}
local colors = {0x00EEEE,0xEEEE00,0xEE00EE,0x00EE00,0xEE0000,0x0000EE,0xEE9900,[99]=0x111111}
local gridSettings = {
height = 17,
width = 10,
spawn = {x=4,y=1},
fallDelay = 2
}
local blockSettings = {
x0 = 20,
y0 = 40,
size = 20,
margin = 2
}

local blockDef = {
type = 12,
height = blockSettings.size,
width = blockSettings.size
}
local mice = {}
local miceCount = 0

function main()
tfm.exec.disableAfkDeath (true)
tfm.exec.disableAutoNewGame (true)
tfm.exec.disableAutoScore (true)
tfm.exec.disableAutoShaman (true)
tfm.exec.disableAutoTimeLeft (true)
tfm.exec.newGame(map)

for name in pairs(tfm.get.room.playerList) do
eventNewPlayer(name)
end
end

function eventNewPlayer(name)
if miceCount > 2 then return end

miceCount = miceCount + 1
mice[name] = {
isPlaying = false,
score = 0,
grid = nil,
bag = nil,
currentPiece = nil,
pieceId = nil,
id = miceCount,
pieceCount = 0,
fallTimer = 0
}
for _,k in pairs(keys) do
tfm.exec.bindKeyboard(name, k, true)
end

makeFrame(name)
startGame(name)
end

function startGame(name)
mice[name].grid = emptyGrid()
mice[name].bag = newBag()
mice[name].score = 0

getNewPiece(name)
mice[name].isPlaying = true

--{type = 0, x = 0, y = 0, grid=nil}
end

function eventLoop(t,tr)
for name,mouse in pairs(mice) do
if mouse.isPlaying then
if mouse.currentPiece then
if mouse.fallTimer > gridSettings.fallDelay then
if collision(name, mice[name].pieceGrid, mice[name].currentPiece.x, mice[name].currentPiece.y, 0, 1) then
lockPiece(name)
checkLines(name)
checkGameOver(name)
else
mice[name].currentPiece.y = mice[name].currentPiece.y + 1
drawPiece(name)
end
mouse.fallTimer = 0
else
mouse.fallTimer = mouse.fallTimer + 1
end
end
end
end
end

function getNewPiece(name)
local type = mice[name].bag[#mice[name].bag]
mice[name].currentPiece = {
type = type,
x = gridSettings.spawn.x,
y = gridSettings.spawn.y
}
mice[name].pieceGrid = getPieceGrid(type)
mice[name].pieceCount = mice[name].pieceCount + 1
table.remove(mice[name].bag)

if #mice[name].bag == 0 then
mice[name].bag = newBag()
end

drawPiece(name)
--drawNextPiece(name, mice[name].bag[#bag])
end

function drawPiece(name)
for i=1,#mice[name].pieceGrid do
for j=1,#mice[name].pieceGrid[i] do
if mice[name].pieceGrid[i][j] > 0 then
local id = mice[name].id*10000 + mice[name].pieceCount*4 + mice[name].pieceGrid[i][j]-1
drawBlock(name, mice[name].currentPiece.x+j-1, mice[name].currentPiece.y+i-1, mice[name].currentPiece.type, id)
end
end
end
end

function drawBlock(name, x, y, type, id)
blockDef.color = colors[type]
--local id = mice[name].id*10000 + mice[name].pieceCount*4 + blockId-1

local x0,y0,size,margin = blockSettings.x0, blockSettings.y0, blockSettings.size, blockSettings.margin
y0 = y0 - (size+margin)*2
x0 = x0 + (mice[name].id - 1) * ((size+margin)*gridSettings.width + margin + size)
x1 = x0 + (x-1)*size + x*margin + size/2
y1 = y0 + (y-1)*size + y*margin + size/2
tfm.exec.addPhysicObject(id, x1, y1, blockDef)
end

function getPieceGrid(type)
local p = {}
for i=1,#pieceGrids[type] do
p[i] = {}
for j=1,#pieceGrids[type][i] do
p[i][j] = pieceGrids[type][i][j]
end
end
return p
end

function emptyGrid()
local grid = {}
for i=1,gridSettings.height do
grid[i]={}
for j=1,gridSettings.width do
grid[i][j] = 0
end
end
return grid
end

function newBag()
local pieces = {1,2,3,4,5,6,7}
local bag = {}
for i=1,7 do
local a = math.random(#pieces)
table.insert(bag, pieces[a])
table.remove(pieces, a)
end
return bag
end

function rotate(m1)
local n = #m1

local m2 = {}
for i=1,n do
m2[i] = {}
end

for i=1,n do
for j=1,n do
m2[i][j] = m1[n-j+1][i]
end
end

return m2
end

function eventKeyboard(name, key, down, x, y)
if not mice[name].isPlaying then return end

if key == keys.left then
if not collision(name, mice[name].pieceGrid, mice[name].currentPiece.x, mice[name].currentPiece.y, -1, 0) then
mice[name].currentPiece.x = mice[name].currentPiece.x - 1
drawPiece(name)
end
elseif key == keys.right then
if not collision(name, mice[name].pieceGrid, mice[name].currentPiece.x, mice[name].currentPiece.y, 1, 0) then
mice[name].currentPiece.x = mice[name].currentPiece.x + 1
drawPiece(name)
end
elseif key == keys.down then
if not collision(name, mice[name].pieceGrid, mice[name].currentPiece.x, mice[name].currentPiece.y, 0, 1) then
mice[name].currentPiece.y = mice[name].currentPiece.y + 1
drawPiece(name)
mice[name].fallTimer = 0
end
elseif key == keys.up then
mice[name].fallTimer = 0
while not collision(name, mice[name].pieceGrid, mice[name].currentPiece.x, mice[name].currentPiece.y, 0, 1) do
mice[name].currentPiece.y = mice[name].currentPiece.y + 1
end
drawPiece(name)
lockPiece(name)
checkLines(name)
checkGameOver(name)
elseif key == keys.rotate then
local rotatedGrid = rotate(mice[name].pieceGrid)
if collision(name, rotatedGrid, mice[name].currentPiece.x, mice[name].currentPiece.y, 0, 0) then
if collision(name, rotatedGrid, mice[name].currentPiece.x, mice[name].currentPiece.y, -1, 0) then
if collision(name, rotatedGrid, mice[name].currentPiece.x, mice[name].currentPiece.y, 1, 0) then
else
mice[name].pieceGrid = rotatedGrid
mice[name].currentPiece.x = mice[name].currentPiece.x + 1
drawPiece(name)
end
else
mice[name].pieceGrid = rotatedGrid
mice[name].currentPiece.x = mice[name].currentPiece.x - 1
drawPiece(name)
end
else
mice[name].pieceGrid = rotatedGrid
drawPiece(name)
end
end
end

function collision(name, pieceGrid, x, y, offsetX, offsetY)
offsetX = offsetX or 0
offsetY = offsetY or 0
x = x + offsetX
y = y + offsetY
for i=1,#pieceGrid do
for j=1,#pieceGrid[i] do
if pieceGrid[i][j]>0 then
if ((not mice[name].grid[y+i-1]) or (not mice[name].grid[y+i-1][x+j-1]) or (mice[name].grid[y+i-1][x+j-1]~=0)) then
return true
end
end
end
end
return false
end

function lockPiece(name)
local pieceGrid = mice[name].pieceGrid
local grid = mice[name].grid
local x,y = mice[name].currentPiece.x, mice[name].currentPiece.y
local type = mice[name].currentPiece.type

for i=1,#pieceGrid do
for j=1,#pieceGrid[i] do
if pieceGrid[i][j]>0 then
mice[name].grid[y+i-1][x+j-1] = {id = mice[name].id*10000 + mice[name].pieceCount*4 + pieceGrid[i][j]-1, type = type}
end
end
end
end

function checkLines(name)
local pieceGrid = mice[name].pieceGrid
local grid = mice[name].grid
local x,y = mice[name].currentPiece.x, mice[name].currentPiece.y
local type = mice[name].currentPiece.type

for i=1,#pieceGrid do
for j=1,#pieceGrid[i] do
if pieceGrid[i][j]>0 then
if lineFull(grid, y+i-1) then
clearLine(name, grid, y+i-1)
end
break
end
end
end
end

function lineFull(grid, i)
for j=1,#grid[i] do
if grid[i][j] == 0 then
return false
end
end
return true
end

function clearLine(name, grid, line)
for j=1,#grid[line] do
tfm.exec.removePhysicObject(grid[line][j].id)
grid[line][j] = 0
end
for i=line,2,-1 do
for j=1,#grid[i] do
if grid[i-1][j] ~= 0 then
drawBlock(name, j, i, grid[i-1][j].type, grid[i-1][j].id)
grid[i][j] = {type = grid[i-1][j].type, id = grid[i-1][j].id}
grid[i-1][j] = 0
elseif grid[i][j] ~= 0 then
tfm.exec.removePhysicObject(grid[i][j].id)
grid[i][j] = 0
end
end
end
end

function checkGameOver(name)
local grid = mice[name].grid
local gameOver = false
for i=1,2 do
for j=1,#grid[i] do
if grid[i][j] ~= 0 then
gameOver = true
break
end
if gameOver then break end
end
end

if gameOver then
mice[name].isPlaying = false
darkenBlocks(name)
else
getNewPiece(name)
end
end

function darkenBlocks(name)
local grid = mice[name].grid
for i=1,#grid do
for j=1,#grid[i] do
if grid[i][j] ~= 0 then
drawBlock(name, j, i, 99, grid[i][j].id)
end
end
end
end

function makeFrame(name)
local x0,y0,size,margin = blockSettings.x0, blockSettings.y0, blockSettings.size, blockSettings.margin

local vWall = {
width = size,
height = (size+margin)*(gridSettings.height-2) + margin + size*2,
color = 0x324650,
type = 12
}

local hWall = {
width = (size+margin)*gridSettings.width + margin + size*2,
height = size,
color = 0x324650,
type = 12
}

if miceCount == 1 then
tfm.exec.addPhysicObject(miceCount*10 + 1, x0-size/2, y0 + vWall.height/2 - size, vWall)
end

tfm.exec.addPhysicObject(miceCount*10 + 2, x0 + (hWall.width-size)*(miceCount-1) + hWall.width -size-size/2, y0+vWall.height/2-size, vWall)
tfm.exec.addPhysicObject(miceCount*10 + 3, x0 + (hWall.width-size)*(miceCount-1) + hWall.width/2 - size, y0-margin-size/2, hWall)
tfm.exec.addPhysicObject(miceCount*10 + 4, x0 + (hWall.width-size)*(miceCount-1) + hWall.width/2 - size, y0+vWall.height-size-size/2, hWall)

tfm.exec.movePlayer(name, (hWall.width-size)*(miceCount-1) + hWall.width/2 - size, y0 + vWall.height/2 - size)
--x0 = x0 + (mice[name].id - 1) * ((size+margin)*gridSettings.width + margin + size)
end

main()


I was planning to do a more robust version with all the players being able to play in rotation. I'd also like to try to make a multiplayer mode where many people could play in the same grid. I even have some ideas to mix the game with TFM gameplay.

If you have any ideas and enough motivation you can try to implement them yourself or do whatever you want with the script.

Dernière modification le 1481312160000
Crazysushi
« Citoyen »
1407602160000
    • Crazysushi#0000
    • Profil
    • Derniers messages
#2
  0
:o This is a fun script! :D

#firstpost
Nicolasledu
« Citoyen »
1407610380000
    • Nicolasledu#0000
    • Profil
    • Derniers messages
    • Tribu
#3
  0
Better script : http://atelier801.com/topic?f=6&t=580173&p=5#m94
Haku
« Sénateur »
1407612180000
    • Haku#0807
    • Profil
    • Derniers messages
#4
  0
Nicolasledu a dit :
Better script : http://atelier801.com/topic?f=6&amp;t=580173&amp;p=5#m94

It's nicely done and simple. It's rather different though.

Something I liked about this script is that you can customize it in whatever way you want. You can even change the pieces or the size of the pieces easily.
Henrypika
« Citoyen »
1407884580000
    • Henrypika#0000
    • Profil
    • Derniers messages
#5
  0
i love tetris and transformice(and pokemon)
Jojololly
« Citoyen »
1408268160000
    • Jojololly#0000
    • Profil
    • Derniers messages
#6
  0
This is amazing!
Anthonyjones
« Censeur »
1419673740000
    • Anthonyjones#0000
    • Profil
    • Derniers messages
    • Tribu
#7
  0
I love play tetris :)

You need add score like point for example: # lines x Level = point. May if combo can be count.
Make level 1 - 9 only max with 10 line per times will be level up and increase speed.
When Game is Over then use say !new will be new game.
If want set control say !hotkey or !control may can change setting control.

Good work c:
Triggeralpha
« Citoyen »
1419710580000
    • Triggeralpha#0000
    • Profil
    • Derniers messages
#8
  0
IJKL to move the pieces?!?!

Why not customizable controls?!!
Ren
« Citoyen »
1593947400000
    • Ren#7061
    • Profil
    • Derniers messages
    • Tribu
#9
  0
It worked :3
Thank you!
  • Forums
  • /
  • Transformice
  • /
  • Modules
  • /
  • [script] Tetris
© Atelier801 2018

Equipe Conditions Générales d'Utilisation Politique de Confidentialité Contact

Version 1.27