tumblr-game 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fb49313b5d2341aa1d5792bd1b0edca7085443e8
4
+ data.tar.gz: 6f73ae46e8a2dd4e9f29d4d5300d5d0b19e9c8c2
5
+ SHA512:
6
+ metadata.gz: 07917b3af2116ff2c74d6d5b8e12669d3bb96fc193e2b3c83bee7f23a4ca8590cb667ac683004ab20d2843cf73ed09eb002a66f6307550e81910ef48ff71d7c3
7
+ data.tar.gz: 67d4cbc6426b34bbdf111ec8ec73e43e7929f214bddd51f7229ed81433a8496a7877610ba2e655aa0760670c7be5663a96ab711ead3c79f4da2958a782c984da
data/bin/Main ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ require 'tumblr-game'
3
+
4
+ myGame = Game.new(1250, 900)
5
+ myGame.start
data/lib/game.rb ADDED
@@ -0,0 +1,33 @@
1
+ require_relative 'tumblr-game/gosu.rb'
2
+ require_relative 'tumblr-game/tumblr.rb'
3
+
4
+ class Game
5
+
6
+ def initialize(w, h) # dimensions of 1250 x 900 are appropriate
7
+ @width = w
8
+ @height = h
9
+ end
10
+
11
+ def start
12
+ # ask for user input to get the blog name
13
+ puts "Enter the name of the blog: "
14
+ blogName = gets.chop
15
+
16
+ if blogName.empty? then
17
+ puts "Error: Please enter a Tumblr blog name."
18
+ else
19
+ ## create a new TumblrAPIObject so we can issue calls to the tumblr api for data
20
+ tumblr = TumblrAPIObject.new(blogName)
21
+
22
+ ## query the API. if there were enough results then advance, else issue an error
23
+ if (tumblr.query == 5) then
24
+ tumblr.create_images # create and save the images for the game
25
+
26
+ window = MyWindow.new(@width, @height) # start the game
27
+ window.show
28
+ else
29
+ puts "Error downloading pictures from Tumblr. Please check your internet connection or the blog name you entered. Blogs need at least 5 pictures."
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,60 @@
1
+ require_relative 'tile.rb'
2
+
3
+ class Board
4
+
5
+ ## initialize function of the board class: this will accept 10 parameters that are each a path to an image
6
+ # it will then populate the tile array appropriately with each image
7
+ def initialize(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10)
8
+ @tile_Array = Array.new(10)
9
+ @tile_Array[0] = t1
10
+ @tile_Array[1] = t2
11
+ @tile_Array[2] = t3
12
+ @tile_Array[3] = t4
13
+ @tile_Array[4] = t5
14
+ @tile_Array[5] = t6
15
+ @tile_Array[6] = t7
16
+ @tile_Array[7] = t8
17
+ @tile_Array[8] = t9
18
+ @tile_Array[9] = t10
19
+
20
+ # counter that will keep track of how many matches have occurred. when this counter reaches 5 before the time limit ends, the player wins
21
+ @matchedCount = 0
22
+ end
23
+
24
+ # draw function of the board class: this will go through the tile array and draw each tile
25
+ def draw_board
26
+ for i in 0..9
27
+ @tile_Array[i].draw
28
+ end
29
+ end
30
+
31
+ # this function will flip a given tile to the face indicated by the parameter bool. false = facedown, true = face up
32
+ def flip_tile(ind, bool)
33
+ @tile_Array[ind].flip(bool)
34
+ end
35
+
36
+ # this function will get the id from the desired tile
37
+ def get_id(ind)
38
+ return @tile_Array[ind].return_id
39
+ end
40
+
41
+ # this function will set a tile to its "matched" configuration
42
+ def matchTile(ind)
43
+ @tile_Array[ind].set_match
44
+ end
45
+
46
+ # this function will get the matched status of a specific tile and return it
47
+ def is_matched(ind)
48
+ return @tile_Array[ind].return_match
49
+ end
50
+
51
+ # this function will increment the match counter to reflect a user successfully matching tiles
52
+ def inc_match
53
+ @matchedCount += 1
54
+ end
55
+
56
+ # this function will return the amount of matched tiles
57
+ def return_count
58
+ return @matchedCount
59
+ end
60
+ end
@@ -0,0 +1,342 @@
1
+ require 'gosu'
2
+ require_relative 'board.rb'
3
+ require_relative 'tile.rb'
4
+ require_relative 'timer.rb'
5
+
6
+ class MyWindow < Gosu::Window
7
+
8
+ ## constants that will represent the background color for the window when the user loses
9
+ LTOP_COLOR = Gosu::Color.new(255, 225, 100, 100)
10
+ LBOTTOM_COLOR = Gosu::Color.new(255, 200, 125, 125)
11
+
12
+ ## constants that will represent the background color for the window when the user wins
13
+ WTOP_COLOR = Gosu::Color.new(255, 100, 225, 100)
14
+ WBOTTOM_COLOR = Gosu::Color.new(255, 125, 225, 125)
15
+
16
+ ## constants that will represent the background color for the window
17
+ TOP_COLOR = Gosu::Color.new(255, 44, 71, 98)
18
+ BOTTOM_COLOR = Gosu::Color.new(255, 125, 125, 125)
19
+
20
+ def initialize(w, h)
21
+
22
+ ## define variables for the width and height of the window
23
+ @width = w
24
+ @height = h
25
+
26
+ ## variables for WIN or LOSE state
27
+ @win = false
28
+ @lose = false
29
+
30
+ ## initialize the window and caption it
31
+ super(@width, @height, false)
32
+ self.caption = 'Tumblr Image Match!'
33
+
34
+ ## define a variable that indicates what state the game is in
35
+ # when phase = 0, that indicates that the user is in the "picking" stage of the game and is selecting a face down card for an
36
+ # initial guess
37
+ # when phase = 1, that indicates that the user is in the "matching" stage of the game and is selecting another card to see if it
38
+ # matches the previous one. after the user clicks the mouse, the images will be shown and then they will be left face up if they match,
39
+ # otherwise both cards will go back to facedown. phase is always set to 0 after this stage.
40
+ @phase = 0
41
+ @frameCounter = 0
42
+
43
+ ## define two variables that indicates the ID of the first card that was selected when phase was 0 and the ID of the second card selected when
44
+ # phase = 1
45
+ @picked = 0
46
+ @picked2 = 0
47
+
48
+ # variable that indicates the second card was actually picked
49
+ @pickedSecond = false
50
+
51
+ ## initialize a Timer object that will keep track of the time remaining for the given game
52
+ # also create a Gosu font object that will be passed to the Timer object
53
+ font = Gosu::Font.new(self, Gosu::default_font_name, 20)
54
+ @timer = Timer.new(font, @width/2, @height/2)
55
+
56
+ ## create a font for the end of the game, either win or lose
57
+ @endFont = Gosu::Font.new(self, Gosu::default_font_name, 20)
58
+
59
+ # create a pseudorandom number generator for shuffling
60
+ prng = Random.new(Random.new_seed())
61
+
62
+ # create a hash of the image names that we are using along with their associated id
63
+ imgNames = Hash[
64
+ 'image1.png' => 0,
65
+ 'image2.png' => 1,
66
+ 'image3.png' => 2,
67
+ 'image4.png' => 3,
68
+ 'image5.png' => 4,
69
+ 'image6.png' => 0,
70
+ 'image7.png' => 1,
71
+ 'image8.png' => 2,
72
+ 'image9.png' => 3,
73
+ 'image10.png' => 4 ]
74
+
75
+ ## Durstenfield's Shuffling Algorithm
76
+ # convert the hash from above into a two-dimensional array that will be shuffled in order to
77
+ # randomize the appearance of the images on the board
78
+ imgArr = imgNames.to_a
79
+ for i in(9).downto(1)
80
+ j = prng.rand(i)
81
+ tempK = imgArr[i][0]
82
+ tempV = imgArr[i][1]
83
+ imgArr[i][0] = imgArr[j][0]
84
+ imgArr[i][1] = imgArr[j][1]
85
+ imgArr[j][0] = tempK
86
+ imgArr[j][1] = tempV
87
+ end
88
+
89
+ # create the Gosu images so they can be put in to each Tile object
90
+ chk = Gosu::Image.new(self, "check.png", true)
91
+ img0 = Gosu::Image.new(self, "tumblr.png", true) #the back of each card
92
+ img1 = Gosu::Image.new(self, imgArr[0][0], true)
93
+ img2 = Gosu::Image.new(self, imgArr[1][0], true)
94
+ img3 = Gosu::Image.new(self, imgArr[2][0], true)
95
+ img4 = Gosu::Image.new(self, imgArr[3][0], true)
96
+ img5 = Gosu::Image.new(self, imgArr[4][0], true)
97
+ img6 = Gosu::Image.new(self, imgArr[5][0], true)
98
+ img7 = Gosu::Image.new(self, imgArr[6][0], true)
99
+ img8 = Gosu::Image.new(self, imgArr[7][0], true)
100
+ img9 = Gosu::Image.new(self, imgArr[8][0], true)
101
+ img10 = Gosu::Image.new(self, imgArr[9][0], true)
102
+
103
+ # initialize the game board with the tile objects
104
+ @game_board = Board.new( Tile.new(img1, 0, 0, img0, imgArr[0][1], chk),
105
+ Tile.new(img2, 250, 0, img0, imgArr[1][1], chk),
106
+ Tile.new(img3, 500, 0, img0, imgArr[2][1], chk),
107
+ Tile.new(img4, 750, 0, img0, imgArr[3][1], chk),
108
+ Tile.new(img5, 1000, 0, img0, imgArr[4][1], chk),
109
+ Tile.new(img6, 0, 500, img0, imgArr[5][1], chk),
110
+ Tile.new(img7, 250, 500, img0, imgArr[6][1], chk),
111
+ Tile.new(img8, 500, 500, img0, imgArr[7][1], chk),
112
+ Tile.new(img9, 750, 500, img0, imgArr[8][1], chk),
113
+ Tile.new(img10, 1000, 500, img0, imgArr[9][1], chk) )
114
+ end
115
+
116
+ ## update function. this function contains all of the game logic
117
+ def update
118
+ ## only allow the user to pick tiles if the frameCounter is at 0
119
+ # this means that the user can only pick tiles when two tiles are not being displayed
120
+ # after the 96 frames of display are over, we then call the check_match function and set the frameCounter back to 0
121
+ if @frameCounter == 0 then
122
+ pick_tiles
123
+ elsif @frameCounter == 96 then
124
+ check_match
125
+ check_win
126
+ @frameCounter = 0
127
+ end
128
+
129
+ ## if the user has picked their second tile then we increment the frame counter to display both of them for a few seconds
130
+ if @phase == 1 and @pickedSecond then
131
+ @frameCounter += 1
132
+ end
133
+
134
+ ## update the Timer object
135
+ @timer.update
136
+ end
137
+
138
+ ## draw function. this function will draw one of the three backgrounds for the game. if the game is in the LOSE state, if the time has run out and the
139
+ # player has not matched 5 tiles, the player loses. if the player matches 5 tiles before the time runs out, then they win. else, the regular game
140
+ # board is drawn for the user to play on
141
+ def draw
142
+ if (!@win and @lose) or (@frameCounter == 0 and @game_board.return_count < 5 and @timer.return_time == 0)
143
+ draw_lose
144
+ elsif (@win and !@lose)
145
+ draw_win
146
+ elsif (!@win and !@lose)
147
+ draw_background
148
+ @game_board.draw_board
149
+ @timer.draw
150
+ end
151
+ end
152
+
153
+ ## pick_tiles function
154
+ # this function has two parts. if the game is in phase 0, the user is picking an initial tile. the function will check the location
155
+ # of the user's mouse and flip the appropriate tile and keep track of which tile was flipped. then, the game is set to phase 1.
156
+ # in phase 1, the user is picking the tile they want to match with the first one. if a tile is successfully picked, the pickedSecond
157
+ # variable is set to true and the game stores which tile was picked and flipped.
158
+ def pick_tiles
159
+ if button_down? Gosu::KbEscape then
160
+ close
161
+ end
162
+ if @phase == 0
163
+ if button_down? Gosu::MsLeft then
164
+ # TILE 1
165
+ if (mouse_x >= 0 and mouse_x <= 250) and (mouse_y >= 0 and mouse_y <= 250) and (!@game_board.is_matched(0)) then
166
+ @game_board.flip_tile(0, true)
167
+ @picked = 0
168
+ @phase = 1
169
+ # TILE 2
170
+ elsif (mouse_x > 250 and mouse_x <= 500) and (mouse_y >= 0 and mouse_y <= 250) and (!@game_board.is_matched(1)) then
171
+ @game_board.flip_tile(1, true)
172
+ @picked = 1
173
+ @phase = 1
174
+ # TILE 3
175
+ elsif (mouse_x > 500 and mouse_x <= 750) and (mouse_y >= 0 and mouse_y <= 250) and (!@game_board.is_matched(2)) then
176
+ @game_board.flip_tile(2, true)
177
+ @picked = 2
178
+ @phase = 1
179
+ # TILE 4
180
+ elsif (mouse_x > 750 and mouse_x <= 1000) and (mouse_y >= 0 and mouse_y <= 250) and (!@game_board.is_matched(3))then
181
+ @game_board.flip_tile(3, true)
182
+ @picked = 3
183
+ @phase = 1
184
+ # TILE 5
185
+ elsif (mouse_x > 1000 and mouse_x <= 1250) and (mouse_y >= 0 and mouse_y <= 250) and (!@game_board.is_matched(4)) then
186
+ @game_board.flip_tile(4, true)
187
+ @picked = 4
188
+ @phase = 1
189
+ # TILE 6
190
+ elsif (mouse_x >= 0 and mouse_x <= 250) and (mouse_y > 500 and mouse_y <= 750) and (!@game_board.is_matched(5)) then
191
+ @game_board.flip_tile(5, true)
192
+ @picked = 5
193
+ @phase = 1
194
+ # TILE 7
195
+ elsif (mouse_x > 250 and mouse_x <= 500) and (mouse_y > 500 and mouse_y <= 750) and (!@game_board.is_matched(6)) then
196
+ @game_board.flip_tile(6, true)
197
+ @picked = 6
198
+ @phase = 1
199
+ # TILE 8
200
+ elsif (mouse_x > 500 and mouse_x <= 750) and (mouse_y > 500 and mouse_y <= 750) and (!@game_board.is_matched(7)) then
201
+ @game_board.flip_tile(7, true)
202
+ @picked = 7
203
+ @phase = 1
204
+ # TILE 9
205
+ elsif (mouse_x > 750 and mouse_x <= 1000) and (mouse_y > 500 and mouse_y <= 750) and (!@game_board.is_matched(8)) then
206
+ @game_board.flip_tile(8, true)
207
+ @picked = 8
208
+ @phase = 1
209
+ # TILE 10
210
+ elsif (mouse_x > 1000 and mouse_x <= 1250) and (mouse_y > 500 and mouse_y <= 750) and (!@game_board.is_matched(9)) then
211
+ @game_board.flip_tile(9, true)
212
+ @picked = 9
213
+ @phase = 1
214
+ end
215
+ sleep(0.2) # needs delay because gosu's mouse click event seems to be hyper sensitive and picks up multiple clicks instead of 1
216
+ end
217
+ else @phase == 1
218
+ if button_down? Gosu::MsLeft then
219
+ # TILE 1
220
+ if (mouse_x >= 0 and mouse_x <= 250) and (mouse_y >= 0 and mouse_y <= 250) and (@picked != 0) and (!@game_board.is_matched(0)) then
221
+ @game_board.flip_tile(0, true)
222
+ @picked2 = 0
223
+ @pickedSecond = true
224
+ # TILE 2
225
+ elsif (mouse_x > 250 and mouse_x <= 500) and (mouse_y >= 0 and mouse_y <= 250) and (@picked != 1) and (!@game_board.is_matched(1)) then
226
+ @game_board.flip_tile(1, true)
227
+ @picked2 = 1
228
+ @pickedSecond = true
229
+ # TILE 3
230
+ elsif (mouse_x > 500 and mouse_x <= 750) and (mouse_y >= 0 and mouse_y <= 250) and (@picked != 2) and (!@game_board.is_matched(2)) then
231
+ @game_board.flip_tile(2, true)
232
+ @picked2 = 2
233
+ @pickedSecond = true
234
+ # TILE 4
235
+ elsif (mouse_x > 750 and mouse_x <= 1000) and (mouse_y >= 0 and mouse_y <= 250) and (@picked != 3) and (!@game_board.is_matched(3)) then
236
+ @game_board.flip_tile(3, true)
237
+ @picked2 = 3
238
+ @pickedSecond = true
239
+ # TILE 5
240
+ elsif (mouse_x > 1000 and mouse_x <= 1250) and (mouse_y >= 0 and mouse_y <= 250) and (@picked != 4) and (!@game_board.is_matched(4)) then
241
+ @game_board.flip_tile(4, true)
242
+ @picked2 = 4
243
+ @pickedSecond = true
244
+ # TILE 6
245
+ elsif (mouse_x >= 0 and mouse_x <= 250) and (mouse_y > 500 and mouse_y <= 750) and (@picked != 5) and (!@game_board.is_matched(5)) then
246
+ @game_board.flip_tile(5, true)
247
+ @picked2 = 5
248
+ @pickedSecond = true
249
+ # TILE 7
250
+ elsif (mouse_x > 250 and mouse_x <= 500) and (mouse_y > 500 and mouse_y <= 750) and (@picked != 6) and (!@game_board.is_matched(6)) then
251
+ @game_board.flip_tile(6, true)
252
+ @picked2 = 6
253
+ @pickedSecond = true
254
+ # TILE 8
255
+ elsif (mouse_x > 500 and mouse_x <= 750) and (mouse_y > 500 and mouse_y <= 750) and (@picked != 7) and (!@game_board.is_matched(7)) then
256
+ @game_board.flip_tile(7, true)
257
+ @picked2 = 7
258
+ @pickedSecond = true
259
+ # TILE 9
260
+ elsif (mouse_x > 750 and mouse_x <= 1000) and (mouse_y > 500 and mouse_y <= 750) and (@picked != 8) and (!@game_board.is_matched(8)) then
261
+ @game_board.flip_tile(8, true)
262
+ @picked2 = 8
263
+ @pickedSecond = true
264
+ # TILE 10
265
+ elsif (mouse_x > 1000 and mouse_x <= 1250) and (mouse_y > 500 and mouse_y <= 750) and (@picked != 9) and (!@game_board.is_matched(9)) then
266
+ @game_board.flip_tile(9, true)
267
+ @picked2 = 9
268
+ @pickedSecond = true
269
+ end
270
+ sleep(0.2) # needs delay because gosu's mouse click event seems to be hyper sensitive and picks up multiple clicks instead of 1
271
+ end
272
+ end
273
+ end
274
+
275
+ ## the check_match function will be called after the the frameCounter has reached a certain point.
276
+ # this function will check if the two tiles that have been selected match. it will compare the strings that determine each
277
+ # tile's id. if the strings are the same then the two tiles match; the strings are the path to that tile's main image.
278
+ # if the strings do not match then the tiles are flipped over and the game is set back to phase 0.
279
+ def check_match
280
+ if @phase == 1 and @pickedSecond == true
281
+ # check if they match
282
+ if(@game_board.get_id(@picked) == @game_board.get_id(@picked2))
283
+ @game_board.matchTile(@picked)
284
+ @game_board.matchTile(@picked2)
285
+
286
+ # increment match counter
287
+ @game_board.inc_match
288
+ else
289
+ # flip the tiles
290
+ @game_board.flip_tile(@picked, false)
291
+ @game_board.flip_tile(@picked2, false)
292
+ end
293
+ @pickedSecond = false
294
+ @phase = 0
295
+ end
296
+ end
297
+
298
+ ## this function specifies that the cursor should be displayed when it is inside the main gosu window
299
+ def needs_cursor?
300
+ true
301
+ end
302
+
303
+ ## function that will be used to draw a blue background with a slight gradient towards the bottom
304
+ def draw_background
305
+ draw_quad( 0, 0, TOP_COLOR,
306
+ @width, 0, TOP_COLOR,
307
+ 0, @height, BOTTOM_COLOR,
308
+ @width, @height, BOTTOM_COLOR,
309
+ -1 )
310
+ end
311
+
312
+ ## function that draws the background and winning font when the user wins the game
313
+ def draw_win
314
+ draw_quad( 0, 0, WTOP_COLOR,
315
+ @width, 0, WTOP_COLOR,
316
+ 0, @height, WBOTTOM_COLOR,
317
+ @width, @height, WBOTTOM_COLOR,
318
+ -1 )
319
+ @endFont.draw("You win!", @width/2 - 150, @height/2, 0, 4, 4, 0xFFFFFFFF)
320
+ end
321
+
322
+ ## function that draws the background and losing font when the users loses the game
323
+ def draw_lose
324
+ draw_quad( 0, 0, LTOP_COLOR,
325
+ @width, 0, LTOP_COLOR,
326
+ 0, @height, LBOTTOM_COLOR,
327
+ @width, @height, LBOTTOM_COLOR,
328
+ -1 )
329
+ @endFont.draw("You lose...", @width/2 - 150, @height/2, 0, 4, 4, 0xFFFFFFFF)
330
+ end
331
+
332
+ ## this function is called at the end of the display period for images. since pictures display for a few seconds before flipping over again,
333
+ # this function will allow the user to win if they flip over the last pair when the time left is less than the display period.
334
+ def check_win
335
+ if @game_board.return_count == 5 and @timer.return_time >= 0
336
+ @win = true
337
+ end
338
+ if @game_board.return_count < 5 and @timer.return_time == 0
339
+ @lose = true
340
+ end
341
+ end
342
+ end
@@ -0,0 +1,51 @@
1
+ require 'gosu'
2
+
3
+ class Tile
4
+
5
+ ## initialize function of the Tile class: this will create a Tile object with all necessary parameters
6
+ # Parameters: image name, xLocation, and yLocation. image name is the name of the image that was saved, and the location variables
7
+ # give the Tile's location on the board
8
+ # img2 will be used as the back of the image for when it is face-down
9
+ # idStr is the string that contains the path of the image for the given tile, it will be used for comparisons between tiles
10
+ def initialize( img, xL, yL, img2, idS, chkImg )
11
+ @image = img
12
+ @rev = img2
13
+ @xLocation = xL
14
+ @yLocation = yL
15
+ @flipped = false
16
+ @matched = false
17
+ @idStr = idS
18
+ @chkImage = chkImg
19
+ end
20
+
21
+ # draw function of the Tile class: this will draw the image at the Tile's specified location
22
+ def draw
23
+ if !@flipped and !@matched then
24
+ @rev.draw(@xLocation, @yLocation, 0)
25
+ elsif @flipped and !@matched
26
+ @image.draw(@xLocation, @yLocation, 0)
27
+ else
28
+ @chkImage.draw(@xLocation, @yLocation, 0)
29
+ end
30
+ end
31
+
32
+ # flip function of the Tile class: this will make the Tile be drawn with its true image
33
+ def flip( bool )
34
+ @flipped = bool
35
+ end
36
+
37
+ # return_id function of the Tile class: this will return the given Tile's id to be used in comparisons
38
+ def return_id
39
+ return @idStr
40
+ end
41
+
42
+ # sets the matched attribute of a Tile object to true. this will result in the tile displaying a checkmark for completion
43
+ def set_match
44
+ @matched = true
45
+ end
46
+
47
+ # this function will return a tile's matched status
48
+ def return_match
49
+ return @matched
50
+ end
51
+ end
@@ -0,0 +1,44 @@
1
+ require 'gosu'
2
+
3
+ class Timer
4
+
5
+ ## initialize function of the timer class
6
+ # this will create a Timer object with parameters
7
+ # fontParam = a Gosu font object
8
+ # pX = the x coordinate of where the font will display
9
+ # pY = the y coordinate of where the font will display
10
+ # the timer object also maintains a frame count and a count of the total time
11
+ def initialize(fontParam, pX, pY)
12
+ @font = fontParam
13
+ @frameCounter = 0
14
+ @totalTime = 30
15
+ @posX = pX
16
+ @posY = pY
17
+ end
18
+
19
+ ## the update function of the timer class does most of the timing job
20
+ # the frame counter variable is updated every 16.666666 ms (Gosu's default refresh rate)
21
+ # this leads to about 60 frames per second, so every 60 frames the total time count will decrease by 1
22
+ def update
23
+ if @frameCounter < 60 then
24
+ @frameCounter += 1
25
+ else
26
+ @frameCounter = 0
27
+ if @totalTime != 0 then
28
+ @totalTime -= 1
29
+ end
30
+ end
31
+ end
32
+
33
+ ## the draw function of the timer utilizes the draw function of the font object that was passed in
34
+ # it will draw the font object with text being the time remaining at location (posX, posY) in the window
35
+ def draw
36
+ @font.draw("Time: #{@totalTime}", @posX - 100, @posY, 0, 2, 2, 0xFFFFFFFF)
37
+ end
38
+
39
+ ## the return time function of this class will return the time that is remaining in the game
40
+ def return_time
41
+ return @totalTime
42
+ end
43
+
44
+ end
@@ -0,0 +1,74 @@
1
+ require 'open-uri'
2
+ require 'tumblr_client'
3
+
4
+ class TumblrAPIObject
5
+
6
+ ## creates a new Tumblr API Object that is able to pull information from Tumblr's servers
7
+ # the initialize function has a single parameter that will accept a string that is the URL of the desired blog.
8
+ # this function will process and strip down the string into the following format: blogname.tumblr.com/
9
+ def initialize(input)
10
+ # Authenticate via OAuth
11
+ @client = Tumblr::Client.new({
12
+ :consumer_key => 'eYFF887FauAjtA92YZLBwPI0TmYCNrYOyVBGvPD8GOCvkCFyCy',
13
+ :consumer_secret => 'e888vLuYxDwPyWSfGJCfkNOwro8su3LX2CiUTMcZwfop7uhqPM',
14
+ :oauth_token => 'hlBXwpRcylfwZk5ZnTcRMfUxOYb6oQO8P9cnAjcL2PeUlJYaSh',
15
+ :oauth_token_secret => 'oOPzfs94gBGrSa6Fm5BjxZEFWouptnYeB6DbshQuzmsbkx84ao'
16
+ })
17
+
18
+ # process the parameter string input to convert it into the right format
19
+ # store it in the variable url that will be usd in the query function
20
+ # the regular expression in the sub function will replace urls of the form http://blogname.tumblr.com or http://www.blogname.tumblr.com with
21
+ # blogname.tumblr.com/
22
+ @url = input.downcase.sub(/http:\/\/(www.){,1}/, '')
23
+ end
24
+
25
+ ## uses the Tumblr API object to request pictures. It pulls 10 pictures right now but is only using 5.
26
+ # for each picture, it finds the url of that image with a width of 250px. it then stores that url in the imageArray.
27
+ # if the query to the Tumblr API was empty, this function returns a 0 indicating an error. If the results were nonempty,
28
+ # the function returns the number of pictures grabbed.
29
+ def query
30
+ # Make the request
31
+ results = @client.posts(@url, :type => "photo", :limit => 5)
32
+ i = 0
33
+
34
+ # if there were no results, return 0 which will give an error in game.rb
35
+ if results.empty?
36
+ return 0
37
+ elsif (results.size != 2) and (results["posts"].size == 5) # results will have a size of two if there was an error with the tumblr API, also need 5 pictures
38
+ @imageArray = Array.new(5)
39
+ begin
40
+ @imageArray[i] = results["posts"][i]["photos"][0]["alt_sizes"][1]["url"].sub(/_[4-5]00/, '_250') # regular expression will select pictures 250px wide
41
+ i += 1
42
+ end while i < 5
43
+ return @imageArray.size
44
+ else
45
+ return results.size
46
+ end
47
+ end
48
+
49
+ ## this function initializes a hash that maps each image to an identifier. we only grabbed 5 images but this function copies each one
50
+ # to end up with 10 pictures. the function then opens each picture using modulo arithmetic to loop back around.
51
+ # FUTURE GOAL: INSTEAD OF COPYING EACH IMAGE ONCE, CHANGE THIS FUNCTION TO JUST OPEN THE 5 IMAGES.
52
+ def create_images
53
+ imgNames = Hash[
54
+ 'image1.png' => 0,
55
+ 'image2.png' => 1,
56
+ 'image3.png' => 2,
57
+ 'image4.png' => 3,
58
+ 'image5.png' => 4,
59
+ 'image6.png' => 0,
60
+ 'image7.png' => 1,
61
+ 'image8.png' => 2,
62
+ 'image9.png' => 3,
63
+ 'image10.png' => 4 ]
64
+
65
+ i = 0
66
+ imgNames.each_key do |imgN|
67
+ open(imgN, 'wb') do |file|
68
+ puts "Loading..."
69
+ file << open(@imageArray[i%5]).read
70
+ end
71
+ i += 1
72
+ end
73
+ end
74
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tumblr-game
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Joe Canero
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.9
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.9
27
+ - !ruby/object:Gem::Dependency
28
+ name: gosu
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.7.50
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.7.50
41
+ - !ruby/object:Gem::Dependency
42
+ name: multipart-post
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.2.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.2.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: tumblr_client
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.8.2
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.8.2
69
+ description: A simple game that allows users to download images from Tumblr blogs
70
+ and use them for an image matching game.
71
+ email: caneroj1@tcnj.edu
72
+ executables:
73
+ - Main
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - lib/game.rb
78
+ - lib/tumblr-game/tumblr.rb
79
+ - lib/tumblr-game/gosu.rb
80
+ - lib/tumblr-game/tile.rb
81
+ - lib/tumblr-game/board.rb
82
+ - lib/tumblr-game/timer.rb
83
+ - bin/Main
84
+ homepage: http://rubygems.org/gems/hola
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.0.3
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Tumblr Image Matching game!
108
+ test_files: []