minigl 2.2.2 → 2.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,108 +1,108 @@
1
- require 'test/unit'
2
- require_relative '../lib/minigl'
3
- include MiniGL
4
-
5
- class SpriteTest < Test::Unit::TestCase
6
- def setup
7
- @window = GameWindow.new 800, 600, false
8
- Res.prefix = File.expand_path(File.dirname(__FILE__)) + '/data'
9
- end
10
-
11
- def test_sprite_position
12
- s = Sprite.new 10, 20, :image
13
- assert_equal 10, s.x
14
- assert_equal 20, s.y
15
- s = Sprite.new -100, 200, :image
16
- assert_equal -100, s.x
17
- assert_equal 200, s.y
18
- end
19
-
20
- def test_sprite_animation
21
- s = Sprite.new 10, 20, :image, 3, 1
22
- indices = [0, 1, 2]
23
- interval = 1
24
- 3.times { s.animate indices, interval }
25
- assert_equal 0, s.img_index
26
- 5.times { s.animate indices, interval }
27
- assert_equal 2, s.img_index
28
-
29
- 10.times { s.animate_once indices, interval }
30
- assert_equal 2, s.img_index
31
- s.animate_once indices, interval
32
- assert_equal 2, s.img_index
33
-
34
- indices2 = [0, 1, 2, 3]
35
- s.animate_once indices2, 1
36
- assert_equal 0, s.img_index
37
- 10.times { s.animate_once indices2, interval }
38
- assert_equal 3, s.img_index
39
-
40
- s.set_animation 0
41
- 2.times { s.animate_once indices2, 1 }
42
- assert_equal 1, s.img_index
43
- end
44
-
45
- def test_sprite_visibility
46
- # m = Map.new(1, 1, 800, 600)
47
- s = Sprite.new 10, 20, :image, 3, 1 # the sprite will be 1 x 1 pixel
48
- assert s.visible?
49
- s.x = 800
50
- assert(!(s.visible?))
51
- s.x = -1
52
- assert(!(s.visible?))
53
- s.x = 0
54
- assert s.visible?
55
- s.y = 600
56
- assert(!(s.visible?))
57
- end
58
- end
59
-
60
- class GameObjectTest < Test::Unit::TestCase
61
- def setup
62
- @window = GameWindow.new 800, 600, false
63
- Res.prefix = File.expand_path(File.dirname(__FILE__)) + '/data'
64
- end
65
-
66
- def test_game_object_attributes
67
- o = GameObject.new 10, 20, 3, 1, :image
68
- assert_equal 10, o.x
69
- assert_equal 20, o.y
70
- assert_equal 3, o.w
71
- assert_equal 1, o.h
72
- assert_equal 0, o.speed.x
73
- assert_equal 0, o.speed.y
74
- assert_equal 0, o.stored_forces.x
75
- assert_equal 0, o.stored_forces.y
76
- end
77
-
78
- def test_game_object_animation
79
- o = GameObject.new 10, 20, 3, 1, :image, nil, 3, 1
80
- indices = [0, 1, 2]
81
- interval = 10
82
- 5.times { o.animate indices, interval }
83
- assert_equal 0, o.img_index
84
- o.set_animation 0
85
- 5.times { o.animate indices, interval }
86
- assert_equal 0, o.img_index
87
- 5.times { o.animate indices, interval }
88
- assert_equal 1, o.img_index
89
- end
90
-
91
- def test_game_object_visibility
92
- # m = Map.new(1, 1, 800, 600)
93
- o = GameObject.new 10, 20, 30, 30, :square, Vector.new(-10, -10)
94
- assert o.visible?
95
- o.x = 800
96
- assert o.visible?
97
- o.x = 810
98
- assert(!(o.visible?))
99
- o.x = -30
100
- assert o.visible?
101
- o.y = -50
102
- assert(!(o.visible?))
103
- o.x = 0; o.y = -30
104
- assert o.visible?
105
- o.y = 610
106
- assert(!(o.visible?))
107
- end
108
- end
1
+ require 'test/unit'
2
+ require_relative '../lib/minigl'
3
+ include MiniGL
4
+
5
+ class SpriteTest < Test::Unit::TestCase
6
+ def setup
7
+ @window = GameWindow.new 800, 600, false
8
+ Res.prefix = File.expand_path(File.dirname(__FILE__)) + '/data'
9
+ end
10
+
11
+ def test_sprite_position
12
+ s = Sprite.new 10, 20, :image
13
+ assert_equal 10, s.x
14
+ assert_equal 20, s.y
15
+ s = Sprite.new -100, 200, :image
16
+ assert_equal -100, s.x
17
+ assert_equal 200, s.y
18
+ end
19
+
20
+ def test_sprite_animation
21
+ s = Sprite.new 10, 20, :image, 3, 1
22
+ indices = [0, 1, 2]
23
+ interval = 1
24
+ 3.times { s.animate indices, interval }
25
+ assert_equal 0, s.img_index
26
+ 5.times { s.animate indices, interval }
27
+ assert_equal 2, s.img_index
28
+
29
+ 10.times { s.animate_once indices, interval }
30
+ assert_equal 2, s.img_index
31
+ s.animate_once indices, interval
32
+ assert_equal 2, s.img_index
33
+
34
+ indices2 = [0, 1, 2, 3]
35
+ s.animate_once indices2, 1
36
+ assert_equal 0, s.img_index
37
+ 10.times { s.animate_once indices2, interval }
38
+ assert_equal 3, s.img_index
39
+
40
+ s.set_animation 0
41
+ 2.times { s.animate_once indices2, 1 }
42
+ assert_equal 1, s.img_index
43
+ end
44
+
45
+ def test_sprite_visibility
46
+ # m = Map.new(1, 1, 800, 600)
47
+ s = Sprite.new 10, 20, :image, 3, 1 # the sprite will be 1 x 1 pixel
48
+ assert s.visible?
49
+ s.x = 800
50
+ assert(!(s.visible?))
51
+ s.x = -1
52
+ assert(!(s.visible?))
53
+ s.x = 0
54
+ assert s.visible?
55
+ s.y = 600
56
+ assert(!(s.visible?))
57
+ end
58
+ end
59
+
60
+ class GameObjectTest < Test::Unit::TestCase
61
+ def setup
62
+ @window = GameWindow.new 800, 600, false
63
+ Res.prefix = File.expand_path(File.dirname(__FILE__)) + '/data'
64
+ end
65
+
66
+ def test_game_object_attributes
67
+ o = GameObject.new 10, 20, 3, 1, :image
68
+ assert_equal 10, o.x
69
+ assert_equal 20, o.y
70
+ assert_equal 3, o.w
71
+ assert_equal 1, o.h
72
+ assert_equal 0, o.speed.x
73
+ assert_equal 0, o.speed.y
74
+ assert_equal 0, o.stored_forces.x
75
+ assert_equal 0, o.stored_forces.y
76
+ end
77
+
78
+ def test_game_object_animation
79
+ o = GameObject.new 10, 20, 3, 1, :image, nil, 3, 1
80
+ indices = [0, 1, 2]
81
+ interval = 10
82
+ 5.times { o.animate indices, interval }
83
+ assert_equal 0, o.img_index
84
+ o.set_animation 0
85
+ 5.times { o.animate indices, interval }
86
+ assert_equal 0, o.img_index
87
+ 5.times { o.animate indices, interval }
88
+ assert_equal 1, o.img_index
89
+ end
90
+
91
+ def test_game_object_visibility
92
+ # m = Map.new(1, 1, 800, 600)
93
+ o = GameObject.new 10, 20, 30, 30, :square, Vector.new(-10, -10)
94
+ assert o.visible?
95
+ o.x = 800
96
+ assert o.visible?
97
+ o.x = 810
98
+ assert(!(o.visible?))
99
+ o.x = -30
100
+ assert o.visible?
101
+ o.y = -50
102
+ assert(!(o.visible?))
103
+ o.x = 0; o.y = -30
104
+ assert o.visible?
105
+ o.y = 610
106
+ assert(!(o.visible?))
107
+ end
108
+ end
@@ -1,39 +1,39 @@
1
- require_relative '../lib/minigl'
2
- include MiniGL
3
-
4
- class MyGame < GameWindow
5
- def initialize
6
- super 800, 600, false
7
-
8
- @tile1 = Res.img :tile2
9
- @tile2 = Res.img :tile2b
10
- @map = Map.new 25, 17, 200, 200, 800, 600, true
11
- @p = Vector.new -1, -1
12
- end
13
-
14
- def needs_cursor?
15
- true
16
- end
17
-
18
- def update
19
- KB.update
20
- Mouse.update
21
- p = @map.get_map_pos Mouse.x, Mouse.y
22
- @p = p if @map.is_in_map p
23
-
24
- @map.move_camera 0, -4.5 if KB.key_down? Gosu::KbUp
25
- @map.move_camera 4.5, 0 if KB.key_down? Gosu::KbRight
26
- @map.move_camera 0, 4.5 if KB.key_down? Gosu::KbDown
27
- @map.move_camera -4.5, 0 if KB.key_down? Gosu::KbLeft
28
- @map.set_camera 0, 0 if KB.key_down? Gosu::KbReturn
29
- end
30
-
31
- def draw
32
- @map.foreach do |i, j, x, y|
33
- if (i - @p.x).abs <= 1 and (j - @p.y).abs <= 1; @tile2.draw x, y, 0
34
- else; @tile1.draw x, y, 0; end
35
- end
36
- end
37
- end
38
-
39
- MyGame.new.show
1
+ require_relative '../lib/minigl'
2
+ include MiniGL
3
+
4
+ class MyGame < GameWindow
5
+ def initialize
6
+ super 800, 600, false
7
+
8
+ @tile1 = Res.img :tile2
9
+ @tile2 = Res.img :tile2b
10
+ @map = Map.new 25, 17, 200, 200, 800, 600, true
11
+ @p = Vector.new -1, -1
12
+ end
13
+
14
+ def needs_cursor?
15
+ true
16
+ end
17
+
18
+ def update
19
+ KB.update
20
+ Mouse.update
21
+ p = @map.get_map_pos Mouse.x, Mouse.y
22
+ @p = p if @map.is_in_map p
23
+
24
+ @map.move_camera 0, -4.5 if KB.key_down? Gosu::KbUp
25
+ @map.move_camera 4.5, 0 if KB.key_down? Gosu::KbRight
26
+ @map.move_camera 0, 4.5 if KB.key_down? Gosu::KbDown
27
+ @map.move_camera -4.5, 0 if KB.key_down? Gosu::KbLeft
28
+ @map.set_camera 0, 0 if KB.key_down? Gosu::KbReturn
29
+ end
30
+
31
+ def draw
32
+ @map.foreach do |i, j, x, y|
33
+ if (i - @p.x).abs <= 1 and (j - @p.y).abs <= 1; @tile2.draw x, y, 0
34
+ else; @tile1.draw x, y, 0; end
35
+ end
36
+ end
37
+ end
38
+
39
+ MyGame.new.show
@@ -1,57 +1,57 @@
1
- require 'test/unit'
2
- require_relative '../lib/minigl'
3
- include MiniGL
4
-
5
- class MapTest < Test::Unit::TestCase
6
- def test_map_attributes
7
- m = Map.new 16, 16, 300, 200
8
- assert_equal 16, m.tile_size.x
9
- assert_equal 16, m.tile_size.y
10
- assert_equal 300, m.size.x
11
- assert_equal 200, m.size.y
12
- assert_equal 0, m.cam.x
13
- assert_equal 0, m.cam.y
14
- m = Map.new 25, 17, 49, 133
15
- assert_equal 25, m.tile_size.x
16
- assert_equal 17, m.tile_size.y
17
- assert_equal 49, m.size.x
18
- assert_equal 133, m.size.y
19
- assert_equal 0, m.cam.x
20
- assert_equal 0, m.cam.y
21
- end
22
-
23
- def test_absolute_size
24
- m = Map.new 16, 16, 300, 200
25
- v = m.get_absolute_size
26
- assert_equal 300 * 16, v.x
27
- assert_equal 200 * 16, v.y
28
- end
29
-
30
- def test_center
31
- m = Map.new 16, 16, 300, 200
32
- v = m.get_center
33
- assert_equal 300 * 16 / 2, v.x
34
- assert_equal 200 * 16 / 2, v.y
35
- end
36
-
37
- def test_screen_pos
38
- m = Map.new 16, 16, 300, 200
39
- v = m.get_screen_pos 8, 5
40
- assert_equal 8 * 16, v.x
41
- assert_equal 5 * 16, v.y
42
- end
43
-
44
- def test_map_pos
45
- m = Map.new 16, 16, 300, 200
46
- v = m.get_map_pos 410, 300
47
- assert_equal 25, v.x
48
- assert_equal 18, v.y
49
- end
50
-
51
- def test_in_map
52
- m = Map.new 16, 16, 300, 200
53
- assert m.is_in_map(Vector.new 30, 20)
54
- assert m.is_in_map(Vector.new 299, 199)
55
- assert !m.is_in_map(Vector.new 300, 200)
56
- end
57
- end
1
+ require 'test/unit'
2
+ require_relative '../lib/minigl'
3
+ include MiniGL
4
+
5
+ class MapTest < Test::Unit::TestCase
6
+ def test_map_attributes
7
+ m = Map.new 16, 16, 300, 200
8
+ assert_equal 16, m.tile_size.x
9
+ assert_equal 16, m.tile_size.y
10
+ assert_equal 300, m.size.x
11
+ assert_equal 200, m.size.y
12
+ assert_equal 0, m.cam.x
13
+ assert_equal 0, m.cam.y
14
+ m = Map.new 25, 17, 49, 133
15
+ assert_equal 25, m.tile_size.x
16
+ assert_equal 17, m.tile_size.y
17
+ assert_equal 49, m.size.x
18
+ assert_equal 133, m.size.y
19
+ assert_equal 0, m.cam.x
20
+ assert_equal 0, m.cam.y
21
+ end
22
+
23
+ def test_absolute_size
24
+ m = Map.new 16, 16, 300, 200
25
+ v = m.get_absolute_size
26
+ assert_equal 300 * 16, v.x
27
+ assert_equal 200 * 16, v.y
28
+ end
29
+
30
+ def test_center
31
+ m = Map.new 16, 16, 300, 200
32
+ v = m.get_center
33
+ assert_equal 300 * 16 / 2, v.x
34
+ assert_equal 200 * 16 / 2, v.y
35
+ end
36
+
37
+ def test_screen_pos
38
+ m = Map.new 16, 16, 300, 200
39
+ v = m.get_screen_pos 8, 5
40
+ assert_equal 8 * 16, v.x
41
+ assert_equal 5 * 16, v.y
42
+ end
43
+
44
+ def test_map_pos
45
+ m = Map.new 16, 16, 300, 200
46
+ v = m.get_map_pos 410, 300
47
+ assert_equal 25, v.x
48
+ assert_equal 18, v.y
49
+ end
50
+
51
+ def test_in_map
52
+ m = Map.new 16, 16, 300, 200
53
+ assert m.is_in_map(Vector.new 30, 20)
54
+ assert m.is_in_map(Vector.new 299, 199)
55
+ assert !m.is_in_map(Vector.new 300, 200)
56
+ end
57
+ end
@@ -1,77 +1,77 @@
1
- require 'gosu'
2
- require_relative '../lib/minigl'
3
- include MiniGL
4
-
5
- class MyGame < GameWindow
6
- def initialize
7
- super 800, 600, false
8
-
9
- @obj = GameObject.new(0, 0, 50, 50, :square)
10
- @obj2 = GameObject.new(100, 0, 50, 50, :square2)
11
-
12
- @obsts = [
13
- Block.new(0, 600, 800, 1, false),
14
- Block.new(-1, 0, 1, 600, false),
15
- Block.new(800, 0, 1, 600, false),
16
- Block.new(300, 430, 50, 50),
17
- # Block.new(375, 550, 50, 50, true),
18
- # Block.new(150, 200, 20, 300, false),
19
- # Block.new(220, 300, 100, 20, true),
20
- # Block.new(485, 490, 127, 10, false),
21
- ]
22
- @ramps = [
23
- Ramp.new(200, 550, 200, 50, true),
24
- Ramp.new(0, 200, 150, 300, false),
25
- Ramp.new(150, 500, 150, 100, false),
26
- Ramp.new(500, 500, 150, 100, true),
27
- Ramp.new(650, 300, 150, 200, true),
28
- Ramp.new(650, 500, 150, 100, true),
29
- ]
30
-
31
- # @cycle = [Vector.new(100, 530), Vector.new(650, 500)]
32
- # @cyc_obj = GameObject.new(@cycle[0].x, @cycle[0].y, 50, 50, :square)
33
- # @cyc_obj.instance_eval('@passable = true')
34
- # @obsts.push @cyc_obj
35
- end
36
-
37
- def update
38
- KB.update
39
-
40
- forces = Vector.new(0, 0)
41
- if @obj.bottom
42
- forces.y -= 15 if KB.key_pressed?(Gosu::KbSpace)
43
- forces.x -= 0.5 if KB.key_down?(Gosu::KbLeft)
44
- forces.x += 0.5 if KB.key_down?(Gosu::KbRight)
45
- forces.x -= @obj.speed.x * 0.1
46
- else
47
- forces.x -= 0.2 if KB.key_down?(Gosu::KbLeft)
48
- forces.x += 0.2 if KB.key_down?(Gosu::KbRight)
49
- end
50
- @obj.move(forces, @obsts, @ramps)
51
-
52
- speed = Vector.new(0, 0)
53
- speed.y -= 3 if KB.key_down? Gosu::KbW
54
- speed.y += 3 if KB.key_down? Gosu::KbS
55
- speed.x -= 3 if KB.key_down? Gosu::KbA
56
- speed.x += 3 if KB.key_down? Gosu::KbD
57
- @obj2.move(speed, @obsts, @ramps, true)
58
- end
59
-
60
- def draw
61
- @obj.draw
62
- @obj2.draw
63
- @obsts.each do |o|
64
- draw_quad o.x, o.y, 0xffffffff,
65
- o.x + o.w, o.y, 0xffffffff,
66
- o.x + o.w, o.y + o.h, 0xffffffff,
67
- o.x, o.y + o.h, 0xffffffff, 0
68
- end
69
- @ramps.each do |r|
70
- draw_triangle r.left ? r.x + r.w : r.x, r.y, 0xffffffff,
71
- r.x + r.w, r.y + r.h, 0xffffffff,
72
- r.x, r.y + r.h, 0xffffffff, 0
73
- end
74
- end
75
- end
76
-
1
+ require 'gosu'
2
+ require_relative '../lib/minigl'
3
+ include MiniGL
4
+
5
+ class MyGame < GameWindow
6
+ def initialize
7
+ super 800, 600, false
8
+
9
+ @obj = GameObject.new(0, 0, 50, 50, :square)
10
+ @obj2 = GameObject.new(100, 0, 50, 50, :square2)
11
+
12
+ @obsts = [
13
+ Block.new(0, 600, 800, 1, false),
14
+ Block.new(-1, 0, 1, 600, false),
15
+ Block.new(800, 0, 1, 600, false),
16
+ Block.new(300, 430, 50, 50),
17
+ # Block.new(375, 550, 50, 50, true),
18
+ # Block.new(150, 200, 20, 300, false),
19
+ # Block.new(220, 300, 100, 20, true),
20
+ # Block.new(485, 490, 127, 10, false),
21
+ ]
22
+ @ramps = [
23
+ Ramp.new(200, 550, 200, 50, true),
24
+ Ramp.new(0, 200, 150, 300, false),
25
+ Ramp.new(150, 500, 150, 100, false),
26
+ Ramp.new(500, 500, 150, 100, true),
27
+ Ramp.new(650, 300, 150, 200, true),
28
+ Ramp.new(650, 500, 150, 100, true),
29
+ ]
30
+
31
+ # @cycle = [Vector.new(100, 530), Vector.new(650, 500)]
32
+ # @cyc_obj = GameObject.new(@cycle[0].x, @cycle[0].y, 50, 50, :square)
33
+ # @cyc_obj.instance_eval('@passable = true')
34
+ # @obsts.push @cyc_obj
35
+ end
36
+
37
+ def update
38
+ KB.update
39
+
40
+ forces = Vector.new(0, 0)
41
+ if @obj.bottom
42
+ forces.y -= 15 if KB.key_pressed?(Gosu::KbSpace)
43
+ forces.x -= 0.5 if KB.key_down?(Gosu::KbLeft)
44
+ forces.x += 0.5 if KB.key_down?(Gosu::KbRight)
45
+ forces.x -= @obj.speed.x * 0.1
46
+ else
47
+ forces.x -= 0.2 if KB.key_down?(Gosu::KbLeft)
48
+ forces.x += 0.2 if KB.key_down?(Gosu::KbRight)
49
+ end
50
+ @obj.move(forces, @obsts, @ramps)
51
+
52
+ speed = Vector.new(0, 0)
53
+ speed.y -= 3 if KB.key_down? Gosu::KbW
54
+ speed.y += 3 if KB.key_down? Gosu::KbS
55
+ speed.x -= 3 if KB.key_down? Gosu::KbA
56
+ speed.x += 3 if KB.key_down? Gosu::KbD
57
+ @obj2.move(speed, @obsts, @ramps, true)
58
+ end
59
+
60
+ def draw
61
+ @obj.draw
62
+ @obj2.draw
63
+ @obsts.each do |o|
64
+ draw_quad o.x, o.y, 0xffffffff,
65
+ o.x + o.w, o.y, 0xffffffff,
66
+ o.x + o.w, o.y + o.h, 0xffffffff,
67
+ o.x, o.y + o.h, 0xffffffff, 0
68
+ end
69
+ @ramps.each do |r|
70
+ draw_triangle r.left ? r.x + r.w : r.x, r.y, 0xffffffff,
71
+ r.x + r.w, r.y + r.h, 0xffffffff,
72
+ r.x, r.y + r.h, 0xffffffff, 0
73
+ end
74
+ end
75
+ end
76
+
77
77
  MyGame.new.show