Class: Game

Inherits:
Object
  • Object
show all
Defined in:
lib/tumblr-game.rb

Overview

GAME CLASS. This class takes care of getting user input from the user, passing it to the appropriate class for error checking, starting the game, and ending the game. A Game object must be created. The user must then call the initialize function, pass it the parameters, and then call start. Those three steps are all that is necessary to start the game.

Instance Method Summary collapse

Constructor Details

#initialize(w, h) ⇒ Game

this function will accept the dimensions of the window to be created and initialize a Game object



12
13
14
15
# File 'lib/tumblr-game.rb', line 12

def initialize(w, h) # dimensions of 1250 x 900 are appropriate
  @width = w
  @height = h
end

Instance Method Details

#deleteObject

this function will delete the files that have been made over the course of the game. this function is automatically called by this class after the window is closed



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tumblr-game.rb', line 45

def delete
  File.delete( File.join(Dir.home, 'Desktop/image1.png'),
               File.join(Dir.home, 'Desktop/image2.png'),
               File.join(Dir.home, 'Desktop/image3.png'),
               File.join(Dir.home, 'Desktop/image4.png'),
               File.join(Dir.home, 'Desktop/image5.png'),
               File.join(Dir.home, 'Desktop/image6.png'),
               File.join(Dir.home, 'Desktop/image7.png'),
               File.join(Dir.home, 'Desktop/image8.png'),
               File.join(Dir.home, 'Desktop/image9.png'),
               File.join(Dir.home, 'Desktop/image10.png')
  )
end

#startObject

this function will get the name of the blog from the user, and attempt to use the Tumblr API to query for pictures. if there is an error with the user input, say if it is empty or the blog is invalid, the game ends. the user must then re-execute the ruby file



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tumblr-game.rb', line 19

def start
  # ask for user input to get the blog name
  puts "Enter the name of the blog: "
  blogName = gets.chop

  if blogName.strip.empty? || !(blogName.include? "com") then 
    puts "Error: Please enter a Tumblr blog name."
  else
    ## create a new TumblrAPIObject so we can issue calls to the tumblr api for data
    tumblr = TumblrAPIObject.new(blogName)

    ## query the API. if there were enough results then advance, else issue an error
    if (tumblr.query == 5) then
      tumblr.create_images                                # create and save the images for the game

      window = MyWindow.new(@width, @height)              # start the game
      window.show
      self.delete
    else
      puts "Error downloading pictures from Tumblr. Please check your internet connection or the blog name you entered. Blogs need at least 5 pictures."
    end
  end
end