Advertisement
Guest User

Untitled

a guest
Feb 29th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. tmr = Timer.new()
  2. time_left = 20
  3. game_over = true
  4. idx = 1
  5. Graphics.init()
  6. Sound.init()
  7. clap = Sound.openOgg(System.currentDirectory().."sfx/clap.ogg", false)
  8. boo = Sound.openOgg(System.currentDirectory().."sfx/boo.ogg", false)
  9. f1 = Graphics.loadImage(System.currentDirectory().."faces/face1.png")
  10. f2 = Graphics.loadImage(System.currentDirectory().."faces/face2.png")
  11. f3 = Graphics.loadImage(System.currentDirectory().."faces/face3.png")
  12. f4 = Graphics.loadImage(System.currentDirectory().."faces/face4.png")
  13. w = Graphics.loadImage(System.currentDirectory().."faces/wanted.png")
  14. faces = {f1, f2, f3, f4}
  15. face_width = 30
  16. face_height = 27
  17. voices = {"Start new game","Exit Game"}
  18. red = Color.new(255, 0, 0)
  19. white = Color.new(255, 255, 255)
  20. yellow = Color.new(255, 255, 0)
  21. oldpad = 0 -- Prevents interpreter error on startup
  22. wrong_faces = {}
  23. function SetGame(stage)
  24. wrong_faces = {}
  25. h, m, s = System.getTime()
  26. math.randomseed(h*3600+m*60+s)
  27. winner_id = math.random(1, 4)
  28. winner_x = math.random(0, 320) - 5
  29. winner_y = math.random(0, 240) - 5
  30.  
  31. num_faces = stage * 4
  32. i = 1
  33. while i <= num_faces do
  34. face_id = math.random(1, 4)
  35. while face_id == winner_id do
  36. face_id = math.random(1, 4)
  37. end
  38. face_x = math.random(0, 320) - 5
  39. face_y = math.random(0, 240) - 5
  40. table.insert(wrong_faces, {["id"] = face_id, ["x"] = face_x, ["y"] = face_y})
  41. i = i + 1
  42. end
  43. end
  44.  
  45. while true do
  46. Screen.refresh()
  47. Screen.clear(TOP_SCREEN)
  48. Screen.clear(BOTTOM_SCREEN)
  49. pad = Controls.read()
  50. -- Main menu
  51. if game_over then
  52. Screen.debugPrint(5, 5, "WANT3D v.1.2", red, TOP_SCREEN)
  53. for i, voice in pairs(voices) do
  54. if i == idx then
  55. color = red
  56. x = 10
  57. else
  58. color = white
  59. x = 5
  60. end
  61. Screen.debugPrint(x, 50 + 15 * i, voice, color, TOP_SCREEN)
  62. end
  63. if Controls.check(pad, KEY_A) and not Controls.check(oldpad, KEY_A) then
  64. if idx == 1 then -- Start new game
  65. game_over = false
  66. found = false
  67. tmr = Timer.new()
  68. game_started = false
  69. lv = 1
  70. SetGame(lv)
  71. elseif idx == 2 then -- Exit Game
  72. for i, face in pairs(faces) do
  73. Graphics.freeImage(face)
  74. end
  75. Graphics.freeImage(w)
  76. Sound.close(clap)
  77. Sound.close(boo)
  78. Sound.term()
  79. Graphics.term()
  80. System.exit()
  81. end
  82. elseif Controls.check(pad, KEY_DUP) and not Controls.check(oldpad, KEY_DUP) then
  83. idx = idx - 1
  84. if idx == 0 then
  85. idx = #voices
  86. end
  87. elseif Controls.check(pad, KEY_DDOWN) and not Controls.check(oldpad, KEY_DDOWN) then
  88. idx = idx + 1
  89. if idx > #voices then
  90. idx = 1
  91. end
  92. end
  93. else
  94. -- Game
  95. if game_started then
  96. if Timer.getTime(tmr) > (time_left * 1000) and not found then
  97. game_over = true
  98. Timer.destroy(tmr)
  99. to_confirm = true
  100. while to_confirm do
  101. Screen.refresh()
  102. Screen.clear(TOP_SCREEN)
  103. Screen.clear(BOTTOM_SCREEN)
  104. Graphics.initBlend(BOTTOM_SCREEN)
  105. Graphics.drawImage(winner_x, winner_y, faces[winner_id])
  106. Graphics.termBlend()
  107. Screen.debugPrint(120,50,"Game Over!", red, TOP_SCREEN)
  108. Screen.debugPrint(130,80,"Score: " .. (lv - 1), white, TOP_SCREEN)
  109. Screen.debugPrint(75,95,"Press A to return menu", white, TOP_SCREEN)
  110. pad = Controls.read()
  111. if Controls.check(pad, KEY_A) then
  112. game_started = false
  113. time_left = 30
  114. game_over = true
  115. to_confirm = false
  116. end
  117. Screen.flip()
  118. Screen.waitVblankStart()
  119. end
  120. elseif found then
  121. Graphics.initBlend(BOTTOM_SCREEN)
  122. Graphics.drawImage(winner_x, winner_y, faces[winner_id])
  123. Graphics.termBlend()
  124. if Timer.getTime(tmr) > 3000 then
  125. SetGame(lv)
  126. Timer.reset(tmr)
  127. game_started = false
  128. found = false
  129. end
  130. else
  131. if Controls.check(pad, KEY_TOUCH) and not Controls.check(oldpad, KEY_TOUCH) then
  132. x, y = Controls.readTouch()
  133. if x > winner_x and x < winner_x + face_width and y > winner_y and y < winner_y + face_height then
  134. time_left = math.ceil(time_left + 5 - (Timer.getTime(tmr) / 1000))
  135. Timer.reset(tmr)
  136. found = true
  137. lv = lv + 1
  138. Sound.play(clap, NO_LOOP)
  139. else
  140. time_left = time_left - 3
  141. Sound.play(boo, NO_LOOP)
  142. end
  143. elseif Controls.check(pad, KEY_START) then
  144. game_started = false
  145. time_left = 30
  146. game_over = true
  147. end
  148. Graphics.initBlend(TOP_SCREEN)
  149. Graphics.drawImage(150, 25, w)
  150. Graphics.drawImage(170, 75, faces[winner_id])
  151. Graphics.termBlend()
  152. Graphics.initBlend(BOTTOM_SCREEN)
  153. Graphics.drawScaleImage(winner_x, winner_y, faces[winner_id], 0.5, 0.5)
  154. for i, wrong in pairs(wrong_faces) do
  155. Graphics.drawScaleImage(wrong.x, wrong.y, faces[wrong.id], 0.5, 0.5)
  156. end
  157. Graphics.termBlend()
  158. if time_left - (Timer.getTime(tmr) / 1000) < 0 then
  159. Screen.debugPrint(190,200, "0", white, TOP_SCREEN)
  160. else
  161. Screen.debugPrint(190,200, math.ceil(time_left - (Timer.getTime(tmr) / 1000)), white, TOP_SCREEN)
  162. end
  163. if not game_started then
  164. Timer.destroy(tmr)
  165. end
  166. end
  167. Screen.debugPrint(2, 223, "Score: " .. (lv - 1), white, TOP_SCREEN)
  168. else
  169. if Timer.getTime(tmr) < 3000 then
  170. Screen.debugPrint(120, 150, "You must find...", Color.new(255, 255, 255), TOP_SCREEN)
  171. else
  172. Timer.reset(tmr)
  173. game_started = true
  174. end
  175. end
  176.  
  177. end
  178.  
  179. Screen.flip()
  180. Screen.waitVblankStart()
  181. oldpad = pad
  182. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement