fork download
  1. print( '=== love test === : '.._VERSION )
  2.  
  3. require 'class'
  4. require 'LoveUtil'
  5. require 'EnglishData' -- 問題文データ
  6.  
  7. --[[
  8. # 概要
  9. 瞬間英作文ゲーム。
  10.  
  11. # ゴール
  12. - 繰り返しやりたくなるようにすること
  13. - まんべんなく出題されること
  14.  
  15. # 機能
  16. - スコア
  17. - スコア保存機能
  18. - ミス回数集計機能
  19. - 出題グループ選択機能
  20.  
  21. ]]
  22.  
  23. function strip( str )
  24. local beginIndex = 1
  25. local endIndex = 1
  26. for i=1, #str do
  27. if str:sub(i,i) ~= ' ' and
  28. str:sub(i,i) ~= '\t' then
  29. beginIndex = i
  30. break
  31. end
  32. end
  33. for i=#str, 0, -1 do
  34. if str:sub(i,i) ~= ' ' and
  35. str:sub(i,i) ~= '\t' then
  36. endIndex = i
  37. break
  38. end
  39. end
  40. return str:sub( beginIndex, endIndex )
  41. end
  42.  
  43. function GenerateTableOrder( tableCount, is_shuffle, is_reverse )
  44. ret = {}
  45. if is_reverse then
  46. for i=1, tableCount do
  47. ret[i] = tableCount - i + 1
  48. end
  49. else
  50. for i=1, tableCount do
  51. ret[i] = i
  52. end
  53. end
  54. if is_shuffle then
  55. for i=1, tableCount*5 do
  56. local a = math.random( 10 )
  57. local b = math.random( 10 )
  58. ret[a], ret[b] = ret[b], ret[a]
  59. end
  60. end
  61. return ret
  62. end
  63.  
  64. ----------------------------------------------------------------------
  65. -- Game
  66. ----------------------------------------------------------------------
  67. Game = Class()
  68. function Game:init()
  69. -- self.Font
  70. -- self.EnglishDataLst
  71. -- self.LastResumeParam
  72. -- self.Coroutine
  73. end
  74.  
  75. function Game:load()
  76. self.Coroutine = coroutine.create( function( ... ) self:CoMain( ... ) end )
  77. coroutine.resume( self.Coroutine )
  78. end
  79. function Game:keypressed( key, unicode )
  80. Game.CoResume( self.Coroutine, Game.ResumeType.Keyboard, key, unicode )
  81. end
  82. function Game:update( dt )
  83. Game.CoResume( self.Coroutine, Game.ResumeType.Update )
  84. end
  85. function Game:draw()
  86. Game.CoResume( self.Coroutine, Game.ResumeType.Draw )
  87. end
  88.  
  89. function Game:GetEnglishDataCount()
  90. return #self.EnglishDataLst
  91. end
  92. function Game:GetEnglishData( index )
  93. return self.EnglishDataLst[index]
  94. end
  95.  
  96. function Game.DrawJapanese( text )
  97. LoveUtil.DrawTextCenter( text, 0, love.graphics.getHeight() / 2 - 40 )
  98. end
  99.  
  100. function Game.DrawInputString( text, pronpt, cursor )
  101. local posY = love.graphics.getHeight() / 2
  102. local inputStr = text
  103. inputStrPosX, inputStrWidth = LoveUtil.DrawTextCenter( inputStr, 0, posY )
  104. if pronpt then
  105. love.graphics.print( '>', inputStrPosX - 13, posY )
  106. end
  107. if cursor then
  108. love.graphics.print( '|', inputStrPosX + inputStrWidth, posY )
  109. end
  110. end
  111.  
  112. Game.ResumeType = { Update=1, Draw=2, Keyboard=3 }
  113. function Game:CoYield()
  114. self.LastResumeParam = { coroutine.yield() }
  115. return self.LastResumeParam
  116. end
  117. function Game.CoResume( ... )
  118. local result = { coroutine.resume( ... ) }
  119. if result[1] == false then -- コルーチン内でエラーがあった場合
  120. print( result[2] )
  121. end
  122. end
  123.  
  124. function Game:CoMain()
  125. -- 初期化
  126. self.Font = love.graphics.newFont( 'RictyDiscord-Regular.ttf', 20 )
  127. love.graphics.setFont( self.Font )
  128. self.EnglishDataLst = {}
  129. for i=1, #EnglishData_SyunkanEisakubun do
  130. self.EnglishDataLst[i] = EnglishData.new( EnglishData_SyunkanEisakubun[i] )
  131. end
  132.  
  133. self:CoYield()
  134.  
  135. while true do
  136. -- モードセレクト画面
  137. local selectIndex, is_shuffle = self:CoModeSelect()
  138.  
  139. -- 項目出現順番を決定
  140. local ItemOrder = GenerateTableOrder( self.EnglishDataLst[selectIndex]:GetItemCount(), is_shuffle )
  141. local CurrentItemIndex = 1
  142.  
  143. -- 問題回答ループ
  144. while true do
  145. local CurrentItemIndexRaw = ItemOrder[CurrentItemIndex]
  146. local japaneseString = self.EnglishDataLst[selectIndex]:GetItemTextJapanese( CurrentItemIndexRaw )
  147. local englishString = self.EnglishDataLst[selectIndex]:GetItemTextEnglish ( CurrentItemIndexRaw )
  148. local result = self:CoInputAnswer( japaneseString, englishString )
  149. if result == 'Next' then
  150. -- 次の問題へ
  151. CurrentItemIndex = CurrentItemIndex + 1
  152.  
  153. -- 終了チェック
  154. if CurrentItemIndex > self.EnglishDataLst[selectIndex]:GetItemCount() then
  155. break
  156. end
  157. elseif result == 'Escape' then
  158. -- 中止
  159. break
  160. end
  161. end
  162.  
  163. end
  164. end
  165.  
  166. function Game:CoModeSelect()
  167. local is_shuffle = false
  168. local CursorPos = 1
  169. local resumeParam = self.LastResumeParam
  170. while true do
  171. -- キーボード入力の場合
  172. if resumeParam[1] == Game.ResumeType.Keyboard then
  173. local key, unicode = resumeParam[2], resumeParam[3]
  174. if key == 'up' then
  175. CursorPos = CursorPos - 1
  176. if CursorPos < 1 then CursorPos = 1 end
  177. end
  178. if key == 'down' then
  179. CursorPos = CursorPos + 1
  180. if CursorPos > self:GetEnglishDataCount() then CursorPos = self:GetEnglishDataCount() end
  181. end
  182. if key == 'return' then
  183. return CursorPos, is_shuffle
  184. end
  185. if key == 's' then
  186. is_shuffle = not is_shuffle
  187. end
  188. -- 描画の場合
  189. elseif resumeParam[1] == Game.ResumeType.Draw then
  190. LoveUtil.DrawTextCenter( '問題選択', 0, 20 )
  191. LoveUtil.DrawTextCenter( 'シャッフル(sキーで切り替え)', 0, love.graphics.getHeight() - 40 )
  192. if is_shuffle then
  193. LoveUtil.DrawTextCenter( 'ON', 0, love.graphics.getHeight() - 20 )
  194. else
  195. LoveUtil.DrawTextCenter( 'OFF', 0, love.graphics.getHeight() - 20 )
  196. end
  197. for i=1, self:GetEnglishDataCount() do
  198. -- love.graphics.print( i..' : "'..self:GetEnglishData( i ):GetTitle()..'"', love.graphics.getWidth() / 2 - 60, i * 20 )
  199. local posY = i * 20 + 40
  200. local posX, width = LoveUtil.DrawTextCenter( self:GetEnglishData( i ):GetTitle(), 0, posY )
  201. if i == CursorPos then
  202. local r, g, b, a = love.graphics.getColor()
  203. love.graphics.setColor( 128, 128, 255, 96 )
  204. love.graphics.rectangle( 'fill', posX, posY, width, 20 )
  205. love.graphics.setColor( r, g, b, a )
  206. end
  207. end
  208. end
  209. resumeParam = self:CoYield()
  210. end
  211. end
  212.  
  213. function Game:CoInputAnswer( japaneseString, englishString )
  214. local resumeParam = self.LastResumeParam
  215. local thisFrameInputString = ''
  216. local currentCollectCount = 0
  217. local beginTime = love.timer.getTime()
  218. local GetTimeElapse = function() return love.timer.getTime() - beginTime end
  219. -- print ( 'japaneseString:'..japaneseString )
  220. -- print ( 'englishString :'..englishString )
  221. while true do
  222. -- キーボード入力の場合
  223. if resumeParam[1] == Game.ResumeType.Keyboard then
  224. local key, unicode = resumeParam[2], resumeParam[3]
  225. if unicode > 31 and unicode < 127 then
  226. thisFrameInputString = thisFrameInputString .. string.char(unicode)
  227. end
  228. if key == 'escape' then
  229. return 'Escape'
  230. end
  231. -- 更新処理の場合
  232. elseif resumeParam[1] == Game.ResumeType.Update then
  233. if englishString:lower():find( thisFrameInputString:lower(), currentCollectCount + 1, true ) == currentCollectCount + 1 then
  234. currentCollectCount = currentCollectCount + #thisFrameInputString
  235. -- 文章が完成したら、次の問題へ
  236. if currentCollectCount == #englishString then
  237. return 'Next'
  238. end
  239. end
  240. thisFrameInputString = ''
  241. -- 描画の場合
  242. elseif resumeParam[1] == Game.ResumeType.Draw then
  243. Game.DrawJapanese( japaneseString )
  244. Game.DrawInputString( englishString:sub( 1, currentCollectCount ), true--[[pronpt]], ( GetTimeElapse() % 2 ) < 1.0 --[[cursor]] )
  245. end
  246. resumeParam = self:CoYield()
  247. end
  248. end
  249.  
  250. ----------------------------------------------------------------------
  251. -- Love Callback
  252. ----------------------------------------------------------------------
  253. function love.load()
  254. GameIns = Game.new()
  255. GameIns:load()
  256. end
  257.  
  258. function love.keypressed(key, unicode)
  259. GameIns:keypressed(key, unicode)
  260. end
  261.  
  262. function love.update(dt)
  263. GameIns:update(dt)
  264. end
  265.  
  266. function love.draw()
  267. GameIns:draw()
  268. end
Runtime error #stdin #stdout 0.02s 2496KB
stdin
Standard input is empty
stdout
=== love test === : Lua 5.1