smalruby 0.0.22 → 0.0.23

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of smalruby might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: de6e1c7079c9d6009b4bfc8b01273a56cfc6f5f6
4
- data.tar.gz: c2a06dc918e1b954326223a0b1c0f285a676e4c5
3
+ metadata.gz: a76d017fb8b6cd731e780d4a89a9f7c72c69cf30
4
+ data.tar.gz: 54b075a46a50612a8669df99649ecf9286ea4fba
5
5
  SHA512:
6
- metadata.gz: 47aef39007fd0eab1c8a7a3231919a088878aa317d3a9d8c0bd1cad4b36098b510ec7ece627b41453215146cf196a7835ec389039ba5256283b5025649425e13
7
- data.tar.gz: ad6ee34acd7a6c23baef0911ca120ded188829fdad58996941f4eb10822ed181fb56e14e80d5d8fe78fae9bb15976fc314ce7e825efc22939e4af71da06cbbbf
6
+ metadata.gz: 6c67d0a0ca8c4cc53608166a1d811fc0444645cfb11a69fc5babfb1e5b3f60860990b40f8e4fb5733d364e6e42da55ae17db94ddb068a69c73ccb8525d6185d1
7
+ data.tar.gz: 1123ef3818f59b4fdbde50e166a98f785fe06266c1dc79c2bd77e83b675f5cd31927b1d02ec58392643f13040fd7fdab2a577181913a3ce91a93f7dc690e38ee
@@ -80,7 +80,7 @@ module Smalruby
80
80
  private
81
81
 
82
82
  DEFAULT_COLOR_OPTION = {
83
- color: 'white'
83
+ color: 'black'
84
84
  }
85
85
  private_constant :DEFAULT_COLOR_OPTION
86
86
 
@@ -25,6 +25,8 @@ module Smalruby
25
25
  attr_accessor :costumes
26
26
  attr_accessor :costume_index
27
27
  attr_reader :rotation_style
28
+ attr_reader :enable_pen
29
+ attr_accessor :pen_color
28
30
 
29
31
  def initialize(option = {})
30
32
  defaults = {
@@ -47,6 +49,8 @@ module Smalruby
47
49
  @threads = []
48
50
  @checking_hit_targets = []
49
51
  @angle = 0 unless Util.windows?
52
+ @enable_pen = false
53
+ @pen_color = 'black'
50
54
 
51
55
  self.scale_x = 1.0
52
56
  self.scale_y = 1.0
@@ -75,8 +79,7 @@ module Smalruby
75
79
 
76
80
  # ( )歩動かす
77
81
  def move(val = 1)
78
- self.x += @vector[:x] * val
79
- self.y += @vector[:y] * val
82
+ self.position = [x + @vector[:x] * val, y + @vector[:y] * val]
80
83
  end
81
84
 
82
85
  # ( )歩後ろに動かす
@@ -84,6 +87,58 @@ module Smalruby
84
87
  move(-val)
85
88
  end
86
89
 
90
+ # X座標を( )にする
91
+ def x=(val)
92
+ left = x + center_x
93
+ top = y + center_y
94
+
95
+ if val < 0
96
+ val = 0
97
+ elsif val + image.width >= Window.width
98
+ val = Window.width - image.width
99
+ end
100
+
101
+ super(val)
102
+
103
+ draw_pen(left, top, x + center_x, y + center_y) if @enable_pen
104
+ end
105
+
106
+ # Y座標を( )にする
107
+ def y=(val)
108
+ left = x + center_x
109
+ top = y + center_y
110
+
111
+ if val < 0
112
+ val = 0
113
+ elsif val + image.height >= Window.height
114
+ val = Window.height - image.height
115
+ end
116
+ super(val)
117
+
118
+ draw_pen(left, top, x + center_x, y + center_y) if @enable_pen
119
+ end
120
+
121
+ # X座標を( )、Y座標を( )にする
122
+ def position=(val)
123
+ if @enable_pen
124
+ @enable_pen = false
125
+ left = x + center_x
126
+ top = y + center_y
127
+ self.x = val[0]
128
+ self.y = val[1]
129
+ draw_pen(left, top, x + center_x, y + center_y)
130
+ @enable_pen = true
131
+ else
132
+ self.x = val[0]
133
+ self.y = val[1]
134
+ end
135
+ end
136
+
137
+ # X座標、Y座標
138
+ def position
139
+ [x, y]
140
+ end
141
+
87
142
  # くるっと振り返る
88
143
  def turn
89
144
  sync_angle(@vector[:x] * -1, @vector[:y] * -1)
@@ -191,12 +246,16 @@ module Smalruby
191
246
  }
