Advertisement
Guest User

Untitled

a guest
Oct 16th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.44 KB | None | 0 0
  1.  
  2. GAME.LUA
  3.        module(..., package.seeall);
  4. function clean (event)
  5.     print("cleaned")
  6. end
  7.     function new ()
  8.         math.randomseed( os.time() )
  9.         local Scene = display.newGroup()
  10.         local bg1 = display.newGroup ()
  11.  
  12.        require "sqlite3"
  13.    
  14.  
  15.         local timeDelay = 1000 --Cat de repede se numara . 1/1 secunda
  16.         local runMode = true
  17.         local bg = display.newImage("bg.png",0,0)
  18.         bg1:insert(bg)
  19.         local lives = 5
  20.         local lifeIcons={}
  21.         local maxLives = 5
  22.         local i
  23.  
  24.         local function health(currentLives)
  25.                for i = 1, currentLives do
  26.                         if lifeIcons[i] then
  27.                                 lifeIcons[i].isVisible = true
  28.                         else
  29.                                 lifeIcons[i] = display.newImage("heart.png")
  30.                                 lifeIcons[i].x =560 + (lifeIcons[i].contentWidth * (i - 1))
  31.                                 lifeIcons[i].y = 30 -- start at 10,10
  32.                                  bg1:insert(lifeIcons[i])
  33.                         end
  34.                 end
  35.                 for i = currentLives + 1, #lifeIcons do
  36.                     if maxLives > 0 then
  37.                         lifeIcons[i].isVisible = false
  38.                     end
  39.                
  40.                 end
  41.         end
  42.         health(maxLives)
  43.  
  44.         local utilitarex = display.contentWidth/1.5
  45.         local utilitarey = display.contentHeight/18
  46.  
  47.         local timerr = display.newImage("timer1.png")
  48.          bg1:insert(timerr)
  49.         timerr.x = utilitarex
  50.         timerr.y = utilitarey
  51.  
  52.         local fiftyfifty = display.newImage("50.png")
  53.          bg1:insert(fiftyfifty)
  54.         fiftyfifty.x =utilitarex+90
  55.         fiftyfifty.y = utilitarey
  56.  
  57.         local nextquestion  = display.newImage("sageata2.png")
  58.          bg1:insert(nextquestion)
  59.         nextquestion.x = utilitarex+180
  60.         nextquestion.y = utilitarey
  61.  
  62.  
  63.         local correct = display.newImage("button_ok.png")
  64.          bg1:insert(correct)
  65.         correct.x = display.contentWidth/2
  66.         correct.y = display.contentHeight/1.4
  67.  
  68.          local gresit = display.newImage("button_wrong.png")
  69.           bg1:insert(gresit)
  70.         gresit.x = display.contentWidth/2
  71.         gresit.y = display.contentHeight/1.4
  72.  
  73.  
  74.         local buton1 = display.newImage("buton.png")
  75.          bg1:insert(buton1)
  76.        
  77.         buton1.xReference = 432/2
  78.         buton1.yReference = 77/2
  79.  
  80.         buton1.x = display.contentWidth /(4.78/2)
  81.         buton1.y = display.contentHeight - display.contentHeight/3
  82.  
  83.         local buton2 = display.newImage("buton.png")
  84.         buton2.xReference = 432/2
  85.         buton2.yReference = 77/2
  86.         bg1:insert(buton2)
  87.  
  88.         buton2.x = display.contentWidth /(4.78/2)
  89.         buton2.y = display.contentHeight - display.contentHeight/8
  90.  
  91.         local buton3 = display.newImage("buton.png")
  92.         buton3.xReference = 432/2
  93.         buton3.yReference = 77/2
  94.         buton3.x = display.contentWidth -display.contentWidth/12
  95.         buton3.y = display.contentHeight - display.contentHeight/3
  96.         bg1:insert(buton3)
  97.  
  98.         local buton4 = display.newImage("buton.png")
  99.         buton4.xReference = 432/2
  100.         buton4.yReference = 77/2
  101.         buton4.x = display.contentWidth -display.contentWidth/12
  102.         buton4.y = display.contentHeight - display.contentHeight/8
  103.         bg1:insert(buton4)
  104.  
  105.         local path = system.pathForFile ( "SQL.db", system.DocumentsDirectory)
  106.         local db = sqlite3.open(path)
  107.  
  108.         local irgroup = display.newGroup()
  109.         local question =display.newText("",10,10,system.nativeFont,36)
  110.         question:setReferencePoint(display.CenterReferencePoint)
  111.         question.x = display.contentWidth/2
  112.         question.y = display.contentHeight/3
  113.         bg1:insert(question)
  114.  
  115.         local raspuns1 = display.newText("",display.contentWidth /(4.78/2),500,system.nativeFont,36)
  116.         raspuns1:setReferencePoint(display.CenterReferencePoint)
  117.         raspuns1.x = display.contentWidth /(4.78/2) -buton1.width/2
  118.         raspuns1.y= display.contentHeight-display.contentHeight/5.5
  119.         bg1:insert(raspuns1)
  120.  
  121.  
  122.         local raspuns2 = display.newText("",display.contentWidth /(4.78/2),10,system.nativeFont,36)
  123.         raspuns2:setReferencePoint(display.CenterReferencePoint)
  124.         raspuns2.x = display.contentWidth /(4.78/2) -buton1.width/2
  125.         raspuns2.y= display.contentHeight-display.contentHeight/2.6
  126.         bg1:insert(raspuns2)
  127.  
  128.         local raspuns3= display.newText("", display.contentWidth-display.contentWidth /(4.78/2),10,system.nativeFont,36)
  129.         raspuns3:setReferencePoint(display.CenterReferencePoint)
  130.         raspuns3.x = display.contentWidth/1.34
  131.         raspuns3.y = display.contentHeight-display.contentHeight/5.5
  132.         bg1:insert(raspuns3)
  133.  
  134.         local raspuns4= display.newText("",10,10,system.nativeFont,36)
  135.         raspuns4:setReferencePoint(display.CenterReferencePoint)
  136.         raspuns4.x = display.contentWidth/1.34
  137.         raspuns4.y = display.contentHeight-display.contentHeight/2.6
  138.         bg1:insert(raspuns4)
  139.  
  140.         irgroup : insert(question)
  141.         irgroup : insert(raspuns1)
  142.         irgroup : insert(raspuns2)
  143.         irgroup : insert(raspuns3)
  144.         irgroup :  insert(raspuns4)
  145.  
  146.         local intrebare
  147.         local raspunscorect
  148.         local  raspunsgresit={}
  149.         local random
  150.  
  151.         local function questionsanswers()
  152.             if maxLives > 0 then
  153.         correct.isVisible = false
  154.         gresit.isVisible = false
  155.         random = math.random(53)
  156.         print(random)
  157.         for row in db:nrows("SELECT *  FROM intrebari where id ="..random) do
  158.                 intrebare = row.Question
  159.                 raspunscorect = row.Correct_Answer
  160.                 raspunsgresit[0]= row.Wrong_Answer1
  161.                 raspunsgresit[1]=row.Wrong_Answer2
  162.                 raspunsgresit[2]=row.Wrong_Answer3
  163.                 raspunsgresit[3]=row.Wrong_Answer4
  164.                                 break
  165.         end
  166.          print(raspunscorect)
  167.  
  168.             question.text = intrebare
  169.             bg1:insert(question)
  170.  
  171.             raspuns2.text = raspunsgresit[0]   --ATENTIE LA ORDINE!!! MI-A FOST LENE
  172.             bg1:insert(raspuns2)
  173.             raspuns1.text = raspunsgresit[1]
  174.             bg1:insert(raspuns1)
  175.             raspuns3.text = raspunsgresit[2]
  176.             bg1:insert(raspuns3)
  177.             raspuns4.text = raspunsgresit[3]
  178.             bg1:insert(raspuns4)
  179.             random = math.random(1,54)
  180.  
  181.         end
  182.         end
  183.        
  184.         if maxLives > 0 then
  185.         questionsanswers()
  186.         end
  187.        
  188.  
  189.         local t = display.newText( "20", 115, 105, "DS-Digital", 50 )
  190.  
  191.         t:setTextColor( 0, 0, 0 )
  192.          local count = 20
  193.         function t:timer( event )
  194.            
  195.  
  196.                 count = 20-event.count
  197.  
  198.  
  199.                 self.text = count
  200.            
  201.            
  202.                 if count == 0 then
  203.                         timer.cancel( event.source )
  204.                         t.text = "20"
  205.                        
  206.                        maxLives = maxLives - 1
  207.                        
  208.                         health(maxLives)
  209.                         questionsanswers()
  210.                        
  211.                         timerID = timer.performWithDelay( timeDelay, self,-1 )
  212.                    
  213.                 end
  214.                
  215.         end
  216. if maxLives > 0 then
  217.         timerID = timer.performWithDelay( timeDelay, t,-1 )
  218.        
  219. end
  220.  
  221.          local function checkcorrectanswer1(event)
  222.             if(raspunsgresit[0]==raspunscorect) then
  223.                 correct.isVisible = true
  224.                 print("Corect")
  225.                 timer.performWithDelay ( 1000, questionsanswers )
  226.                 timer.cancel(timerID)
  227.                     timerID = timer.performWithDelay( timeDelay, t,-1 )
  228.                     t.text = "20"
  229.             else
  230.             gresit.isVisible = true
  231.            
  232.             maxLives = maxLives - 1
  233.              health(maxLives)
  234.              timer.performWithDelay ( 1000, questionsanswers )
  235.                 timer.cancel(timerID)
  236.                     timerID = timer.performWithDelay( timeDelay, t,-1 )
  237.                     t.text = "20"
  238.             timer.performWithDelay ( 1000, questionsanswers )
  239.              end
  240.          end
  241.  
  242.           local function checkcorrectanswer2(event)
  243.             if(raspunsgresit[1]==raspunscorect) then
  244.                 correct.isVisible = true
  245.                 print("Corect")
  246.                 timer.performWithDelay ( 1000, questionsanswers )
  247.                 timer.cancel(timerID)
  248.                     timerID = timer.performWithDelay( timeDelay, t,-1 )
  249.                     t.text = "20"
  250.             else
  251.             gresit.isVisible = true
  252.             maxLives = maxLives - 1
  253.                         health(maxLives)
  254.                         timer.performWithDelay ( 1000, questionsanswers )
  255.                 timer.cancel(timerID)
  256.                     timerID = timer.performWithDelay( timeDelay, t,-1 )
  257.                     t.text = "20"
  258.             timer.performWithDelay ( 1000, questionsanswers )
  259.              end
  260.          end
  261.  
  262.          local function checkcorrectanswer3(event)
  263.             if(raspunsgresit[3]==raspunscorect) then
  264.                 correct.isVisible = true
  265.                 print("Corect")
  266.                
  267.                 timer.cancel(timerID)
  268.                     timerID = timer.performWithDelay( timeDelay, t,-1 )
  269.                     t.text = "20"
  270.                     timer.performWithDelay ( 1000, questionsanswers )
  271.  
  272.             else
  273.             gresit.isVisible = true
  274.             maxLives = maxLives - 1
  275.                         health(maxLives)
  276.                         timer.performWithDelay ( 1000, questionsanswers )
  277.                 timer.cancel(timerID)
  278.                     timerID = timer.performWithDelay( timeDelay, t,-1 )
  279.                     t.text = "20"
  280.             timer.performWithDelay ( 1000, questionsanswers )
  281.              end
  282.          end
  283.  
  284.          local function checkcorrectanswer4(event)
  285.             if(raspunsgresit[2]==raspunscorect) then
  286.                 correct.isVisible = true
  287.                 print("Corect")
  288.                 timer.performWithDelay ( 1000, questionsanswers )
  289.                 timer.cancel(timerID)
  290.                     timerID = timer.performWithDelay( timeDelay, t,-1 )
  291.                     t.text = "20"
  292.             else
  293.             gresit.isVisible = true
  294.             maxLives = maxLives - 1
  295.                         health(maxLives)
  296.                         timer.performWithDelay ( 1000, questionsanswers )
  297.                        
  298.                 timer.cancel(timerID)
  299.                     timerID = timer.performWithDelay( timeDelay, t,-1 )
  300.                     t.text = "20"
  301.             timer.performWithDelay ( 1000, questionsanswers )
  302.                        
  303.              end
  304.          end
  305.  
  306.          --Cele 3 utilitare
  307.  
  308.         local function deletequestions(event)
  309.  
  310.                 questionsanswers()
  311.                 t.text = "20"
  312.                 timer.cancel(timerID)
  313.                     timerID = timer.performWithDelay( timeDelay, t,-1 )
  314.                     nextquestion.isVisible = false
  315.                     nextquestion:removeEventListener ( "tap", deletequestions )
  316.         end
  317.         local function addtimer(event)
  318.              t.text = "20"
  319.                 timer.cancel(timerID)
  320.                     timerID = timer.performWithDelay( timeDelay, t,-1 )
  321.                     timerr.isVisible = false
  322.                     timerr:removeEventListener ( "tap", timerr )
  323.         end
  324.         local function r2answers(event)
  325.  
  326.             if(raspunsgresit[0]~=raspunscorect and raspunsgresit[1]~=raspunscorect) then
  327.             raspuns2.text = ""
  328.             raspuns1.text= ""
  329.             elseif(raspunsgresit[0]~=raspunscorect and raspunsgresit[2]~=raspunscorect) then
  330.             raspuns2.text = ""
  331.             raspuns3.text= ""
  332.             elseif(raspunsgresit[0]~=raspunscorect and raspunsgresit[3]~=raspunscorect) then
  333.             raspuns2.text = ""
  334.             raspuns4.text= ""
  335.  
  336.             elseif(raspunsgresit[1]~=raspunscorect and raspunsgresit[2]~=raspunscorect) then
  337.             raspuns3.text=""
  338.             raspuns1.text=""
  339.  
  340.             elseif(raspunsgresit[1]~=raspunscorect and raspunsgresit[3]~=raspunscorect) then
  341.             raspuns1.text=""
  342.             raspuns4.text=""
  343.  
  344.             elseif(raspunsgresit[2]~=raspunscorect and raspunsgresit[3]~=raspunscorect) then
  345.             raspuns3.text=""
  346.             raspuns2.text=""
  347.  
  348.  
  349.             else
  350.             raspuns1.text = ""
  351.             raspuns4.text= ""
  352.             end
  353.             fiftyfifty.isVisible = false
  354.  
  355.         end
  356.  
  357.         local function clean()
  358.             timer.cancel(timerID)
  359.         end
  360.        
  361.         local function hasHealth()
  362.             if maxLives == 0 then
  363.                
  364.                
  365.             director:changeScene("endgame")
  366.             end
  367.         end
  368.        
  369.         --EventListeners
  370.         fiftyfifty:addEventListener("tap",r2answers)
  371.         nextquestion:addEventListener("tap", deletequestions)
  372.         timerr:addEventListener("tap",addtimer)
  373.         buton1:addEventListener("tap",checkcorrectanswer1)
  374.         buton2:addEventListener("tap",checkcorrectanswer2)
  375.         buton3:addEventListener("tap",checkcorrectanswer3)
  376.         buton4:addEventListener("tap",checkcorrectanswer4)
  377.         Runtime:addEventListener("enterFrame", hasHealth)
  378.  
  379.     Scene:insert(bg1)
  380.     return Scene
  381. end
  382.  
  383.  
  384.  
  385. ENDGAME.LUA
  386. module(..., package.seeall);
  387. function clean (event)
  388.     print("cleaned")
  389. end
  390.  
  391. function new()
  392.      local scene = display.newGroup()
  393.      local GUI = display.newGroup()
  394.  
  395.  
  396.     local bghd = display.newImage("bg.png")
  397.    
  398.     local playa_btn = display.newRect(40,40,500,500)
  399.  
  400.  
  401.      playa_btn:setFillColor(255,255,255)
  402.      playa_btn.scene = "menu"
  403.      
  404.     GUI:insert(bghd)
  405.      GUI:insert(playa_btn)
  406.  
  407.      local function changeScene(e)
  408.                if(e.phase == "ended" or e.phase == "cancelled" ) then
  409.                 print("test")
  410.                    director:changeScene("menu")
  411.                end
  412.      end
  413.      playa_btn:addEventListener ("touch", changeScene)
  414.      scene:insert(GUI)
  415.  
  416.      return scene
  417. end
  418.  
  419.  
  420. MENU.LUA
  421.  
  422. module(..., package.seeall);
  423. function clean (event)
  424.     print("cleaned")
  425. end
  426. function new()
  427.      local scene = display.newGroup()
  428.      local GUI = display.newGroup()
  429.  
  430.  
  431.     local bghd = display.newImage("bghd.png")
  432.    
  433.     local play_btn = display.newText ("Play!", 0, 0, native.systemFont,24)
  434.      play_btn.x = display.contentWidth * 0.5
  435.      play_btn.y = display.contentHeight * 0.9
  436.      play_btn:setTextColor(255,255,255)
  437.      play_btn.scene = "game"
  438.      
  439.     GUI:insert(bghd)
  440.      GUI:insert(play_btn)
  441.  
  442.      local function changeScene(e)
  443.                if(e.phase == "ended" or e.phase == "cancelled" ) then
  444.                    director:changeScene(e.target.scene)
  445.                end
  446.      end
  447.      play_btn:addEventListener ("touch", changeScene)
  448.      scene:insert(GUI)
  449.  
  450.      return scene
  451. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement