-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpace_Ruby.rb
More file actions
156 lines (114 loc) · 2.63 KB
/
Space_Ruby.rb
File metadata and controls
156 lines (114 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
require 'gosu'
module ZOrder
BACKGROUND, STARS, PLAYER, UI = *0..3
end
class Tutorial < Gosu::Window
def initialize
super 640, 480
self.caption = "Space Ruby"
@background_image = Gosu::Image.new("media/space.png", :tileable => true)
@player = Player.new
@player.wrap(320, 240)
@star_anim = Gosu::Image.load_tiles("media/star.png", 25, 25)
@stars = Array.new
@font = Gosu::Font.new(30)
end
def update
if Gosu.button_down?(Gosu::KbLeft)
@player.turn_left
end
if Gosu.button_down?(Gosu::KbRight)
@player.turn_right
end
if Gosu.button_down?(Gosu::KbUp)
@player.accelarate
end
if Gosu.button_down? (Gosu::KbDown)
@player.accelarate
end
@player.move
@player.collect_stars(@stars)
if rand(100) < 4 and @stars.size < 25
@stars.push(Star.new(@star_anim))
end
end
def draw
@background_image.draw(0, 0, ZOrder::BACKGROUND)
@player.draw
@stars.each { |star| star.draw }
@font.draw("Score: #{@player.score}", 10, 10, ZOrder::UI, 1.0, 1.0, Gosu::Color::GREEN)
end
def button_down(id)
if id == Gosu::KbEscape
close
else
super
end
end
end
class Player
attr_reader :score
def initialize
@image = Gosu::Image.new("media/starfighter.bmp")
@beep = Gosu::Sample.new("media/beep.wav")
@x = @y = @vel_x = @vel_y = @angle = 0.0
@score = 0
end
def wrap(x, y)
@x = x
@y = y
end
def turn_left
@angle -= 4.5
end
def turn_right
@angle += 4.5
end
def accelarate
@vel_x += Gosu.offset_x(@angle, 0.5)
@vel_y += Gosu.offset_y(@angle, 0.5)
end
def move
@x += @vel_x
@y += @vel_y
@x %= 640
@y %= 480
@vel_x *= 0.95
@vel_y *= 0.95
end
def draw
@image.draw_rot(@x, @y, 1, @angle)
end
def score
@score
end
def collect_stars(stars)
stars.reject! do |star|
if Gosu.distance(@x, @y, star.x, star.y) < 35
@score += 10
@beep.play
true
else
false
end
end
end
end
class Star
attr_reader :x, :y
def initialize(animation)
@animation = animation
@color = Gosu::Color::BLACK.dup
@color.red = rand(256 - 40) + 40
@color.green = rand(256 - 40) + 40
@color.blue = rand(256 - 40) + 40
@x = rand * 640
@y = rand * 480
end
def draw
img = @animation[Gosu.milliseconds / 100 % @animation.size]
img.draw(@x - img.width / 2.0, @y - img.height / 2.0,
ZOrder::STARS, 1, 1, @color, :add)
end
end
Tutorial.new.show