nyle 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +37 -0
- data/Rakefile +2 -0
- data/doc/Nyle_reference.pdf +0 -0
- data/lib/nyle/frame.rb +107 -0
- data/lib/nyle/screen.rb +106 -0
- data/lib/nyle/version.rb +3 -0
- data/lib/nyle.rb +345 -0
- data/nyle.gemspec +44 -0
- data/samples/image/n_b_crocodile.png +0 -0
- data/samples/image/n_b_river.gif +0 -0
- data/samples/image/n_f_banana.png +0 -0
- data/samples/image/n_f_panda.png +0 -0
- data/samples/image/n_f_strawberry.png +0 -0
- data/samples/image/n_f_weapon.png +0 -0
- data/samples/image/n_i_bg.jpg +0 -0
- data/samples/image/n_i_chara.png +0 -0
- data/samples/nyle_basics.rb +69 -0
- data/samples/nyle_basics2.rb +70 -0
- data/samples/nyle_block.rb +188 -0
- data/samples/nyle_colors.rb +309 -0
- data/samples/nyle_falling.rb +298 -0
- data/samples/nyle_rpg.rb +98 -0
- data/samples/nyle_shape.rb +70 -0
- data/samples/nyle_sketch.rb +45 -0
- data/samples/nyle_tree.rb +48 -0
- data/samples/nyle_walk_notrace.rb +54 -0
- data/samples/nyle_walk_trace.rb +54 -0
- data/samples/readme_samples.txt +29 -0
- metadata +143 -0
@@ -0,0 +1,298 @@
|
|
1
|
+
|
2
|
+
require 'nyle'
|
3
|
+
|
4
|
+
class Field
|
5
|
+
SKYLINE = 400
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
_init_score
|
9
|
+
end
|
10
|
+
|
11
|
+
private def _init_score
|
12
|
+
@score = 0
|
13
|
+
@click_count = 0
|
14
|
+
end
|
15
|
+
|
16
|
+
def draw
|
17
|
+
Nyle.draw_rect( 0, 0, 640, SKYLINE, {color: :SKY_BLUE, fill: true})
|
18
|
+
Nyle.draw_rect( 0, SKYLINE, 640, 480, {color: :FOREST_GREEN, fill: true})
|
19
|
+
Nyle.draw_text(80, 400 + 24, "Your score #{@score} points Click count #{@click_count} times", {color: :WHITE, size: 24})
|
20
|
+
end
|
21
|
+
|
22
|
+
def add_score(score, click_count)
|
23
|
+
@score += score
|
24
|
+
@click_count += click_count
|
25
|
+
end
|
26
|
+
|
27
|
+
def game_clear?
|
28
|
+
@score >= 100
|
29
|
+
end
|
30
|
+
|
31
|
+
def init_score
|
32
|
+
_init_score
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
class Item
|
38
|
+
MOVING_RANGE_X = 640
|
39
|
+
MOVING_RANGE_Y = 480
|
40
|
+
|
41
|
+
attr_reader :x, :y
|
42
|
+
|
43
|
+
def initialize(filename, xrange, sx = 1.0, sy = 1.0)
|
44
|
+
@image = Nyle.load_image(filename, {sx: sx, sy: sy, color_key: :WHITE})
|
45
|
+
@xrange = xrange
|
46
|
+
@sx = sx
|
47
|
+
@sy = sy
|
48
|
+
reset
|
49
|
+
end
|
50
|
+
|
51
|
+
def reset
|
52
|
+
@x = rand(MOVING_RANGE_X)
|
53
|
+
@y = 0
|
54
|
+
@speed_x = (@xrange == 0 ? 0 : rand(@xrange) - (@xrange - 1) / 2)
|
55
|
+
@speed_y = rand(10) + 1
|
56
|
+
end
|
57
|
+
|
58
|
+
def drop
|
59
|
+
@x = @x + @speed_x
|
60
|
+
@y = @y + @speed_y
|
61
|
+
if @y > MOVING_RANGE_Y
|
62
|
+
reset
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def centerx
|
67
|
+
return @x + @image.width / 2
|
68
|
+
end
|
69
|
+
|
70
|
+
def centery
|
71
|
+
return @y + @image.height / 2
|
72
|
+
end
|
73
|
+
|
74
|
+
def draw
|
75
|
+
Nyle.draw_image(@x, @y, @image)
|
76
|
+
end
|
77
|
+
|
78
|
+
def clicked?
|
79
|
+
clicked = false
|
80
|
+
if Nyle.mouse_press?(1)
|
81
|
+
diffx = Nyle.mouse_x - self.centerx
|
82
|
+
diffy = Nyle.mouse_y - self.centery
|
83
|
+
if (diffx > -20) and (diffx < 20) and (diffy > -20) and (diffy < 20)
|
84
|
+
clicked = true
|
85
|
+
end
|
86
|
+
end
|
87
|
+
return clicked
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
class Weapon < Item
|
93
|
+
def initialize
|
94
|
+
super("image/n_f_weapon.png", 7, 0.4, 0.4)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
class Banana < Item
|
99
|
+
def initialize
|
100
|
+
super("image/n_f_banana.png", 0, 0.6, 0.6)
|
101
|
+
end
|
102
|
+
|
103
|
+
def get_score
|
104
|
+
return 10
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
class Strawberry < Item
|
109
|
+
def initialize
|
110
|
+
super("image/n_f_strawberry.png", 0, 0.6, 0.6)
|
111
|
+
end
|
112
|
+
|
113
|
+
def get_score
|
114
|
+
return 20
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
|
119
|
+
class Hero
|
120
|
+
MOVING_RANGE_X = 640
|
121
|
+
|
122
|
+
def initialize(x, y)
|
123
|
+
@image = Nyle.load_image("./image/n_f_panda.png", {sx: 0.3, sy: 0.3, color_key: :WHITE})
|
124
|
+
@x = x - @image.width / 2
|
125
|
+
@y = y - @image.height
|
126
|
+
@speed = 3.5
|
127
|
+
end
|
128
|
+
|
129
|
+
def move(dx, dy)
|
130
|
+
@x += (dx * @speed)
|
131
|
+
@x = 0 if @x <= 0
|
132
|
+
@x = MOVING_RANGE_X - @image.width if @x >= MOVING_RANGE_X - @image.width
|
133
|
+
end
|
134
|
+
|
135
|
+
def draw
|
136
|
+
Nyle.draw_image(@x, @y, @image)
|
137
|
+
end
|
138
|
+
|
139
|
+
def hit?(item)
|
140
|
+
diffx = _centerx - item.centerx
|
141
|
+
diffy = _centery - item.centery
|
142
|
+
if (diffx > -20) and (diffx < 20) and (diffy > -20) and (diffy < 20)
|
143
|
+
return true
|
144
|
+
else
|
145
|
+
return false
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
private def _centerx
|
150
|
+
return @x + @image.width / 2
|
151
|
+
end
|
152
|
+
|
153
|
+
private def _centery
|
154
|
+
return @y + @image.height / 2
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
|
159
|
+
|
160
|
+
class Screen_GameOver < Nyle::Screen
|
161
|
+
def initialize
|
162
|
+
super(640, 480, {bgcolor: :BEIGE})
|
163
|
+
end
|
164
|
+
def draw
|
165
|
+
Nyle.draw_text( 60, 200, "Game over! Thank you for playing.", {})
|
166
|
+
|
167
|
+
Nyle.draw_text(450, 420, "[SPC] to Retry", {size: 20})
|
168
|
+
Nyle.draw_text(450, 450, "[ESC] to Finish", {size: 20})
|
169
|
+
|
170
|
+
@status = :REPLAY if Nyle.key_press?(KEY_space)
|
171
|
+
Nyle.quit if Nyle.key_press?(KEY_Escape)
|
172
|
+
end
|
173
|
+
|
174
|
+
def suspend
|
175
|
+
end
|
176
|
+
|
177
|
+
def resume
|
178
|
+
# Initialize status
|
179
|
+
@status = nil
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
class Screen_GameClear < Nyle::Screen
|
184
|
+
def initialize
|
185
|
+
super(640, 480)
|
186
|
+
end
|
187
|
+
def draw
|
188
|
+
Nyle.draw_text( 80, 200, "Congratulations! You've won!", {color: :RED})
|
189
|
+
Nyle.draw_text(450, 450, "[ESC] to Finish", {size: 20})
|
190
|
+
|
191
|
+
Nyle.quit if Nyle.key_press?(KEY_Escape)
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
|
196
|
+
class Screen_Play < Nyle::Screen
|
197
|
+
NWEAPON = 3
|
198
|
+
NBANANA = 2
|
199
|
+
NSTRAWBERRY = 1
|
200
|
+
|
201
|
+
def initialize
|
202
|
+
super(640, 480)
|
203
|
+
setup
|
204
|
+
end
|
205
|
+
|
206
|
+
def setup
|
207
|
+
@field = Field.new
|
208
|
+
@hero = Hero.new(@width / 2, Field::SKYLINE)
|
209
|
+
@weapons = []
|
210
|
+
@bananas = []
|
211
|
+
@strawberries = []
|
212
|
+
NWEAPON.times { @weapons << Weapon.new }
|
213
|
+
NBANANA.times { @bananas << Banana.new }
|
214
|
+
NSTRAWBERRY.times { @strawberries << Strawberry.new }
|
215
|
+
end
|
216
|
+
|
217
|
+
def draw
|
218
|
+
@field.draw
|
219
|
+
@hero.draw
|
220
|
+
@weapons.each { |weapon| weapon.draw }
|
221
|
+
@bananas.each { |banana| banana.draw }
|
222
|
+
@strawberries.each { |strawberrie| strawberrie.draw }
|
223
|
+
end
|
224
|
+
|
225
|
+
def update
|
226
|
+
@weapons.each { |weapon| weapon.drop }
|
227
|
+
@bananas.each { |banana| banana.drop }
|
228
|
+
@strawberries.each { |strawberrie| strawberrie.drop }
|
229
|
+
_detect
|
230
|
+
@hero.move( 1, 0) if Nyle.key_down?(KEY_Right)
|
231
|
+
@hero.move(-1, 0) if Nyle.key_down?(KEY_Left)
|
232
|
+
Nyle.quit if Nyle.key_press?(KEY_Escape)
|
233
|
+
end
|
234
|
+
|
235
|
+
private def _detect
|
236
|
+
(@weapons + @bananas + @strawberries).each do |item|
|
237
|
+
# detect collision about hero and item
|
238
|
+
if @hero.hit?(item)
|
239
|
+
if item.instance_of?(Weapon) # weapon
|
240
|
+
@status = :GAMEOVER
|
241
|
+
else # fruit
|
242
|
+
@field.add_score(item.get_score, 0)
|
243
|
+
@status = :GAMECLEAR if @field.game_clear?
|
244
|
+
item.reset
|
245
|
+
end
|
246
|
+
end
|
247
|
+
# detect click on item
|
248
|
+
if item.clicked?
|
249
|
+
if item.instance_of?(Weapon) # weapon
|
250
|
+
@status = :GAMEOVER
|
251
|
+
else # fruit
|
252
|
+
@field.add_score(item.get_score, 1)
|
253
|
+
@status = :GAMECLEAR if @field.game_clear?
|
254
|
+
item.reset
|
255
|
+
end
|
256
|
+
end
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
def suspend
|
261
|
+
end
|
262
|
+
|
263
|
+
def resume
|
264
|
+
# Initialize status
|
265
|
+
@status = nil
|
266
|
+
|
267
|
+
# Initialize positon of items
|
268
|
+
@weapons.each { |weapon| weapon.reset }
|
269
|
+
@bananas.each { |banana| banana.reset }
|
270
|
+
@strawberries.each { |strawberrie| strawberrie.reset }
|
271
|
+
|
272
|
+
# Initialize score
|
273
|
+
@field.init_score
|
274
|
+
end
|
275
|
+
|
276
|
+
end
|
277
|
+
|
278
|
+
|
279
|
+
class Frame < Nyle::Frame
|
280
|
+
def initialize
|
281
|
+
super(640, 480)
|
282
|
+
|
283
|
+
@screen_play = Screen_Play.new
|
284
|
+
@screen_gameover = Screen_GameOver.new
|
285
|
+
@screen_gameclear = Screen_GameClear.new
|
286
|
+
self.set_current(@screen_play)
|
287
|
+
|
288
|
+
# Transition table
|
289
|
+
@transition << {current: @screen_play, status: :GAMEOVER, next: @screen_gameover}
|
290
|
+
@transition << {current: @screen_play, status: :GAMECLEAR, next: @screen_gameclear}
|
291
|
+
@transition << {current: @screen_gameover, status: :REPLAY, next: @screen_play}
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
|
296
|
+
Frame.new.show_all
|
297
|
+
Gtk.main
|
298
|
+
|
data/samples/nyle_rpg.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
|
2
|
+
require 'nyle'
|
3
|
+
|
4
|
+
class Character
|
5
|
+
def initialize(x, y, tiles)
|
6
|
+
@x = x
|
7
|
+
@y = y
|
8
|
+
@tiles = tiles
|
9
|
+
@tx = 0 # initial index of @tiles for x
|
10
|
+
@ty = 0 # initial index of @tiles for y
|
11
|
+
end
|
12
|
+
|
13
|
+
def draw
|
14
|
+
Nyle.draw_image(@x, @y, @tiles[@tx][@ty])
|
15
|
+
end
|
16
|
+
|
17
|
+
def move(dx, dy)
|
18
|
+
@ty = 0 if dy > 0 # index of @tiles for down
|
19
|
+
@ty = 1 if dx < 0 # index of @tiles for left
|
20
|
+
@ty = 2 if dx > 0 # index of @tiles for right
|
21
|
+
@ty = 3 if dy < 0 # index of @tiles for up
|
22
|
+
|
23
|
+
@tx += 1 # animation to walk
|
24
|
+
@tx = 0 if @tx > 2 # range = (0..2)
|
25
|
+
|
26
|
+
@x += dx # drawing position(x)
|
27
|
+
@y += dy # drawing position(y)
|
28
|
+
|
29
|
+
@x = -@tiles[0][0].width if @x > Nyle.screen_width # right end
|
30
|
+
@x = Nyle.screen_width if @x < -@tiles[0][0].width # left end
|
31
|
+
@y = -@tiles[0][0].height if @y > Nyle.screen_height # lower end
|
32
|
+
@y = Nyle.screen_height if @y < -@tiles[0][0].height # upper end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class Screen < Nyle::Screen
|
37
|
+
def initialize
|
38
|
+
super(510, 390)
|
39
|
+
@image = Nyle.load_image("./image/n_i_bg.jpg", {sx: 0.5, sy: 0.5})
|
40
|
+
@image_half_right = Nyle.load_image("./image/n_i_bg.jpg", {sx: 0.5, sy: 0.5, cx: 320, cy: 0, cw: 320, ch: 480})
|
41
|
+
@image_half_low = Nyle.load_image("./image/n_i_bg.jpg", {sx: 0.5, sy: 0.5, cx: 0, cy: 240, cw: 640, ch: 240})
|
42
|
+
@image_quarter = Nyle.load_image("./image/n_i_bg.jpg", {sx: 0.5, sy: 0.5, cx: 320, cy: 240, cw: 320, ch: 240})
|
43
|
+
|
44
|
+
@tiles01 = Nyle.load_image_tiles("./image/n_i_chara.png", 1, 1) # itself
|
45
|
+
@tiles03 = Nyle.load_image_tiles("./image/n_i_chara.png", 3, 1) # divide into 3 pieces horizontally
|
46
|
+
@tiles04 = Nyle.load_image_tiles("./image/n_i_chara.png", 1, 4) # divide into 4 pieces vertically
|
47
|
+
@tiles12 = Nyle.load_image_tiles("./image/n_i_chara.png", 3, 4) # divide into 12 pieces vertically and horizontally
|
48
|
+
|
49
|
+
@tiles_player = Nyle.load_image_tiles("./image/n_i_chara.png", 3, 4, {sx: 1.25, sy: 1.25}) # divide into 12 pieces and enlarge
|
50
|
+
@character = Character.new(140, 150, @tiles_player)
|
51
|
+
end
|
52
|
+
|
53
|
+
def draw
|
54
|
+
Nyle.draw_image( 10, 10, @image)
|
55
|
+
Nyle.draw_image(340, 10, @image_half_right)
|
56
|
+
Nyle.draw_image( 10, 260, @image_half_low)
|
57
|
+
Nyle.draw_image(340, 260, @image_quarter)
|
58
|
+
|
59
|
+
for i in (0...1)
|
60
|
+
for j in (0...1)
|
61
|
+
Nyle.draw_image( 10 + i * 50, 10, @tiles01[i][j])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
for i in (0...3)
|
66
|
+
for j in (0...1)
|
67
|
+
Nyle.draw_image(200 + i * 50, 10, @tiles03[i][j])
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
for i in (0...1)
|
72
|
+
for j in (0...4)
|
73
|
+
Nyle.draw_image( 10, 200 + j * 50, @tiles04[i][j])
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
for i in (0...3)
|
78
|
+
for j in (0...4)
|
79
|
+
Nyle.draw_image(200 + i * 50, 200 + j * 50, @tiles12[i][j])
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
@character.draw
|
84
|
+
|
85
|
+
Nyle.quit if Nyle.key_press?(KEY_Escape)
|
86
|
+
end
|
87
|
+
|
88
|
+
def update
|
89
|
+
dx = (Nyle.key_down?(KEY_Right) ? 1 : (Nyle.key_down?(KEY_Left) ? -1 : 0))
|
90
|
+
dy = (Nyle.key_down?(KEY_Down) ? 1 : (Nyle.key_down?(KEY_Up) ? -1 : 0))
|
91
|
+
@character.move(dx, dy)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
Screen.new.show_all
|
97
|
+
Gtk.main
|
98
|
+
|
@@ -0,0 +1,70 @@
|
|
1
|
+
|
2
|
+
require 'nyle'
|
3
|
+
|
4
|
+
module Points
|
5
|
+
def star(x, y)
|
6
|
+
[
|
7
|
+
[x, y - 50],
|
8
|
+
[x + 14, y - 20],
|
9
|
+
[x + 47, y - 15],
|
10
|
+
[x + 23, y + 7],
|
11
|
+
[x + 29, y + 40],
|
12
|
+
[x, y + 25],
|
13
|
+
[x - 29, y + 40],
|
14
|
+
[x - 23, y + 7],
|
15
|
+
[x - 47, y - 15],
|
16
|
+
[x - 14, y - 20]
|
17
|
+
]
|
18
|
+
end
|
19
|
+
|
20
|
+
def para_star(x, y)
|
21
|
+
rl, rm, rs = 70, 50, 30 # radius(large, middle, small)
|
22
|
+
count = 10
|
23
|
+
angle = 360.0 / (count * 4) # central angle of each vertex
|
24
|
+
deg = Math::PI / 180.0
|
25
|
+
|
26
|
+
points = []
|
27
|
+
for i in 1..count
|
28
|
+
points << [x + rs * Math.cos((i * 4 - 3) * angle * deg), y + rs * Math.sin((i * 4 - 3) * angle * deg)]
|
29
|
+
points << [x + rm * Math.cos((i * 4 - 2) * angle * deg), y + rm * Math.sin((i * 4 - 2) * angle * deg)]
|
30
|
+
points << [x + rs * Math.cos((i * 4 - 1) * angle * deg), y + rs * Math.sin((i * 4 - 1) * angle * deg)]
|
31
|
+
points << [x + rl * Math.cos((i * 4 - 0) * angle * deg), y + rl * Math.sin((i * 4 - 0) * angle * deg)]
|
32
|
+
end
|
33
|
+
points
|
34
|
+
end
|
35
|
+
|
36
|
+
def wave(x, y)
|
37
|
+
points = []
|
38
|
+
for deg in 0..(360 * 5)
|
39
|
+
th = -deg * (Math::PI / 180)
|
40
|
+
points << [deg / 5 + x, Math.sin(th) * 20 + y]
|
41
|
+
end
|
42
|
+
points
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
class Screen < Nyle::Screen
|
48
|
+
include Points
|
49
|
+
def initialize
|
50
|
+
super
|
51
|
+
end
|
52
|
+
def draw
|
53
|
+
Nyle.draw_shape(star(120, 100), {color: :GOLD, weight: 5, close: true})
|
54
|
+
Nyle.draw_shape(star(280, 100), {color: :GOLD, weight: 5, close: true, fill: true})
|
55
|
+
Nyle.draw_shape(star(440, 100), {color: :GOLD, a: 0.5, weight: 5, close: true, fill: true})
|
56
|
+
|
57
|
+
Nyle.draw_shape(para_star(120, 240), {color: :BROWN, weight: 3, close: true})
|
58
|
+
Nyle.draw_shape(para_star(280, 240), {color: :BROWN, weight: 3, close: true, fill: true})
|
59
|
+
Nyle.draw_shape(para_star(440, 240), {color: :BROWN, a: 0.8, weight: 3, close: true, fill: true})
|
60
|
+
|
61
|
+
Nyle.draw_shape(wave(100, 360), {color: :SKY_BLUE, weight: 5, cap: :ROUND})
|
62
|
+
Nyle.draw_shape(wave(100, 410), {color: :SKY_BLUE, weight: 5, cap: :ROUND, fill: true})
|
63
|
+
|
64
|
+
Nyle.quit if Nyle.key_press?(KEY_Escape)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
Screen.new.show_all
|
69
|
+
Gtk.main
|
70
|
+
|
@@ -0,0 +1,45 @@
|
|
1
|
+
|
2
|
+
require 'nyle'
|
3
|
+
|
4
|
+
class Screen < Nyle::Screen
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
super(600, 400, {trace: true})
|
8
|
+
@color = :BLUE
|
9
|
+
end
|
10
|
+
|
11
|
+
def draw
|
12
|
+
#printf "(%3d , %3d) down:%6s press:%6s release:%6s\n",
|
13
|
+
# Nyle.mouse_x,
|
14
|
+
# Nyle.mouse_y,
|
15
|
+
# Nyle.mouse_down[1],
|
16
|
+
# Nyle.mouse_press[1],
|
17
|
+
# Nyle.mouse_release[1]
|
18
|
+
|
19
|
+
Nyle.draw_circle(Nyle.mouse_x, Nyle.mouse_y, 10, {color: @color, fill: true}) if Nyle.mouse_down?(1) # draw (left-click)
|
20
|
+
Nyle.draw_circle(Nyle.mouse_x, Nyle.mouse_y, 10, {color: :WHITE, fill: true}) if Nyle.mouse_down?(3) # erase (right-click)
|
21
|
+
|
22
|
+
Nyle.draw_text( 10, 20, "[mouse left ] draw", {size: 16})
|
23
|
+
Nyle.draw_text( 10, 40, "[mouse right] erase", {size: 16})
|
24
|
+
Nyle.draw_text( 10, 70, "[space] save", {size: 16})
|
25
|
+
Nyle.draw_text( 10, 380, "[r]", {size: 28, color: :RED })
|
26
|
+
Nyle.draw_text( 50, 380, "[g]", {size: 28, color: :FOREST_GREEN })
|
27
|
+
Nyle.draw_text( 90, 380, "[b]", {size: 28, color: :BLUE })
|
28
|
+
Nyle.draw_text(140, 380, "press key to change color", {size: 18})
|
29
|
+
end
|
30
|
+
|
31
|
+
def update
|
32
|
+
@color = :RED if Nyle.key_press?(KEY_r)
|
33
|
+
@color = :FOREST_GREEN if Nyle.key_press?(KEY_g)
|
34
|
+
@color = :BLUE if Nyle.key_press?(KEY_b)
|
35
|
+
|
36
|
+
Nyle.save_image("./sketch.png") if Nyle.key_press?(KEY_space)
|
37
|
+
|
38
|
+
Nyle.quit if Nyle.key_press?(KEY_Escape)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
Screen.new.show_all
|
44
|
+
Gtk.main
|
45
|
+
|
@@ -0,0 +1,48 @@
|
|
1
|
+
|
2
|
+
require 'nyle'
|
3
|
+
|
4
|
+
class Screen < Nyle::Screen
|
5
|
+
def initialize
|
6
|
+
super(300, 200)
|
7
|
+
setup
|
8
|
+
end
|
9
|
+
|
10
|
+
def setup
|
11
|
+
@theta = nil # rotation angle
|
12
|
+
end
|
13
|
+
|
14
|
+
def draw
|
15
|
+
@theta = _map(Nyle.mouse_x, 0, @width, 0, Math::PI/2)
|
16
|
+
Nyle.translate(@width / 2, @height);
|
17
|
+
_branch(60)
|
18
|
+
|
19
|
+
Nyle.quit if Nyle.key_press?(KEY_Escape)
|
20
|
+
end
|
21
|
+
|
22
|
+
private def _map(v, s1, e1, s2, e2)
|
23
|
+
v = v.to_f if v.integer? # Convert integer into float
|
24
|
+
(v - s1) / (e1 - s1) * (e2 - s2) + s2 # Adjust v in between s1 and e1 to between s2 and e2
|
25
|
+
end
|
26
|
+
|
27
|
+
private def _branch(len)
|
28
|
+
sw = _map(len, 2, 120, 1, 10) # adjust weight as length of branch
|
29
|
+
Nyle.draw_line(0, 0, 0, -len, {weight: sw}) # draw branch
|
30
|
+
Nyle.translate(0, -len) # move origin
|
31
|
+
len *= 0.66 # adjust length (two-thirds)
|
32
|
+
if len > 2
|
33
|
+
Nyle.cr.save do
|
34
|
+
Nyle.rotate(@theta) # rotate
|
35
|
+
_branch(len)
|
36
|
+
end
|
37
|
+
Nyle.cr.save do
|
38
|
+
Nyle.rotate(-@theta) # rotate
|
39
|
+
_branch(len)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
Screen.new.show_all
|
47
|
+
Gtk.main
|
48
|
+
|
@@ -0,0 +1,54 @@
|
|
1
|
+
|
2
|
+
require 'nyle'
|
3
|
+
|
4
|
+
class Walker
|
5
|
+
RADIUS = 3
|
6
|
+
def initialize(max_x, max_y)
|
7
|
+
@max_x = max_x
|
8
|
+
@max_y = max_y
|
9
|
+
@x = max_x / 2.0
|
10
|
+
@y = max_y / 2.0
|
11
|
+
@step = 5
|
12
|
+
end
|
13
|
+
|
14
|
+
# Randomly move up, down, left, right
|
15
|
+
def move
|
16
|
+
stepx = rand(3) - 1
|
17
|
+
stepy = rand(3) - 1
|
18
|
+
@x += stepx * @step
|
19
|
+
@y += stepy * @step
|
20
|
+
|
21
|
+
# x,y limitter
|
22
|
+
@x = 0 if @x < 0
|
23
|
+
@x = @max_x if @x > @max_x
|
24
|
+
@y = 0 if @y < 0
|
25
|
+
@y = @max_y if @y > @max_y
|
26
|
+
end
|
27
|
+
|
28
|
+
def draw
|
29
|
+
Nyle.draw_circle(@x, @y, RADIUS, {color: :RED, fill: true})
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
class Screen < Nyle::Screen
|
35
|
+
|
36
|
+
def initialize
|
37
|
+
super(64 * 5, 48 * 5, {bgcolor: :IVORY})
|
38
|
+
@walker = Walker.new(@width, @height)
|
39
|
+
end
|
40
|
+
|
41
|
+
def draw
|
42
|
+
@walker.draw
|
43
|
+
end
|
44
|
+
|
45
|
+
def update
|
46
|
+
@walker.move
|
47
|
+
Nyle.quit if Nyle.key_press?(KEY_Escape)
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
Screen.new.show_all
|
53
|
+
Gtk.main
|
54
|
+
|
@@ -0,0 +1,54 @@
|
|
1
|
+
|
2
|
+
require 'nyle'
|
3
|
+
|
4
|
+
class Walker
|
5
|
+
RADIUS = 3
|
6
|
+
def initialize(max_x, max_y)
|
7
|
+
@max_x = max_x
|
8
|
+
@max_y = max_y
|
9
|
+
@x = max_x / 2.0
|
10
|
+
@y = max_y / 2.0
|
11
|
+
@step = 5
|
12
|
+
end
|
13
|
+
|
14
|
+
# Randomly move up, down, left, right
|
15
|
+
def move
|
16
|
+
stepx = rand(3) - 1
|
17
|
+
stepy = rand(3) - 1
|
18
|
+
@x += stepx * @step
|
19
|
+
@y += stepy * @step
|
20
|
+
|
21
|
+
# x,y limitter
|
22
|
+
@x = 0 if @x < 0
|
23
|
+
@x = @max_x if @x > @max_x
|
24
|
+
@y = 0 if @y < 0
|
25
|
+
@y = @max_y if @y > @max_y
|
26
|
+
end
|
27
|
+
|
28
|
+
def draw
|
29
|
+
Nyle.draw_circle(@x, @y, RADIUS, {color: :RED, fill: true})
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
class Screen < Nyle::Screen
|
35
|
+
|
36
|
+
def initialize
|
37
|
+
super(64 * 5, 48 * 5, {bgcolor: :IVORY, trace: true})
|
38
|
+
@walker = Walker.new(@width, @height)
|
39
|
+
end
|
40
|
+
|
41
|
+
def draw
|
42
|
+
@walker.draw
|
43
|
+
end
|
44
|
+
|
45
|
+
def update
|
46
|
+
@walker.move
|
47
|
+
Nyle.quit if Nyle.key_press?(KEY_Escape)
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
Screen.new.show_all
|
53
|
+
Gtk.main
|
54
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
|
2
|
+
[samples]
|
3
|
+
|
4
|
+
nyle_basics.rb ... basics
|
5
|
+
nyle_basics2.rb ... basics (in case of using 'include' statement to ommit 'Nyle.' modifier)
|
6
|
+
nyle_block.rb ... breakout
|
7
|
+
nyle_colors.rb ... color list
|
8
|
+
nyle_falling.rb ... falling game
|
9
|
+
nyle_rpg.rb ... roll playing game like
|
10
|
+
nyle_shape.rb ... shapes
|
11
|
+
nyle_sketch.rb ... sketch tool
|
12
|
+
nyle_tree.rb ... fractal tree
|
13
|
+
nyle_walk_notrace.rb ... random walk (no trace)
|
14
|
+
nyle_walk_trace.rb ... random walk (trace)
|
15
|
+
|
16
|
+
|
17
|
+
[image files]
|
18
|
+
|
19
|
+
image/n_b_crocodile.png ... crocodile (image for nyle_basics.rb)
|
20
|
+
image/n_b_river.gif ... river (image for nyle_basics.rb)
|
21
|
+
|
22
|
+
image/n_f_banana.png ... banana (image for nyle_falling.rb)
|
23
|
+
image/n_f_panda.png ... panda (image for nyle_falling.rb)
|
24
|
+
image/n_f_strawberry.png ... strawberry(image for nyle_falling.rb)
|
25
|
+
image/n_f_weapon.png ... weapon (image for nyle_falling.rb))
|
26
|
+
|
27
|
+
image/n_i_bg.png ... background(image for nyle_rgb.rb)
|
28
|
+
image/n_i_chara.png ... character (image for nyle_rgb.rb)
|
29
|
+
|