Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Mice Sweeper--
- --[[
- First, please google "mine sweeper" if you don't know that too popular game.
- Mice should press Space bar on the cells to open them. If there is a mine under a tile someone has tried to open he is damaged his health. The hearts on a mouse's head show his health. He dies when he has lost all his health and he lose half of his score. Players respawn in 10 seconds after they die.
- A mouse gets 10 points whenever he opens a safe tile (without mine)
- Tiles are opened not just after a player presses space bar on them. It takes a little bit before a tile is opened so that other players can open it too. After the first player has pressed Space bar on a cell, if another player also presses space bar on the same cell before it is opened, he also get 10 points if that cell is safe.
- ]]
- players = {};
- countl = 0;
- tiles = {};
- checkList = {};
- cellsOpened = 0;
- ncells = 0;
- SIZE = 40;
- WIDTH = 20;
- HEIGHT = 15;
- HP = 3;
- around = {{-1, -1}, {0, -1}, {1, -1}, {-1, 0}, {1, 0}, {-1, 1}, {0, 1}, {1, 1}};
- ID = {ncells = 1, help=2};
- gd1={
- type=12,
- width=SIZE - 2,
- height=SIZE - 2,
- foreground=false,
- friction=0.3,
- restitution=0.2,
- angle=0,
- color = 0x948f82,
- miceCollision=false,
- groundCollision=true,
- dynamic=false,
- fixedRotation=false,
- mass=0,
- linearDamping=0,
- angularDamping=0
- }
- gd2 = {
- type=12,
- width=SIZE - 2,
- height=SIZE - 2,
- foreground=false,
- friction=0.3,
- restitution=0.2,
- angle=0,
- color = 0xba8f66,
- miceCollision=false,
- groundCollision=true,
- dynamic=false,
- fixedRotation=false,
- mass=0,
- linearDamping=0,
- angularDamping=0
- }
- gd3 = {
- type=0,
- width=10,
- height=10,
- foreground=false,
- friction=0.3,
- restitution=0.2,
- angle=45,
- color = 0x948f82,
- miceCollision=true,
- groundCollision=true,
- dynamic=false,
- fixedRotation=false,
- mass=0,
- linearDamping=0,
- angularDamping=0
- }
- images = {"15199769c74.png", "1519bf78f15.png", "1519bf7c43b.png", "1519bf7f5f0.png", "1519bf90206.png", "1519bf91cc8.png", "1519bf9568c.png", "1519bfa1fb7.png", "1519bfb1049.png"};
- hearts = {"1519fffc1f5.png", "151a003b85a.png", "151a00e7417.png"};
- text = {
- welcome = "Welcome to Mice Sweeper! Type !help for help",
- begin = "The round has begun. Open the tiles avoiding mines",
- help = "Welcome to Mice Sweeper<br/>The rules are simple. There are tiles and some of them have mines buried under them. So, don't open them.<br><li>Press Space Bar to open a tile on it</li><li>You get 10 points for every opening a safe tile</li><li>The number on a tile stands for the number of mines around it</li><li>Your health is 3 at the beginning and when you open a mine tile you lose one.</li><li>You die when your health is all lost. You lose half of your score at the same time.</li>",
- respawn = "You will respawn in 10 seconds"
- }
- function sendChatMessage(message, name, prefix, f)
- if name~=nil then
- message=text[message] or message;
- if prefix then
- message=prefix..message
- end
- if f then
- message=message:format(table.unpack(f))
- end
- tfm.exec.chatMessage(message, name);
- else
- for name,player in pairs(tfm.get.room.playerList) do
- sendChatMessage(message, name, prefix, f)
- end
- end
- end
- function eventLoop(time, remaining)
- countl = countl + 1;
- if remaining < 500 then
- finishRound();
- local s = '<C><P Ca="" L="%d" H="%d"/><Z><S><S X="%d" L="%d" Y="%d" H="%d" P="0,0,0.3,0.2,0,0,0,0" c="2" T="12" o="FFFFFF"/><S X="5" L="10" Y="%d" H="%d" P="0,0,0.3,0.2,0,0,0,0" T="10" /><S X="%d" L="%d" Y="30" H="10" P="0,0,0.3,0.2,0,0,0,0" T="10" /><S X="%d" L="10" Y="%d" H="%d" P="0,0,0.3,0.2,0,0,0,0" T="10" /><S X="%d" L="%d" Y="%d" H="10" P="0,0,0.3,0.2,0,0,0,0" T="10" />%s</S><D><DS X="25" Y="50" /></D><O /></Z></C>';
- local h = SIZE * HEIGHT + 20;
- local w = SIZE * WIDTH + 20;
- local t = {};
- for x = 1, WIDTH do
- for y = 1, HEIGHT do
- table.insert(t, string.format('<S lua="%d" X="%d" L="%d" Y="%d" H="%d" c="2" T="0" o="948f82"/>', (y - 1) * WIDTH + x, (x - 0.5) * SIZE + 10, SIZE-2, (y - 0.5) * SIZE + 35, SIZE-2));
- end
- end
- for x = 1, WIDTH - 1 do
- for y = 1, HEIGHT - 1 do
- if (x + 2 * y) % 4 == 0 then
- table.insert(t, string.format('<S X="%d" L="10" Y="%d" H="10" P="0,0,15,0.2,45,0,0,0" T="4"/>', x * SIZE + 10, y * SIZE + 35));
- end
- end
- end
- s = string.format(s, w, h + 25,
- w / 2, w, h / 2 + 25, h,
- h / 2 + 25, h,
- w / 2, w,
- w - 5, h / 2 + 25, h,
- w / 2, w, h - 5 + 25,
- table.concat(t, ""));
- tfm.exec.newGame(s);
- else
- local x, y, tile
- for i, t in pairs(checkList) do
- x = (i - 1) % WIDTH + 1;
- y = (i - x) / WIDTH + 1;
- tile = tiles[x][y];
- if tile[2] then --bomb
- local p;
- for j, name in pairs(t) do
- p = players[name];
- if p.hp == 1 then
- tfm.exec.killPlayer(name);
- p.loopRespawn = countl + 20;
- p.score = math.floor(p.score / 2);
- tfm.exec.setPlayerScore(name, p.score, false);
- sendChatMessage("respawn", name);
- else
- p.hp = p.hp - 1;
- tfm.exec.removeImage(p.heartID);
- p.heartID = addHeart(name);
- end
- end
- else
- for j, name in pairs(t) do
- players[name].score = players[name].score + 10;
- tfm.exec.setPlayerScore(name, players[name].score, false);
- end
- end
- openTile(x, y, true);
- end
- checkList = {};
- if cellsOpened == ncells then
- eventLoop(0, 0);
- end
- for name, player in pairs(players) do
- if player.loopRespawn == countl then
- tfm.exec.respawnPlayer(name);
- player.hp = HP;
- player.heartID = addHeart(name);
- end
- end
- end
- end
- function finishRound()
- if ncells == 0 then return end
- local t = {};
- for name, player in pairs(players) do
- table.insert(t, {name, player.score});
- end
- table.sort(t, function(a,b)return a[2] > b[2] end);
- for i = 1, #t do
- t[i] = string.format("%d: %s %d", i, t[i][1], t[i][2]);
- end
- local s = (ncells == cellsOpened and "All the mines were sweeped!") or "Time is up";
- sendChatMessage(s .. "\nScores: \n" .. table.concat(t, "\n"));
- end
- function openTile(x, y, autoOpen)
- local tile, sum;
- if tiles[x][y][1] == 0 then return end
- if tiles[x][y][2] then
- sum = 9;
- else
- cellsOpened = cellsOpened + 1;
- ui.addTextArea(ID.ncells, string.format("<font color='#FF0000'><b>%d/%d</b></font>", cellsOpened, ncells), nil, 5, 27, 100, 30, 0, 0, 0, true);
- sum = 0;
- local _x, _y;
- for j, xy in pairs(around) do
- _x, _y = x + xy[1], y + xy[2];
- tile = tiles[_x] and tiles[_x][_y];
- if tile then
- sum = sum + ((tile[2] and 1) or 0);
- end
- end
- end
- tiles[x][y][3] = sum;
- tiles[x][y][1] = 0;
- tfm.exec.removePhysicObject((y - 1) * WIDTH + x);
- if sum > 0 then
- tfm.exec.addImage(images[sum], "_1000", (x - 1) * SIZE + 10, (y - 1) * SIZE + 35);
- end
- if autoOpen and sum == 0 then
- local _x, _y;
- for j, xy in pairs(around) do
- _x, _y = x + xy[1], y + xy[2];
- tile = tiles[_x] and tiles[_x][_y];
- if tile and tile[1] ~= 0 then
- openTile(_x, _y, true);
- end
- end
- end
- end
- function eventNewPlayer(name)
- players[name] = {
- name = name,
- score = 0,
- hp = HP;
- };
- system.bindKeyboard(name, 32, true, true);
- system.bindKeyboard(name, 72, true, true);
- if tiles and tiles[1] then
- for x = 1, WIDTH do
- for y = 1, HEIGHT do
- if tiles[x][y][1] == 0 then
- tfm.exec.removePhysicObject((y - 1) * WIDTH + x);
- if tiles[x][y][3] > 0 then
- tfm.exec.addImage(images[tiles[x][y][3]], "_1000", (x - 1) * SIZE + 10, (y - 1) * SIZE + 35, name);
- end
- end
- end
- end
- end
- sendChatMessage("welcome", name);
- players[name].loopRespawn = countl + 20;
- end
- function eventKeyboard(name, code, down, x, y)
- if code == 32 then
- x = math.floor((x - 10) / SIZE) + 1;
- y = math.floor((y - 35) / SIZE) + 1;
- local ind = (y - 1) * WIDTH + x;
- if tiles[x] and tiles[x][y] and tiles[x][y][1] == 1 then
- if checkList[ind] == nil then
- checkList[ind] = {};
- end
- table.insert(checkList[ind], name);
- end
- elseif code == 72 then
- ui.addPopup(ID.help, 0, text.help, name, 200, 60, 400, true);
- end
- end
- function eventPlayerLeft(name)
- players[name] = nil;
- end
- function eventNewGame()
- tiles = {};
- checkList = {};
- for x = 1, WIDTH do
- tiles[x] = {};
- for y = 1, HEIGHT do
- tiles[x][y] = {1};
- end
- end
- local i = 1;
- local nmine = math.floor(WIDTH * HEIGHT * 0.23);
- ncells = HEIGHT * WIDTH - nmine;
- cellsOpened = 0;
- ui.addTextArea(ID.ncells, string.format("<font color='#FF0000'><b>%d/%d</b></font>", cellsOpened, ncells), nil, 5, 27, 100, 30, 0, 0, 0, true);
- local x, y;
- while i <= nmine do
- x = math.random(WIDTH);
- y = math.random(HEIGHT);
- if not tiles[x][y][2] then
- tiles[x][y][2] = true;
- i = i + 1;
- end
- end
- for name, player in pairs(players) do
- player.score = 0;
- player.hp = HP;
- tfm.exec.setPlayerScore(name, 0, false);
- player.heartID = addHeart(name);
- end
- tfm.exec.setGameTime(360);
- end
- function addHeart(name, hp)
- hp = hp or players[name].hp;
- return tfm.exec.addImage(hearts[hp], "$"..name, -20, -38);
- end
- function eventChatCommand(name, com)
- if com == "help" then
- ui.addPopup(ID.help, 0, text.help, name, 200, 60, 400, true);
- end
- end
- tfm.exec.disableAutoNewGame(true);
- tfm.exec.disableAutoShaman(true);
- tfm.exec.disableAutoTimeLeft(true);
- tfm.exec.disableAfkDeath(true);
- for name, player in pairs(tfm.get.room.playerList) do
- eventNewPlayer(name);
- end
- math.randomseed(os.time());
- eventLoop(0,0)
Advertisement
Add Comment
Please, Sign In to add comment