gosu 0.7.9.1-universal-darwin

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,139 @@
1
+ # This example demonstrates the use of the TextInput functionality.
2
+ # One can tab through, or click into the text fields and change it's contents.
3
+
4
+ # At its most basic form, you only need to create a new TextInput instance and
5
+ # set the text_input attribute of your window to it. Until you set this
6
+ # attribute to nil again, the TextInput object will build a text that can be
7
+ # accessed via TextInput#text.
8
+
9
+ # The TextInput object also maintains the position of the caret as the index
10
+ # of the character that it's left to via the caret_pos attribute. Furthermore,
11
+ # if there is a selection, the selection_start attribute yields its beginning,
12
+ # using the same indexing scheme. If there is no selection, selection_start
13
+ # is equal to caret_pos.
14
+
15
+ # A TextInput object is purely abstract, though; drawing the input field is left
16
+ # to the user. In this case, we are subclassing TextInput to add this code.
17
+ # As with most of Gosu, how this is handled is completely left open; the scheme
18
+ # presented here is not mandatory! Gosu only aims to provide enough code for
19
+ # games (or intermediate UI toolkits) to be built upon it.
20
+
21
+ begin
22
+ # In case you use Gosu via RubyGems.
23
+ require 'rubygems'
24
+ rescue LoadError
25
+ # In case you don't.
26
+ end
27
+
28
+ require 'gosu'
29
+
30
+ class TextField < Gosu::TextInput
31
+ # Some constants that define our appearance.
32
+ INACTIVE_COLOR = 0xcc666666
33
+ ACTIVE_COLOR = 0xccff6666
34
+ SELECTION_COLOR = 0xcc0000ff
35
+ CARET_COLOR = 0xffffffff
36
+ PADDING = 5
37
+
38
+ attr_reader :x, :y
39
+
40
+ def initialize(window, font, x, y)
41
+ # TextInput's constructor doesn't expect any arguments.
42
+ super()
43
+
44
+ @window, @font, @x, @y = window, font, x, y
45
+
46
+ # Start with a self-explanatory text in each field.
47
+ self.text = "Click to change text"
48
+ end
49
+
50
+ def draw
51
+ # Depending on whether this is the currently selected input or not, change the
52
+ # background's color.
53
+ if @window.text_input == self then
54
+ background_color = ACTIVE_COLOR
55
+ else
56
+ background_color = INACTIVE_COLOR
57
+ end
58
+ @window.draw_quad(x - PADDING, y - PADDING, background_color,
59
+ x + width + PADDING, y - PADDING, background_color,
60
+ x - PADDING, y + height + PADDING, background_color,
61
+ x + width + PADDING, y + height + PADDING, background_color, 0)
62
+
63
+ # Calculate the position of the caret and the selection start.
64
+ pos_x = x + @font.text_width(self.text[0...self.caret_pos])
65
+ sel_x = x + @font.text_width(self.text[0...self.selection_start])
66
+
67
+ # Draw the selection background, if any; if not, sel_x and pos_x will be
68
+ # the same value, making this quad empty.
69
+ @window.draw_quad(sel_x, y, SELECTION_COLOR,
70
+ pos_x, y, SELECTION_COLOR,
71
+ sel_x, y + height, SELECTION_COLOR,
72
+ pos_x, y + height, SELECTION_COLOR, 0)
73
+
74
+ # Draw the caret; again, only if this is the currently selected field.
75
+ if @window.text_input == self then
76
+ @window.draw_line(pos_x, y, CARET_COLOR,
77
+ pos_x, y + height, CARET_COLOR, 0)
78
+ end
79
+
80
+ # Finally, draw the text itself!
81
+ @font.draw(self.text, x, y, 0)
82
+ end
83
+
84
+ # This text field grows with the text that's being entered.
85
+ # (Without clipping, one has to be a bit creative about this ;) )
86
+ def width
87
+ @font.text_width(self.text)
88
+ end
89
+
90
+ def height
91
+ @font.height
92
+ end
93
+
94
+ # Hit-test for selecting a text field with the mouse.
95
+ def under_point?(mouse_x, mouse_y)
96
+ mouse_x > x - PADDING and mouse_x < x + width + PADDING and
97
+ mouse_y > y - PADDING and mouse_y < y + height + PADDING
98
+ end
99
+ end
100
+
101
+ class TextInputWindow < Gosu::Window
102
+ def initialize
103
+ super(300, 200, false)
104
+ self.caption = "Text Input Example"
105
+
106
+ font = Gosu::Font.new(self, Gosu::default_font_name, 20)
107
+
108
+ # Set up an array of three text fields.
109
+ @text_fields = Array.new(3) { |index| TextField.new(self, font, 50, 30 + index * 50) }
110
+
111
+ @cursor = Gosu::Image.new(self, "media/Cursor.png", false)
112
+ end
113
+
114
+ def draw
115
+ @text_fields.each { |tf| tf.draw }
116
+ @cursor.draw(mouse_x, mouse_y, 0)
117
+ end
118
+
119
+ def button_down(id)
120
+ if id == Gosu::KbTab then
121
+ # Tab key will not be 'eaten' by text fields; use for switching through
122
+ # text fields.
123
+ index = @text_fields.index(self.text_input) || -1
124
+ self.text_input = @text_fields[(index + 1) % @text_fields.size]
125
+ elsif id == Gosu::KbEscape then
126
+ # Escape key will not be 'eaten' by text fields; use for deselecting.
127
+ if self.text_input then
128
+ self.text_input = nil
129
+ else
130
+ close
131
+ end
132
+ elsif id == Gosu::MsLeft then
133
+ # Mouse click: Select text field based on mouse position.
134
+ self.text_input = @text_fields.find { |tf| tf.under_point?(mouse_x, mouse_y) }
135
+ end
136
+ end
137
+ end
138
+
139
+ TextInputWindow.new.show
@@ -0,0 +1,137 @@
1
+ begin
2
+ # In case you use Gosu via RubyGems.
3
+ require 'rubygems'
4
+ rescue LoadError
5
+ # In case you don't.
6
+ end
7
+
8
+ require 'gosu'
9
+
10
+ module ZOrder
11
+ Background, Stars, Player, UI = *0..3
12
+ end
13
+
14
+ class Player
15
+ attr_reader :score
16
+
17
+ def initialize(window)
18
+ @image = Gosu::Image.new(window, "media/Starfighter.bmp", false)
19
+ @beep = Gosu::Sample.new(window, "media/Beep.wav")
20
+ @x = @y = @vel_x = @vel_y = @angle = 0.0
21
+ @score = 0
22
+ end
23
+
24
+ def warp(x, y)
25
+ @x, @y = x, y
26
+ end
27
+
28
+ def turn_left
29
+ @angle -= 4.5
30
+ end
31
+
32
+ def turn_right
33
+ @angle += 4.5
34
+ end
35
+
36
+ def accelerate
37
+ @vel_x += Gosu::offset_x(@angle, 0.5)
38
+ @vel_y += Gosu::offset_y(@angle, 0.5)
39
+ end
40
+
41
+ def move
42
+ @x += @vel_x
43
+ @y += @vel_y
44
+ @x %= 640
45
+ @y %= 480
46
+
47
+ @vel_x *= 0.95
48
+ @vel_y *= 0.95
49
+ end
50
+
51
+ def draw
52
+ @image.draw_rot(@x, @y, ZOrder::Player, @angle)
53
+ end
54
+
55
+ def collect_stars(stars)
56
+ stars.reject! do |star|
57
+ if Gosu::distance(@x, @y, star.x, star.y) < 35 then
58
+ @score += 10
59
+ @beep.play
60
+ true
61
+ else
62
+ false
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ class Star
69
+ attr_reader :x, :y
70
+
71
+ def initialize(animation)
72
+ @animation = animation
73
+ @color = Gosu::Color.new(0xff000000)
74
+ @color.red = rand(255 - 40) + 40
75
+ @color.green = rand(255 - 40) + 40
76
+ @color.blue = rand(255 - 40) + 40
77
+ @x = rand * 640
78
+ @y = rand * 480
79
+ end
80
+
81
+ def draw
82
+ img = @animation[Gosu::milliseconds / 100 % @animation.size]
83
+ img.draw(@x - img.width / 2.0, @y - img.height / 2.0,
84
+ ZOrder::Stars, 1, 1, @color, :additive)
85
+ end
86
+ end
87
+
88
+ class GameWindow < Gosu::Window
89
+ def initialize
90
+ super(640, 480, false)
91
+ self.caption = "Gosu Tutorial Game"
92
+
93
+ @background_image = Gosu::Image.new(self, "media/Space.png", true)
94
+
95
+ @player = Player.new(self)
96
+ @player.warp(320, 240)
97
+
98
+ @star_anim = Gosu::Image::load_tiles(self, "media/Star.png", 25, 25, false)
99
+ @stars = Array.new
100
+
101
+ @font = Gosu::Font.new(self, Gosu::default_font_name, 20)
102
+ end
103
+
104
+ def update
105
+ if button_down? Gosu::Button::KbLeft or button_down? Gosu::Button::GpLeft then
106
+ @player.turn_left
107
+ end
108
+ if button_down? Gosu::Button::KbRight or button_down? Gosu::Button::GpRight then
109
+ @player.turn_right
110
+ end
111
+ if button_down? Gosu::Button::KbUp or button_down? Gosu::Button::GpButton0 then
112
+ @player.accelerate
113
+ end
114
+ @player.move
115
+ @player.collect_stars(@stars)
116
+
117
+ if rand(100) < 4 and @stars.size < 25 then
118
+ @stars.push(Star.new(@star_anim))
119
+ end
120
+ end
121
+
122
+ def draw
123
+ @background_image.draw(0, 0, ZOrder::Background)
124
+ @player.draw
125
+ @stars.each { |star| star.draw }
126
+ @font.draw("Score: #{@player.score}", 10, 10, ZOrder::UI, 1.0, 1.0, 0xffffff00)
127
+ end
128
+
129
+ def button_down(id)
130
+ if id == Gosu::Button::KbEscape then
131
+ close
132
+ end
133
+ end
134
+ end
135
+
136
+ window = GameWindow.new
137
+ window.show
Binary file
@@ -0,0 +1,25 @@
1
+ #....................................................#
2
+ #....................................................#
3
+ #.............xx......x.x............................#
4
+ #............x..x....................................#
5
+ #x....x...x..x.......#####..xxx....................x.#
6
+ #.x.........................xxx.........##.........x.#
7
+ #...............""..........###...##..........##.....#
8
+ #..##..###..##..##...................................#
9
+ #........................................xx........###
10
+ #.............................###....................#
11
+ ##....##.............................................#
12
+ #....................##....##......##....##....##....#
13
+ #.................................................x..#
14
+ #...x....##....##.......x...x.....................x..#
15
+ #.....x...............x...x...x...................x..#
16
+ #......x...##.....##.................................#
17
+ #.......x.........................................#..#
18
+ #...........##........#...#...#..#.......x...........#
19
+ #...#................................................#
20
+ #....."""".................x.......#..#####...###....#
21
+ #x....#......................##......................#
22
+ #"""""#.....#.....x..................#...............#
23
+ ##xxxx......#........................................#
24
+ ##xxxx...#####............."...""""".................#
25
+ ######"""#############################################
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gosu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.7.9.1
5
+ platform: universal-darwin
6
+ authors:
7
+ - Julian Raschke
8
+ - Jan Luecker
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2008-04-13 00:00:00 +02:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: 2D game development library. The library features easy to use and game-friendly interfaces to 2D graphics and text (accelerated by 3D hardware), sound samples and music as well as keyboard, mouse and gamepad/joystick input. Includes demos for integration with RMagick, Chipmunk and Ruby-OpenGL.
18
+ email: julian@raschke.de
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - lib/gosu.bundle
27
+ - README
28
+ - LICENSE
29
+ - examples/ChipmunkIntegration.rb
30
+ - examples/CptnRuby.rb
31
+ - examples/MoreChipmunkAndRMagick.rb
32
+ - examples/OpenGLIntegration.rb
33
+ - examples/RMagickIntegration.rb
34
+ - examples/TextInput.rb
35
+ - examples/Tutorial.rb
36
+ - examples/media/Beep.wav
37
+ - examples/media/CptnRuby Gem.png
38
+ - examples/media/CptnRuby Map.txt
39
+ - examples/media/CptnRuby Tileset.png
40
+ - examples/media/CptnRuby.png
41
+ - examples/media/Cursor.png
42
+ - examples/media/Earth.png
43
+ - examples/media/Explosion.wav
44
+ - examples/media/LargeStar.png
45
+ - examples/media/Sky.jpg
46
+ - examples/media/Smoke.png
47
+ - examples/media/Soldier.png
48
+ - examples/media/Space.png
49
+ - examples/media/Star.png
50
+ - examples/media/Starfighter.bmp
51
+ has_rdoc: false
52
+ homepage: http://code.google.com/p/gosu/
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 1.8.1
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.1.1
74
+ signing_key:
75
+ specification_version: 2
76
+ summary: 2D game development library.
77
+ test_files: []
78
+