-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathKonami.scala
64 lines (53 loc) · 1.61 KB
/
Konami.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.awt.BorderLayout
import java.awt.event.KeyEvent
import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.SwingConstants
import rx.lang.scala.Observable
import rx.observables.SwingObservable
import rx.lang.scala.JavaConversions
class Win1 extends JFrame {
val label = new JLabel("")
def run = {
initLayout
val konami = Observable.just(
38, // up
38, // up
40, // down
40, // down
37, // left
39, // right
37, // left
39, // right
66, // b
65 // a
)
val pressedKeys = JavaConversions.toScalaObservable(SwingObservable.fromKeyEvents(this))
.filter(_.getID() == KeyEvent.KEY_RELEASED)
.map(_.getKeyCode())
val bingo = pressedKeys.sliding(10, 1)
.flatMap(window => (window zip konami).forall(p => p._1 == p._2))
.filter(identity)
bingo.subscribe(_ => label.setText(label.getText() + " KONAMI "))
}
def initLayout = {
setLayout(new BorderLayout)
label.setHorizontalAlignment(SwingConstants.CENTER)
add(label, BorderLayout.CENTER)
setSize(400, 400)
setResizable(false)
setTitle("Enter the Konami Code!")
setVisible(true)
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
}
}
object Konami {
def main(args: Array[String]): Unit = {
// Schedule a job for the event-dispatching thread: creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable {
override def run(): Unit = {
new Win1().run
}
});
}
}