Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.88 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.   end
  51.  
  52.   def draw  
  53.     @img.draw_rot(@x, @y, 1, @angle, @turn_x, @turn_y, @scale, @scale, @finalColor);
  54.   end
  55. end
  56.  
  57. class GameWindow < Gosu::Window
  58.   def initialize
  59.     super(640, 480, false)
  60.     self.caption = "Amie-Jo'z Game"
  61.  
  62.     @background_image = Gosu::Image.new(self, "tornado.png", true)
  63.     @ducky_image = Gosu::Image.new(self, "duck.png", true)
  64.     @ducks = Array.new
  65.     @font = Gosu::Font.new(self, Gosu::default_font_name, 20)
  66.     @score = 0
  67.     @lives = 10
  68.     @squeak = Gosu::Sample.new(self, "squeak.wav")
  69.     @alreadyClicked = false
  70.   end
  71.  
  72.   def update
  73.     if !button_down? Gosu::Button::MsLeft
  74.       @alreadyClicked = false
  75.     end
  76.     if rand(100) < 4 and @ducks.size < 25 then
  77.       @ducks.push(Ducky.new(@ducky_image, rand(2)))
  78.     end
  79.    
  80.     @ducks.reject! do |star|
  81.       if Gosu::distance(star.x, star.y, self.mouse_x, self.mouse_y) < 50 and button_down? Gosu::Button::MsLeft and @alreadyClicked! and @lives > 0
  82.         @score += 1
  83.         @squeak.play
  84.         @alreadyClicked = true
  85.         true
  86.       elsif star.timer > 100
  87.         if @lives > -1
  88.           @lives -= 1
  89.     end
  90.         true
  91.       else
  92.         false
  93.       end
  94.   end
  95.      
  96.   @ducks.each { |ducky| ducky.update }
  97. end
  98.  
  99.   def draw
  100.     @background_image.draw(0, 0, 0);
  101.     @ducks.each { |ducky| ducky.draw }
  102.     @font.draw("Score: #{@score}", 10, 10, 2, 1.0, 1.0, 0xffffffff)
  103.     @font.draw("Lives: #{@lives}", 10, 30, 2, 1.0, 1.0, 0xffffffff)
  104.    
  105.     if @lives < 0
  106.       @font.draw("GAME OVER!!1!", 120, 80, 2, 3.0, 3.0, 0xffffffff)
  107.       @font.draw("Press Enter to Restart.", 240, 160, 2, 1.0, 1.0, 0xffffffff)
  108.     end
  109.   end
  110.  
  111.   def button_down(id)
  112.     if id == Gosu::Button::KbEscape
  113.       close
  114.     end
  115.     if id == Gosu::Button::KbReturn
  116.       restart
  117.     end
  118.   end
  119.  
  120.   def needs_cursor?
  121.     true
  122.   end
  123.  
  124.   def restart
  125.     @ducks = Array.new
  126.     @score = 0
  127.     @lives = 10
  128.   end
  129. end
  130.  
  131. window = GameWindow.new
  132. window.show
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement