gameclickfast 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/gameclickfast.rb +209 -0
  2. metadata +48 -0
@@ -0,0 +1,209 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # Import the library.
4
+ require 'tk'
5
+
6
+ # Parameters.
7
+ Width = 5 # Width of button grid.
8
+ Height = 5 # Height of button grid.
9
+ MinWait = 200 # Smallest button change wait (ms)
10
+ MaxWait = 1400 # Largest button change wait (ms)
11
+ InitWait = 800 # Initial button change wait (ms)
12
+ LossRate = 2000 # Frequency to take away points.
13
+
14
+ # Set defaults. Some we keep in constants to use later.
15
+ BG = '#ccffcc'
16
+ TkOption.add('*background', BG)
17
+ TkOption.add('*activeBackground', '#ddffdd')
18
+ FG = '#006600'
19
+ TkOption.add('*foreground', FG)
20
+ TkOption.add('*activeForeground', FG)
21
+ TkOption.add('*troughColor', '#99dd99')
22
+
23
+ # Root window.
24
+ root = TkRoot.new('background' => BG) { title 'Click Fast' }
25
+
26
+ # Button from the panel
27
+ class PanelButton < TkButton
28
+ private
29
+ # Exchange colors on the button.
30
+ def cswap
31
+ for p in [['background', 'foreground'],
32
+ ['activebackground', 'activeforeground']]
33
+ c = cget(p[0])
34
+ configure(p[0] => cget(p[1]))
35
+ configure(p[1] => c)
36
+ end
37
+ end
38
+ public
39
+ # Initialize the button within the widget sup, at position pos (zero-based)
40
+ # with the number num. When pressed, send the score (+ or -) to cmd.
41
+ # Scorekeeper is an object which implements an up and down methods to
42
+ # receive score changes.
43
+ def initialize(sup, pos, num, scorekeeper)
44
+ super(sup, 'text' => num.to_s, 'command' => proc { self.pushed },
45
+ 'activeforeground' => '#990000', 'activebackground' => '#ffdddd')
46
+ grid('row' => pos / Width + 1, 'column' => pos % Width, 'sticky' => 'news')
47
+ @active = false
48
+ @scorekeeper = scorekeeper
49
+ end
50
+ attr_reader :active
51
+
52
+ # Activate or deactivate the button.
53
+ def activate
54
+ if not @active
55
+ cswap
56
+ @active = true
57
+ end
58
+ end
59
+ def deactivate
60
+ if @active
61
+ cswap
62
+ @active = false
63
+ end
64
+ end
65
+
66
+ # When pushed, send our number, or negative our number, to the scorekeeping
67
+ # command.
68
+ def pushed
69
+ n = self.cget('text').to_i
70
+ if @active
71
+ @scorekeeper.up(n)
72
+ else
73
+ @scorekeeper.down(n)
74
+ end
75
+ end
76
+ end
77
+
78
+ # This class calls reduces the score at the indicated time rate.
79
+ class ScoreTimer
80
+ # This object will call scorekeeper.down(step) each rate ms.
81
+ def initialize(scorekeeper, rate = 500, step = 1)
82
+ @scorekeeper = scorekeeper
83
+ @rate = rate
84
+ @step = step
85
+
86
+ Tk.after(rate, proc { self.change })
87
+ end
88
+
89
+ # Reduce the score periodically
90
+ def change
91
+ @scorekeeper.down(@step)
92
+ Tk.after(@rate, proc { self.change })
93
+ end
94
+ end
95
+
96
+ # This is a box displaying a count-up timer in minutes and seconds to tenths
97
+ # m:ss.d
98
+ class TimeCounter < TkLabel
99
+ # Initialize. Displays zero and starts the ticking event.
100
+ def initialize(root)
101
+ super(root, "text" => '0:00.0', 'anchor' => 'e')
102
+ @count = 0
103
+ Tk.after(100, proc { self.change })
104
+ end
105
+
106
+ # One clock tick (tenths of a second). Increment the counter, then build
107
+ # the new display value.
108
+ def change
109
+ @count += 1
110
+ self.configure('text' =>
111
+ sprintf("%d:%02d.%d",
112
+ @count / 600, (@count / 10) % 60, @count % 10))
113
+ Tk.after(100, proc { self.change })
114
+ end
115
+ end
116
+
117
+ # This is the main application GUI.
118
+ class App
119
+ private
120
+ # Set the score value.
121
+ def setscore(val)
122
+ color = if val < 0 then 'red' else FG end
123
+ @slab.configure('text' => val.to_s, 'foreground' => color)
124
+ end
125
+ public
126
+ # The wait attribute is the amount of time (ms) between button changes.
127
+ attr_writer :wait
128
+
129
+ # Initialize it and have the applicate drawn in the root window.
130
+ def initialize(root)
131
+ # This is the label containing the score. Initially zero.
132
+ @slab = TkLabel.new(root) {
133
+ text "0"
134
+ anchor 'e'
135
+ grid('row' => 0, 'column' => 0, 'columnspan' => Width / 2,
136
+ 'sticky' => 'w')
137
+ }
138
+
139
+ # This is the timer window at upper right.
140
+ TimeCounter.new(root).
141
+ grid('row' => 0, 'column' => Width/2, 'columnspan' => (Width+1)/2,
142
+ 'sticky' => 'e')
143
+
144
+ # Create the buttons. First, make an array of numbers from 1 to the
145
+ # number of buttons, then create the buttons, each labelled with a
146
+ # number chosen at random from the list, so thare are no repeats.
147
+ nums = (1..Height*Width).to_a;
148
+ @buts= [ ]
149
+ for n in (0...Height*Width)
150
+ pos = rand(nums.length)
151
+ @buts.push(PanelButton.new(root, n, nums[pos], self))
152
+ nums.delete_at(pos)
153
+ end
154
+
155
+ # This creates the slider to adjust the speed of the game. The proc is
156
+ # called whenever the slider changes, and is sent the new setting.
157
+ scale = TkScale.new('command' => proc { |v| self.wait = v.to_i } ) {
158
+ orient "horizontal" # Which way the slider goes.
159
+ from MinWait # Value of smallest setting
160
+ to MaxWait # Value of largest setting
161
+ showvalue false # Don't show the numeric value of the setting.
162
+ grid('row' => Height + 1, 'column' => 1, 'columnspan' => Width - 2,
163
+ 'sticky' => 'news')
164
+ }
165
+ scale.set(InitWait)
166
+
167
+ # Labels by the slider.
168
+ TkLabel.new {
169
+ text "Fast"
170
+ anchor "w"
171
+ grid("row" => Height + 1, 'column' => 0, 'sticky' => 'w')
172
+ }
173
+ TkLabel.new {
174
+ text "Slow"
175
+ anchor "e"
176
+ grid("row" => Height + 1, 'column' => Width-1, 'sticky' => 'e')
177
+ }
178
+
179
+ @wait = InitWait
180
+
181
+ # Decrement the score every LossRate period.
182
+ @timer = ScoreTimer.new(self, LossRate)
183
+ self.change
184
+ end
185
+
186
+ # Actions to increase or decrease the score.
187
+ def up(delta)
188
+ setscore(@slab.cget('text').to_i + delta)
189
+ end
190
+ def down(delta)
191
+ setscore(@slab.cget('text').to_i - delta)
192
+ end
193
+
194
+ # Change (or set, if none is yet set) the active button. It deactivates
195
+ # the button in @buts[0], It then chooses some other button at random,
196
+ # activates that and swaps it into position 0.
197
+ def change
198
+ @buts[0].deactivate
199
+ pos = rand(@buts.length - 1) + 1
200
+ @buts[0], @buts[pos] = @buts[pos], @buts[0]
201
+ @buts[0].activate
202
+
203
+ Tk.after(@wait, proc { self.change })
204
+ end
205
+ end
206
+
207
+ a = App.new(root)
208
+
209
+ Tk.mainloop
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gameclickfast
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - mahesh dey
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-19 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! 'A game file u need to write [require ''gameclickfast''] in ur ruby
15
+ file '
16
+ email: maheshdey9@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/gameclickfast.rb
22
+ homepage: http://maheshdey9.wix.com/mypics
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.24
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: This is a game file, u need to create a ruby file for eg:samplr.rb and u
46
+ need to write [require 'gameclickfast'] in ur ruby file and save it. Now u can run
47
+ it using cdm and u can see the game where u have to click on the highlighted number.
48
+ test_files: []