×

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] Snake game
[Script] Snake game
Intasmg
« Citoyen »
1484030940000
    • Intasmg#0000
    • Profil
    • Derniers messages
    • Tribu
#1
  1
  • main
  • Game modes
  • Code

http://i.imgur.com/IEIxtkj.gif



Remember those old classic games that were so simple, yet so entertaining? Tetris, pacman, space invaders... and the one I recreated kind of in Transformice, Snake!

Snake is a game featuring a green line, commonly called "Snake", that moves around the map trying to get the food. If the snake gets the food,
its length increases. It's important that the snake don't eat itself or attempt to leave the map, as it will result in a game over!

You move the snake using the arrow keys or WASD.

In order to play, you must first change this line:

Code Lua

1
2
--Write your name here
admin = "Intasmg"

and put your name in the quotation marks. This is an important step because the script won't work if admin is a player that is not in the room, you'll get an error like this:

http://i.imgur.com/57w3WPs.png

The only two things the admin can do right now is start playing the game when executed, and stop running it when clicking in the Exit textarea.

Check the game modes! ->
This game is so simple that you can think a lots of game modes for it! At this moment it only has two:

  • Classic: The old classic game mode. You control the snake using the arrow keys or WASD, and your objective is to catch every food that appears in the map. If you hit yourself or leave the map, game over. Note that if you reach 11 of length, the food textarea will start moving around every 9-12 sec at random positions.

    This game mode was made one-player only and everyone else were just watching you play, really boring. I wanted to include all the players in the room to being able to play, so I made a "queue system". To enter to the queue you just have to click to the Play textarea, and wait until you get to the 1st position.

  • http://i.imgur.com/nD2WhKP.gif


  • SnakeShaman (SS): This game mode has a different gameplay that the Snake game, because it is combined with the Transformice gameplay! The shaman, instead of building, has to control the snake and help the mice to reach the cheese and go back to hole. There's no collision or food in here.

    To play this, there must be at least 3 mice in the game, and nobody must be playing the Classic mode. It can be kind of buggy because I haven't tested it so much.

Have any idea? Critique? Find something wrong? Let me know it, and post it in this thread.

Check the code! ->
Messy code is messy.
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
--Write your name here
admin = "Intasmg"

add = ui.addTextArea
rem = ui.removeTextArea

isPlaying = false
foodMov = 0

gamemode = "Classic"

playersAlive = 0

--Arrow keys and WASD
LEFT = 0; UP = 1; RIGHT = 2; DOWN = 3;

function main()
admin = admin:lower():gsub("^%l", string.upper) --All letters must be lowercase except the first one
local errorText, name = string.format("%q is not in the room, or doesn't exist.", admin)
local map = "@7004831"
--Whoever is in the pos [1] is the current player
playerList = {admin} --Admin is the first player
--Admin name control
for k in pairs(tfm.get.room.playerList) do
if k==admin then
name = true
break
end
end
if not name then
error(errorText)
end
tfm.exec.setGameTime(0, false)
tfm.exec.disableAfkDeath()
tfm.exec.disableAutoShaman()
tfm.exec.disableAutoTimeLeft()
tfm.exec.disableAutoNewGame()
tfm.exec.disableMortCommand()
tfm.exec.newGame(map)
ui.setMapName("Snake!\n")
add(-3, "<a href='event:play'>Classic</a>", nil, 755, 375)
add(-4, "<a href='event:exit'>Exit</a>", admin, 605, 375)
add(-5, "<a href='event:SS'>SnakeShaman</a>", admin, 650, 375)
end

