-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathbreakout_wall_game.rb
217 lines (180 loc) · 4.81 KB
/
breakout_wall_game.rb
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# gem install gosu
require 'gosu'
WIDTH = 800
HEIGHT = 600
PADDLE_WIDTH = 100
PADDLE_HEIGHT = 15
BALL_RADIUS = 10
LIVES_FONT = Gosu::Font.new(40)
class Paddle
VEL = 5
attr_accessor :x, :y, :width, :height, :color
def initialize(x, y, width, height, color)
@x = x
@y = y
@width = width
@height = height
@color = color
end
def draw
Gosu.draw_rect(@x, @y, @width, @height, @color)
end
def move(direction = 1)
@x += VEL * direction
end
end
class Ball
VEL = 5
attr_accessor :x, :y, :radius, :color, :x_vel, :y_vel
def initialize(x, y, radius, color)
@x = x
@y = y
@radius = radius
@color = color
@x_vel = 0
@y_vel = -VEL
end
def move
@x += @x_vel
@y += @y_vel
end
def set_vel(x_vel, y_vel)
@x_vel = x_vel
@y_vel = y_vel
end
def draw
Gosu.draw_circle(@x, @y, @radius, 0, @color)
end
end
class Brick
attr_accessor :x, :y, :width, :height, :health, :max_health, :colors, :color
def initialize(x, y, width, height, health, colors)
@x = x
@y = y
@width = width
@height = height
@health = health
@max_health = health
@colors = colors
@color = colors[0]
end
def draw
Gosu.draw_rect(@x, @y, @width, @height, @color)
end
def collide(ball)
return false unless (ball.x <= @x + @width) && (ball.x >= @x)
return false unless (ball.y - ball.radius <= @y + @height)
hit
ball.set_vel(ball.x_vel, ball.y_vel * -1)
true
end
def hit
@health -= 1
@color = interpolate(*@colors, @health.to_f / @max_health)
end
def interpolate(color_a, color_b, t)
color_a.zip(color_b).map { |a, b| (a + (b - a) * t).to_i }
end
end
class GameWindow < Gosu::Window
def initialize
super(WIDTH, HEIGHT, false)
self.caption = "Sk. Salahuddin - Morning 01 Batch"
@font = Gosu::Font.new(40)
@paddle = Paddle.new(WIDTH / 2 - PADDLE_WIDTH / 2, HEIGHT - PADDLE_HEIGHT - 5, PADDLE_WIDTH, PADDLE_HEIGHT, Gosu::Color::WHITE)
@ball = Ball.new(WIDTH / 2, @paddle.y - BALL_RADIUS, BALL_RADIUS, Gosu::Color::RED)
@bricks = generate_bricks(3, 10)
@lives = 3
end
def generate_bricks(rows, cols)
gap = 2
brick_width = WIDTH / cols - gap
brick_height = 20
bricks = []
rows.times do |row|
cols.times do |col|
brick = Brick.new(col * brick_width + gap * col, row * brick_height + gap * row, brick_width, brick_height, 2, [[0, 255, 0], [255, 0, 0]])
bricks << brick
end
end
bricks
end
def reset
@paddle.x = WIDTH / 2 - PADDLE_WIDTH / 2
@paddle.y = HEIGHT - PADDLE_HEIGHT - 5
@ball.x = WIDTH / 2
@ball.y = @paddle.y - BALL_RADIUS
end
def draw
Gosu.draw_rect(0, 0, WIDTH, HEIGHT, Gosu::Color::BLACK)
@paddle.draw
@ball.draw
@bricks.each(&:draw)
@font.draw_text("Lives: #{@lives}", 10, HEIGHT - @font.height - 10, 0)
unless @game_over.nil?
@font.draw_text(@game_over, WIDTH / 2 - @font.text_width(@game_over) / 2, HEIGHT / 2 - @font.height / 2, 0)
end
end
def update
if @lives.zero?
@game_over = "You Lost!"
reset
@bricks = generate_bricks(3, 10)
sleep(3)
@lives = 3
@game_over = nil
elsif @bricks.empty?
@game_over = "You Won!"
reset
@bricks = generate_bricks(3, 10)
sleep(3)
@lives = 3
@game_over = nil
else
@ball.move
ball_collision
ball_paddle_collision
@bricks.each do |brick|
brick.collide(@ball)
@bricks.delete(brick) if brick.health <= 0
end
if @ball.y + BALL_RADIUS >= HEIGHT
@lives -= 1
@ball.x = @paddle.x + @paddle.width / 2
@ball.y = @paddle.y - BALL_RADIUS
@ball.set_vel(0, @ball.VEL * -1)
end
end
end
def ball_collision
if @ball.x - BALL_RADIUS <= 0 || @ball.x + BALL_RADIUS >= WIDTH
@ball.set_vel(@ball.x_vel * -1, @ball.y_vel)
end
if @ball.y + BALL_RADIUS >= HEIGHT || @ball.y - BALL_RADIUS <= 0
@ball.set_vel(@ball.x_vel, @ball.y_vel * -1)
end
end
def ball_paddle_collision
return unless @ball.x <= @paddle.x + @paddle.width && @ball.x >= @paddle.x
return unless @ball.y + @ball.radius >= @paddle.y
paddle_center = @paddle.x + @paddle.width / 2
distance_to_center = @ball.x - paddle_center
percent_width = distance_to_center.to_f / @paddle.width
angle = percent_width * 90
angle_radians = Gosu::degrees_to_radians(angle)
x_vel = Math.sin(angle_radians) * @ball.VEL
y_vel = Math.cos(angle_radians) * @ball.VEL * -1
@ball.set_vel(x_vel, y_vel)
end
def button_down(id)
case id
when Gosu::KB_LEFT
@paddle.move(-1) if @paddle.x - Paddle::VEL >= 0
when Gosu::KB_RIGHT
@paddle.move(1) if @paddle.x + @paddle.width + Paddle::VEL <= WIDTH
when Gosu::KB_ESCAPE
close
end
end
end
GameWindow.new.show