192
247
  opts = process_optional_arguments(options, defaults)
193
248
 
249
+ message = opts[:message].to_s
250
+ return if message == @current_message
251
+
252
+ @current_message = message
253
+
194
254
  if @balloon
195
255
  @balloon.vanish
196
256
  @balloon = nil
197
257
  end
198
258
 
199
- message = opts[:message].to_s
200
259
  return if message.empty?
201
260
 
202
261
  lines = message.to_s.lines.map { |l| l.scan(/.{1,10}/) }.flatten
@@ -222,7 +281,7 @@ module Smalruby
222
281
  frame_size + margin_size + (font.size + 1) * row,
223
282
  line, font, [0, 0, 0])
224
283
  end
225
- @balloon = Sprite.new(self.x, self.y, image)
284
+ @balloon = Sprite.new(x, y, image)
226
285
  end
227
286
 
228
287
  # 次のコスチュームにする
@@ -249,12 +308,12 @@ module Smalruby
249
308
 
250
309
  # 左右の端に着いた?
251
310
  def reach_left_or_right_wall?
252
- self.x < 0 || self.x >= (Window.width - image.width)
311
+ x <= 0 || x >= (Window.width - image.width)
253
312
  end
254
313
 
255
314
  # 上下の端に着いた?
256
315
  def reach_top_or_bottom_wall?
257
- self.y < 0 || self.y >= (Window.height - image.height)
316
+ y <= 0 || y >= (Window.height - image.height)
258
317
  end
259
318
 
260
319
  def hit?(other)
@@ -265,6 +324,7 @@ module Smalruby
265
324
 
266
325
  # @!group 音
267
326
 
327
+ # ( )の音を鳴らす
268
328
  def play(option = {})
269
329
  defaults = {
270
330
  name: 'piano_do.wav'
@@ -276,6 +336,23 @@ module Smalruby
276
336
 
277
337
  # @!endgroup
278
338
 
339
+ # @!group ペン
340
+
341
+ # ペンを下ろす
342
+ def down_pen
343
+ @enable_pen = true
344
+ end
345
+
346
+ # ペンを上げる
347
+ def up_pen
348
+ @enable_pen = false
349
+ end
350
+
351
+ # @!method pen_color=(val)
352
+ # ペンの色を( )にする
353
+
354
+ # @!endgroup
355
+
279
356
  # @!group ハードウェア
280
357
 
281
358
  # LED
@@ -318,16 +395,6 @@ module Smalruby
318
395
  def draw
319
396
  draw_balloon
320
397
 
321
- if self.x < 0
322
- self.x = 0
323
- elsif self.x + image.width >= Window.width
324
- self.x = Window.width - image.width
325
- end
326
- if self.y < 0
327
- self.y = 0
328
- elsif self.y + image.height >= Window.height
329
- self.y = Window.height - image.height
330
- end
331
398
  super
332
399
  end
333
400
 
@@ -453,6 +520,12 @@ module Smalruby
453
520
 
454
521
  private
455
522
 
523
+ def draw_pen(left, top, right, bottom)
524
+ world.current_stage.line(left: left, top: top,
525
+ right: right, bottom: bottom,
526
+ color: @pen_color)
527
+ end
528
+
456
529
  def sync_angle(x, y)
457
530
  a = Math.acos(x / Math.sqrt(x**2 + y**2)) * 180 / Math::PI
458
531
  a = 360 - a if y < 0
@@ -482,13 +555,13 @@ module Smalruby
482
555
 
483
556
  def draw_balloon
484
557
  if @balloon
485
- @balloon.x = self.x + image.width / 2
558
+ @balloon.x = x + image.width / 2
486
559
  if @balloon.x < 0
487
560
  @balloon.x = 0
488
561
  elsif @balloon.x + @balloon.image.width >= Window.width
489
562
  @balloon.x = Window.width - @balloon.image.width
490
563
  end
491
- @balloon.y = self.y - @balloon.image.height
564
+ @balloon.y = y - @balloon.image.height
492
565
  if @balloon.y < 0
493
566
  @balloon.y = 0
494
567
  elsif @balloon.y + @balloon.image.height >= Window.height
@@ -155,8 +155,8 @@ module Smalruby
155
155
 
156
156
  # Smalrubyの色名からDXRubyの色コードに変換する
157
157
  def smalruby_to_dxruby(color)
158
- if color.is_a?(String)
159
- color = color.downcase
158
+ if color.is_a?(String) || color.is_a?(Symbol)
159
+ color = color.to_s.downcase
160
160
  if color == 'random'
161
161
  [rand(0..0xff), rand(0..0xff), rand(0..0xff)]
162
162
  elsif NAME_TO_CODE.key?(color)
@@ -14,6 +14,10 @@ module Smalruby
14
14
 
15
15
  # @!method off
16
16
  # LEDをオフにする
17
+
18
+ def stop
19
+ off
20
+ end
17
21
  end
18
22
  end
19
23
  end
@@ -36,6 +36,10 @@ module Smalruby
36
36
  digital_write(pins[3], Dino::Board::LOW)
37
37
  end
38
38
 
39
+ def stop
40
+ off
41
+ end
42
+
39
43
  private
40
44
 
41
45
  def after_initialize(_ = {})
@@ -38,6 +38,10 @@ module Smalruby
38
38
  analog_write(pins[2], calc_value(color[2]))
39
39
  end
40
40
 
41
+ def stop
42
+ off
43
+ end
44
+
41
45
  private
42
46
 
43
47
  def after_initialize(_ = {})
@@ -15,6 +15,10 @@ module Smalruby
15
15
  super(board: world.board, pin: Pin.smalruby_to_dino(options[:pin]))
16
16
  end
17
17
 
18
+ def stop
19
+ @board.remove_analog_hardware(self)
20
+ end
21
+
18
22
  private
19
23
 
20
24
  def after_initialize(options = {})
@@ -46,6 +46,16 @@ module Smalruby
46
46
  @initialized_hardware = true
47
47
  end
48
48
 
49
+ # ハードウェアを停止させる
50
+ def stop
51
+ @hardware_cache.synchronize do
52
+ @hardware_cache.values.each do |h|
53
+ h.stop if h.respond_to?(:stop)
54
+ end
55
+ @hardware_cache.clear
56
+ end
57
+ end
58
+
49
59
  # ハードウェアのインスタンスを生成する
50
60
  #
51
61
  # 作成したハードウェアのインスタンスはキャッシュする
@@ -11,6 +11,11 @@ module Smalruby
11
11
 
12
12
  super(opts.reject { |k, _| defaults.keys.include?(k) })
13
13
 
14
+ # HACK: ステージを一番最初に描画する
15
+ World.instance.objects.delete(self)
16
+ World.instance.objects.unshift(self)
17
+ World.instance.current_stage = self
18
+
14
19
  fill(color: opts[:color])
15
20
  end
16
21
  end
@@ -1,5 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module Smalruby
4
- VERSION = '0.0.22'
4
+ VERSION = '0.0.23'
5
5
  end
@@ -8,6 +8,7 @@ module Smalruby
8
8
 
9
9
  attr_accessor :objects
10
10
  attr_accessor :board
11
+ attr_accessor :current_stage
11
12
  attr_reader :sensor_change_queue
12
13
  attr_reader :button_change_queue
13
14
 
data/lib/smalruby.rb CHANGED
@@ -25,7 +25,11 @@ module Smalruby
25
25
  @started = true
26
26
  begin
27
27
  if world.objects.any? { |o| /console/i !~ o.class.name }
28
- start_window_application
28
+ begin
29
+ start_window_application
30
+ ensure
31
+ Hardware.stop
32
+ end
29
33
  else
30
34
  start_console_application
31
35
  end
@@ -114,6 +118,9 @@ module Smalruby
114
118
  end
115
119
 
116
120
  if first
121
+ unless world.objects.any? { |o| o.is_a?(Stage) }
122
+ Stage.new(color: 'white')
123
+ end
117
124
  world.objects.each do |object|
118
125
  object.start
119
126
  end
data/samples/canvas.rb CHANGED
@@ -4,6 +4,6 @@ require 'smalruby'
4
4
  canvas1 = Canvas.new
5
5
  canvas1.on(:start) do
6
6
  draw_font(x: 0, y: 0, string: 'こんにちは', size: 32)
7
- line(x1: 0, y1: 100, x2: 100, y2: 200, color: "white")
7
+ line(x1: 0, y1: 100, x2: 100, y2: 200, color: "red")
8
8
  box_fill(left: 0, top: 300, right: 100, bottom: 400, color: "green")
9
9
  end
@@ -1,12 +1,12 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  require "smalruby"
3
3
 
4
- canvas1 = Canvas.new
4
+ canvas1 = Stage.new(color: 'black')
5
5
 
6
- car1 = Character.new(x: 0, y: 148, costume: "car1.png", visible: false)
7
- car2 = Character.new(x: 639, y: 148, costume: "car2.png", visible: false)
8
- car3 = Character.new(x: 0, y: 348, costume: "car3.png", visible: false)
9
- car4 = Character.new(x: 639, y: 348, costume: "car4.png", visible: false)
6
+ car1 = Character.new(x: 0, y: 148, costume: "car1.png", visible: false, rotation_style: :left_right)
7
+ car2 = Character.new(x: 639, y: 148, costume: "car2.png", visible: false, rotation_style: :left_right)
8
+ car3 = Character.new(x: 0, y: 348, costume: "car3.png", visible: false, rotation_style: :left_right)
9
+ car4 = Character.new(x: 639, y: 348, costume: "car4.png", visible: false, rotation_style: :left_right)
10
10
 
11
11
  canvas1.on(:start) do
12
12
  draw_font(x: 0, y: 0, string: "画面をクリックして隠れている車を4つ探してね", size: 32)
data/samples/pen.rb ADDED
@@ -0,0 +1,38 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'smalruby'
3
+
4
+ car1 = Character.new(x: 0, y: 0, costume: 'car1.png', rotation_style: :left_right)
5
+
6
+ car1.on(:start) do
7
+ loop do
8
+ if enable_pen
9
+ say(message: 'ペンを下げる')
10
+ else
11
+ say(message: 'ペンを上げる')
12
+ end
13
+ end
14
+ end
15
+
16
+ car1.on(:click) do
17
+ if enable_pen
18
+ up_pen
19
+ else
20
+ down_pen
21
+ end
22
+ end
23
+
24
+ car1.on(:key_down, K_LEFT) do
25
+ self.x -= 10
26
+ end
27
+
28
+ car1.on(:key_down, K_RIGHT) do
29
+ self.x += 10
30
+ end
31
+
32
+ car1.on(:key_down, K_UP) do
33
+ self.y -= 10
34
+ end
35
+
36
+ car1.on(:key_down, K_DOWN) do
37
+ self.y += 10
38
+ end
data/samples/pen2.rb ADDED
@@ -0,0 +1,54 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'smalruby'
3
+
4
+ car1 = Character.new(x: 0, y: 0, costume: 'car1.png', rotation_style: :free)
5
+
6
+ car1.on(:start) do
7
+ loop do
8
+ if enable_pen
9
+ say(message: 'ペンを下げる')
10
+ else
11
+ say(message: 'ペンを上げる')
12
+ end
13
+ end
14
+ end
15
+
16
+ car1.on(:click) do
17
+ if enable_pen
18
+ up_pen
19
+ else
20
+ down_pen
21
+ end
22
+ end
23
+
24
+ car1.on(:key_push, K_1) do
25
+ self.pen_color = 'red'
26
+ end
27
+
28
+ car1.on(:key_push, K_2) do
29
+ self.pen_color = 'blue'
30
+ end
31
+
32
+ car1.on(:key_push, K_3) do
33
+ self.pen_color = 'green'
34
+ end
35
+
36
+ car1.on(:key_push, K_4) do
37
+ self.pen_color = 'black'
38
+ end
39
+
40
+ car1.on(:key_down, K_LEFT) do
41
+ rotate(-15)
42
+ end
43
+
44
+ car1.on(:key_down, K_RIGHT) do
45
+ rotate(15)
46
+ end
47
+
48
+ car1.on(:key_down, K_UP) do
49
+ move(30)
50
+ end
51
+
52
+ car1.on(:key_down, K_DOWN) do
53
+ move(-30)
54
+ end
data/samples/tree.rb CHANGED
@@ -33,7 +33,7 @@ class Tree
33
33
  y = @y
34
34
  @x += Math.cos(@radian) * length
35
35
  @y += Math.sin(@radian) * length
36
- @canvas.line(x1: x, y1: y, x2: @x, y2: @y, color: "white")
36
+ @canvas.line(x1: x, y1: y, x2: @x, y2: @y, color: "green")
37
37
  end
38
38
 
39
39
  def backward(length)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smalruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.22
4
+ version: 0.0.23
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kouji Takao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-31 00:00:00.000000000 Z
11
+ date: 2014-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -286,6 +286,8 @@ files:
286
286
  - samples/hardware_servo.rb
287
287
  - samples/hardware_two_wheel_drive_car.rb
288
288
  - samples/hit_car.rb
289
+ - samples/pen.rb
290
+ - samples/pen2.rb
289
291
  - samples/piano.rb
290
292
  - samples/tree.rb
291
293
  - smalruby.gemspec