Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.89 KB | None | 0 0
  1. require 'rubygems'
  2. require 'gosu'
  3.  
  4. class Ducky
  5.   attr_reader :x, :y, :timer
  6.  
  7.   def initialize(image, turnsLeft)
  8.     @img = image
  9.     @scale = (rand * 0.3) + 0.1
  10.     @x = rand * 640
  11.     @y = rand * 480
  12.     @turn_x = rand * 0.6
  13.     @turn_y = rand * 0.6
  14.     @angle = rand(360)
  15.     @turnDir = turnsLeft
  16.     @colorMod = 0
  17.     @alphaMod = 0
  18.     @finalColor = 0xffffffff
  19.     @timer = 0
  20.   end
  21.  
  22.   def update
  23.     if @turnDir == 0
  24.       @angle += 1
  25.       if  @angle > 360
  26.         @angle = 0
  27.       end
  28.     end
  29.     if @turnDir == 1
  30.        @angle -= 1
  31.       if  @angle < 0
  32.         @angle = 360
  33.       end
  34.     end
  35.    
  36.     if @colorMod < 255
  37.       if rand(2) == 1
  38.         @colorMod += 1
  39.       end
  40.     else
  41.         @alphaMod += 5
  42.         if @alphaMod > 100
  43.           @alphaMod = 0
  44.         end
  45.        
  46.         @timer += 1
  47.     end
  48.    
  49.    @finalColor = 0xffffffff - (0x00000101 * @colorMod) - (0x01000000 * @alphaMod)
  50.    
  51.   end
  52.  
  53.   def draw  
  54.     @img.draw_rot(@x, @y, 1, @angle, @turn_x, @turn_y, @scale, @scale, @finalColor);
  55.   end
  56. end
  57.  
  58. class GameWindow < Gosu::Window
  59.   def initialize
  60.     super(640, 480, false)
  61.     self.caption = "Amie-Jo'z Game"
  62.  
  63.     @background_image = Gosu::Image.new(self, "tornado.png", true)
  64.     @ducky_image = Gosu::Image.new(self, "duck.png", true)
  65.     @ducks = Array.new
  66.     @font = Gosu::Font.new(self, Gosu::default_font_name, 20)
  67.     @score = 0
  68.     @lives = 10
  69.     @squeak = Gosu::Sample.new(self, "squeak.wav")
  70.     @alreadyClicked = false
  71.   end
  72.  
  73.   def update
  74.  
  75.     if !button_down? Gosu::Button::MsLeft
  76.       @alreadyClicked = false
  77.     end
  78.     if rand(100) < 4 and @ducks.size < 25 then
  79.       @ducks.push(Ducky.new(@ducky_image, rand(2)))
  80.     end
  81.    
  82.     @ducks.reject! do |star|
  83.       if Gosu::distance(star.x, star.y, self.mouse_x, self.mouse_y) < 50 and button_down? Gosu::Button::MsLeft and @alreadyClicked != true and @lives > 0
  84.         @score += 1
  85.         @squeak.play
  86.         @alreadyClicked = true
  87.         true
  88.       elsif star.timer > 100
  89.         if @lives > -1
  90.           @lives -= 1
  91.         end
  92.         true
  93.       else
  94.         false
  95.       end
  96.     end
  97.      
  98.  
  99.    
  100.   @ducks.each { |ducky| ducky.update }
  101. end
  102.  
  103.   def draw
  104.     @background_image.draw(0, 0, 0);
  105.     @ducks.each { |ducky| ducky.draw }
  106.     @font.draw("Score: #{@score}", 10, 10, 2, 1.0, 1.0, 0xffffffff)
  107.     @font.draw("Lives: #{@lives}", 10, 30, 2, 1.0, 1.0, 0xffffffff)
  108.    
  109.     if @lives < 0
  110.       @font.draw("GAME OVER!!1!", 120, 80, 2, 3.0, 3.0, 0xffffffff)
  111.       @font.draw("Press Enter to Restart.", 240, 160, 2, 1.0, 1.0, 0xffffffff)
  112.     end
  113.   end
  114.  
  115.   def button_down(id)
  116.     if id == Gosu::Button::KbEscape
  117.       close
  118.     end
  119.     if id == Gosu::Button::KbReturn
  120.       restart
  121.     end
  122.   end
  123.  
  124.   def needs_cursor?
  125.     true
  126.   end
  127.  
  128.   def restart
  129.     @ducks = Array.new
  130.     @score = 0
  131.     @lives = 10
  132.   end
  133. end
  134.  
  135. window = GameWindow.new
  136. window.show
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement