-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ball.cs
221 lines (212 loc) · 7.28 KB
/
Ball.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace AsteroidGame
{
class Ball
{
public float X { get; set; }
public float Y { get; set; }
public float XVelocity { get; set; }
public float YVelocity { get; set; }
public float Height { get; set; }
public float Width { get; set; }
public float Rotation { get; set; }
public bool UseRotation { get; set; }
public float ScreenWidth { get; set; } //width of game screen
public float ScreenHeight { get; set; } //height of game screen
public bool Visible { get; set; } //is ball visible on screen
public int Score { get; set; }
public int bricksCleared { get; set; } //number of bricks cleared this level
private Texture2D imgBall { get; set; }
private SpriteBatch spriteBatch; //allows us to write on backbuffer when we need to draw self
private GameContent gameContent;
public Ball(float screenWidth, float screenHeight, SpriteBatch spriteBatch, GameContent gameContent)
{
X = 0;
Y = 0;
XVelocity = 0;
YVelocity = 0;
Rotation = 0;
imgBall = gameContent.Ball;
Width = imgBall.Width;
Height = imgBall.Height;
this.spriteBatch = spriteBatch;
this.gameContent = gameContent;
ScreenWidth = screenWidth;
ScreenHeight = screenHeight;
Visible = false;
Score = 0;
bricksCleared = 0;
UseRotation = true;
}
public void Draw()
{
if (Visible == false)
{
return;
}
if (UseRotation)
{
Rotation += .1f;
if (Rotation > 3 * Math.PI)
{
Rotation = 0;
}
}
spriteBatch.Draw(imgBall, new Vector2(X, Y), null, Color.White, Rotation, new Vector2(Width / 2, Height / 2), 1.0f, SpriteEffects.None, 0);
}
public void Launch(float x, float y, float xVelocity, float yVelocity)
{
PlaySound(gameContent.BallLaunch);
if (Visible == true)
{
return; //ball already exists, ignore
}
Visible = true;
X = x;
Y = y;
XVelocity = xVelocity;
YVelocity = yVelocity;
}
public static void PlaySound(SoundEffect sound)
{
float volume = 1;
float pitch = 0.0f;
float pan = 0.0f;
sound.Play(volume, pitch, pan);
}
public bool Move(Wall wall, Paddle paddle)
{
if (Visible == false)
{
return false;
}
X = X + XVelocity;
Y = Y + YVelocity;
//check for wall hits
if (X < 1)
{
PlaySound(gameContent.Wallhit);
X = 1;
XVelocity = XVelocity * -1;
}
if (X > ScreenWidth - Width + 5)
{
PlaySound(gameContent.Wallhit);
X = ScreenWidth - Width + 5;
XVelocity = XVelocity * -1;
}
if (Y < 1)
{
PlaySound(gameContent.Wallhit);
Y = 1;
YVelocity = YVelocity * -1;
}
if (Y > ScreenHeight)
{
PlaySound(gameContent.Wallhit);
Visible = false;
Y = 0;
return false;
}
Rectangle paddleRect = new Rectangle((int)paddle.X, (int)paddle.Y, (int)paddle.Width, (int)paddle.Height);
Rectangle ballRect = new Rectangle((int)X, (int)Y, (int)Width, (int)Height);
if (HitTest(paddleRect, ballRect))
{
PlaySound(gameContent.PaddleHit);
int offset = Convert.ToInt32((paddle.Width - (paddle.X + paddle.Width - X + Width / 2)));
offset = offset / 5;
if (offset < 0)
{
offset = 0;
}
switch (offset)
{
case 0:
XVelocity = -6;
break;
case 1:
XVelocity = -5;
break;
case 2:
XVelocity = -4;
break;
case 3:
XVelocity = -3;
break;
case 4:
XVelocity = -2;
break;
case 5:
XVelocity = -1;
break;
case 6:
XVelocity = 1;
break;
case 7:
XVelocity = 2;
break;
case 8:
XVelocity = 3;
break;
case 9:
XVelocity = 4;
break;
case 10:
XVelocity = 5;
break;
default:
XVelocity = 6;
break;
}
YVelocity = YVelocity * -1;
Y = paddle.Y - Height + 1;
return true;
}
bool hitBrick = false;
for (int i = 0; i < 7; i++)
{
if (hitBrick == false)
{
for (int j = 0; j < 10; j++)
{
Brick brick = wall.BrickWall[i, j];
if (brick.Visible)
{
Rectangle brickRect = new Rectangle((int)brick.X, (int)brick.Y, (int)brick.Width, (int)brick.Height);
if (HitTest(ballRect, brickRect))
{
PlaySound(gameContent.BrickHit);
brick.Visible = false;
Score = Score + 7 - i;
YVelocity = YVelocity * -1;
bricksCleared++;
hitBrick = true;
break;
}
}
}
}
}
return true;
}
public static bool HitTest(Rectangle r1, Rectangle r2)
{
if (Rectangle.Intersect(r1, r2) != Rectangle.Empty)
{
return true;
}
else
{
return false;
}
}
}
}