gosu 0.8.6-x86-mingw32 → 0.8.7-x86-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gosu/Audio.hpp +171 -171
- data/Gosu/Bitmap.hpp +96 -96
- data/Gosu/Color.hpp +204 -204
- data/Gosu/Directories.hpp +36 -36
- data/Gosu/Font.hpp +83 -83
- data/Gosu/Gosu.hpp +34 -34
- data/Gosu/Graphics.hpp +115 -115
- data/Gosu/GraphicsBase.hpp +110 -110
- data/Gosu/IO.hpp +269 -269
- data/Gosu/Image.hpp +122 -122
- data/Gosu/ImageData.hpp +61 -61
- data/Gosu/Input.hpp +149 -149
- data/Gosu/Inspection.hpp +14 -14
- data/Gosu/Math.hpp +135 -135
- data/Gosu/Platform.hpp +93 -93
- data/Gosu/Sockets.hpp +156 -156
- data/Gosu/TR1.hpp +56 -56
- data/Gosu/Text.hpp +71 -71
- data/Gosu/TextInput.hpp +70 -70
- data/Gosu/Utility.hpp +28 -28
- data/Gosu/Version.hpp +19 -19
- data/Gosu/Window.hpp +145 -145
- data/examples/ChipmunkIntegration.rb +275 -275
- data/examples/CptnRuby.rb +223 -223
- data/examples/GosuZen.rb +68 -68
- data/examples/MoreChipmunkAndRMagick.rb +155 -155
- data/examples/OpenGLIntegration.rb +225 -225
- data/examples/RMagickIntegration.rb +417 -417
- data/examples/TextInput.rb +154 -154
- data/examples/Tutorial.rb +130 -130
- data/examples/media/Beep.wav +0 -0
- data/examples/media/CptnRuby Map.txt b/data/examples/media/CptnRuby → Map.txt +0 -0
- data/examples/media/Explosion.wav +0 -0
- data/examples/media/Landscape.svg +9 -9
- data/examples/media/Space.png +0 -0
- data/examples/media/Star.png +0 -0
- data/examples/media/Starfighter.bmp +0 -0
- data/lib/1.8/gosu.so +0 -0
- data/lib/1.9/gosu.so +0 -0
- data/lib/2.0/gosu.so +0 -0
- data/lib/2.1/gosu.so +0 -0
- data/lib/FreeImage.dll +0 -0
- data/lib/OpenAL32.dll +0 -0
- data/lib/gosu.rb +19 -16
- data/lib/gosu/patches.rb +81 -81
- data/lib/gosu/preview.rb +143 -139
- data/lib/gosu/run.rb +11 -11
- data/lib/gosu/swig_patches.rb +60 -60
- data/lib/gosu/zen.rb +89 -89
- metadata +5 -5
data/examples/TextInput.rb
CHANGED
@@ -1,154 +1,154 @@
|
|
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
|
-
require 'rubygems'
|
22
|
-
require 'gosu'
|
23
|
-
|
24
|
-
class TextField < Gosu::TextInput
|
25
|
-
# Some constants that define our appearance.
|
26
|
-
INACTIVE_COLOR = 0xcc666666
|
27
|
-
ACTIVE_COLOR = 0xccff6666
|
28
|
-
SELECTION_COLOR = 0xcc0000ff
|
29
|
-
CARET_COLOR = 0xffffffff
|
30
|
-
PADDING = 5
|
31
|
-
|
32
|
-
attr_reader :x, :y
|
33
|
-
|
34
|
-
def initialize(window, font, x, y)
|
35
|
-
# TextInput's constructor doesn't expect any arguments.
|
36
|
-
super()
|
37
|
-
|
38
|
-
@window, @font, @x, @y = window, font, x, y
|
39
|
-
|
40
|
-
# Start with a self-explanatory text in each field.
|
41
|
-
self.text = "Click to change text"
|
42
|
-
end
|
43
|
-
|
44
|
-
# Example filter method. You can truncate the text to employ a length limit (watch out
|
45
|
-
# with Ruby 1.8 and UTF-8!), limit the text to certain characters etc.
|
46
|
-
def filter text
|
47
|
-
text.upcase
|
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
|
-
# (Usually one would use clip_to and scroll around on the text field.)
|
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
|
-
|
100
|
-
# Tries to move the caret to the position specifies by mouse_x
|
101
|
-
def move_caret(mouse_x)
|
102
|
-
# Test character by character
|
103
|
-
1.upto(self.text.length) do |i|
|
104
|
-
if mouse_x < x + @font.text_width(text[0...i]) then
|
105
|
-
self.caret_pos = self.selection_start = i - 1;
|
106
|
-
return
|
107
|
-
end
|
108
|
-
end
|
109
|
-
# Default case: user must have clicked the right edge
|
110
|
-
self.caret_pos = self.selection_start = self.text.length
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
class TextInputWindow < Gosu::Window
|
115
|
-
def initialize
|
116
|
-
super(300, 200, false)
|
117
|
-
self.caption = "Text Input Example"
|
118
|
-
|
119
|
-
font = Gosu::Font.new(self, Gosu::default_font_name, 20)
|
120
|
-
|
121
|
-
# Set up an array of three text fields.
|
122
|
-
@text_fields = Array.new(3) { |index| TextField.new(self, font, 50, 30 + index * 50) }
|
123
|
-
|
124
|
-
@cursor = Gosu::Image.new(self, "media/Cursor.png", false)
|
125
|
-
end
|
126
|
-
|
127
|
-
def draw
|
128
|
-
@text_fields.each { |tf| tf.draw }
|
129
|
-
@cursor.draw(mouse_x, mouse_y, 0)
|
130
|
-
end
|
131
|
-
|
132
|
-
def button_down(id)
|
133
|
-
if id == Gosu::KbTab then
|
134
|
-
# Tab key will not be 'eaten' by text fields; use for switching through
|
135
|
-
# text fields.
|
136
|
-
index = @text_fields.index(self.text_input) || -1
|
137
|
-
self.text_input = @text_fields[(index + 1) % @text_fields.size]
|
138
|
-
elsif id == Gosu::KbEscape then
|
139
|
-
# Escape key will not be 'eaten' by text fields; use for deselecting.
|
140
|
-
if self.text_input then
|
141
|
-
self.text_input = nil
|
142
|
-
else
|
143
|
-
close
|
144
|
-
end
|
145
|
-
elsif id == Gosu::MsLeft then
|
146
|
-
# Mouse click: Select text field based on mouse position.
|
147
|
-
self.text_input = @text_fields.find { |tf| tf.under_point?(mouse_x, mouse_y) }
|
148
|
-
# Advanced: Move caret to clicked position
|
149
|
-
self.text_input.move_caret(mouse_x) unless self.text_input.nil?
|
150
|
-
end
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
TextInputWindow.new.show
|
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
|
+
require 'rubygems'
|
22
|
+
require 'gosu'
|
23
|
+
|
24
|
+
class TextField < Gosu::TextInput
|
25
|
+
# Some constants that define our appearance.
|
26
|
+
INACTIVE_COLOR = 0xcc666666
|
27
|
+
ACTIVE_COLOR = 0xccff6666
|
28
|
+
SELECTION_COLOR = 0xcc0000ff
|
29
|
+
CARET_COLOR = 0xffffffff
|
30
|
+
PADDING = 5
|
31
|
+
|
32
|
+
attr_reader :x, :y
|
33
|
+
|
34
|
+
def initialize(window, font, x, y)
|
35
|
+
# TextInput's constructor doesn't expect any arguments.
|
36
|
+
super()
|
37
|
+
|
38
|
+
@window, @font, @x, @y = window, font, x, y
|
39
|
+
|
40
|
+
# Start with a self-explanatory text in each field.
|
41
|
+
self.text = "Click to change text"
|
42
|
+
end
|
43
|
+
|
44
|
+
# Example filter method. You can truncate the text to employ a length limit (watch out
|
45
|
+
# with Ruby 1.8 and UTF-8!), limit the text to certain characters etc.
|
46
|
+
def filter text
|
47
|
+
text.upcase
|
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
|
+
# (Usually one would use clip_to and scroll around on the text field.)
|
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
|
+
|
100
|
+
# Tries to move the caret to the position specifies by mouse_x
|
101
|
+
def move_caret(mouse_x)
|
102
|
+
# Test character by character
|
103
|
+
1.upto(self.text.length) do |i|
|
104
|
+
if mouse_x < x + @font.text_width(text[0...i]) then
|
105
|
+
self.caret_pos = self.selection_start = i - 1;
|
106
|
+
return
|
107
|
+
end
|
108
|
+
end
|
109
|
+
# Default case: user must have clicked the right edge
|
110
|
+
self.caret_pos = self.selection_start = self.text.length
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
class TextInputWindow < Gosu::Window
|
115
|
+
def initialize
|
116
|
+
super(300, 200, false)
|
117
|
+
self.caption = "Text Input Example"
|
118
|
+
|
119
|
+
font = Gosu::Font.new(self, Gosu::default_font_name, 20)
|
120
|
+
|
121
|
+
# Set up an array of three text fields.
|
122
|
+
@text_fields = Array.new(3) { |index| TextField.new(self, font, 50, 30 + index * 50) }
|
123
|
+
|
124
|
+
@cursor = Gosu::Image.new(self, "media/Cursor.png", false)
|
125
|
+
end
|
126
|
+
|
127
|
+
def draw
|
128
|
+
@text_fields.each { |tf| tf.draw }
|
129
|
+
@cursor.draw(mouse_x, mouse_y, 0)
|
130
|
+
end
|
131
|
+
|
132
|
+
def button_down(id)
|
133
|
+
if id == Gosu::KbTab then
|
134
|
+
# Tab key will not be 'eaten' by text fields; use for switching through
|
135
|
+
# text fields.
|
136
|
+
index = @text_fields.index(self.text_input) || -1
|
137
|
+
self.text_input = @text_fields[(index + 1) % @text_fields.size]
|
138
|
+
elsif id == Gosu::KbEscape then
|
139
|
+
# Escape key will not be 'eaten' by text fields; use for deselecting.
|
140
|
+
if self.text_input then
|
141
|
+
self.text_input = nil
|
142
|
+
else
|
143
|
+
close
|
144
|
+
end
|
145
|
+
elsif id == Gosu::MsLeft then
|
146
|
+
# Mouse click: Select text field based on mouse position.
|
147
|
+
self.text_input = @text_fields.find { |tf| tf.under_point?(mouse_x, mouse_y) }
|
148
|
+
# Advanced: Move caret to clicked position
|
149
|
+
self.text_input.move_caret(mouse_x) unless self.text_input.nil?
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
TextInputWindow.new.show
|
data/examples/Tutorial.rb
CHANGED
@@ -1,131 +1,131 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'gosu'
|
3
|
-
|
4
|
-
module ZOrder
|
5
|
-
Background, Stars, Player, UI = *0..3
|
6
|
-
end
|
7
|
-
|
8
|
-
class Player
|
9
|
-
attr_reader :score
|
10
|
-
|
11
|
-
def initialize(window)
|
12
|
-
@image = Gosu::Image.new(window, "media/Starfighter.bmp", false)
|
13
|
-
@beep = Gosu::Sample.new(window, "media/Beep.wav")
|
14
|
-
@x = @y = @vel_x = @vel_y = @angle = 0.0
|
15
|
-
@score = 0
|
16
|
-
end
|
17
|
-
|
18
|
-
def warp(x, y)
|
19
|
-
@x, @y = x, y
|
20
|
-
end
|
21
|
-
|
22
|
-
def turn_left
|
23
|
-
@angle -= 4.5
|
24
|
-
end
|
25
|
-
|
26
|
-
def turn_right
|
27
|
-
@angle += 4.5
|
28
|
-
end
|
29
|
-
|
30
|
-
def accelerate
|
31
|
-
@vel_x += Gosu::offset_x(@angle, 0.5)
|
32
|
-
@vel_y += Gosu::offset_y(@angle, 0.5)
|
33
|
-
end
|
34
|
-
|
35
|
-
def move
|
36
|
-
@x += @vel_x
|
37
|
-
@y += @vel_y
|
38
|
-
@x %= 640
|
39
|
-
@y %= 480
|
40
|
-
|
41
|
-
@vel_x *= 0.95
|
42
|
-
@vel_y *= 0.95
|
43
|
-
end
|
44
|
-
|
45
|
-
def draw
|
46
|
-
@image.draw_rot(@x, @y, ZOrder::Player, @angle)
|
47
|
-
end
|
48
|
-
|
49
|
-
def collect_stars(stars)
|
50
|
-
stars.reject! do |star|
|
51
|
-
if Gosu::distance(@x, @y, star.x, star.y) < 35 then
|
52
|
-
@score += 10
|
53
|
-
@beep.play
|
54
|
-
true
|
55
|
-
else
|
56
|
-
false
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
class Star
|
63
|
-
attr_reader :x, :y
|
64
|
-
|
65
|
-
def initialize(animation)
|
66
|
-
@animation = animation
|
67
|
-
@color = Gosu::Color.new(0xff000000)
|
68
|
-
@color.red = rand(256 - 40) + 40
|
69
|
-
@color.green = rand(256 - 40) + 40
|
70
|
-
@color.blue = rand(256 - 40) + 40
|
71
|
-
@x = rand * 640
|
72
|
-
@y = rand * 480
|
73
|
-
end
|
74
|
-
|
75
|
-
def draw
|
76
|
-
img = @animation[Gosu::milliseconds / 100 % @animation.size]
|
77
|
-
img.draw(@x - img.width / 2.0, @y - img.height / 2.0,
|
78
|
-
ZOrder::Stars, 1, 1, @color, :add)
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
class GameWindow < Gosu::Window
|
83
|
-
def initialize
|
84
|
-
super(640, 480, false)
|
85
|
-
self.caption = "Gosu Tutorial Game"
|
86
|
-
|
87
|
-
@background_image = Gosu::Image.new(self, "media/Space.png", true)
|
88
|
-
|
89
|
-
@player = Player.new(self)
|
90
|
-
@player.warp(320, 240)
|
91
|
-
|
92
|
-
@star_anim = Gosu::Image::load_tiles(self, "media/Star.png", 25, 25, false)
|
93
|
-
@stars = Array.new
|
94
|
-
|
95
|
-
@font = Gosu::Font.new(self, Gosu::default_font_name, 20)
|
96
|
-
end
|
97
|
-
|
98
|
-
def update
|
99
|
-
if button_down? Gosu::KbLeft or button_down? Gosu::GpLeft then
|
100
|
-
@player.turn_left
|
101
|
-
end
|
102
|
-
if button_down? Gosu::KbRight or button_down? Gosu::GpRight then
|
103
|
-
@player.turn_right
|
104
|
-
end
|
105
|
-
if button_down? Gosu::KbUp or button_down? Gosu::GpButton0 then
|
106
|
-
@player.accelerate
|
107
|
-
end
|
108
|
-
@player.move
|
109
|
-
@player.collect_stars(@stars)
|
110
|
-
|
111
|
-
if rand(100) < 4 and @stars.size < 25 then
|
112
|
-
@stars.push(Star.new(@star_anim))
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
def draw
|
117
|
-
@background_image.draw(0, 0, ZOrder::Background)
|
118
|
-
@player.draw
|
119
|
-
@stars.each { |star| star.draw }
|
120
|
-
@font.draw("Score: #{@player.score}", 10, 10, ZOrder::UI, 1.0, 1.0, 0xffffff00)
|
121
|
-
end
|
122
|
-
|
123
|
-
def button_down(id)
|
124
|
-
if id == Gosu::KbEscape then
|
125
|
-
close
|
126
|
-
end
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
window = GameWindow.new
|
1
|
+
require 'rubygems'
|
2
|
+
require 'gosu'
|
3
|
+
|
4
|
+
module ZOrder
|
5
|
+
Background, Stars, Player, UI = *0..3
|
6
|
+
end
|
7
|
+
|
8
|
+
class Player
|
9
|
+
attr_reader :score
|
10
|
+
|
11
|
+
def initialize(window)
|
12
|
+
@image = Gosu::Image.new(window, "media/Starfighter.bmp", false)
|
13
|
+
@beep = Gosu::Sample.new(window, "media/Beep.wav")
|
14
|
+
@x = @y = @vel_x = @vel_y = @angle = 0.0
|
15
|
+
@score = 0
|
16
|
+
end
|
17
|
+
|
18
|
+
def warp(x, y)
|
19
|
+
@x, @y = x, y
|
20
|
+
end
|
21
|
+
|
22
|
+
def turn_left
|
23
|
+
@angle -= 4.5
|
24
|
+
end
|
25
|
+
|
26
|
+
def turn_right
|
27
|
+
@angle += 4.5
|
28
|
+
end
|
29
|
+
|
30
|
+
def accelerate
|
31
|
+
@vel_x += Gosu::offset_x(@angle, 0.5)
|
32
|
+
@vel_y += Gosu::offset_y(@angle, 0.5)
|
33
|
+
end
|
34
|
+
|
35
|
+
def move
|
36
|
+
@x += @vel_x
|
37
|
+
@y += @vel_y
|
38
|
+
@x %= 640
|
39
|
+
@y %= 480
|
40
|
+
|
41
|
+
@vel_x *= 0.95
|
42
|
+
@vel_y *= 0.95
|
43
|
+
end
|
44
|
+
|
45
|
+
def draw
|
46
|
+
@image.draw_rot(@x, @y, ZOrder::Player, @angle)
|
47
|
+
end
|
48
|
+
|
49
|
+
def collect_stars(stars)
|
50
|
+
stars.reject! do |star|
|
51
|
+
if Gosu::distance(@x, @y, star.x, star.y) < 35 then
|
52
|
+
@score += 10
|
53
|
+
@beep.play
|
54
|
+
true
|
55
|
+
else
|
56
|
+
false
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
class Star
|
63
|
+
attr_reader :x, :y
|
64
|
+
|
65
|
+
def initialize(animation)
|
66
|
+
@animation = animation
|
67
|
+
@color = Gosu::Color.new(0xff000000)
|
68
|
+
@color.red = rand(256 - 40) + 40
|
69
|
+
@color.green = rand(256 - 40) + 40
|
70
|
+
@color.blue = rand(256 - 40) + 40
|
71
|
+
@x = rand * 640
|
72
|
+
@y = rand * 480
|
73
|
+
end
|
74
|
+
|
75
|
+
def draw
|
76
|
+
img = @animation[Gosu::milliseconds / 100 % @animation.size]
|
77
|
+
img.draw(@x - img.width / 2.0, @y - img.height / 2.0,
|
78
|
+
ZOrder::Stars, 1, 1, @color, :add)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
class GameWindow < Gosu::Window
|
83
|
+
def initialize
|
84
|
+
super(640, 480, false)
|
85
|
+
self.caption = "Gosu Tutorial Game"
|
86
|
+
|
87
|
+
@background_image = Gosu::Image.new(self, "media/Space.png", true)
|
88
|
+
|
89
|
+
@player = Player.new(self)
|
90
|
+
@player.warp(320, 240)
|
91
|
+
|
92
|
+
@star_anim = Gosu::Image::load_tiles(self, "media/Star.png", 25, 25, false)
|
93
|
+
@stars = Array.new
|
94
|
+
|
95
|
+
@font = Gosu::Font.new(self, Gosu::default_font_name, 20)
|
96
|
+
end
|
97
|
+
|
98
|
+
def update
|
99
|
+
if button_down? Gosu::KbLeft or button_down? Gosu::GpLeft then
|
100
|
+
@player.turn_left
|
101
|
+
end
|
102
|
+
if button_down? Gosu::KbRight or button_down? Gosu::GpRight then
|
103
|
+
@player.turn_right
|
104
|
+
end
|
105
|
+
if button_down? Gosu::KbUp or button_down? Gosu::GpButton0 then
|
106
|
+
@player.accelerate
|
107
|
+
end
|
108
|
+
@player.move
|
109
|
+
@player.collect_stars(@stars)
|
110
|
+
|
111
|
+
if rand(100) < 4 and @stars.size < 25 then
|
112
|
+
@stars.push(Star.new(@star_anim))
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def draw
|
117
|
+
@background_image.draw(0, 0, ZOrder::Background)
|
118
|
+
@player.draw
|
119
|
+
@stars.each { |star| star.draw }
|
120
|
+
@font.draw("Score: #{@player.score}", 10, 10, ZOrder::UI, 1.0, 1.0, 0xffffff00)
|
121
|
+
end
|
122
|
+
|
123
|
+
def button_down(id)
|
124
|
+
if id == Gosu::KbEscape then
|
125
|
+
close
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
window = GameWindow.new
|
131
131
|
window.show
|