Advertisement
Guest User

rps.lua

a guest
Sep 17th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.33 KB | None | 0 0
  1. lang={
  2.  ru={
  3.   rock=    "[ камень ]",
  4.   scissors="[ножницы ]",
  5.   paper=   "[ бумага ]",
  6.   rock2="камень",
  7.   scissors2="ножницы",
  8.   paper2="бумага",
  9.   win1="выиграл игрок 1: ",
  10.   win2="выиграл игрок 2: ",
  11.   wins="побед: ",
  12.   draw="НИЧЬЯ!!",
  13.  },
  14.  en={
  15.   rock=    "[  rock  ]",
  16.   scissors="[scissors]",
  17.   paper=   "[ paper  ]",
  18.   rock="rock",
  19.   scissors="scissors",
  20.   paper="paper",
  21.   win1="WIN player 1: ",
  22.   win2="WIN player 2: ",
  23.   wins="wins: ",
  24.   draw="DRAW!!",
  25.  }
  26. }
  27. slang="ru"
  28.  
  29. local gpu=require("component").gpu
  30. local event=require("event")
  31. require("term").clear()
  32.  
  33. local gameCount=2
  34. local obj={}
  35. local player={}
  36.  
  37. function testClick(self,x,y)
  38.   return (x>=self.x)and(y>=self.y)and(x<self.x+self.w)and(y<self.y+self.h)
  39. end
  40.  
  41. function offObj()
  42.   return false
  43. end
  44.  
  45. function newButton(x,y,w,h,id)
  46.   table.insert(obj,{x=x,y=y,w=w,h=h,isClicked=testClick,id=id})
  47. end
  48.  
  49. function drawPlayer(x,y,id)
  50.   newButton(x,y+1,10,1,id)
  51.   newButton(x,y+2,10,1,id)
  52.   newButton(x,y+3,10,1,id)
  53.   gpu.fill(x,y,10,4," ")
  54.   gpu.set(x,y,player[id].name)
  55.   gpu.set(x,y+1,lang[slang]["rock"])
  56.   gpu.set(x,y+2,lang[slang]["scissors"])
  57.   gpu.set(x,y+3,lang[slang]["paper"])
  58.   gpu.set(x,y+4,lang[slang]["wins"]..player[id].win)
  59. end
  60.  
  61. gpu.setBackground(0x999999)
  62. gpu.fill(1,1,10,4," ")
  63. gpu.fill(15,1,10,4," ")
  64. newButton(1,1,10,4)
  65. newButton(15,1,10,4)
  66. isRunning=true
  67. tmp=2
  68. while isRunning do
  69.   _,_,x,y,_,name=event.pull("touch")
  70.   for i=1,#obj do
  71.     if obj[i]:isClicked(x,y) then
  72.       player[i]={}
  73.       player[i].name=name
  74.       player[i].bit=-1
  75.       player[i].win=0
  76.       gpu.setBackground(0x009900)
  77.       gpu.fill(obj[i].x,obj[i].y,obj[i].w,obj[i].h," ")
  78.       gpu.set(obj[i].x,obj[i].y,player[i].name)
  79.       obj[i].isClicked=offObj
  80.       tmp=tmp-1
  81.     end
  82.   end
  83.   if tmp==0 then
  84.     isRunning=false
  85.   end
  86. end
  87.  
  88. gpu.setBackground(0x000000)
  89. --os.exit()
  90. --for _=1,gameCount do
  91. while (player[1].win~=2)and(player[2].win~=2) do
  92. obj={}
  93. require("term").clear()
  94. drawPlayer(1,1,1)
  95. drawPlayer(15,1,2)
  96.  
  97. isRunning=true
  98.  
  99. tmp=0
  100.  
  101. while isRunning do
  102.   _,_,x,y,_,name=event.pull("touch")
  103.   for i=1,#obj do
  104.     if obj[i]:isClicked(x,y) then
  105. --print(i,obj[i].id)
  106.       if name==player[obj[i].id].name then
  107.         tmp=tmp+1
  108. --        gpu.set(149,49,i.."")
  109.         pos=(i-1)%3+1
  110.         player[obj[i].id].bit=pos
  111.         pos=math.floor((i-1)/3)
  112.         if pos==0 then
  113.           gpu.fill(1,2,10,3," ")
  114.         else
  115.           gpu.fill(15,2,10,3," ")
  116.         end
  117.         obj[pos*3+1].isClicked=offObj;obj[pos*3+2].isClicked=offObj;obj[pos*3+3].isClicked=offObj
  118.       end
  119.     end
  120.   end
  121.   if tmp==2 then
  122.     isRunning=false
  123.   end
  124. end
  125. --print(player[1].bit,player[2].bit)
  126. require("term").clear()
  127. if player[1].bit==player[2].bit then
  128.   print(lang[slang]["draw"])
  129. --elseif ((player[1].bit==1)and(player[2].bit==2))or((player[1].bit==2)and(player[1])
  130. elseif (player[1].bit==player[2].bit-1)or(player[1].bit==player[2].bit+2)then
  131.   print(lang[slang]["win1"]..player[1].name)
  132.   player[1].win=player[1].win+1
  133. else
  134.   print(lang[slang]["win2"]..player[2].name)
  135.   player[2].win=player[2].win+1
  136. end
  137. ms={lang[slang]["rock2"],lang[slang]["scissors2"],lang[slang]["paper2"]}
  138. print(ms[player[1].bit],ms[player[2].bit])
  139. event.pull("key_down")
  140. end
  141. if player[1].win>player[2].win then
  142.   io.write(lang[slang]["win1"])
  143. else
  144.   io.write(lang[slang]["win2"])
  145. end
  146. io.write(player[1].win.." VS "..player[2].win)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement