smalruby 0.0.2-x86-mingw32

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.

@@ -0,0 +1,124 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'readline'
3
+
4
+ module Smalruby
5
+ # コンソールを表現するクラス
6
+ #
7
+ # コンソールとは、Windowsでは"コマンドプロンプト"、Macでは"ターミナル
8
+ # "、Linuxでは"ターミナル"のことです。キーボードの操作を受け付けたり、
9
+ # コマンドを実行したり、その実行結果を表示するために使います。
10
+ #
11
+ # @example 画面に「こんにちは、世界!」を表示する
12
+ # require 'smalruby'
13
+ # console1 = Console.new
14
+ # console1.on(:start) do
15
+ # puts('こんにちは、世界!')
16
+ # end
17
+ # start
18
+ class Console < Character
19
+ def initialize
20
+ super(0, 0)
21
+ end
22
+
23
+ def loop(&block)
24
+ Kernel.loop(&block)
25
+ end
26
+
27
+ # @!group 出力
28
+
29
+ # @!method p(object)
30
+ # データを読みやすい形に整形して表示します。
31
+ # プログラムの途中経過を表示したりするときに使います。
32
+ #
33
+ # @param [Object] object 表示したいデータ
34
+ # @return void
35
+ #
36
+ # @see Kernel.#p
37
+
38
+ # @!method puts(message)
39
+ # メッセージを表示します。
40
+ #
41
+ # @param [String] message メッセージ
42
+ # @return void
43
+ #
44
+ # @see Kernel.#puts
45
+ #
46
+ # @example 縦に「こんにちは」と表示する
47
+ # require 'smalruby'
48
+ # console1 = Console.new
49
+ # console1.on(:start) do
50
+ # puts('こ')
51
+ # puts('ん')
52
+ # puts('に')
53
+ # puts('ち')
54
+ # puts('は')
55
+ # end
56
+ # start
57
+
58
+ # @!method print(message)
59
+ # メッセージを表示します。
60
+ # メッセージの中に「\n」を含めないと改行されないため、続けてメッセー
61
+ # ジを表示するときに使います。
62
+ #
63
+ # @param [String] message メッセージ
64
+ # @return void
65
+ #
66
+ # @see Kernel.#print
67
+ #
68
+ # @example 横に「こんにちは」と表示する
69
+ # require 'smalruby'
70
+ # console1 = Console.new
71
+ # console1.on(:start) do
72
+ # print('こ')
73
+ # print('ん')
74
+ # print('に')
75
+ # print('ち')
76
+ # puts('は')
77
+ # end
78
+ # start
79
+
80
+ # @!endgroup
81
+
82
+ # @!group 入力
83
+
84
+ # @!method readline(prompt = '')
85
+ # returnキーを押すまでキーボードの操作を受け付けます。
86
+ #
87
+ # @param [String] prompt 画面の左端に表示する文字列
88
+ # @return [String] キーボードから入力した文字列
89
+ #
90
+ # @see Readline.readline
91
+ #
92
+ # @example 「> 」を表示してキーボードからの操作を受け付ける
93
+ # require 'smalruby'
94
+ # console1 = Console.new
95
+ # console1.on(:start) do
96
+ # readline('> ')
97
+ # end
98
+ # start
99
+ def_delegator :Readline, :readline
100
+
101
+ # @!endgroup
102
+
103
+ # @!group 実行
104
+
105
+ # @!method system(program)
106
+ # コマンドを実行します。
107
+ #
108
+ # @param [String] program コマンド
109
+ # @return [bool] コマンドの実行に成功した場合はtrue
110
+ #
111
+ # @see Kernel.#system
112
+
113
+ # コマンドの実行に成功した場合はtrueを返します。
114
+ #
115
+ # @return [bool] 直前のコマンドの実行結果
116
+ #
117
+ # @see $?
118
+ def system_failed?
119
+ return $CHILD_STATUS != 0
120
+ end
121
+
122
+ # @!endgroup
123
+ end
124
+ end
@@ -0,0 +1,24 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Smalruby
3
+ # イベントハンドラを表現するクラス
4
+ class EventHandler
5
+ attr_accessor :object
6
+ attr_accessor :options
7
+ attr_accessor :block
8
+
9
+ # @param [Object] object 操作対象
10
+ # @param [Array] options イベントハンドラのオプション
11
+ # @param [Proc] block イベントハンドラ
12
+ def initialize(object, options, &block)
13
+ @object = object
14
+ @options = options
15
+ @block = block
16
+ end
17
+
18
+ def call(*args)
19
+ return Thread.start(@object, @block) { |object, block|
20
+ object.instance_exec(*args, &block)
21
+ }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module Smalruby
4
+ VERSION = '0.0.2'
5
+ end
@@ -0,0 +1,15 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'singleton'
3
+
4
+ module Smalruby
5
+ # 環境を表現するクラス
6
+ class World
7
+ include Singleton
8
+
9
+ attr_accessor :objects
10
+
11
+ def initialize
12
+ @objects = []
13
+ end
14
+ end
15
+ end
data/lib/smalruby.rb ADDED
@@ -0,0 +1,138 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'smalruby/version'
3
+ require 'active_support/all'
4
+ require 'active_support/concurrency/latch'
5
+ require 'dxruby'
6
+
7
+ require 'smalruby/world'
8
+ require 'smalruby/color'
9
+ require 'smalruby/character'
10
+ require 'smalruby/event_handler'
11
+
12
+ module Smalruby
13
+ extend ActiveSupport::Autoload
14
+
15
+ autoload :Console
16
+ autoload :Canvas
17
+
18
+ module_function
19
+
20
+ def start
21
+ @started = true
22
+ begin
23
+ if world.objects.any? { |o| /console/i !~ o.class.name }
24
+ # ウィンドウアプリケーション
25
+ Window.caption = File.basename($PROGRAM_NAME)
26
+ first = true
27
+ Window.fps = 15
28
+ Window.loop do
29
+ lock do
30
+ if first
31
+ world.objects.each do |object|
32
+ object.start
33
+ end
34
+ first = false
35
+ end
36
+ if Input.key_down?(K_ESCAPE)
37
+ exit
38
+ end
39
+ if Input.mouse_push?(M_LBUTTON) || Input.mouse_push?(M_RBUTTON) ||
40
+ Input.mouse_push?(M_MBUTTON)
41
+ x, y = Input.mouse_pos_x, Input.mouse_pos_y
42
+ s = Sprite.new(x, y)
43
+ s.collision = [0, 0, 1, 1]
44
+ buttons = []
45
+ if Input.mouse_down?(M_LBUTTON)
46
+ buttons << :left
47
+ end
48
+ if Input.mouse_down?(M_RBUTTON)
49
+ buttons << :right
50
+ end
51
+ if Input.mouse_down?(M_MBUTTON)
52
+ buttons << :center
53
+ end
54
+ s.check(world.objects).each do |o|
55
+ if o.respond_to?(:click)
56
+ o.click(buttons)
57
+ end
58
+ end
59
+ end
60
+ if (keys = Input.keys).length > 0
61
+ key_down_and_push(keys)
62
+ end
63
+ world.objects.delete_if { |o|
64
+ if !o.alive?
65
+ o.join
66
+ end
67
+ o.vanished?
68
+ }
69
+ Sprite.draw(world.objects)
70
+ end
71
+ end
72
+ else
73
+ # コンソールアプリケーション
74
+ world.objects.each do |object|
75
+ object.start
76
+ end
77
+ world.objects.each(&:join)
78
+ end
79
+ rescue SystemExit
80
+ end
81
+ end
82
+
83
+ def started?
84
+ return @started
85
+ end
86
+
87
+ def world
88
+ return World.instance
89
+ end
90
+
91
+ def await
92
+ @draw_mutex.synchronize do
93
+ @draw_cv.wait(@draw_mutex)
94
+ end
95
+ end
96
+
97
+ private
98
+
99
+ @started = false
100
+ @draw_mutex = Mutex.new
101
+ @draw_cv = ConditionVariable.new
102
+
103
+ class << self
104
+
105
+ private
106
+
107
+ def lock(&block)
108
+ @draw_mutex.synchronize do
109
+ yield
110
+ @draw_cv.broadcast
111
+ end
112
+ end
113
+
114
+ def key_down_and_push(keys)
115
+ world.objects.each do |o|
116
+ if o.respond_to?(:key_down)
117
+ o.key_down(keys)
118
+ end
119
+ end
120
+ pushed_keys = keys.select { |key| Input.key_push?(key) }
121
+ if pushed_keys.length > 0
122
+ world.objects.each do |o|
123
+ if o.respond_to?(:key_push)
124
+ o.key_push(pushed_keys)
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
131
+
132
+ include Smalruby
133
+
134
+ at_exit do
135
+ if !Smalruby.started?
136
+ Smalruby.start
137
+ end
138
+ end
@@ -0,0 +1,41 @@
1
+ MethodLength:
2
+ Enabled: false
3
+
4
+ ClassLength:
5
+ Enabled: false
6
+
7
+ Documentation:
8
+ Enabled: false
9
+
10
+ HandleExceptions:
11
+ Enabled: false
12
+
13
+ Blocks:
14
+ Enabled: false
15
+
16
+ FavorUnlessOverNegatedIf:
17
+ Enabled: false
18
+
19
+ IfUnlessModifier:
20
+ Enabled: false
21
+
22
+ RedundantReturn:
23
+ Enabled: false
24
+
25
+ AsciiComments:
26
+ Enabled: false
27
+
28
+ CaseEquality:
29
+ Enabled: false
30
+
31
+ SymbolName:
32
+ Enabled: false
33
+
34
+ StringLiterals:
35
+ Enabled: false
36
+
37
+ AsciiIdentifiers:
38
+ Enabled: false
39
+
40
+ VariableName:
41
+ Enabled: false
data/samples/canvas.rb ADDED
@@ -0,0 +1,9 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'smalruby'
3
+
4
+ canvas1 = Canvas.new
5
+ canvas1.on(:start) do
6
+ draw_font(x: 0, y: 0, string: 'こんにちは', size: 32)
7
+ line(x1: 0, y1: 100, x2: 100, y2: 200, color: "white")
8
+ box_fill(left: 0, top: 300, right: 100, bottom: 400, color: "green")
9
+ end
data/samples/car.rb ADDED
@@ -0,0 +1,15 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'smalruby'
3
+
4
+ car1 = Character.new(x: 0, y: 0, costume: 'car1.png')
5
+
6
+ car1.on(:start) do
7
+ loop do
8
+ move(5)
9
+ turn_if_reach_wall
10
+ end
11
+ end
12
+
13
+ car1.on(:click) do
14
+ turn
15
+ end
@@ -0,0 +1,17 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'smalruby'
3
+
4
+ console1 = Console.new
5
+
6
+ console1.on(:start) do
7
+ loop do
8
+ input = readline('> ')
9
+ if !input || input.empty?
10
+ break
11
+ end
12
+ system(input)
13
+ if system_failed?
14
+ puts(['command not found: ', input].join)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,110 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "smalruby"
3
+
4
+ canvas1 = Canvas.new
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)
10
+
11
+ canvas1.on(:start) do
12
+ draw_font(x: 0, y: 0, string: "画面をクリックして隠れている車を4つ探してね", size: 32)
13
+ draw_font(x: 0, y: 32, string: "ヒント1:線の少し上だよ", size: 24)
14
+ draw_font(x: 0, y: 56, string: "ヒント2:上下に2台ずつだよ", size: 24)
15
+ draw_font(x: 0, y: 80, string: "ヒント3:クリックの色は白→青→緑→赤の順に車に近いよ", size: 24)
16
+ box_fill(left: 0, top: 200, right: 639, bottom: 204, color: "white")
17
+ box_fill(left: 0, top: 400, right: 639, bottom: 404, color: "white")
18
+
19
+ loop do
20
+ のこり台数 = 0
21
+ if !car1.visible
22
+ のこり台数 += 1
23
+ end
24
+ if !car2.visible
25
+ のこり台数 += 1
26
+ end
27
+ if !car3.visible
28
+ のこり台数 += 1
29
+ end
30
+ if !car4.visible
31
+ のこり台数 += 1
32
+ end
33
+ box_fill(left: 0, top: 450, right: 639, bottom: 479, color: "black")
34
+ if のこり台数 == 0
35
+ draw_font(x: 0, y: 450, string: "やったね、全部見つけたよ!!", size: 24)
36
+ else
37
+ draw_font(x: 0, y: 450, string: "あと#{ のこり台数 }台みつけてね", size: 24)
38
+ end
39
+ end
40
+ end
41
+ canvas1.on(:click, :left) do |x, y|
42
+ canvas2 = Canvas.new(x: x - 9, y: y - 9, width: 20, height: 20)
43
+ canvas2.on(:start) do
44
+ if !car1.visible && car1.distance(x, y) < 32 ||
45
+ !car2.visible && car2.distance(x, y) < 32 ||
46
+ !car3.visible && car3.distance(x, y) < 32 ||
47
+ !car4.visible && car4.distance(x, y) < 32
48
+ color = "red"
49
+ elsif !car1.visible && car1.distance(x, y) < 64 ||
50
+ !car2.visible && car2.distance(x, y) < 64 ||
51
+ !car3.visible && car3.distance(x, y) < 64 ||
52
+ !car4.visible && car4.distance(x, y) < 64
53
+ color = "green"
54
+ elsif !car1.visible && car1.distance(x, y) < 128 ||
55
+ !car2.visible && car2.distance(x, y) < 128 ||
56
+ !car3.visible && car3.distance(x, y) < 128 ||
57
+ !car4.visible && car4.distance(x, y) < 128
58
+ color = "blue"
59
+ else
60
+ color = "white"
61
+ end
62
+ circle_fill(x: 9, y: 9, r: 9, color: color)
63
+ sleep(0.5)
64
+ vanish
65
+ end
66
+ end
67
+
68
+ car1.on(:start) do
69
+ speed = rand(1..5)
70
+ loop do
71
+ move(speed)
72
+ turn_if_reach_wall
73
+ end
74
+ end
75
+ car1.on(:click) do
76
+ self.visible = true
77
+ end
78
+
79
+ car2.on(:start) do
80
+ speed = rand(10..15)
81
+ loop do
82
+ move(speed)
83
+ turn_if_reach_wall
84
+ end
85
+ end
86
+ car2.on(:click) do
87
+ self.visible = true
88
+ end
89
+
90
+ car3.on(:start) do
91
+ speed = rand(1..5)
92
+ loop do
93
+ move(speed)
94
+ turn_if_reach_wall
95
+ end
96
+ end
97
+ car3.on(:click) do
98
+ self.visible = true
99
+ end
100
+
101
+ car4.on(:start) do
102
+ speed = rand(10..15)
103
+ loop do
104
+ move(speed)
105
+ turn_if_reach_wall
106
+ end
107
+ end
108
+ car4.on(:click) do
109
+ self.visible = true
110
+ end
data/smalruby.gemspec ADDED
@@ -0,0 +1,44 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'smalruby/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ platform = ENV['GEM_PLATFORM'] || Gem::Platform.local.to_s
8
+ is_windows = /windows|mingw|cygwin/i.match(platform)
9
+
10
+ spec.name = 'smalruby'
11
+ if is_windows
12
+ spec.platform = 'x86-mingw32'
13
+ end
14
+ spec.version = Smalruby::VERSION
15
+ spec.authors = ['Kouji Takao']
16
+ spec.email = ['kouji.takao@gmail.com']
17
+ spec.description = %q{smalruby is a 2D game development library. This is part of "Smalruby" project that is a learning ruby programming environment for kids.}
18
+ spec.summary = %q{2D game development library for kids.}
19
+ spec.homepage = 'https://github.com/smalruby/smalruby'
20
+ spec.license = 'MIT'
21
+
22
+ spec.files = `git ls-files`.split($/)
23
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
24
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.3'
28
+ spec.add_development_dependency 'rake'
29
+ spec.add_development_dependency 'yard'
30
+ spec.add_development_dependency 'redcarpet'
31
+ spec.add_development_dependency 'rspec'
32
+ spec.add_development_dependency 'coveralls'
33
+ spec.add_development_dependency 'travis-lint'
34
+ spec.add_development_dependency 'rubocop'
35
+ spec.add_development_dependency 'guard-rspec'
36
+ spec.add_development_dependency 'guard-rubocop'
37
+
38
+ spec.add_runtime_dependency 'activesupport'
39
+ if is_windows
40
+ spec.add_runtime_dependency 'dxruby'
41
+ else
42
+ spec.add_runtime_dependency 'dxruby_sdl'
43
+ end
44
+ end