repl.it
Ruby

No description

fork
loading
Files
  • main.rb
  • CGL.rb
  • style-guide-for-ruby-noobs
main.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
require 'io/console'
require './CGL.rb'

###KEYPRESS STUFF
def readChar
	STDIN.echo = false
	STDIN.raw!
	input = STDIN.getc.chr
	if input == "\e"
		input << STDIN.read_nonblock(3) rescue nil
		input << STDIN.read_nonblock(2) rescue nil
	end
ensure
	STDIN.echo = true
	STDIN.cooked!
	return input
end

def clear
	puts "\e[2J\e[0;0H"
end

puts "\e[?25l" # hide cursor

@k = {
	left: "left",
	right: "right",
	up: "up",
	down: "down",
	enter: "enter",
	escape: "escape",
	delete: "delete",
	space: "space",
	tab: "tab"
}

def keypress key = nil
	c = readChar
	
	case c
		when " "
			if key == nil
				return @k[:space]
			else
				return @k[key] == @k[:space]
			end
		when "\t"
			if key == nil
				return @k[:tab]
			else
				return @k[key] == @k[:tab]
			end
		when "\r"
			if key == nil
				return @k[:enter]
			else
				return @k[key] == @k[:enter]
			end
		when "\e"
			if key == nil
				return @k[:escape]
			else
				return @k[key] == @k[:escape]
			end
		when "\e[A"
			if key == nil
				return @k[:up]
			else
				return @k[key] == @k[:up]
			end
		when "\e[B"
			if key == nil
				return @k[:down]
			else
				return @k[key] == @k[:down]
			end
		when "\e[C"
			if key == nil
				return @k[:right]
			else
				return @k[key] == @k[:right]
			end
		when "\e[D"
			if key == nil
				return @k[:left]
			else
				return @k[key] == @k[:left]
			end
		when "\177"
			if key == nil
				return @k[:delete]
			else
				return @k[key] == @k[:delete]
			end
		when "\u0003"
			exit 0
		when /^.$/
			if key == nil
				return c
			else
				return (@k[key] == c.inspect or key == c)
			end
		else
			if key == nil
				return c
			else
				return (@k[key] == c.inspect or key == c)
			end
	end
end



@main_matrix = Matrix.new 50, 40

@player_paddle = vline @main_matrix, [0, 200, 0], 5, 20, 10
@player_paddle.collect &:draw

@ball = CGPoint.new @main_matrix, 25, 20, [255, 255, 0]#[200, 200, 200]
@ball.draw

@ball_direction = {horiz: -1, vert: 1}

@computer_paddle = vline @main_matrix, [200, 0, 0], 45, 20, 10
@computer_paddle.collect &:draw

#top
(hline @main_matrix, [255, 255, 255], 1, 2, 94).collect &:draw

#bottom
(hline @main_matrix, [255, 255, 255], 1, 39, 94).collect &:draw

#left
(vline @main_matrix, [255, 255, 255], 1, 3, 67).collect &:draw

#right
(vline @main_matrix, [255, 255, 255], 49, 3, 67).collect &:draw

@frame_speed = 0.05
@key = ""
@player_score, @computer_score = 0, 0
@cpu_movement = 0

puts "\e[10;60HSpeed options: 1 - slow, 2 - normal, 3 - fast (kinda buggy)"

sleep 3



# in a separate thread so other things can occur while waiting for the next key event.
get_key_event = Thread.new do
	loop do
		@key = keypress
		next
	end
end

main_loop = Thread.new do
	loop do
		#player's paddle movement
		case @key
		when "w", "up"
			if @player_paddle[0].y >= 4
				@player_paddle.each do |pixel|
					pixel.move 0, -1
				end
			end
		when "s", "down"
			if @player_paddle[-1].y <= 37
				@player_paddle.each do |pixel|
					pixel.move 0, 1
				end
			end
		when "1" then @frame_speed = 0.1
		when "2" then @frame_speed = 0.05
		when "3" then @frame_speed = 0.03
		else end
		
		if @ball.x > 30 and ((@computer_paddle[0].y - 5)..(@computer_paddle[0].y + 3)).include? @ball.y
			@cpu_movement = 0
		elsif @ball.x > 30 and ((@computer_paddle[-1].y - 2)..(@computer_paddle[-1].y + 6)).include? @ball.y
			@cpu_movement = 1
		elsif @computer_paddle[0].y <= 12
			@cpu_movement = 1
		elsif @computer_paddle[-1].y >= 25
			@cpu_movement = 0
		else
			@cpu_movement = rand 2
		end
		
		#computer's paddle movement
		case @cpu_movement
		when 0
			if @computer_paddle[0].y >= 4
				@computer_paddle.each do |pixel|
					pixel.move 0, -1
				end
			end
		when 1
			if @computer_paddle[-1].y <= 37
				@computer_paddle.each do |pixel|
					pixel.move 0, 1
				end
			end
		else end
		
		#move ball
		@ball.move @ball_direction[:horiz], @ball_direction[:vert]
		
		##ball/wall collision
		
		#left wall
		if @ball.x <= 2
			@computer_score += 1
			
			@ball.move 25, [email protected]_direction[:vert]
			
			@ball_direction[:horiz] = 1
			@ball_direction[:vert]  = [email protected]_direction[:vert] #works like a flip-flop toggle
			sleep 2
		end
		
		#right wall
		if @ball.x >= 48
			@player_score += 1
			
			@ball.move -25, [email protected]_direction[:vert]
			
			@ball_direction[:horiz] = -1
			@ball_direction[:vert]  = [email protected]_direction[:vert]
			sleep 2
		end
		
		#top and bottom walls
		if @ball.y < 3 or @ball.y > 38
			@ball_direction[:vert] = [email protected]_direction[:vert]
			@ball.move 0, @ball_direction[:vert]
		end
		
		
		
		##ball/player's-paddle collision
		if (@ball.x == 4 or @ball.x == 6) and @ball.y >= @player_paddle[0].y and @ball.y <= @player_paddle[-1].y
			@ball_direction[:horiz] = [email protected]_direction[:horiz]
		end
		
		if @ball.x == 5 and @ball.y >= @player_paddle[0].y and @ball.y <= @player_paddle[-1].y
			@ball_direction[:vert] = [email protected]_direction[:vert]
			@ball.move 2, 0
		end
		
		##ball/computer's-paddle collision
		if (@ball.x == 44 or @ball.x == 46) and @ball.y >= @computer_paddle[0].y and @ball.y <= @computer_paddle[-1].y
			@ball_direction[:horiz] = [email protected]_direction[:horiz]
		end	
		
		if @ball.x == 45 and @ball.y >= @computer_paddle[0].y and @ball.y <= @computer_paddle[-1].y
			@ball_direction[:vert] = [email protected]_direction[:vert]
			@ball.move -2, 0
		end
		
		
		
		puts "\e[30;60HPlayer: #{@player_score} Computer: #{@computer_score}\e[0;0H"
		
		break if @player_score == 11 or @computer_score == 11
		
		sleep @frame_speed
	end
	
	if @player_score == 11
		puts "\e[10;10H\e[5m\e[38;2;0;200;0mPlayer wins!\s[0m"
	elsif @computer_score == 11
		puts "\e[10;10H\e[5m\e[38;2;200;0;0mComputer wins!\s[0m"
	end
end



get_key_event.join
main_loop.join

nil
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]
?