-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBall.vb
196 lines (128 loc) · 6.32 KB
/
Ball.vb
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
Public Class Ball
Public RefForm As frmMain
Public RefPaddle As Paddle
Public RefBoard As Board
Public Size As Size
Public Position As PointF
Public Velocity As PointF
Public MaxHorizontalDeviation As Single
Private Img As Image
'actual values (after computing with the resize ratio)
Public Function ActualSize() As Size
Dim rr As SizeF = Me.RefForm.ResizeRatio
Return New Size(Me.Size.Width * rr.Width, Me.Size.Height * rr.Height)
End Function
Public Function ActualPosition() As PointF
Dim rr As SizeF = Me.RefForm.ResizeRatio
Return New PointF(Me.Position.X * rr.Width, Me.Position.Y * rr.Height)
End Function
Public Function ActualVelocity() As PointF
Dim rr As SizeF = Me.RefForm.ResizeRatio
Return New PointF(Me.Velocity.X * rr.Width, Me.Velocity.Y * rr.Height)
End Function
Public Sub New(ByVal RefForm As frmMain, ByVal RefPaddle As Paddle, ByVal RefBoard As Board, ByVal Position As PointF, ByVal Velocity As PointF)
Me.RefForm = RefForm
Me.RefPaddle = RefPaddle
Me.RefBoard = RefBoard
Me.Position = Position
Me.Velocity = Velocity
Me.Img = Image.FromFile("meta\ball.png")
End Sub
Public Sub DrawSimple(ByVal g As Graphics)
Dim p As Pen = New Pen(Color.Black)
Dim pB As Pen = New Pen(Color.Gray)
Dim rect As Rectangle = New Rectangle(Me.ActualPosition.X - Me.ActualSize.Width / 2, Me.ActualPosition.Y - Me.ActualSize.Height / 2, Me.ActualSize.Width, Me.ActualSize.Height)
g.FillEllipse(p.Brush, rect)
g.DrawEllipse(pB, rect)
End Sub
Public Sub Draw(ByVal g As Graphics)
Dim rect As Rectangle = New Rectangle(Me.ActualPosition.X - Me.ActualSize.Width / 2, Me.ActualPosition.Y - Me.ActualSize.Height / 2, Me.ActualSize.Width, Me.ActualSize.Height)
g.DrawImage(Me.Img, rect)
End Sub
Public Sub NormalizeVelocity(ByVal Vel As Single)
Dim r As Single = Vel / Math.Sqrt(Me.Velocity.X ^ 2 + Me.Velocity.Y ^ 2)
Me.Velocity.X *= r
Me.Velocity.Y *= r
End Sub
Public Sub ProcessDynamics()
Me.Position.X += Me.Velocity.X
Me.Position.Y += Me.Velocity.Y
'collision with wall
If Me.Position.X + Me.Size.Width / 2 > Me.RefForm.BasicBoardWidth Then
Me.Velocity.X *= -1.0F
Me.Position.X = Me.RefForm.BasicBoardWidth - Me.Size.Width / 2
'sound
If Not Me.RefForm.muteChB.Checked Then SoundPlayer.sndPlaySound("meta\wall.wav", 1)
ElseIf Me.Position.X - Me.Size.Width / 2 < 0 Then
Me.Velocity.X *= -1.0F
Me.Position.X = Me.Size.Width / 2
'sound
If Not Me.RefForm.muteChB.Checked Then SoundPlayer.sndPlaySound("meta\wall.wav", 1)
End If
If Me.Position.Y - Me.Size.Height / 2 < 0 Then
Me.Velocity.Y *= -1.0F
Me.Position.Y = Me.Size.Height / 2
'sound
If Not Me.RefForm.muteChB.Checked Then SoundPlayer.sndPlaySound("meta\wall.wav", 1)
End If
'ball lost
If Me.Position.Y > Me.RefPaddle.Position.Y Then
' GAME OVER
Me.RefForm.timeT.Stop()
Me.RefForm.gameOverL.Visible = True
'sound
If Not Me.RefForm.muteChB.Checked Then SoundPlayer.sndPlaySound("meta\lose.wav", 1)
End If
'collision with paddle
If Me.Position.Y + Me.Size.Height / 2 > Me.RefPaddle.Position.Y - Me.RefPaddle.Size.Height / 2 Then
If (Me.Position.X + Me.Size.Width / 2 > Me.RefPaddle.Position.X - Me.RefPaddle.Size.Width / 2) And (Me.Position.X - Me.Size.Width / 2 < Me.RefPaddle.Position.X + Me.RefPaddle.Size.Width / 2) Then
Me.Position.Y = Me.RefPaddle.Position.Y - Me.RefPaddle.Size.Height / 2 - Me.Size.Height / 2
Me.Velocity.Y *= -1.0F
'aditional horizontal deviation for the ball
Me.Velocity.X += 2 * Me.MaxHorizontalDeviation * (Me.Position.X - Me.RefPaddle.Position.X) / Me.RefPaddle.Size.Width
Me.NormalizeVelocity(Me.RefForm.LevelVelocity(Me.RefForm.Level))
'sound
If Not Me.RefForm.muteChB.Checked Then SoundPlayer.sndPlaySound("meta\paddle.wav", 1)
End If
End If
'collision with bricks
'(optimization)
If Me.Position.Y - Me.Size.Height < Me.RefBoard.BrickHeight * Me.RefBoard.BrickNum.Height Then
Try
Dim p As Point
If Me.Velocity.Y < 0 Then
p = Me.RefBoard.BrickAtPoint(Me.Position.X, Me.Position.Y - Me.Size.Height / 2)
Else
p = Me.RefBoard.BrickAtPoint(Me.Position.X, Me.Position.Y + Me.Size.Height / 2)
End If
If (p.Y <= Me.RefBoard.BrickNum.Height) Then
If Not (Me.RefBoard.BrickGrid(p.X, p.Y) Is Nothing) Then
Me.Velocity.Y *= -1
Me.RefForm.Score += Me.RefBoard.BrickGrid(p.X, p.Y).Points
Me.RefBoard.BrickGrid(p.X, p.Y) = Nothing
'sound
If Not Me.RefForm.muteChB.Checked Then SoundPlayer.sndPlaySound("meta\brick.wav", 1)
Me.RefBoard.VerGameWon()
End If
End If
If Me.Velocity.X < 0 Then
p = Me.RefBoard.BrickAtPoint(Me.Position.X - Me.Size.Width, Me.Position.Y)
Else
p = Me.RefBoard.BrickAtPoint(Me.Position.X + Me.Size.Width, Me.Position.Y)
End If
If (p.Y <= Me.RefBoard.BrickNum.Height) Then
If Not (Me.RefBoard.BrickGrid(p.X, p.Y) Is Nothing) Then
Me.Velocity.X *= -1
Me.RefForm.Score += Me.RefBoard.BrickGrid(p.X, p.Y).Points
Me.RefBoard.BrickGrid(p.X, p.Y) = Nothing
'sound
If Not Me.RefForm.muteChB.Checked Then SoundPlayer.sndPlaySound("meta\brick.wav", 1)
Me.RefBoard.VerGameWon()
End If
End If
Catch ex As Exception
'if out of bounds exceptions are found
End Try
End If
End Sub
End Class