graphics 1.0.0b1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ require "graphics"
4
+
5
+ class MathSimulation < Graphics::Simulation
6
+ def initialize
7
+ super 640, 640, 16, "Math"
8
+ end
9
+
10
+ def draw n
11
+ graph_paper
12
+
13
+ (0..w).each do |x|
14
+ y = 2*x*x + 2*x - 12
15
+ y /= 320.0
16
+
17
+ next if y < 0
18
+ next if y > h
19
+
20
+ rect x, y, 3, 3, :red, :filled
21
+ end
22
+
23
+ text "2x^2 +2x -12", 10, h-font.height, :black
24
+ end
25
+
26
+ def graph_paper
27
+ clear :white
28
+
29
+ hline 1, :black
30
+ vline 0, :black
31
+
32
+ (0..w).step(25).each do |x|
33
+ (0..h).step(25).each do |y|
34
+ line 0, y, 5, y, :black if x == 0
35
+ line x, 1, x, 6, :black if y == 0
36
+ point x, h-y, :black
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ MathSimulation.new.run
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ require "graphics"
4
+
5
+ class TargetSimulation < Graphics::Simulation
6
+ def initialize
7
+ super 640, 640, 16, "Target Practice"
8
+
9
+ register_color :darker_green, 0, 16, 0
10
+ register_color :dark_green, 64, 96, 64
11
+ register_color :dark_blue, 0, 0, 96
12
+ end
13
+
14
+ def draw n
15
+ clear :darker_green
16
+
17
+ (0..640).step(64).each do |r|
18
+ hline r, :dark_green
19
+ vline r, :dark_green
20
+ circle 320, 320, r, :dark_green unless r > 320
21
+ end
22
+
23
+ x, y, * = mouse
24
+ line x, 0, x, 640, :white
25
+ line 0, y, 640, y, :white
26
+
27
+ fps n
28
+ end
29
+ end
30
+
31
+ TargetSimulation.new.run
@@ -0,0 +1,160 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ require "graphics"
4
+
5
+ D2R = Math::PI / 180.0
6
+
7
+ class Tank < Graphics::Body
8
+ ACCELERATE = 0.25
9
+ DECELERATE = 0.125
10
+ ROTATION = 2
11
+ TICK_ENERGY = 5
12
+ SHOT_ENERGY = 100
13
+
14
+ BULLET_SPEED = 9.0
15
+ MAX_SPEED = 4.0
16
+ MAX_ROTATION = 360
17
+ MAX_ENERGY = 100
18
+
19
+ attr_accessor :t
20
+ attr_accessor :e
21
+
22
+ def initialize w
23
+ super
24
+
25
+ self.x = w.w / 2
26
+ self.y = w.h / 2
27
+ self.e = 0
28
+
29
+ self.t = Turret.new self
30
+ end
31
+
32
+ def update
33
+ self.e += TICK_ENERGY
34
+
35
+ t.update x, y
36
+ move
37
+ limit
38
+ clip
39
+ end
40
+
41
+ def limit
42
+ self.e = MAX_ENERGY if e > MAX_ENERGY
43
+
44
+ if m > MAX_SPEED then
45
+ self.m = MAX_SPEED
46
+ elsif m < 0 then
47
+ self.m = 0
48
+ end
49
+ end
50
+
51
+ def fire
52
+ if e >= SHOT_ENERGY then
53
+ self.e -= SHOT_ENERGY
54
+ t.fire
55
+ end
56
+ end
57
+
58
+ def draw
59
+ w.blit w.body_img, x, y, a
60
+ t.draw
61
+ end
62
+
63
+ def turn_right; turn(-ROTATION); aim_right; end
64
+ def turn_left; turn ROTATION; aim_left; end
65
+
66
+ def aim_right; self.t.turn(-ROTATION); end
67
+ def aim_left; self.t.turn ROTATION; end
68
+
69
+ def accelerate; self.m += ACCELERATE; self.t.m = m; end
70
+ def decelerate; self.m -= DECELERATE; self.t.m = m; end
71
+ end
72
+
73
+ class Turret < Graphics::Body
74
+ def initialize tank
75
+ self.w = tank.w
76
+ self.x = tank.x
77
+ self.y = tank.y
78
+ self.a = tank.a
79
+ self.m = tank.m
80
+ end
81
+
82
+ def fire
83
+ b = Bullet.new w, x, y, a, m
84
+ b.move_by a, 15
85
+ end
86
+
87
+ def update x, y
88
+ self.x = x
89
+ self.y = y
90
+ end
91
+
92
+ def draw
93
+ w.blit w.turret_img, x, y, a
94
+ end
95
+ end
96
+
97
+ class Bullet < Graphics::Body
98
+ def initialize w, x, y, a, m
99
+ self.w = w
100
+ self.x = x
101
+ self.y = y
102
+ self.a = a
103
+ self.m = m + 5
104
+ w.bullets << self
105
+ end
106
+
107
+ def update
108
+ move
109
+ w.bullets.delete self if clip
110
+ end
111
+
112
+ def draw
113
+ w.rect x, y, 2, 2, :white
114
+ w.debug "%.2f", m
115
+ end
116
+ end
117
+
118
+ class TargetSimulation < Graphics::Simulation
119
+ attr_accessor :tank, :bullets
120
+ attr_accessor :body_img
121
+ attr_accessor :turret_img
122
+
123
+ def initialize
124
+ super 640, 640, 16, "Target Practice"
125
+
126
+ SDL::Key.enable_key_repeat 50, 10
127
+
128
+ self.tank = Tank.new self
129
+ self.bullets = []
130
+
131
+ self.body_img = image "resources/images/body.png"
132
+ self.turret_img = image "resources/images/turret.png"
133
+ end
134
+
135
+ def handle_keys
136
+ exit if SDL::Key.press? SDL::Key::ESCAPE
137
+ tank.turn_right if SDL::Key.press? SDL::Key::RIGHT
138
+ tank.turn_left if SDL::Key.press? SDL::Key::LEFT
139
+ tank.accelerate if SDL::Key.press? SDL::Key::UP
140
+ tank.decelerate if SDL::Key.press? SDL::Key::DOWN
141
+ tank.aim_left if SDL::Key.press? SDL::Key::SEMICOLON
142
+ tank.aim_right if SDL::Key.press? SDL::Key::Q
143
+ tank.fire if SDL::Key.press? SDL::Key::SPACE
144
+ end
145
+
146
+ def update n
147
+ tank.update
148
+
149
+ bullets.each(&:update)
150
+ end
151
+
152
+ def draw n
153
+ clear
154
+ tank.draw
155
+ bullets.each(&:draw)
156
+ fps n
157
+ end
158
+ end
159
+
160
+ TargetSimulation.new.run
@@ -0,0 +1,173 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ require "graphics"
4
+
5
+ D2R = Math::PI / 180.0
6
+
7
+ class Tank
8
+ ACCELERATE = 0.25
9
+ DECELERATE = 0.125
10
+ ROTATION = 2
11
+ TICK_ENERGY = 5
12
+ SHOT_ENERGY = 100
13
+
14
+ MAX_SPEED = 4.0
15
+ MAX_ROTATION = 360
16
+ MAX_ENERGY = 100
17
+
18
+ attr_accessor :angle, :speed, :x, :y, :sim
19
+ attr_accessor :turret, :energy
20
+
21
+ def initialize x, y
22
+ self.x = x
23
+ self.y = y
24
+ self.angle = 0
25
+ self.turret = 0
26
+ self.speed = 0
27
+ self.energy = 0
28
+ end
29
+
30
+ def update
31
+ rad = angle * D2R
32
+
33
+ self.x += Math.cos(rad) * speed
34
+ self.y += Math.sin(rad) * speed
35
+
36
+ self.energy += TICK_ENERGY
37
+
38
+ limit
39
+ end
40
+
41
+ def limit
42
+ self.angle %= MAX_ROTATION
43
+ self.turret %= MAX_ROTATION
44
+ self.energy = MAX_ENERGY if energy > MAX_ENERGY
45
+
46
+ if speed > MAX_SPEED then
47
+ self.speed = MAX_SPEED
48
+ elsif speed < 0 then
49
+ self.speed = 0
50
+ end
51
+ end
52
+
53
+ def fire
54
+ rad = turret * D2R
55
+ x2 = x + Math.cos(rad) * 20
56
+ y2 = y + Math.sin(rad) * 20
57
+
58
+ if energy >= SHOT_ENERGY then
59
+ self.energy -= SHOT_ENERGY
60
+ Bullet.new x2, y2, turret, speed
61
+ end
62
+ end
63
+
64
+ def turn_right; self.angle -= ROTATION; aim_right; end
65
+ def turn_left; self.angle += ROTATION; aim_left; end
66
+
67
+ def aim_right; self.turret -= ROTATION; end
68
+ def aim_left; self.turret += ROTATION; end
69
+
70
+ def accelerate; self.speed += ACCELERATE; end
71
+ def decelerate; self.speed -= DECELERATE; end
72
+ end
73
+
74
+ class Bullet
75
+ attr_accessor :x, :y, :a, :v
76
+
77
+ def initialize x, y, a, v
78
+ self.x = x
79
+ self.y = y
80
+ self.a = a
81
+ self.v = v + 5
82
+ end
83
+
84
+ def update
85
+ rad = a * D2R
86
+
87
+ self.x += Math.cos(rad) * v
88
+ self.y += Math.sin(rad) * v
89
+ end
90
+ end
91
+
92
+ class TargetSimulation < Graphics::Simulation
93
+ attr_accessor :tank, :bullets
94
+ attr_accessor :body_img
95
+ attr_accessor :turret_img
96
+
97
+ def initialize
98
+ super 640, 640, 16, "Target Practice"
99
+
100
+ SDL::Key.enable_key_repeat 50, 10
101
+
102
+ self.tank = Tank.new w/2, h/2
103
+ self.bullets = []
104
+
105
+ screen.set_alpha SDL::SRCALPHA, 128
106
+
107
+ self.body_img = sprite 40, 30 do
108
+ rect 0, 0, 39, 29, :white
109
+ rect 0, 4, 39, 21, :white
110
+
111
+ line 0, 2, 39, 2, :white
112
+ line 0, 27, 39, 27, :white
113
+ end
114
+
115
+ a, b = 41, 16
116
+ self.turret_img = sprite a, b do
117
+ rect a/2-8, b/2-8, 15, 15, :white
118
+ angle a/2, b/2, 0, 28, :white
119
+ line a/2+20, b/2-2, a/2+20, b/2+2, :white
120
+ end
121
+ end
122
+
123
+ def handle_keys
124
+ exit if SDL::Key.press? SDL::Key::ESCAPE
125
+ tank.turn_right if SDL::Key.press? SDL::Key::RIGHT
126
+ tank.turn_left if SDL::Key.press? SDL::Key::LEFT
127
+ tank.accelerate if SDL::Key.press? SDL::Key::UP
128
+ tank.decelerate if SDL::Key.press? SDL::Key::DOWN
129
+ tank.aim_left if SDL::Key.press? SDL::Key::SEMICOLON
130
+ tank.aim_right if SDL::Key.press? SDL::Key::Q
131
+ fire if SDL::Key.press? SDL::Key::SPACE
132
+ end
133
+
134
+ def fire
135
+ bullet = tank.fire
136
+ bullets << bullet if bullet
137
+ end
138
+
139
+ def update n
140
+ tank.update
141
+
142
+ bullets.each(&:update)
143
+
144
+ if tank.x < 0 then tank.x = 0 elsif tank.x > w then tank.x = w end
145
+ if tank.y < 0 then tank.y = 0 elsif tank.y > h then tank.y = h end
146
+ end
147
+
148
+ def draw n
149
+ clear
150
+ draw_tank
151
+ draw_bullets
152
+ fps n
153
+ end
154
+
155
+ AA = SDL::Surface::TRANSFORM_AA
156
+
157
+ def draw_tank
158
+ x, y, a, t = tank.x, tank.y, tank.angle, tank.turret
159
+
160
+ blit body_img, x, y, a, AA
161
+ blit turret_img, x, y, t, AA
162
+
163
+ debug "%3d @ %.2f @ %d", tank.angle, tank.speed, tank.energy
164
+ end
165
+
166
+ def draw_bullets
167
+ bullets.each do |b|
168
+ rect b.x, b.y, 2, 2, :white
169
+ end
170
+ end
171
+ end
172
+
173
+ TargetSimulation.new.run
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ require "graphics"
4
+
5
+ class TargetSimulation < Graphics::Simulation
6
+ attr_accessor :bombs
7
+
8
+ def initialize
9
+ super 640, 640, 16, "Target Practice"
10
+
11
+ self.bombs = []
12
+ register_color :darker_green, 0, 16, 0
13
+ register_color :dark_green, 64, 96, 64
14
+ register_color :dark_blue, 0, 0, 96
15
+ end
16
+
17
+ def handle_event event, n
18
+ bombs << [n, event.x, h-event.y] if SDL::Event::MouseButtonDown === event
19
+ super
20
+ end
21
+
22
+ def draw n
23
+ clear :darker_green
24
+
25
+ bombs.each do |(birth, bx, by)|
26
+ r = n - birth
27
+ r = [r, 100].min
28
+ circle bx, by, r, :dark_blue, :fill
29
+ circle bx, by, r, :red unless r == 100
30
+ end
31
+
32
+ (0..640).step(64).each do |r|
33
+ hline r, :dark_green
34
+ vline r, :dark_green
35
+ circle 320, 320, r, :dark_green unless r > 320
36
+ end
37
+
38
+ x, y, * = mouse
39
+ line x, 0, x, 640, :white
40
+ line 0, y, 640, y, :white
41
+
42
+ fps n
43
+ end
44
+ end
45
+
46
+ TargetSimulation.new.run