-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
161 lines (151 loc) · 4.66 KB
/
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
require 'gosu'
require_relative 'menu.rb'
require_relative 'player.rb'
require_relative 'ball.rb'
class Game < Gosu::Window
WIDTH = 640
HEIGHT = 480
Ran = Random.new
def initialize
super WIDTH, HEIGHT, false
self.caption = "Pong Game v 1.0"
@ball = Ball.new(self)
@player_1 = Player.new(self, "./images/player_1.png")
@player_2 = Player.new(self, "./images/player_2.png")
@player_2.y = 10
@font = Gosu::Font.new(self, 'Arial', 20)
@menu = Menu.new(self)
@menu.add_item(Gosu::Image.from_text(self,"Start Game", 'Arial', 20), WIDTH/2 - 198/2, (HEIGHT - 39)/2, 1, lambda{@show_game = true})
@menu.add_item(Gosu::Image.from_text(self,"Exit", 'Arial', 20), WIDTH/2 - 197/2, (HEIGHT - 39 +198/2)/2, 1, lambda {self.close})
@cursor = Gosu::Image.new(self, "./images/cursor.png")
end
def button_down (id)
if id == Gosu::MsLeft
@menu.clicked
end
end
def draw_border
end
def hits?(p , b)
if -(b.y - p.y) <= b.image.height
if b.x <= p.x
if -(b.x - p.x)< b.image.width
return true
end
else
if (b.x - p.x) < p.image.width
return true
else
return false
end
end
end
end
def hits2?(p , b)
if (b.y - p.y) <= p.image.height
if b.x <= p.x
if -(b.x - p.x)< b.image.width
return true
return false
end
else
if (b.x - p.x) < p.image.width
return true
return false
end
end
end
return false
end
def move_player(player, r, l)
if button_down?(r)
player.right
elsif button_down?(l)
player.left
end
end
def move_ball
@ball.x += @ball.vx
@ball.y += @ball.vy
end
def wins
if @player_1.score == 10
return 1
elsif @player_2.score == 10
return 2
end
return
end
def check_for_collision
if hits? @player_1, @ball
@ball.vx = 5 if button_down?(Gosu::KbRight)
@ball.vy *= -1
elsif hits2? @player_2, @ball
if button_down?(Gosu::KbD)
@ball.vx = 5
end
@ball.vy *= -1
else
if @ball.x >= WIDTH - @ball.image.width
@ball.vx = -5
elsif @ball.x <=0
@ball.vx = 5
end
## Game Over ##
if @ball.y + @ball.image.width > 480
@ball.x = ( WIDTH - @ball.image.width - -Ran.rand(40..80))/2
@ball.y = ( HEIGHT - @ball.image.height )/2
@ball.vx = %w{-2 2}.sample.to_i
@ball.vy = -2
@player_2.score +=1
elsif @ball.y < -10
@ball.x = (WIDTH - @ball.image.width - Ran.rand(40..80))/2
@ball.y = (HEIGHT - @ball.image.height)/2
@ball.vx = %w{-2 2}.sample.to_i
@ball.vy = 2
@player_1.score +=1
end
end
end
def update
if @show_game != true
@menu.update
else
if !wins
check_for_collision
move_ball
move_player(@player_1, Gosu::KbRight, Gosu::KbLeft)
move_player(@player_2, Gosu::KbD, Gosu::KbA)
else
@menu2.update
end
end
end
=begin
For testing purposes
if @player_1.x >= 500
@player_1.vx = -5
elsif @player_1.x <= 0
@player_1.vx = 5
end
@player_1.x += @player_1.vx
=end
def draw
if @show_game != true
@menu.draw
@cursor.draw self.mouse_x, self.mouse_y, 0
else
if wins
@font.draw("Game over", WIDTH/4+ 120/2, HEIGHT/2 - 120/2, 0, 2, 5,Gosu::Color::RED)
@font.draw("Player #{wins} wins", WIDTH/4 +100, HEIGHT/2 +20, 0,1,2)
else
@font.draw("Player 1: #{@player_1.score}", 10, 10, 2,1,1, Gosu::Color::RED) #,1,1, Gosu::Color::WHITE)
@font.draw("Player 2: #{@player_2.score}", 10,25,2, 1, 1, Gosu::Color::GREEN) #,1,1, Gosu::Color::WHITE)
@ball.draw
@player_1.draw
@player_2.draw
end
end
end
end
Game.new.show