-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ball.cs
60 lines (49 loc) · 1.77 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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace pong {
public class Ball {
static Texture2D TEXTURE;
Rectangle _screen_bounds;
Vector2 _position;
Vector2 _velocity;
public Ball(Rectangle screen_bounds) {
_screen_bounds = screen_bounds;
_position = screen_bounds.Center.ToVector2();
}
public Rectangle GetBounds() {
return new Rectangle(TEXTURE.Bounds.X + (int) _position.X,
TEXTURE.Bounds.Y + (int) _position.Y,
TEXTURE.Bounds.Width,
TEXTURE.Bounds.Height);
}
public static void SetTexture(Texture2D texture) {
TEXTURE = texture;
}
public static Texture2D GetTexture() {
return TEXTURE;
}
public Vector2 GetVelocity() {
return _velocity;
}
public void SetVelocity(Vector2 velocity) {
_velocity = velocity;
}
public void SetPosition(Vector2 position) {
_position = position;
}
public void Update(GameTime gameTime) {
_position += _velocity * gameTime.ElapsedGameTime.Milliseconds / 1000.0f;
if(GetBounds().Top < _screen_bounds.Top) {
_position.Y = _screen_bounds.Top;
_velocity.Y *= -1.0f;
} else if(GetBounds().Bottom > _screen_bounds.Bottom) {
_position.Y = _screen_bounds.Bottom - TEXTURE.Height;
_velocity.Y *= -1.0f;
}
}
public void Draw(SpriteBatch spriteBatch) {
spriteBatch.Draw(TEXTURE, _position, Color.White);
}
}
}