-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnavigator.rb
More file actions
147 lines (139 loc) · 4.56 KB
/
Copy pathnavigator.rb
File metadata and controls
147 lines (139 loc) · 4.56 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
class Navigator < Character
def update(map)
dx = 0
dy = 0
turntime = 0.67 #実行時間
turn = 0.1
# 改造部分
#「→」キーを押すとDXRuby上のアイコンとEV3がアイコンが右を向く
if Input.key_push?(K_RIGHT)
@rot += 9
@rot = @rot % 360
@ev3_controller.move_righturn(turn)
end
#「←」キーを押すとDXRuby上のアイコンとEV3がアイコンが左を向く
if Input.key_push?(K_LEFT)
@rot -= 9
@rot = @rot % 360
@ev3_controller.move_lefturn(turn)
end
#「↑」キー押すとDXRuby上のアイコンの向いてる向きにEV3とアイコンが前に進む
if Input.key_push?(K_UP)
if (0..45).cover?(@rot) || (316..359).cover?(@rot)
dy = 1
@ev3_controller.move_forward(turntime)
elsif (46..90).cover?(@rot) || (91..135).cover?(@rot)
dx = -1
@ev3_controller.move_forward(turntime)
elsif (136..180).cover?(@rot) || (181..225).cover?(@rot)
dy = -1
@ev3_controller.move_forward(turntime)
elsif (226..270).cover?(@rot) || (271..315).cover?(@rot)
dx = 1
@ev3_controller.move_forward(turntime)
end
#case文を使った場合、挙動に難あり
# case @rot
# when 0..45 || 315..360
# dy = -1
# @ev3_controller.move_forward(turntime)
# when 90
# dx = 1
# @ev3_controller.move_forward(turntime)
# when 180
# dy = 1
# @ev3_controller.move_forward(turntime)
# when 270
# dx = -1
# @ev3_controller.move_forward(turntime)
# end
end
#「↓」キー押すとDXRuby上のアイコンの向いてる向きにEV3とアイコンが後ろに進む
if Input.key_push?(K_DOWN)
if (0..45).cover?(@rot) || (316..359).cover?(@rot)
dy = -1
@ev3_controller.move_backward(turntime)
elsif (46..90).cover?(@rot) || (91..135).cover?(@rot)
dx = 1
@ev3_controller.move_backward(turntime)
elsif (136..180).cover?(@rot) || (181..225).cover?(@rot)
dy = 1
@ev3_controller.move_backward(turntime)
elsif (226..270).cover?(@rot) || (271..315).cover?(@rot)
dx = -1
@ev3_controller.move_backward(turntime)
end
#case文を使った場合難あり
# case @rot
# when 0
# dy = 1
# @ev3_controller.move_backward(turntime)
# when 90
# dx = -1
# @ev3_controller.move_backward(turntime)
# when 180
# dy = -1
# @ev3_controller.move_backward(turntime)
# when 270
# dx = 1
# @ev3_controller.move_backward(turntime)
end
# reset機能追加
#「D」キーを押すとDXRuby上のアイコンのみが右を向く
if Input.key_push?(K_D)
@rot += 9
@rot = @rot % 360
end
#「A」キーを押すとDXRuby上のアイコンのみが左を向く
if Input.key_push?(K_A)
@rot -= 9
@rot = @rot % 360
end
if Input.key_push?(K_W)
if (0..45).cover?(@rot) || (316..359).cover?(@rot)
dy = 1
elsif (46..90).cover?(@rot) || (91..135).cover?(@rot)
dx = -1
elsif (136..180).cover?(@rot) || (181..225).cover?(@rot)
dy = -1
elsif (226..270).cover?(@rot) || (271..315).cover?(@rot)
dx = 1
end
end
if Input.key_push?(K_S)
if (0..45).cover?(@rot) || (316..359).cover?(@rot)
dy = -1
puts "0度の時後進"
elsif (46..90).cover?(@rot) || (91..135).cover?(@rot)
dx = 1
puts "90度の時後進"
elsif (136..180).cover?(@rot) || (181..225).cover?(@rot)
dy = 1
puts "180度の時後進"
elsif (226..270).cover?(@rot) || (271..315).cover?(@rot)
dx = -1
puts "270度の時後進"
end
end
# reset機能ここまで
update_new_position(map, @x + dx, @y + dy)
# 追加しました
# co = @ev3_controller.get_color
# map.updata_map_data(@x,@y,co)
# ここまで 北村
Window.draw_font(600, 100, "COLOR: #{@ev3_controller.get_color}", @font)
# 追加する部分(色の判定)
color = @ev3_controller.get_color
# color = @brick.get_sensor(COLOR_SENSOR, 2)
case color
when 0 then puts "色がありません"
when 1 then puts "黒色"
when 2 then map.updata_map_data(@x, @y, 2)
when 3 then map.updata_map_data(@x, @y, 3)
when 4 then puts "黄色"
when 5 then map.updata_map_data(@x, @y, 5)
when 6 then map.updata_map_data(@x, @y, 6)
when 7 then puts "茶色"
end
end
end