astral17

rps

Oct 11th, 2016
206
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. bground=0x292929
  2. player1Pos={1,1}
  3. player2Pos={1,15}
  4. colorDraw=0xffff00
  5. colorWin=0x00aa00
  6. colorLose=0xff0000
  7.  
  8. lang={
  9.  ru={
  10.   rock=    "[ камень ]",
  11.   scissors="[ножницы ]",
  12.   paper=   "[ бумага ]",
  13.   rock2="камень",
  14.   scissors2="ножницы",
  15.   paper2="бумага",
  16.   win1="выиграл игрок 1: ",
  17.   win2="выиграл игрок 2: ",
  18.   wins="побед: ",
  19.   draw="НИЧЬЯ!!",
  20.  },
  21.  en={
  22.   rock=    "[  rock  ]",
  23.   scissors="[scissors]",
  24.   paper=   "[ paper  ]",
  25.   rock="rock",
  26.   scissors="scissors",
  27.   paper="paper",
  28.   win1="WIN player 1: ",
  29.   win2="WIN player 2: ",
  30.   wins="wins: ",
  31.   draw="DRAW!!",
  32.  }
  33. }
  34. slang="en"
  35. char={
  36.  f="█", --full
  37.  d="▄", --down
  38.  u="▀", --up
  39. }
  40.  
  41. ---------------------------------------------
  42. local gpu=require("component").gpu
  43. function centerText(x,y,w,text)
  44.   gpu.set(x+math.floor(w/2-string.len(text)/2),y,text)
  45. end
  46.  
  47. function messageBox(title,text,color)
  48.   local x,y=6,7
  49.   local len1,len2=string.len(title),string.len(text)
  50.   local len3=math.max(len1,len2)+2
  51.   gpu.setBackground(0xffffff)
  52.   gpu.fill(x,y,len3,2+3," ")
  53.   gpu.setForeground(color or 0xFF0000)
  54.   centerText(x,y+1,len3,title)
  55.   gpu.setForeground(0x000000)
  56.   centerText(x,y+3,len3,text)
  57.   gpu.setBackground(color or 0xFF0000)
  58.   gpu.setForeground(0xffffff)
  59.   gpu.fill(x,y+5,len3,3," ")
  60.   centerText(x,y+6,len3,"OK!")
  61. end
  62. ---------------------------------------------
  63.  
  64. tex={
  65.  rock={--symbol fore back
  66.   "     ▄▄   ",
  67.   "  ▄█████  ",
  68.   " ▄███████ ",
  69.   " ▀██████▀ ",
  70.   "   ▀▀▀▀   ",
  71.  },
  72.  paper={
  73.   "     ▄    ",
  74.   "   ▄███▄  ",
  75.   " ▄██████▀ ",
  76.   "  ▀███▀   ",
  77.   "    ▀     ",
  78.  },
  79.  scissors={
  80.   " ▄▄    ▄▄ ",
  81.   "  ▀█▄▄█▀  ",
  82.   "  ▄▄██▄▄  ",
  83.   " █  ██  █ ",
  84.   "  ▀▀  ▀▀  ",
  85.  }
  86. }
  87. tcol={
  88.   rock=0x555555,
  89.   paper=0xf9f9f9,
  90.   scissors=0x888888,
  91.   scissors2=0x555500,
  92. }
  93.  
  94. local event=require("event")
  95. require("term").clear()
  96.  
  97. local gameCount=2
  98. local obj={}
  99. local player={}
  100.  
  101. function testClick(self,x,y)
  102.   return (x>=self.x)and(y>=self.y)and(x<self.x+self.w)and(y<self.y+self.h)
  103. end
  104.  
  105. function offObj()
  106.   return false
  107. end
  108.  
  109. function newButton(x,y,w,h,id,type)
  110.   table.insert(obj,{x=x,y=y,w=w,h=h,isClicked=testClick,id=id,type=type})
  111. end
  112.  
  113. function drawImage(x,y,texture,bground)
  114.   gpu.setForeground(tcol[texture])
  115.   bground=bground or 0x000000
  116.   gpu.setBackground(bground)
  117.   for i=1,5 do
  118.     if (i==3)and(texture=="scissors") then
  119.       gpu.setForeground(tcol["scissors2"])
  120.       gpu.set(x,y+i-1,tex[texture][i])
  121.       gpu.setForeground(tcol["scissors"])
  122.       gpu.set(x+4,y+i-1,"██")
  123.       gpu.setForeground(tcol["scissors2"])
  124.     else
  125.       gpu.set(x,y+i-1,tex[texture][i])
  126.     end
  127.   end
  128.   gpu.setForeground(0xffffff)
  129.   gpu.setBackground(0x000000)
  130. end
  131.  
  132. --drawImage(1,1,"rock");drawImage(11,1,"paper");drawImage(21,1,"scissors")
  133. --os.exit()
  134.  
  135. function drawPlayer(x,y,id)
  136.   newButton(x   ,y+1,10,5,id,1)
  137.   newButton(x+11,y+1,10,5,id,2)
  138.   newButton(x+22,y+1,10,5,id,3)
  139. --  gpu.fill(x,y,10,4," ")
  140.   gpu.set(x,y,player[id].name)
  141.   drawImage(x   ,y+1,"rock")
  142.   drawImage(x+22,y+1,"paper")
  143.   drawImage(x+11,y+1,"scissors")
  144. --  gpu.set(x,y+1,lang[slang]["rock"])
  145. --  gpu.set(x,y+2,lang[slang]["scissors"])
  146. --  gpu.set(x,y+3,lang[slang]["paper"])
  147.   gpu.set(x,y+7,lang[slang]["wins"]..player[id].win)
  148. end
  149.  
  150. gpu.setBackground(0x999999)
  151. gpu.fill(1,1,10,4," ")
  152. gpu.fill(15,1,10,4," ")
  153. newButton(1,1,10,4)
  154. newButton(15,1,10,4)
  155. isRunning=true
  156. tmp=2
  157. while isRunning do
  158.   _,_,x,y,_,name=event.pull("touch")
  159.   for i=1,#obj do
  160.     if obj[i]:isClicked(x,y) then
  161.       player[i]={}
  162.       player[i].name=name
  163.       player[i].bit=-1
  164.       player[i].win=0
  165.       gpu.setBackground(0x009900)
  166.       gpu.fill(obj[i].x,obj[i].y,obj[i].w,obj[i].h," ")
  167.       gpu.set(obj[i].x,obj[i].y,player[i].name)
  168.       obj[i].isClicked=offObj
  169.       tmp=tmp-1
  170.     end
  171.   end
  172.   if tmp==0 then
  173.     isRunning=false
  174.   end
  175. end
  176.  
  177. --gpu.setBackground(bground)
  178. --os.exit()
  179. --for _=1,gameCount do
  180. ms={"rock","scissors","paper"}
  181. while (player[1].win~=2)and(player[2].win~=2) do
  182. gpu.setBackground(bground)
  183. obj={}
  184. require("term").clear()
  185. gpu.setBackground(0x000000)
  186. gpu.fill(1,2,32,5," ")
  187. gpu.fill(1,16,32,5," ")
  188. gpu.setBackground(bground)
  189. drawPlayer(player1Pos[1],player1Pos[2],1)
  190. drawPlayer(player2Pos[1],player2Pos[2],2)
  191.  
  192. isRunning=true
  193.  
  194. tmp=0
  195.  
  196. while isRunning do
  197.   _,_,x,y,_,name=event.pull("touch")
  198.   for i=1,#obj do
  199.     if obj[i]:isClicked(x,y) then
  200. --print(i,obj[i].id)
  201.       if name==player[obj[i].id].name then
  202.         tmp=tmp+1
  203. --        gpu.set(149,49,i.."")
  204. --        pos=(i-1)%3+1
  205.         player[obj[i].id].bit=obj[i].type
  206.         pos=math.floor((i-1)/3)
  207.         if pos==0 then
  208.           gpu.fill(1,2,32,5," ")
  209.         else
  210.           gpu.fill(1,16,32,5," ")
  211.         end
  212.         obj[pos*3+1].isClicked=offObj;obj[pos*3+2].isClicked=offObj;obj[pos*3+3].isClicked=offObj
  213.       end
  214.     end
  215.   end
  216.   if tmp==2 then
  217.     isRunning=false
  218.   end
  219. end
  220. --print(player[1].bit,player[2].bit)
  221. --require("term").clear()
  222. drawPlayer(player1Pos[1],player1Pos[2],1)
  223. drawPlayer(player2Pos[1],player2Pos[2],2)
  224. obj={}
  225. if player[1].bit==player[2].bit then
  226. --  print(lang[slang]["draw"])
  227.   tx=(player[1].bit-1)*11
  228.   drawImage(player1Pos[1]+tx,player1Pos[2]+1,ms[player[1].bit],colorDraw)
  229.   drawImage(player2Pos[1]+tx,player2Pos[2]+1,ms[player[2].bit],colorDraw)
  230. --elseif ((player[1].bit==1)and(player[2].bit==2))or((player[1].bit==2)and(player[1])
  231. elseif (player[1].bit==player[2].bit-1)or(player[1].bit==player[2].bit+2)then
  232. --  print(lang[slang]["win1"]..player[1].name)
  233.   drawImage(player1Pos[1]+(player[1].bit-1)*11,player1Pos[2]+1,ms[player[1].bit],colorWin)
  234.   drawImage(player2Pos[1]+(player[2].bit-1)*11,player2Pos[2]+1,ms[player[2].bit],colorLose)
  235.   player[1].win=player[1].win+1
  236. else
  237. --  print(lang[slang]["win2"]..player[2].name)
  238.   drawImage(player1Pos[1]+(player[1].bit-1)*11,player1Pos[2]+1,ms[player[1].bit],colorLose)
  239.   drawImage(player2Pos[1]+(player[2].bit-1)*11,player2Pos[2]+1,ms[player[2].bit],colorWin)
  240.   player[2].win=player[2].win+1
  241. end
  242. --ms={lang[slang]["rock2"],lang[slang]["scissors2"],lang[slang]["paper2"]}
  243. --print(ms[player[1].bit],ms[player[2].bit])
  244. event.pull("key_down")
  245. end
  246. if player[1].win>player[2].win then
  247. --  io.write(lang[slang]["win1"])
  248.   messageBox("GAME OVER",lang[slang]["win1"]..player[1].win.." VS "..player[2].win)
  249. else
  250. --  io.write(lang[slang]["win2"])
  251.   messageBox("GAME OVER",lang[slang]["win2"]..player[2].win.." VS "..player[1].win)
  252. end
  253. --io.write(player[1].win.." VS "..player[2].win)
  254. gpu.setBackground(0x000000)
RAW Paste Data