Guest User

Untitled

a guest
Jun 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1.  
  2. # Compile: coffee -c game.coffee
  3. # Run: coffee game.coffee
  4. # Dependencies: underscore.js / userscore.coffee and backbone.js
  5.  
  6. # Check if the libs we need are already defined
  7. # When running in browser you will need to bundle or add script tags to include these libs
  8. if not _?
  9. require('underscore')
  10. if Backbone?
  11. Events = Backbone.Events
  12. else
  13. {Events} = require('./backbone.js')
  14.  
  15. class GameTimer
  16.  
  17. constructor: (@time, @interval) ->
  18. _.extend(this, Events)
  19. @interval ?= 1000
  20.  
  21. start: ->
  22. @counter = 0
  23. @ticker = setInterval( (=> @tick()) , @interval)
  24. @trigger('start')
  25.  
  26. tick: ->
  27. @counter++
  28. @trigger('tick', {total: @time, counter: @counter, remaining: @time-@counter})
  29. if @counter >= @time
  30. clearInterval(@ticker)
  31. @trigger('finished')
  32.  
  33. pause: ->
  34. clearInterval(@ticker)
  35. @trigger('paused')
  36.  
  37. resume: ->
  38. @ticker = setInterval( (=> @tick()) , @interval)
  39. @trigger('resume')
  40.  
  41. class Game
  42.  
  43. constructor: (@data)->
  44. console.log("new game, game has " + @data['levels'].length+ " levels")
  45.  
  46. startLevel: (level)->
  47. @level = _(@data['levels']).select((x)->x['name']==level)[0]
  48. @position = 0
  49. @score = 0
  50. @timer = new GameTimer(@level['time'])
  51. @timer.start()
  52. @timer.pause()
  53. @timer.bind('finished', (=> @outOfTime()))
  54. @showQuestion()
  55.  
  56. showQuestion: ->
  57. @timer.resume()
  58. @question = @level['questions'][@position]
  59. console.log(@question['question'])
  60. _(@question['answers']).each (answer) ->
  61. console.log(answer)
  62. @score++
  63.  
  64. answer: (idx)->
  65. if @question['answer'] is idx
  66. console.log('Correct answer!')
  67. @timer.pause()
  68. else
  69. console.log('Incorrect answer! Try again??')
  70. @timer.pause()
  71.  
  72. outOfTime: ->
  73. console.log("Too slow!")
  74. @gameOver()
  75.  
  76. gameOver: ->
  77. console.log("You scored: "+@score)
  78.  
  79. data =
  80. levels: [
  81. {
  82. name: 'easy',
  83. time: 10,
  84. questions: [
  85. { question: "what is ?", answers: ['foo','boo','coo'], answer: 0 }
  86. { question: "what are ?", answers: ['foo','boo','coo'], answer: 2 }
  87. { question: "what do ?", answers: ['foo','boo','coo'], answer: 1 }
  88. ]
  89. },{
  90. name: 'medium'
  91. time: 120,
  92. questions: [
  93. { question: "what is ?", answers: ['foo','boo','coo'], answer: 0 }
  94. { question: "what are ?", answers: ['foo','boo','coo'], answer: 2 }
  95. { question: "what do ?", answers: ['foo','boo','coo'], answer: 1 }
  96. ],
  97. }
  98. ]
  99.  
  100. if not window?
  101. game = new Game(data)
  102. game.startLevel('easy')
Add Comment
Please, Sign In to add comment