function checkPlayers(p)
if p == 0 then tfm.exec.newGame(maps[math.random(#maps)]); end
end

function eventNewPlayer(name)
if gamemode=="Classic" then
add(-3, "<a href='event:play'>Play</a>", name, 755, 375)
add(0, "", name, 2, 38, 791, 254, 0x378990, 0x000000)
if isPlaying then
add(-1, "", name, pos.foodX, pos.foodY, 10, 10, math.random(0x00AA00, 0x00FF00), math.random(0x00AA00, 0x00FF00))
ui.setMapName("Snake! <font color='#6A7495'>|</font> <font color='#C2C2DA'>Length: "..len.."</font>\n")
end
tfm.exec.respawnPlayer(name)
end
end

function eventPlayerWon(name)
playersAlive = playersAlive - 1
checkPlayers(playersAlive)
end

function eventPlayerLeft(name)
if gamemode=="Classic" then
for i, k in pairs(playerList) do
if name==k then
for j=i, #playerList do
playerList[j] = playerList[j+1]
end
break
end
end
end
end

function eventPlayerDied(name)
playersAlive = playersAlive - 1
checkPlayers(playersAlive)
end

function eventNewGame()
local xIni = 400
playersAlive = 0
if gamemode~="SS" then return; end
headDef = {type=12, width=20, height=20, foreground=true, friction=0.3, restitution=0.2, angle=0, color=0x4AFF00}
bodyDef = {type=12, width=20, height=20, foreground=true, friction=0.3, restitution=0.2, angle=0, color=0x33AE01}
shamanName = ""
for name in pairs(tfm.get.room.playerList) do
playersAlive = playersAlive + 1
if tfm.get.room.playerList[name].isShaman then shamanName = name; end
end
pos = {x = {}, y = {}, headX = 400, headY = 180}
pressedKey = RIGHT
len = 7
tfm.exec.setUIShamanName("Snake ("..shamanName..")")
tfm.exec.addPhysicObject(1, 400, 180, headDef)
table.foreachi({LEFT, UP, RIGHT, DOWN}, function(_,l) system.bindKeyboard(shamanName, l, true, true) end) --Bind keys to the player
for i=2, len do --Body
xIni = xIni - 20
pos.x[i], pos.y[i] = xIni, 180
tfm.exec.addPhysicObject(i, xIni, 180, bodyDef)
end
timerTextArea(99, "<font size='12'>You are the shaman! Save the mice by moving the snake.</font>", shamanName, 225, 370, 4, nil, nil, 0x1b232a,0x000000)
tfm.exec.killPlayer(shamanName)
tfm.exec.setGameTime (120)
end

function eventTextAreaCallback(id, name, event)
local p = 0
maps = {0,1,2,3,4,5,6,10,11,12,13,15,16,17,18,19,20,21,24,25,26,27,30,39,40,54,56,60,62,66,71,72,73,75,76,79,86,90,91,93,96,99,100,101,102,103,104,105,106,107,115,116,117,118,119,130,131,132,133,134,146}
if event=="play" and gamemode~="SS" then
gamemode = "Classic"
if name~=playerList[1] and not gameover then --Is name the current player?
for i, k in pairs(playerList) do
if k==name then --Is name in the player list?
timerTextArea(100, "Already in the player list, your current position is: "..i..".", name, 275, 300, 5, nil, nil, 0x1b232a,0x000000)
return
end
end
table.insert(playerList, name) --name isn't the current player nor is in the list so ill save his name
timerTextArea(101, "You've been added to the player list, your current position is: "..#playerList..".", name, 275, 327, 5, nil, nil, 0x1b232a,0x000000)
elseif gameover then --Is the game over textarea showing?
timerTextArea(102, "Wait until 'GAME OVER' get erased.", name, 275, 354, 5, nil, nil, 0x1b232a,0x000000)
else
timerTextArea(103, "You're already playing!", name, 275, 378, 5, nil, nil, 0x1b232a,0x000000)
end
elseif event=="SS" and gamemode=="" then
for _ in pairs(tfm.get.room.playerList) do
p = p + 1
end
if p <= 2 then ui.addPopup (0, 0, "You need at least 3 mice in the room to play this.", admin, 275, 304); return; end
tfm.exec.disableAfkDeath(false)
tfm.exec.disableAutoNewGame(false)
tfm.exec.disableAutoShaman(false)
tfm.exec.disableAutoTimeLeft(false)
tfm.exec.disableMortCommand(false)
tfm.exec.disableAllShamanSkills()
gamemode = event --SS
for i=-1, len do
rem(i)
end
tfm.exec.newGame(maps[math.random(#maps)])
elseif gamemode=="SS" and event=="play" and name==admin then
ui.addPopup (1, 1, "Are you sure you want to leave the SnakeShaman mode?", admin, 300, 200)
elseif event=="exit" and name==admin then
system.exit()
end
end

function eventPopupAnswer(id, name, answer)
if answer=="yes" and name==admin then
table.foreachi({LEFT, UP, RIGHT, DOWN}, function(_,l) system.bindKeyboard(shamanName, l, true, false) end)
gamemode = "Classic"
for i=1, len do
tfm.exec.removePhysicObject(i)
end
main()
end
end

function gamemodeClassic(reset)
local xIni = 400 --Body's initial position
rem(-2) --Remove game over textarea if exists
pos = {x = {}, y = {}, headX = 400, headY = 180, foodX = nil, foodY = nil} --Coords of everything here
pressedKey = RIGHT
ui.setMapName("Snake!\n")
if reset then
return
end
len = 3 --Snake's length, don't touch this!!
add(0, "", nil, 2, 38, 791, 254, 0x378990, 0x000000) --Show the "map"
table.foreachi({LEFT, UP, RIGHT, DOWN}, function(_,l) system.bindKeyboard(playerList[1], l, true, true) end) --Bind keys to the player
add(1, "", nil, 400, 180, 10, 10, 0x4AFF00, 0x4AFF00) --Head
for i=2, len do --Body
xIni = xIni - 20
pos.x[i], pos.y[i] = xIni, 180
add(i, "", nil, xIni, 180, 10, 10, 0x33AE01, 0x33AE01)
end
ui.setMapName("Snake! <font color='#6A7495'>|</font> <font color='#C2C2DA'>Length: "..len.."</font>\n")
drawFood() --The food textarea
timerTextArea(99, "<font size='12'>Now is playing: "..playerList[1].."</font>", nil, 275, 273, 6, nil, nil, 0x1b232a,0x000000)
tfm.exec.killPlayer(playerList[1])
end

function drawFood()
local _x, _y
repeat --These are the map's limits
_x = math.random(0, 780)
_y = math.random(40, 280)
until _x%20==0 and _y%20==0 --They must be divisible by 20 to match the movement of the snake
pos.foodX, pos.foodY = _x, _y
add(-1, "", nil, _x, _y, 10, 10, math.random(0x00AA00, 0x00FF00), math.random(0x00AA00, 0x00FF00))
end

keyDelay = false --To avoid weird moves when pressing two keys at the same time

function eventKeyboard(name, key, down)
local function forbiddenMovs(k, k2) --I was testing local functions here, it worked so i left it like this, it could be better though
local e = (k==k2) or (k==RIGHT and k2==LEFT) or (k==LEFT and k2==RIGHT) or (k==DOWN and k2==UP) or (k==UP and k2==DOWN) --Forbidden movs
return e
end
if name==playerList[1] or (name==shamanName and gamemode=="SS") and not keyDelay then
if forbiddenMovs(pressedKey, key) or keyDelay then --If forbiddenMovs or keyDelay is true, the mov won't be executed
return
end
keyDelay = true
pressedKey = key
end
end

function movSnake(k, SS)
local oldX, oldY = pos.headX, pos.headY --Save the head's current pos
local _x, _y = 0, 0
if k==LEFT then --Performs the movement according to the last key pressed
pos.headX = pos.headX - 20
elseif k==DOWN then
pos.headY = pos.headY + 20
elseif k==UP then
pos.headY = pos.headY - 20
elseif k==RIGHT then
pos.headX = pos.headX + 20
end
pos.x[1], pos.y[1] = pos.headX, pos.headY --Save the head's new coords
if SS then tfm.exec.addPhysicObject(1, pos.x[1], pos.y[1], headDef);
else add(1, "", nil, pos.x[1], pos.y[1], 10, 10, 0x4AFF00, 0x4AFF00); end --Show head
for i=2, len do --Show body in its new position
_x, _y = pos.x[i], pos.y[i] --Save old coords
pos.x[i], pos.y[i] = oldX, oldY --Save new coords
if SS then tfm.exec.addPhysicObject(i, oldX, oldY, bodyDef);
else add(i, "", nil, oldX, oldY, 10, 10, 0x33AE01, 0x33AE01); end --Show it in old coords of the following textarea
oldX, oldY = _x, _y --Update coords to the next textarea
end
end

tick = 0
gameover = false

function gameOver()
table.foreachi({LEFT, UP, RIGHT, DOWN}, function(_,l) system.bindKeyboard(playerList[1], l, true, false) end)
gameover = true
tick = 6000 --6 sec until gameover textarea get erased
tfm.exec.respawnPlayer(playerList[1])
isPlaying = false
for i=1, #playerList do
playerList[i] = playerList[i+1]
end
end

function checkCoord()
--Game over--
if ((pos.headX <= -20 or pos.headX >= 800) or (pos.headY <= 20 or pos.headY >= 300)) then
gameOver()
end
for i=2, #pos.x do
if pos.x[i] == pos.headX and pos.y[i] == pos.headY then
gameOver()
end
end
--Grow 1 textarea--
if pos.headX == pos.foodX and pos.headY == pos.foodY then
rem(-1)
drawFood()
len = len + 1
ui.setMapName("Snake! <font color='#6A7495'>|</font> <font color='#C2C2DA'>Length: "..len.."</font>\n")
if len>10 and foodMov==0 then --The food textarea'll start moving when the snake gets 10
foodMov = math.random(9000, 12000)
foodMov = foodMov - (foodMov % 100)
end
end
end

ta = {}
can = 0

function timerTextArea(id, te, tr, x, y, t, wi, hei, bc, bo, alp, fpos) --Same parameters as a textarea, adding time
add(id, te, tr, x, y, wi, hei, bc, bo, alp, fpos) --Show textarea
ta[tostring(can)] = {t*1000, id}
can = can + 1
end

main()

function classicLoop()
if #playerList>0 and not isPlaying and not gameover then --Start playing when playerList has at least one name
isPlaying = true
gamemode = "Classic"
gamemodeClassic()
end
if isPlaying and not gameover then
if len>10 and foodMov>0 then
foodMov = foodMov - 500
if foodMov<=0 then
rem(-1)
drawFood()
foodMov = math.random(9000, 12000)
foodMov = foodMov - (foodMov % 100)
end
end
movSnake(pressedKey)
checkCoord()
end
if tick>0 and gameover then --Esto se ejecuta siempre que tick sea mayor a cero, y gameover sea true
add(-2, "<font size='25' color='#FFFFFF'><p align='center'>GAME OVER</p></font>", nil, 300, 180, nil, nil, math.random(0x000000, 0xBD1E1E))
tick = tick - 500
elseif tick==0 and gameover then
gamemode = ""
gamemodeClassic(true)
for i=1, len do
rem(i)
end
rem(-1)
gameover = false
foodMov = 0
ui.setMapName("Snake! <font color='#6A7495'>|</font> <font color='#C2C2DA'>Waiting player... (click on Classic!)</font>\n")
add(0, "", nil, 2, 38, 791, 254, 0x378990, 0x000000)
end
end

function SSLoop()
movSnake(pressedKey, true)
if pos.x[1] >= 920 or pos.x[1] <= -120 then pressedKey = UP; end
if pos.y[1] >= 420 or pos.y[1] <= -100 then pressedKey = LEFT; end
end

function eventLoop(t, tr)
if gamemode=="Classic" or #playerList>0 then classicLoop();
elseif gamemode=="SS" then SSLoop(); end
if can > 0 then --This weird thing control temporary textareas
for i,k in pairs (ta) do
if ta[i][1] > 0 then
ta[i][1] = ta[i][1] - 500
else
rem(ta[i][2])
ta[i] = nil
can = can - 1
break
end
end
end
if keyDelay then keyDelay = false; end
if tr <= 300 and gamemode == "SS" then checkPlayers(0); end
end --To complete the map you need 520 foods.
Lapinprince
« Consul »
1484032620000
    • Lapinprince#0095
    • Profil
    • Derniers messages
    • Tribu
#2
  0
I really like this, haven't played Snake in a long time and this was a good way to bring back nostalgic memories!
Tat
« Censeur »
1484039340000
    • Tat#0095
    • Profil
    • Derniers messages
    • Tribu
#3
  0
Great module ^^ very nostalgic indeed
Jeronmeo500
« Citoyen »
1531799460000
    • Jeronmeo500#0000
    • Profil
    • Derniers messages
    • Tribu
#4
  0
This is so good!
  • Forums
  • /
  • Transformice
  • /
  • Modules
  • /
  • [Script] Snake game
© Atelier801 2018

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

Version 1.27