/
game.rb
141 lines (117 loc) · 3.58 KB
/
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
# Ultimate Pong v1 - by Andy Wong
require 'rubygems'
require 'gosu'
require 'ashton'
require_relative 'lib/keys'
require_relative 'lib/bounding_box'
require_relative 'lib/ball'
require_relative 'lib/ee_ball'
require_relative 'lib/paddle'
require_relative 'lib/scoreboard'
class GameWindow < Gosu::Window
include Keys
def initialize
super 800, 600, false
self.caption = "Ultimate Pong"
@toggle_ai = false
@toggle_demo = false
@toggle_ee = false
@theme_song = Gosu::Song.new(self, "snd/soundtrack.mp3")
@theme_song.play(true)
@collide_sound = Gosu::Sample.new(self, "snd/collide.mp3")
summon_ball_type(self, 40, 290)
@paddle_left = Paddle.new(self, 10, 250)
@paddle_right = Paddle.new(self, 770, 250)
@score = Scoreboard.new(self)
@ai_status = "AI OFF"
@ai_on = Gosu::Font.new(self, "helvetica", 20)
@ready_to_serve = Gosu::Font.new(self, "helvetica", 20)
@music_toggle = Gosu::Font.new(self, "helvetica", 20)
end
def update
@bouncing_ball.update
@paddle_left.update
@paddle_right.update
computer_ai
ball_collision
scoring
end
def scoring
if @bouncing_ball.x < -30
if @toggle_ee == true
summon_ball_type(self, 660, 290)
else
summon_ball_type(self, 730, 290)
end
@score.player_right += 1
reset_paddle_positions
end
if @bouncing_ball.right > 830
summon_ball_type(self, 40, 290)
@score.player_left += 1
reset_paddle_positions
end
end
def draw
@bouncing_ball.draw
@paddle_left.draw
@paddle_right.draw
@score.draw
if @toggle_ai == true
@ai_on.draw("#{@ai_status}", 370, 30, 0, 1.0, 1.0, 0xffffffff)
elsif @toggle_ai == false && @toggle_demo == true
@ai_on.draw("#{@ai_status}", 345, 30, 0, 1.0, 1.0, 0xffffffff)
end
if @theme_song.playing? == false
@music_toggle.draw("MUSIC OFF", 350, 570, 0, 1.0, 1.0, 0xffffffff)
end
if @bouncing_ball.in_play? == false
@ready_to_serve.draw("Press SPACE to serve!", 310, 300, 0, 1.0, 1.0, 0xffffffff)
end
end
def summon_ball_type(window, x, y)
if @toggle_ee == true
@bouncing_ball = EeBall.new(window, x, 250)
elsif @toggle_ee == false
@bouncing_ball = Ball.new(window, x, y)
end
end
def reset_paddle_positions
@paddle_right.x = 770
@paddle_right.y = 250
@paddle_left.x = 10
@paddle_left.y = 250
end
def ball_collision
if @paddle_left.collide?(@bouncing_ball.left, @bouncing_ball.bottom) == true || @paddle_left.collide?(@bouncing_ball.left, @bouncing_ball.top) == true
if @bouncing_ball.speed < 20
@bouncing_ball.speed += 0.5
end
@collide_sound.play(0.5,1,false)
@bouncing_ball.vx = +5 + @bouncing_ball.speed
@bouncing_ball.quotes
end
if @paddle_right.collide?(@bouncing_ball.right, @bouncing_ball.bottom) == true || @paddle_right.collide?(@bouncing_ball.right, @bouncing_ball.top) == true
if @bouncing_ball.speed < 20
@bouncing_ball.speed += 0.5
end
@collide_sound.play(0.5,1,false)
@bouncing_ball.vx = -5 + -(@bouncing_ball.speed)
@bouncing_ball.quotes
end
end
def computer_ai
if @toggle_ai == true
@ai_status = "AI ON"
@paddle_right.velocity = @bouncing_ball.vy * [0.85,0.85,0.6,0.85,0.6].sample
elsif @toggle_demo == true
@ai_status = "DEMO MODE"
@paddle_right.y = @bouncing_ball.y - 50 + @bouncing_ball.height / 2
@paddle_left.y = @bouncing_ball.y - 50 + @bouncing_ball.height / 2
else
@ai_status = "AI OFF"
end
end
end
window = GameWindow.new
window.show