Created
August 31, 2010 01:36
-
-
Save blowmage/558376 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'rubygems' | |
| require 'gosu' | |
| class LittleBrat < Gosu::Window | |
| LETTER_LIMIT = 100 | |
| def initialize | |
| # Full resolution, fullscreen | |
| super(Gosu.screen_width, Gosu.screen_height, true) | |
| self.caption = "Leave me alone you little brat!" | |
| # The letters the little brat hits on the keyboard | |
| @letters, @new_letters = [], [] | |
| @redraw = true # init this because it needs to be true or false | |
| end | |
| def update | |
| if !@new_letters.empty? | |
| # Add new letter to the queue | |
| while str = @new_letters.shift | |
| @letters.push(Letter.new(self, str)) | |
| end | |
| # Keep the total number of letter sprites at a managable number | |
| @letters = @letters[-LETTER_LIMIT..-1] if @letters.size > LETTER_LIMIT | |
| # We updated the letters, so we need to redraw | |
| @redraw = true | |
| end | |
| end | |
| def draw | |
| @letters.each { |letter| letter.draw } | |
| @redraw = false # Done drawing, don't need to draw no more | |
| end | |
| def needs_redraw? | |
| @redraw # Don't paint/draw unless we need to | |
| end | |
| def needs_cursor? | |
| false # Don't show the cursor, even on Ubuntu | |
| end | |
| def button_down(id) # Callback, not on main thread | |
| close if close? # Close if we press the right keys | |
| @new_letters.push str_from_button_id(id) | |
| end | |
| def str_from_button_id(id) | |
| # TODO: Handle space and arrow keys | |
| Gosu::Window.button_id_to_char(id) || random_letter | |
| end | |
| def random_letter | |
| @random_letters ||= %w{ ▲ ▼ ☻ ♠ ♣ ♥ ♦ ☀ ☁ ☂ ✔ ✘ ☎ ✈ ✪ } | |
| @random_letters[rand @random_letters.size] | |
| end | |
| def close? | |
| (button_down? Gosu::KbEscape or button_down? Gosu::KbLeftControl or | |
| button_down? Gosu::KbRightControl) && | |
| (button_down? Gosu::KbQ or button_down? Gosu::KbW or | |
| button_down? Gosu::KbZ or button_down? Gosu::KbC or | |
| button_down? Gosu::KbBackspace or button_down? Gosu::KbDelete) | |
| end | |
| class Letter | |
| Z_ORDER = 3 | |
| def initialize(window, text) | |
| @image = Gosu::Image.from_text(window, text, Gosu::default_font_name, | |
| random_text_size(window)) | |
| @color = random_color # Image text is white, so this is to modulate it | |
| @x, @y = random_position(window) | |
| end | |
| def draw | |
| @image.draw(@x, @y, Z_ORDER, 0.5, 0.5, @color) | |
| end | |
| def random_text_size(window) | |
| window.height / (rand(3) + 1) | |
| end | |
| def random_color | |
| color = Gosu::Color.new(0xff000000) | |
| color.red = rand(255 - 40) + 40 | |
| color.green = rand(255 - 40) + 40 | |
| color.blue = rand(255 - 40) + 40 | |
| color | |
| end | |
| def random_position(window) | |
| offset = window.height / 20 | |
| [ rand * (window.width + offset) - (offset * 2), | |
| rand * (window.height + offset) - (offset * 2) ] | |
| end | |
| end | |
| end | |
| # Time to shut the brats up! | |
| LittleBrat.new.show |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment